37191bb797a6ff731929748f74177afa094660b5
[blender-staging.git] / intern / cycles / blender / blender_python.cpp
1 /*
2  * Copyright 2011-2013 Blender Foundation
3  *
4  * Licensed under the Apache License, Version 2.0 (the "License");
5  * you may not use this file except in compliance with the License.
6  * You may obtain a copy of the License at
7  *
8  * http://www.apache.org/licenses/LICENSE-2.0
9  *
10  * Unless required by applicable law or agreed to in writing, software
11  * distributed under the License is distributed on an "AS IS" BASIS,
12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13  * See the License for the specific language governing permissions and
14  * limitations under the License.
15  */
16
17 #include <Python.h>
18
19 #include "CCL_api.h"
20
21 #include "blender_sync.h"
22 #include "blender_session.h"
23
24 #include "util_foreach.h"
25 #include "util_md5.h"
26 #include "util_opengl.h"
27 #include "util_path.h"
28
29 #ifdef WITH_OSL
30 #include "osl.h"
31
32 #include <OSL/oslquery.h>
33 #include <OSL/oslconfig.h>
34 #endif
35
36 CCL_NAMESPACE_BEGIN
37
38 static void *pylong_as_voidptr_typesafe(PyObject *object)
39 {
40         if(object == Py_None)
41                 return NULL;
42         return PyLong_AsVoidPtr(object);
43 }
44
45 void python_thread_state_save(void **python_thread_state)
46 {
47         *python_thread_state = (void*)PyEval_SaveThread();
48 }
49
50 void python_thread_state_restore(void **python_thread_state)
51 {
52         PyEval_RestoreThread((PyThreadState*)*python_thread_state);
53         *python_thread_state = NULL;
54 }
55
56 static const char *PyC_UnicodeAsByte(PyObject *py_str, PyObject **coerce)
57 {
58 #ifdef WIN32
59         /* bug [#31856] oddly enough, Python3.2 --> 3.3 on Windows will throw an
60          * exception here this needs to be fixed in python:
61          * see: bugs.python.org/issue15859 */
62         if(!PyUnicode_Check(py_str)) {
63                 PyErr_BadArgument();
64                 return "";
65         }
66 #endif
67         if((*coerce = PyUnicode_EncodeFSDefault(py_str))) {
68                 return PyBytes_AS_STRING(*coerce);
69         }
70         return "";
71 }
72
73 static PyObject *init_func(PyObject *self, PyObject *args)
74 {
75         PyObject *path, *user_path;
76
77         if(!PyArg_ParseTuple(args, "OO", &path, &user_path)) {
78                 return NULL;
79         }
80
81         PyObject *path_coerce = NULL, *user_path_coerce = NULL;
82         path_init(PyC_UnicodeAsByte(path, &path_coerce),
83                   PyC_UnicodeAsByte(user_path, &user_path_coerce));
84         Py_XDECREF(path_coerce);
85         Py_XDECREF(user_path_coerce);
86
87         Py_RETURN_NONE;
88 }
89
90 static PyObject *create_func(PyObject *self, PyObject *args)
91 {
92         PyObject *pyengine, *pyuserpref, *pydata, *pyscene, *pyregion, *pyv3d, *pyrv3d;
93         int preview_osl;
94
95         if(!PyArg_ParseTuple(args, "OOOOOOOi", &pyengine, &pyuserpref, &pydata, &pyscene, &pyregion, &pyv3d, &pyrv3d, &preview_osl))
96                 return NULL;
97
98         /* RNA */
99         PointerRNA engineptr;
100         RNA_pointer_create(NULL, &RNA_RenderEngine, (void*)PyLong_AsVoidPtr(pyengine), &engineptr);
101         BL::RenderEngine engine(engineptr);
102
103         PointerRNA userprefptr;
104         RNA_pointer_create(NULL, &RNA_UserPreferences, (void*)PyLong_AsVoidPtr(pyuserpref), &userprefptr);
105         BL::UserPreferences userpref(userprefptr);
106
107         PointerRNA dataptr;
108         RNA_main_pointer_create((Main*)PyLong_AsVoidPtr(pydata), &dataptr);
109         BL::BlendData data(dataptr);
110
111         PointerRNA sceneptr;
112         RNA_id_pointer_create((ID*)PyLong_AsVoidPtr(pyscene), &sceneptr);
113         BL::Scene scene(sceneptr);
114
115         PointerRNA regionptr;
116         RNA_pointer_create(NULL, &RNA_Region, pylong_as_voidptr_typesafe(pyregion), &regionptr);
117         BL::Region region(regionptr);
118
119         PointerRNA v3dptr;
120         RNA_pointer_create(NULL, &RNA_SpaceView3D, pylong_as_voidptr_typesafe(pyv3d), &v3dptr);
121         BL::SpaceView3D v3d(v3dptr);
122
123         PointerRNA rv3dptr;
124         RNA_pointer_create(NULL, &RNA_RegionView3D, pylong_as_voidptr_typesafe(pyrv3d), &rv3dptr);
125         BL::RegionView3D rv3d(rv3dptr);
126
127         /* create session */
128         BlenderSession *session;
129
130         if(rv3d) {
131                 /* interactive viewport session */
132                 int width = region.width();
133                 int height = region.height();
134
135                 session = new BlenderSession(engine, userpref, data, scene, v3d, rv3d, width, height);
136         }
137         else {
138                 /* override some settings for preview */
139                 if(engine.is_preview()) {
140                         PointerRNA cscene = RNA_pointer_get(&sceneptr, "cycles");
141
142                         RNA_boolean_set(&cscene, "shading_system", preview_osl);
143                         RNA_boolean_set(&cscene, "use_progressive_refine", true);
144                 }
145
146                 /* offline session or preview render */
147                 session = new BlenderSession(engine, userpref, data, scene);
148         }
149
150         python_thread_state_save(&session->python_thread_state);
151
152         session->create();
153
154         python_thread_state_restore(&session->python_thread_state);
155
156         return PyLong_FromVoidPtr(session);
157 }
158
159 static PyObject *free_func(PyObject *self, PyObject *value)
160 {
161         delete (BlenderSession*)PyLong_AsVoidPtr(value);
162
163         Py_RETURN_NONE;
164 }
165
166 static PyObject *render_func(PyObject *self, PyObject *value)
167 {
168         BlenderSession *session = (BlenderSession*)PyLong_AsVoidPtr(value);
169
170         python_thread_state_save(&session->python_thread_state);
171
172         session->render();
173
174         python_thread_state_restore(&session->python_thread_state);
175
176         Py_RETURN_NONE;
177 }
178
179 /* pixel_array and result passed as pointers */
180 static PyObject *bake_func(PyObject *self, PyObject *args)
181 {
182         PyObject *pysession, *pyobject;
183         PyObject *pypixel_array, *pyresult;
184         const char *pass_type;
185         int num_pixels, depth;
186
187         if(!PyArg_ParseTuple(args, "OOsOiiO", &pysession, &pyobject, &pass_type, &pypixel_array,  &num_pixels, &depth, &pyresult))
188                 return NULL;
189
190         BlenderSession *session = (BlenderSession*)PyLong_AsVoidPtr(pysession);
191
192         PointerRNA objectptr;
193         RNA_id_pointer_create((ID*)PyLong_AsVoidPtr(pyobject), &objectptr);
194         BL::Object b_object(objectptr);
195
196         void *b_result = PyLong_AsVoidPtr(pyresult);
197
198         PointerRNA bakepixelptr;
199         RNA_pointer_create(NULL, &RNA_BakePixel, PyLong_AsVoidPtr(pypixel_array), &bakepixelptr);
200         BL::BakePixel b_bake_pixel(bakepixelptr);
201
202         python_thread_state_save(&session->python_thread_state);
203
204         session->bake(b_object, pass_type, b_bake_pixel, (size_t)num_pixels, depth, (float *)b_result);
205
206         python_thread_state_restore(&session->python_thread_state);
207
208         Py_RETURN_NONE;
209 }
210
211 static PyObject *draw_func(PyObject *self, PyObject *args)
212 {
213         PyObject *pysession, *pyv3d, *pyrv3d;
214
215         if(!PyArg_ParseTuple(args, "OOO", &pysession, &pyv3d, &pyrv3d))
216                 return NULL;
217         
218         BlenderSession *session = (BlenderSession*)PyLong_AsVoidPtr(pysession);
219
220         if(PyLong_AsVoidPtr(pyrv3d)) {
221                 /* 3d view drawing */
222                 int viewport[4];
223                 glGetIntegerv(GL_VIEWPORT, viewport);
224
225                 session->draw(viewport[2], viewport[3]);
226         }
227
228         Py_RETURN_NONE;
229 }
230
231 static PyObject *reset_func(PyObject *self, PyObject *args)
232 {
233         PyObject *pysession, *pydata, *pyscene;
234
235         if(!PyArg_ParseTuple(args, "OOO", &pysession, &pydata, &pyscene))
236                 return NULL;
237
238         BlenderSession *session = (BlenderSession*)PyLong_AsVoidPtr(pysession);
239
240         PointerRNA dataptr;
241         RNA_main_pointer_create((Main*)PyLong_AsVoidPtr(pydata), &dataptr);
242         BL::BlendData b_data(dataptr);
243
244         PointerRNA sceneptr;
245         RNA_id_pointer_create((ID*)PyLong_AsVoidPtr(pyscene), &sceneptr);
246         BL::Scene b_scene(sceneptr);
247
248         python_thread_state_save(&session->python_thread_state);
249
250         session->reset_session(b_data, b_scene);
251
252         python_thread_state_restore(&session->python_thread_state);
253
254         Py_RETURN_NONE;
255 }
256
257 static PyObject *sync_func(PyObject *self, PyObject *value)
258 {
259         BlenderSession *session = (BlenderSession*)PyLong_AsVoidPtr(value);
260
261         python_thread_state_save(&session->python_thread_state);
262
263         session->synchronize();
264
265         python_thread_state_restore(&session->python_thread_state);
266
267         Py_RETURN_NONE;
268 }
269
270 static PyObject *available_devices_func(PyObject *self, PyObject *args)
271 {
272         vector<DeviceInfo>& devices = Device::available_devices();
273         PyObject *ret = PyTuple_New(devices.size());
274
275         for(size_t i = 0; i < devices.size(); i++) {
276                 DeviceInfo& device = devices[i];
277                 PyTuple_SET_ITEM(ret, i, PyUnicode_FromString(device.description.c_str()));
278         }
279
280         return ret;
281 }
282
283 #ifdef WITH_OSL
284
285 static PyObject *osl_update_node_func(PyObject *self, PyObject *args)
286 {
287         PyObject *pynodegroup, *pynode;
288         const char *filepath = NULL;
289
290         if(!PyArg_ParseTuple(args, "OOs", &pynodegroup, &pynode, &filepath))
291                 return NULL;
292
293         /* RNA */
294         PointerRNA nodeptr;
295         RNA_pointer_create((ID*)PyLong_AsVoidPtr(pynodegroup), &RNA_ShaderNodeScript, (void*)PyLong_AsVoidPtr(pynode), &nodeptr);
296         BL::ShaderNodeScript b_node(nodeptr);
297
298         /* update bytecode hash */
299         string bytecode = b_node.bytecode();
300
301         if(!bytecode.empty()) {
302                 MD5Hash md5;
303                 md5.append((const uint8_t*)bytecode.c_str(), bytecode.size());
304                 b_node.bytecode_hash(md5.get_hex().c_str());
305         }
306         else
307                 b_node.bytecode_hash("");
308
309         /* query from file path */
310         OSL::OSLQuery query;
311
312         if(!OSLShaderManager::osl_query(query, filepath))
313                 Py_RETURN_FALSE;
314
315         /* add new sockets from parameters */
316         set<void*> used_sockets;
317
318         for(int i = 0; i < query.nparams(); i++) {
319                 const OSL::OSLQuery::Parameter *param = query.getparam(i);
320
321                 /* skip unsupported types */
322                 if(param->varlenarray || param->isstruct || param->type.arraylen > 1)
323                         continue;
324
325                 /* determine socket type */
326                 std::string socket_type;
327                 BL::NodeSocket::type_enum data_type = BL::NodeSocket::type_VALUE;
328                 float4 default_float4 = make_float4(0.0f, 0.0f, 0.0f, 1.0f);
329                 float default_float = 0.0f;
330                 int default_int = 0;
331                 std::string default_string = "";
332                 
333                 if(param->isclosure) {
334                         socket_type = "NodeSocketShader";
335                         data_type = BL::NodeSocket::type_SHADER;
336                 }
337                 else if(param->type.vecsemantics == TypeDesc::COLOR) {
338                         socket_type = "NodeSocketColor";
339                         data_type = BL::NodeSocket::type_RGBA;
340
341                         if(param->validdefault) {
342                                 default_float4[0] = param->fdefault[0];
343                                 default_float4[1] = param->fdefault[1];
344                                 default_float4[2] = param->fdefault[2];
345                         }
346                 }
347                 else if(param->type.vecsemantics == TypeDesc::POINT ||
348                         param->type.vecsemantics == TypeDesc::VECTOR ||
349                         param->type.vecsemantics == TypeDesc::NORMAL)
350                 {
351                         socket_type = "NodeSocketVector";
352                         data_type = BL::NodeSocket::type_VECTOR;
353
354                         if(param->validdefault) {
355                                 default_float4[0] = param->fdefault[0];
356                                 default_float4[1] = param->fdefault[1];
357                                 default_float4[2] = param->fdefault[2];
358                         }
359                 }
360                 else if(param->type.aggregate == TypeDesc::SCALAR) {
361                         if(param->type.basetype == TypeDesc::INT) {
362                                 socket_type = "NodeSocketInt";
363                                 data_type = BL::NodeSocket::type_INT;
364                                 if(param->validdefault)
365                                         default_int = param->idefault[0];
366                         }
367                         else if(param->type.basetype == TypeDesc::FLOAT) {
368                                 socket_type = "NodeSocketFloat";
369                                 data_type = BL::NodeSocket::type_VALUE;
370                                 if(param->validdefault)
371                                         default_float = param->fdefault[0];
372                         }
373                         else if(param->type.basetype == TypeDesc::STRING) {
374                                 socket_type = "NodeSocketString";
375                                 data_type = BL::NodeSocket::type_STRING;
376                                 if(param->validdefault)
377                                         default_string = param->sdefault[0];
378                         }
379                         else
380                                 continue;
381                 }
382                 else
383                         continue;
384
385                 /* find socket socket */
386                 BL::NodeSocket b_sock(PointerRNA_NULL);
387                 if (param->isoutput) {
388                         b_sock = b_node.outputs[param->name.string()];
389                         /* remove if type no longer matches */
390                         if(b_sock && b_sock.bl_idname() != socket_type) {
391                                 b_node.outputs.remove(b_sock);
392                                 b_sock = BL::NodeSocket(PointerRNA_NULL);
393                         }
394                 }
395                 else {
396                         b_sock = b_node.inputs[param->name.string()];
397                         /* remove if type no longer matches */
398                         if(b_sock && b_sock.bl_idname() != socket_type) {
399                                 b_node.inputs.remove(b_sock);
400                                 b_sock = BL::NodeSocket(PointerRNA_NULL);
401                         }
402                 }
403
404                 if(!b_sock) {
405                         /* create new socket */
406                         if(param->isoutput)
407                                 b_sock = b_node.outputs.create(socket_type.c_str(), param->name.c_str(), param->name.c_str());
408                         else
409                                 b_sock = b_node.inputs.create(socket_type.c_str(), param->name.c_str(), param->name.c_str());
410
411                         /* set default value */
412                         if(data_type == BL::NodeSocket::type_VALUE) {
413                                 set_float(b_sock.ptr, "default_value", default_float);
414                         }
415                         else if(data_type == BL::NodeSocket::type_INT) {
416                                 set_int(b_sock.ptr, "default_value", default_int);
417                         }
418                         else if(data_type == BL::NodeSocket::type_RGBA) {
419                                 set_float4(b_sock.ptr, "default_value", default_float4);
420                         }
421                         else if(data_type == BL::NodeSocket::type_VECTOR) {
422                                 set_float3(b_sock.ptr, "default_value", float4_to_float3(default_float4));
423                         }
424                         else if(data_type == BL::NodeSocket::type_STRING) {
425                                 set_string(b_sock.ptr, "default_value", default_string);
426                         }
427                 }
428
429                 used_sockets.insert(b_sock.ptr.data);
430         }
431
432         /* remove unused parameters */
433         bool removed;
434
435         do {
436                 BL::Node::inputs_iterator b_input;
437                 BL::Node::outputs_iterator b_output;
438
439                 removed = false;
440
441                 for (b_node.inputs.begin(b_input); b_input != b_node.inputs.end(); ++b_input) {
442                         if(used_sockets.find(b_input->ptr.data) == used_sockets.end()) {
443                                 b_node.inputs.remove(*b_input);
444                                 removed = true;
445                                 break;
446                         }
447                 }
448
449                 for (b_node.outputs.begin(b_output); b_output != b_node.outputs.end(); ++b_output) {
450                         if(used_sockets.find(b_output->ptr.data) == used_sockets.end()) {
451                                 b_node.outputs.remove(*b_output);
452                                 removed = true;
453                                 break;
454                         }
455                 }
456         } while(removed);
457
458         Py_RETURN_TRUE;
459 }
460
461 static PyObject *osl_compile_func(PyObject *self, PyObject *args)
462 {
463         const char *inputfile = NULL, *outputfile = NULL;
464
465         if(!PyArg_ParseTuple(args, "ss", &inputfile, &outputfile))
466                 return NULL;
467         
468         /* return */
469         if(!OSLShaderManager::osl_compile(inputfile, outputfile))
470                 Py_RETURN_FALSE;
471
472         Py_RETURN_TRUE;
473 }
474 #endif
475
476 static PyObject *system_info_func(PyObject *self, PyObject *value)
477 {
478         string system_info = Device::device_capabilities();
479         return PyUnicode_FromString(system_info.c_str());
480 }
481
482 static PyMethodDef methods[] = {
483         {"init", init_func, METH_VARARGS, ""},
484         {"create", create_func, METH_VARARGS, ""},
485         {"free", free_func, METH_O, ""},
486         {"render", render_func, METH_O, ""},
487         {"bake", bake_func, METH_VARARGS, ""},
488         {"draw", draw_func, METH_VARARGS, ""},
489         {"sync", sync_func, METH_O, ""},
490         {"reset", reset_func, METH_VARARGS, ""},
491 #ifdef WITH_OSL
492         {"osl_update_node", osl_update_node_func, METH_VARARGS, ""},
493         {"osl_compile", osl_compile_func, METH_VARARGS, ""},
494 #endif
495         {"available_devices", available_devices_func, METH_NOARGS, ""},
496         {"system_info", system_info_func, METH_NOARGS, ""},
497         {NULL, NULL, 0, NULL},
498 };
499
500 static struct PyModuleDef module = {
501         PyModuleDef_HEAD_INIT,
502         "_cycles",
503         "Blender cycles render integration",
504         -1,
505         methods,
506         NULL, NULL, NULL, NULL
507 };
508
509 static CCLDeviceInfo *compute_device_list(DeviceType type)
510 {
511         /* device list stored static */
512         static ccl::vector<CCLDeviceInfo> device_list;
513         static ccl::DeviceType device_type = DEVICE_NONE;
514
515         /* create device list if it's not already done */
516         if(type != device_type) {
517                 ccl::vector<DeviceInfo>& devices = ccl::Device::available_devices();
518
519                 device_type = type;
520                 device_list.clear();
521
522                 /* add devices */
523                 int i = 0;
524
525                 foreach(DeviceInfo& info, devices) {
526                         if(info.type == type ||
527                            (info.type == DEVICE_MULTI && info.multi_devices[0].type == type))
528                         {
529                                 CCLDeviceInfo cinfo;
530
531                                 strncpy(cinfo.identifier, info.id.c_str(), sizeof(cinfo.identifier));
532                                 cinfo.identifier[info.id.length()] = '\0';
533
534                                 strncpy(cinfo.name, info.description.c_str(), sizeof(cinfo.name));
535                                 cinfo.name[info.description.length()] = '\0';
536
537                                 cinfo.value = i++;
538
539                                 device_list.push_back(cinfo);
540                         }
541                 }
542
543                 /* null terminate */
544                 if(!device_list.empty()) {
545                         CCLDeviceInfo cinfo = {"", "", 0};
546                         device_list.push_back(cinfo);
547                 }
548         }
549
550         return (device_list.empty())? NULL: &device_list[0];
551 }
552
553
554 CCL_NAMESPACE_END
555
556 void *CCL_python_module_init()
557 {
558         PyObject *mod = PyModule_Create(&ccl::module);
559
560 #ifdef WITH_OSL
561         /* TODO(sergey): This gives us library we've been linking against.
562          *               In theory with dynamic OSL library it might not be
563          *               accurate, but there's nothing in OSL API which we
564          *               might use to get version in runtime.
565          */
566         int curversion = OSL_LIBRARY_VERSION_CODE;
567         PyModule_AddObject(mod, "with_osl", Py_True);
568         Py_INCREF(Py_True);
569         PyModule_AddObject(mod, "osl_version",
570                            Py_BuildValue("(iii)",
571                                           curversion / 10000, (curversion / 100) % 100, curversion % 100));
572         PyModule_AddObject(mod, "osl_version_string",
573                            PyUnicode_FromFormat("%2d, %2d, %2d",
574                                                 curversion / 10000, (curversion / 100) % 100, curversion % 100));
575 #else
576         PyModule_AddObject(mod, "with_osl", Py_False);
577         Py_INCREF(Py_False);
578         PyModule_AddStringConstant(mod, "osl_version", "unknown");
579         PyModule_AddStringConstant(mod, "osl_version_string", "unknown");
580 #endif
581
582 #ifdef WITH_NETWORK
583         PyModule_AddObject(mod, "with_network", Py_True);
584         Py_INCREF(Py_True);
585 #else /* WITH_NETWORK */
586         PyModule_AddObject(mod, "with_network", Py_False);
587         Py_INCREF(Py_False);
588 #endif /* WITH_NETWORK */
589
590         return (void*)mod;
591 }
592
593 CCLDeviceInfo *CCL_compute_device_list(int device_type)
594 {
595         ccl::DeviceType type;
596         switch(device_type) {
597                 case 0:
598                         type = ccl::DEVICE_CUDA;
599                         break;
600                 case 1:
601                         type = ccl::DEVICE_OPENCL;
602                         break;
603                 case 2:
604                         type = ccl::DEVICE_NETWORK;
605                         break;
606                 default:
607                         type = ccl::DEVICE_NONE;
608                         break;
609         }
610         return ccl::compute_device_list(type);
611 }
612