4560c2f8543ea310c56a9cdbaa71e5d58f749649
[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         if(rv3d) {
84                 /* interactive session */
85                 int width = region.width();
86                 int height = region.height();
87
88                 session = new BlenderSession(engine, userpref, data, scene, v3d, rv3d, width, height);
89         }
90         else {
91                 /* offline session */
92                 session = new BlenderSession(engine, userpref, data, scene);
93         }
94         
95         return PyLong_FromVoidPtr(session);
96 }
97
98 static PyObject *free_func(PyObject *self, PyObject *value)
99 {
100         delete (BlenderSession*)PyLong_AsVoidPtr(value);
101
102         Py_RETURN_NONE;
103 }
104
105 static PyObject *render_func(PyObject *self, PyObject *value)
106 {
107         Py_BEGIN_ALLOW_THREADS
108
109         BlenderSession *session = (BlenderSession*)PyLong_AsVoidPtr(value);
110         session->render();
111
112         Py_END_ALLOW_THREADS
113
114         Py_RETURN_NONE;
115 }
116
117 static PyObject *draw_func(PyObject *self, PyObject *args)
118 {
119         PyObject *pysession, *pyv3d, *pyrv3d;
120
121         if(!PyArg_ParseTuple(args, "OOO", &pysession, &pyv3d, &pyrv3d))
122                 return NULL;
123         
124         BlenderSession *session = (BlenderSession*)PyLong_AsVoidPtr(pysession);
125
126         if(PyLong_AsVoidPtr(pyrv3d)) {
127                 /* 3d view drawing */
128                 int viewport[4];
129                 glGetIntegerv(GL_VIEWPORT, viewport);
130
131                 session->draw(viewport[2], viewport[3]);
132         }
133
134         Py_RETURN_NONE;
135 }
136
137 static PyObject *sync_func(PyObject *self, PyObject *value)
138 {
139         BlenderSession *session = (BlenderSession*)PyLong_AsVoidPtr(value);
140         session->synchronize();
141
142         Py_RETURN_NONE;
143 }
144
145 static PyObject *available_devices_func(PyObject *self, PyObject *args)
146 {
147         vector<DeviceInfo>& devices = Device::available_devices();
148         PyObject *ret = PyTuple_New(devices.size());
149
150         for(size_t i = 0; i < devices.size(); i++) {
151                 DeviceInfo& device = devices[i];
152                 PyTuple_SET_ITEM(ret, i, PyUnicode_FromString(device.description.c_str()));
153         }
154
155         return ret;
156 }
157
158 static PyMethodDef methods[] = {
159         {"init", init_func, METH_VARARGS, ""},
160         {"create", create_func, METH_VARARGS, ""},
161         {"free", free_func, METH_O, ""},
162         {"render", render_func, METH_O, ""},
163         {"draw", draw_func, METH_VARARGS, ""},
164         {"sync", sync_func, METH_O, ""},
165         {"available_devices", available_devices_func, METH_NOARGS, ""},
166         {NULL, NULL, 0, NULL},
167 };
168
169 static struct PyModuleDef module = {
170         PyModuleDef_HEAD_INIT,
171         "_cycles",
172         "Blender cycles render integration",
173         -1,
174         methods,
175         NULL, NULL, NULL, NULL
176 };
177
178 CCLDeviceInfo *compute_device_list(DeviceType type)
179 {
180         /* device list stored static */
181         static ccl::vector<CCLDeviceInfo> device_list;
182         static ccl::DeviceType device_type = DEVICE_NONE;
183
184         /* create device list if it's not already done */
185         if(type != device_type) {
186                 ccl::vector<DeviceInfo>& devices = ccl::Device::available_devices();
187
188                 device_type = type;
189                 device_list.clear();
190
191                 /* add devices */
192                 int i = 0;
193
194                 foreach(DeviceInfo& info, devices) {
195                         if(info.type == type ||
196                            (info.type == DEVICE_MULTI && info.multi_devices[0].type == type))
197                         {
198                                 CCLDeviceInfo cinfo = {info.id.c_str(), info.description.c_str(), i++};
199                                 device_list.push_back(cinfo);
200                         }
201                 }
202
203                 /* null terminate */
204                 if(!device_list.empty()) {
205                         CCLDeviceInfo cinfo = {NULL, NULL, 0};
206                         device_list.push_back(cinfo);
207                 }
208         }
209
210         return (device_list.empty())? NULL: &device_list[0];
211 }
212
213
214 CCL_NAMESPACE_END
215
216 void *CCL_python_module_init()
217 {
218         PyObject *mod = PyModule_Create(&ccl::module);
219
220 #ifdef WITH_OSL
221         PyModule_AddObject(mod, "with_osl", Py_True);
222         Py_INCREF(Py_True);
223 #else
224         PyModule_AddObject(mod, "with_osl", Py_False);
225         Py_INCREF(Py_False);
226 #endif
227
228         return (void*)mod;
229 }
230
231 CCLDeviceInfo *CCL_compute_device_list(int opencl)
232 {
233         ccl::DeviceType type = (opencl)? ccl::DEVICE_OPENCL: ccl::DEVICE_CUDA;
234         return ccl::compute_device_list(type);
235 }
236