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