2011-04-27 11:58:34 +00:00
|
|
|
/*
|
|
|
|
* Copyright 2011, Blender Foundation.
|
|
|
|
*
|
|
|
|
* This program is free software; you can redistribute it and/or
|
|
|
|
* modify it under the terms of the GNU General Public License
|
|
|
|
* as published by the Free Software Foundation; either version 2
|
|
|
|
* of the License, or (at your option) any later version.
|
|
|
|
*
|
|
|
|
* This program is distributed in the hope that it will be useful,
|
|
|
|
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
|
|
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
|
|
|
* GNU General Public License for more details.
|
|
|
|
*
|
|
|
|
* You should have received a copy of the GNU General Public License
|
|
|
|
* along with this program; if not, write to the Free Software Foundation,
|
|
|
|
* Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
|
|
|
|
*/
|
|
|
|
|
|
|
|
#include <Python.h>
|
|
|
|
|
2012-01-09 16:58:01 +00:00
|
|
|
#include "CCL_api.h"
|
|
|
|
|
2011-04-27 11:58:34 +00:00
|
|
|
#include "blender_sync.h"
|
|
|
|
#include "blender_session.h"
|
|
|
|
|
2012-01-09 16:58:01 +00:00
|
|
|
#include "util_foreach.h"
|
2011-04-27 11:58:34 +00:00
|
|
|
#include "util_opengl.h"
|
|
|
|
#include "util_path.h"
|
|
|
|
|
|
|
|
CCL_NAMESPACE_BEGIN
|
|
|
|
|
|
|
|
static PyObject *init_func(PyObject *self, PyObject *args)
|
|
|
|
{
|
2011-09-09 12:04:39 +00:00
|
|
|
const char *path, *user_path;
|
2011-04-27 11:58:34 +00:00
|
|
|
|
2011-09-09 12:04:39 +00:00
|
|
|
if(!PyArg_ParseTuple(args, "ss", &path, &user_path))
|
2011-04-27 11:58:34 +00:00
|
|
|
return NULL;
|
|
|
|
|
2011-09-09 12:04:39 +00:00
|
|
|
path_init(path, user_path);
|
2011-04-27 11:58:34 +00:00
|
|
|
|
2011-12-24 02:47:13 +00:00
|
|
|
Py_RETURN_NONE;
|
2011-04-27 11:58:34 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
static PyObject *create_func(PyObject *self, PyObject *args)
|
|
|
|
{
|
2012-01-09 16:58:01 +00:00
|
|
|
PyObject *pyengine, *pyuserpref, *pydata, *pyscene, *pyregion, *pyv3d, *pyrv3d;
|
2011-04-27 11:58:34 +00:00
|
|
|
|
2012-01-09 16:58:01 +00:00
|
|
|
if(!PyArg_ParseTuple(args, "OOOOOOO", &pyengine, &pyuserpref, &pydata, &pyscene, &pyregion, &pyv3d, &pyrv3d))
|
2011-04-27 11:58:34 +00:00
|
|
|
return NULL;
|
|
|
|
|
|
|
|
/* RNA */
|
|
|
|
PointerRNA engineptr;
|
2011-05-05 13:51:33 +00:00
|
|
|
RNA_pointer_create(NULL, &RNA_RenderEngine, (void*)PyLong_AsVoidPtr(pyengine), &engineptr);
|
2011-04-27 11:58:34 +00:00
|
|
|
BL::RenderEngine engine(engineptr);
|
|
|
|
|
2012-01-09 16:58:01 +00:00
|
|
|
PointerRNA userprefptr;
|
|
|
|
RNA_id_pointer_create((ID*)PyLong_AsVoidPtr(pyuserpref), &userprefptr);
|
|
|
|
BL::UserPreferences userpref(userprefptr);
|
|
|
|
|
2011-04-27 11:58:34 +00:00
|
|
|
PointerRNA dataptr;
|
2011-05-05 13:51:33 +00:00
|
|
|
RNA_id_pointer_create((ID*)PyLong_AsVoidPtr(pydata), &dataptr);
|
2011-04-27 11:58:34 +00:00
|
|
|
BL::BlendData data(dataptr);
|
|
|
|
|
|
|
|
PointerRNA sceneptr;
|
2011-05-05 13:51:33 +00:00
|
|
|
RNA_id_pointer_create((ID*)PyLong_AsVoidPtr(pyscene), &sceneptr);
|
2011-04-27 11:58:34 +00:00
|
|
|
BL::Scene scene(sceneptr);
|
|
|
|
|
|
|
|
PointerRNA regionptr;
|
2011-05-05 13:51:33 +00:00
|
|
|
RNA_id_pointer_create((ID*)PyLong_AsVoidPtr(pyregion), ®ionptr);
|
2011-04-27 11:58:34 +00:00
|
|
|
BL::Region region(regionptr);
|
|
|
|
|
|
|
|
PointerRNA v3dptr;
|
2011-05-05 13:51:33 +00:00
|
|
|
RNA_id_pointer_create((ID*)PyLong_AsVoidPtr(pyv3d), &v3dptr);
|
2011-04-27 11:58:34 +00:00
|
|
|
BL::SpaceView3D v3d(v3dptr);
|
|
|
|
|
|
|
|
PointerRNA rv3dptr;
|
2011-05-05 13:51:33 +00:00
|
|
|
RNA_id_pointer_create((ID*)PyLong_AsVoidPtr(pyrv3d), &rv3dptr);
|
2011-04-27 11:58:34 +00:00
|
|
|
BL::RegionView3D rv3d(rv3dptr);
|
|
|
|
|
|
|
|
/* create session */
|
|
|
|
BlenderSession *session;
|
|
|
|
|
2012-09-04 13:29:07 +00:00
|
|
|
Py_BEGIN_ALLOW_THREADS
|
|
|
|
|
2011-04-27 11:58:34 +00:00
|
|
|
if(rv3d) {
|
|
|
|
/* interactive session */
|
|
|
|
int width = region.width();
|
|
|
|
int height = region.height();
|
|
|
|
|
2012-01-09 16:58:01 +00:00
|
|
|
session = new BlenderSession(engine, userpref, data, scene, v3d, rv3d, width, height);
|
2011-04-27 11:58:34 +00:00
|
|
|
}
|
|
|
|
else {
|
|
|
|
/* offline session */
|
2012-01-09 16:58:01 +00:00
|
|
|
session = new BlenderSession(engine, userpref, data, scene);
|
2011-04-27 11:58:34 +00:00
|
|
|
}
|
2012-09-04 13:29:07 +00:00
|
|
|
|
|
|
|
Py_END_ALLOW_THREADS
|
|
|
|
|
2011-04-27 11:58:34 +00:00
|
|
|
return PyLong_FromVoidPtr(session);
|
|
|
|
}
|
|
|
|
|
2011-12-24 02:47:13 +00:00
|
|
|
static PyObject *free_func(PyObject *self, PyObject *value)
|
2011-04-27 11:58:34 +00:00
|
|
|
{
|
2011-12-24 02:47:13 +00:00
|
|
|
delete (BlenderSession*)PyLong_AsVoidPtr(value);
|
2011-04-27 11:58:34 +00:00
|
|
|
|
2011-12-24 02:47:13 +00:00
|
|
|
Py_RETURN_NONE;
|
2011-04-27 11:58:34 +00:00
|
|
|
}
|
|
|
|
|
2011-12-24 02:47:13 +00:00
|
|
|
static PyObject *render_func(PyObject *self, PyObject *value)
|
2011-04-27 11:58:34 +00:00
|
|
|
{
|
|
|
|
Py_BEGIN_ALLOW_THREADS
|
|
|
|
|
2011-12-24 02:47:13 +00:00
|
|
|
BlenderSession *session = (BlenderSession*)PyLong_AsVoidPtr(value);
|
2011-04-27 11:58:34 +00:00
|
|
|
session->render();
|
|
|
|
|
|
|
|
Py_END_ALLOW_THREADS
|
|
|
|
|
2011-12-24 02:47:13 +00:00
|
|
|
Py_RETURN_NONE;
|
2011-04-27 11:58:34 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
static PyObject *draw_func(PyObject *self, PyObject *args)
|
|
|
|
{
|
2011-05-05 13:51:33 +00:00
|
|
|
PyObject *pysession, *pyv3d, *pyrv3d;
|
2011-04-27 11:58:34 +00:00
|
|
|
|
2011-05-05 13:51:33 +00:00
|
|
|
if(!PyArg_ParseTuple(args, "OOO", &pysession, &pyv3d, &pyrv3d))
|
2011-04-27 11:58:34 +00:00
|
|
|
return NULL;
|
|
|
|
|
2011-05-05 13:51:33 +00:00
|
|
|
BlenderSession *session = (BlenderSession*)PyLong_AsVoidPtr(pysession);
|
2011-04-27 11:58:34 +00:00
|
|
|
|
2011-05-05 13:51:33 +00:00
|
|
|
if(PyLong_AsVoidPtr(pyrv3d)) {
|
2011-04-27 11:58:34 +00:00
|
|
|
/* 3d view drawing */
|
|
|
|
int viewport[4];
|
|
|
|
glGetIntegerv(GL_VIEWPORT, viewport);
|
|
|
|
|
2011-08-23 12:20:11 +00:00
|
|
|
session->draw(viewport[2], viewport[3]);
|
2011-04-27 11:58:34 +00:00
|
|
|
}
|
|
|
|
|
2011-12-24 02:47:13 +00:00
|
|
|
Py_RETURN_NONE;
|
2011-04-27 11:58:34 +00:00
|
|
|
}
|
|
|
|
|
2011-12-24 02:47:13 +00:00
|
|
|
static PyObject *sync_func(PyObject *self, PyObject *value)
|
2011-04-27 11:58:34 +00:00
|
|
|
{
|
2012-09-04 13:29:07 +00:00
|
|
|
Py_BEGIN_ALLOW_THREADS
|
|
|
|
|
2011-12-24 02:47:13 +00:00
|
|
|
BlenderSession *session = (BlenderSession*)PyLong_AsVoidPtr(value);
|
2011-04-27 11:58:34 +00:00
|
|
|
session->synchronize();
|
|
|
|
|
2012-09-04 13:29:07 +00:00
|
|
|
Py_END_ALLOW_THREADS
|
|
|
|
|
2011-12-24 02:47:13 +00:00
|
|
|
Py_RETURN_NONE;
|
2011-04-27 11:58:34 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
static PyObject *available_devices_func(PyObject *self, PyObject *args)
|
|
|
|
{
|
2012-01-09 16:58:01 +00:00
|
|
|
vector<DeviceInfo>& devices = Device::available_devices();
|
|
|
|
PyObject *ret = PyTuple_New(devices.size());
|
2011-04-27 11:58:34 +00:00
|
|
|
|
2012-01-09 16:58:01 +00:00
|
|
|
for(size_t i = 0; i < devices.size(); i++) {
|
|
|
|
DeviceInfo& device = devices[i];
|
|
|
|
PyTuple_SET_ITEM(ret, i, PyUnicode_FromString(device.description.c_str()));
|
2011-04-27 11:58:34 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
|
|
|
|
static PyMethodDef methods[] = {
|
|
|
|
{"init", init_func, METH_VARARGS, ""},
|
|
|
|
{"create", create_func, METH_VARARGS, ""},
|
2011-12-24 02:47:13 +00:00
|
|
|
{"free", free_func, METH_O, ""},
|
|
|
|
{"render", render_func, METH_O, ""},
|
2011-04-27 11:58:34 +00:00
|
|
|
{"draw", draw_func, METH_VARARGS, ""},
|
2011-12-24 02:47:13 +00:00
|
|
|
{"sync", sync_func, METH_O, ""},
|
2011-04-27 11:58:34 +00:00
|
|
|
{"available_devices", available_devices_func, METH_NOARGS, ""},
|
2011-05-31 16:19:02 +00:00
|
|
|
{NULL, NULL, 0, NULL},
|
2011-04-27 11:58:34 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
static struct PyModuleDef module = {
|
|
|
|
PyModuleDef_HEAD_INIT,
|
2011-12-24 02:47:13 +00:00
|
|
|
"_cycles",
|
2011-11-07 17:13:32 +00:00
|
|
|
"Blender cycles render integration",
|
2011-04-27 11:58:34 +00:00
|
|
|
-1,
|
2011-05-31 16:19:02 +00:00
|
|
|
methods,
|
|
|
|
NULL, NULL, NULL, NULL
|
2011-04-27 11:58:34 +00:00
|
|
|
};
|
|
|
|
|
2012-01-09 16:58:01 +00:00
|
|
|
CCLDeviceInfo *compute_device_list(DeviceType type)
|
|
|
|
{
|
|
|
|
/* device list stored static */
|
|
|
|
static ccl::vector<CCLDeviceInfo> device_list;
|
|
|
|
static ccl::DeviceType device_type = DEVICE_NONE;
|
|
|
|
|
|
|
|
/* create device list if it's not already done */
|
|
|
|
if(type != device_type) {
|
|
|
|
ccl::vector<DeviceInfo>& devices = ccl::Device::available_devices();
|
|
|
|
|
|
|
|
device_type = type;
|
|
|
|
device_list.clear();
|
|
|
|
|
|
|
|
/* add devices */
|
|
|
|
int i = 0;
|
|
|
|
|
|
|
|
foreach(DeviceInfo& info, devices) {
|
|
|
|
if(info.type == type ||
|
2012-06-09 18:56:12 +00:00
|
|
|
(info.type == DEVICE_MULTI && info.multi_devices[0].type == type))
|
|
|
|
{
|
2012-01-09 16:58:01 +00:00
|
|
|
CCLDeviceInfo cinfo = {info.id.c_str(), info.description.c_str(), i++};
|
|
|
|
device_list.push_back(cinfo);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/* null terminate */
|
|
|
|
if(!device_list.empty()) {
|
|
|
|
CCLDeviceInfo cinfo = {NULL, NULL, 0};
|
|
|
|
device_list.push_back(cinfo);
|
|
|
|
}
|
|
|
|
}
|
2011-04-27 11:58:34 +00:00
|
|
|
|
2012-01-09 16:58:01 +00:00
|
|
|
return (device_list.empty())? NULL: &device_list[0];
|
|
|
|
}
|
2011-08-16 16:15:34 +00:00
|
|
|
|
2012-01-09 16:58:01 +00:00
|
|
|
|
|
|
|
CCL_NAMESPACE_END
|
|
|
|
|
|
|
|
void *CCL_python_module_init()
|
2011-04-27 11:58:34 +00:00
|
|
|
{
|
2012-06-09 18:56:12 +00:00
|
|
|
PyObject *mod = PyModule_Create(&ccl::module);
|
2011-12-24 02:47:13 +00:00
|
|
|
|
|
|
|
#ifdef WITH_OSL
|
|
|
|
PyModule_AddObject(mod, "with_osl", Py_True);
|
|
|
|
Py_INCREF(Py_True);
|
|
|
|
#else
|
|
|
|
PyModule_AddObject(mod, "with_osl", Py_False);
|
|
|
|
Py_INCREF(Py_False);
|
|
|
|
#endif
|
|
|
|
|
2012-01-09 16:58:01 +00:00
|
|
|
return (void*)mod;
|
|
|
|
}
|
|
|
|
|
|
|
|
CCLDeviceInfo *CCL_compute_device_list(int opencl)
|
|
|
|
{
|
|
|
|
ccl::DeviceType type = (opencl)? ccl::DEVICE_OPENCL: ccl::DEVICE_CUDA;
|
|
|
|
return ccl::compute_device_list(type);
|
2011-04-27 11:58:34 +00:00
|
|
|
}
|
|
|
|
|