Cycles: merge of changes from tomato branch.
[blender-staging.git] / intern / cycles / blender / blender_python.cpp
1 /*
2  * Copyright 2011, Blender Foundation.
3  *
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.
8  *
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.
13  *
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.
17  */
18
19 #include <Python.h>
20
21 #include "CCL_api.h"
22
23 #include "blender_sync.h"
24 #include "blender_session.h"
25
26 #include "util_foreach.h"
27 #include "util_opengl.h"
28 #include "util_path.h"
29
30 CCL_NAMESPACE_BEGIN
31
32 static PyObject *init_func(PyObject *self, PyObject *args)
33 {
34         const char *path, *user_path;
35
36         if(!PyArg_ParseTuple(args, "ss", &path, &user_path))
37                 return NULL;
38         
39         path_init(path, user_path);
40
41         Py_RETURN_NONE;
42 }
43
44 static PyObject *create_func(PyObject *self, PyObject *args)
45 {
46         PyObject *pyengine, *pyuserpref, *pydata, *pyscene, *pyregion, *pyv3d, *pyrv3d;
47
48         if(!PyArg_ParseTuple(args, "OOOOOOO", &pyengine, &pyuserpref, &pydata, &pyscene, &pyregion, &pyv3d, &pyrv3d))
49                 return NULL;
50
51         /* RNA */
52         PointerRNA engineptr;
53         RNA_pointer_create(NULL, &RNA_RenderEngine, (void*)PyLong_AsVoidPtr(pyengine), &engineptr);
54         BL::RenderEngine engine(engineptr);
55
56         PointerRNA userprefptr;
57         RNA_id_pointer_create((ID*)PyLong_AsVoidPtr(pyuserpref), &userprefptr);
58         BL::UserPreferences userpref(userprefptr);
59
60         PointerRNA dataptr;
61         RNA_id_pointer_create((ID*)PyLong_AsVoidPtr(pydata), &dataptr);
62         BL::BlendData data(dataptr);
63
64         PointerRNA sceneptr;
65         RNA_id_pointer_create((ID*)PyLong_AsVoidPtr(pyscene), &sceneptr);
66         BL::Scene scene(sceneptr);
67
68         PointerRNA regionptr;
69         RNA_id_pointer_create((ID*)PyLong_AsVoidPtr(pyregion), &regionptr);
70         BL::Region region(regionptr);
71
72         PointerRNA v3dptr;
73         RNA_id_pointer_create((ID*)PyLong_AsVoidPtr(pyv3d), &v3dptr);
74         BL::SpaceView3D v3d(v3dptr);
75
76         PointerRNA rv3dptr;
77         RNA_id_pointer_create((ID*)PyLong_AsVoidPtr(pyrv3d), &rv3dptr);
78         BL::RegionView3D rv3d(rv3dptr);
79
80         /* create session */
81         BlenderSession *session;
82
83         Py_BEGIN_ALLOW_THREADS
84
85         if(rv3d) {
86                 /* interactive session */
87                 int width = region.width();
88                 int height = region.height();
89
90                 session = new BlenderSession(engine, userpref, data, scene, v3d, rv3d, width, height);
91         }
92         else {
93                 /* offline session */
94                 session = new BlenderSession(engine, userpref, data, scene);
95         }
96
97         Py_END_ALLOW_THREADS
98
99         return PyLong_FromVoidPtr(session);
100 }
101
102 static PyObject *free_func(PyObject *self, PyObject *value)
103 {
104         delete (BlenderSession*)PyLong_AsVoidPtr(value);
105
106         Py_RETURN_NONE;
107 }
108
109 static PyObject *render_func(PyObject *self, PyObject *value)
110 {
111         Py_BEGIN_ALLOW_THREADS
112
113         BlenderSession *session = (BlenderSession*)PyLong_AsVoidPtr(value);
114         session->render();
115
116         Py_END_ALLOW_THREADS
117
118         Py_RETURN_NONE;
119 }
120
121 static PyObject *draw_func(PyObject *self, PyObject *args)
122 {
123         PyObject *pysession, *pyv3d, *pyrv3d;
124
125         if(!PyArg_ParseTuple(args, "OOO", &pysession, &pyv3d, &pyrv3d))
126                 return NULL;
127         
128         BlenderSession *session = (BlenderSession*)PyLong_AsVoidPtr(pysession);
129
130         if(PyLong_AsVoidPtr(pyrv3d)) {
131                 /* 3d view drawing */
132                 int viewport[4];
133                 glGetIntegerv(GL_VIEWPORT, viewport);
134
135                 session->draw(viewport[2], viewport[3]);
136         }
137
138         Py_RETURN_NONE;
139 }
140
141 static PyObject *sync_func(PyObject *self, PyObject *value)
142 {
143         Py_BEGIN_ALLOW_THREADS
144
145         BlenderSession *session = (BlenderSession*)PyLong_AsVoidPtr(value);
146         session->synchronize();
147
148         Py_END_ALLOW_THREADS
149
150         Py_RETURN_NONE;
151 }
152
153 static PyObject *available_devices_func(PyObject *self, PyObject *args)
154 {
155         vector<DeviceInfo>& devices = Device::available_devices();
156         PyObject *ret = PyTuple_New(devices.size());
157
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()));
161         }
162
163         return ret;
164 }
165
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},
175 };
176
177 static struct PyModuleDef module = {
178         PyModuleDef_HEAD_INIT,
179         "_cycles",
180         "Blender cycles render integration",
181         -1,
182         methods,
183         NULL, NULL, NULL, NULL
184 };
185
186 CCLDeviceInfo *compute_device_list(DeviceType type)
187 {
188         /* device list stored static */
189         static ccl::vector<CCLDeviceInfo> device_list;
190         static ccl::DeviceType device_type = DEVICE_NONE;
191
192         /* create device list if it's not already done */
193         if(type != device_type) {
194                 ccl::vector<DeviceInfo>& devices = ccl::Device::available_devices();
195
196                 device_type = type;
197                 device_list.clear();
198
199                 /* add devices */
200                 int i = 0;
201
202                 foreach(DeviceInfo& info, devices) {
203                         if(info.type == type ||
204                            (info.type == DEVICE_MULTI && info.multi_devices[0].type == type))
205                         {
206                                 CCLDeviceInfo cinfo = {info.id.c_str(), info.description.c_str(), i++};
207                                 device_list.push_back(cinfo);
208                         }
209                 }
210
211                 /* null terminate */
212                 if(!device_list.empty()) {
213                         CCLDeviceInfo cinfo = {NULL, NULL, 0};
214                         device_list.push_back(cinfo);
215                 }
216         }
217
218         return (device_list.empty())? NULL: &device_list[0];
219 }
220
221
222 CCL_NAMESPACE_END
223
224 void *CCL_python_module_init()
225 {
226         PyObject *mod = PyModule_Create(&ccl::module);
227
228 #ifdef WITH_OSL
229         PyModule_AddObject(mod, "with_osl", Py_True);
230         Py_INCREF(Py_True);
231 #else
232         PyModule_AddObject(mod, "with_osl", Py_False);
233         Py_INCREF(Py_False);
234 #endif
235
236         return (void*)mod;
237 }
238
239 CCLDeviceInfo *CCL_compute_device_list(int opencl)
240 {
241         ccl::DeviceType type = (opencl)? ccl::DEVICE_OPENCL: ccl::DEVICE_CUDA;
242         return ccl::compute_device_list(type);
243 }
244