2 * Copyright 2011, Blender Foundation.
4 * This program is free software; you can redistribute it and/or
5 * modify it under the terms of the GNU General Public License
6 * as published by the Free Software Foundation; either version 2
7 * of the License, or (at your option) any later version.
9 * This program is distributed in the hope that it will be useful,
10 * but WITHOUT ANY WARRANTY; without even the implied warranty of
11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 * GNU General Public License for more details.
14 * You should have received a copy of the GNU General Public License
15 * along with this program; if not, write to the Free Software Foundation,
16 * Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
23 #include "blender_sync.h"
24 #include "blender_session.h"
26 #include "util_foreach.h"
27 #include "util_opengl.h"
28 #include "util_path.h"
32 static PyObject *init_func(PyObject *self, PyObject *args)
34 const char *path, *user_path;
36 if(!PyArg_ParseTuple(args, "ss", &path, &user_path))
39 path_init(path, user_path);
44 static PyObject *create_func(PyObject *self, PyObject *args)
46 PyObject *pyengine, *pyuserpref, *pydata, *pyscene, *pyregion, *pyv3d, *pyrv3d;
48 if(!PyArg_ParseTuple(args, "OOOOOOO", &pyengine, &pyuserpref, &pydata, &pyscene, &pyregion, &pyv3d, &pyrv3d))
53 RNA_pointer_create(NULL, &RNA_RenderEngine, (void*)PyLong_AsVoidPtr(pyengine), &engineptr);
54 BL::RenderEngine engine(engineptr);
56 PointerRNA userprefptr;
57 RNA_id_pointer_create((ID*)PyLong_AsVoidPtr(pyuserpref), &userprefptr);
58 BL::UserPreferences userpref(userprefptr);
61 RNA_id_pointer_create((ID*)PyLong_AsVoidPtr(pydata), &dataptr);
62 BL::BlendData data(dataptr);
65 RNA_id_pointer_create((ID*)PyLong_AsVoidPtr(pyscene), &sceneptr);
66 BL::Scene scene(sceneptr);
69 RNA_id_pointer_create((ID*)PyLong_AsVoidPtr(pyregion), ®ionptr);
70 BL::Region region(regionptr);
73 RNA_id_pointer_create((ID*)PyLong_AsVoidPtr(pyv3d), &v3dptr);
74 BL::SpaceView3D v3d(v3dptr);
77 RNA_id_pointer_create((ID*)PyLong_AsVoidPtr(pyrv3d), &rv3dptr);
78 BL::RegionView3D rv3d(rv3dptr);
81 BlenderSession *session;
83 Py_BEGIN_ALLOW_THREADS
86 /* interactive session */
87 int width = region.width();
88 int height = region.height();
90 session = new BlenderSession(engine, userpref, data, scene, v3d, rv3d, width, height);
94 session = new BlenderSession(engine, userpref, data, scene);
99 return PyLong_FromVoidPtr(session);
102 static PyObject *free_func(PyObject *self, PyObject *value)
104 delete (BlenderSession*)PyLong_AsVoidPtr(value);
109 static PyObject *render_func(PyObject *self, PyObject *value)
111 Py_BEGIN_ALLOW_THREADS
113 BlenderSession *session = (BlenderSession*)PyLong_AsVoidPtr(value);
121 static PyObject *draw_func(PyObject *self, PyObject *args)
123 PyObject *pysession, *pyv3d, *pyrv3d;
125 if(!PyArg_ParseTuple(args, "OOO", &pysession, &pyv3d, &pyrv3d))
128 BlenderSession *session = (BlenderSession*)PyLong_AsVoidPtr(pysession);
130 if(PyLong_AsVoidPtr(pyrv3d)) {
131 /* 3d view drawing */
133 glGetIntegerv(GL_VIEWPORT, viewport);
135 session->draw(viewport[2], viewport[3]);
141 static PyObject *sync_func(PyObject *self, PyObject *value)
143 Py_BEGIN_ALLOW_THREADS
145 BlenderSession *session = (BlenderSession*)PyLong_AsVoidPtr(value);
146 session->synchronize();
153 static PyObject *available_devices_func(PyObject *self, PyObject *args)
155 vector<DeviceInfo>& devices = Device::available_devices();
156 PyObject *ret = PyTuple_New(devices.size());
158 for(size_t i = 0; i < devices.size(); i++) {
159 DeviceInfo& device = devices[i];
160 PyTuple_SET_ITEM(ret, i, PyUnicode_FromString(device.description.c_str()));
166 static PyMethodDef methods[] = {
167 {"init", init_func, METH_VARARGS, ""},
168 {"create", create_func, METH_VARARGS, ""},
169 {"free", free_func, METH_O, ""},
170 {"render", render_func, METH_O, ""},
171 {"draw", draw_func, METH_VARARGS, ""},
172 {"sync", sync_func, METH_O, ""},
173 {"available_devices", available_devices_func, METH_NOARGS, ""},
174 {NULL, NULL, 0, NULL},
177 static struct PyModuleDef module = {
178 PyModuleDef_HEAD_INIT,
180 "Blender cycles render integration",
183 NULL, NULL, NULL, NULL
186 static CCLDeviceInfo *compute_device_list(DeviceType type)
188 /* device list stored static */
189 static ccl::vector<CCLDeviceInfo> device_list;
190 static ccl::DeviceType device_type = DEVICE_NONE;
192 /* create device list if it's not already done */
193 if(type != device_type) {
194 ccl::vector<DeviceInfo>& devices = ccl::Device::available_devices();
202 foreach(DeviceInfo& info, devices) {
203 if(info.type == type ||
204 (info.type == DEVICE_MULTI && info.multi_devices[0].type == type))
206 CCLDeviceInfo cinfo = {info.id.c_str(), info.description.c_str(), i++};
207 device_list.push_back(cinfo);
212 if(!device_list.empty()) {
213 CCLDeviceInfo cinfo = {NULL, NULL, 0};
214 device_list.push_back(cinfo);
218 return (device_list.empty())? NULL: &device_list[0];
224 void *CCL_python_module_init()
226 PyObject *mod = PyModule_Create(&ccl::module);
229 PyModule_AddObject(mod, "with_osl", Py_True);
232 PyModule_AddObject(mod, "with_osl", Py_False);
239 CCLDeviceInfo *CCL_compute_device_list(int opencl)
241 ccl::DeviceType type = (opencl)? ccl::DEVICE_OPENCL: ccl::DEVICE_CUDA;
242 return ccl::compute_device_list(type);