Fix Cycles to mostly work with render layer / depsgraph changes.
[blender.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 "blender/CCL_api.h"
20
21 #include "blender/blender_sync.h"
22 #include "blender/blender_session.h"
23
24 #include "util/util_debug.h"
25 #include "util/util_foreach.h"
26 #include "util/util_logging.h"
27 #include "util/util_md5.h"
28 #include "util/util_opengl.h"
29 #include "util/util_path.h"
30 #include "util/util_string.h"
31 #include "util/util_types.h"
32
33 #ifdef WITH_OSL
34 #include "render/osl.h"
35
36 #include <OSL/oslquery.h>
37 #include <OSL/oslconfig.h>
38 #endif
39
40 CCL_NAMESPACE_BEGIN
41
42 namespace {
43
44 /* Flag describing whether debug flags were synchronized from scene. */
45 bool debug_flags_set = false;
46
47 void *pylong_as_voidptr_typesafe(PyObject *object)
48 {
49         if(object == Py_None)
50                 return NULL;
51         return PyLong_AsVoidPtr(object);
52 }
53
54 /* Synchronize debug flags from a given Blender scene.
55  * Return truth when device list needs invalidation.
56  */
57 bool debug_flags_sync_from_scene(BL::Scene b_scene)
58 {
59         DebugFlagsRef flags = DebugFlags();
60         PointerRNA cscene = RNA_pointer_get(&b_scene.ptr, "cycles");
61         /* Backup some settings for comparison. */
62         DebugFlags::OpenCL::DeviceType opencl_device_type = flags.opencl.device_type;
63         DebugFlags::OpenCL::KernelType opencl_kernel_type = flags.opencl.kernel_type;
64         /* Synchronize shared flags. */
65         flags.viewport_static_bvh = get_enum(cscene, "debug_bvh_type");
66         /* Synchronize CPU flags. */
67         flags.cpu.avx2 = get_boolean(cscene, "debug_use_cpu_avx2");
68         flags.cpu.avx = get_boolean(cscene, "debug_use_cpu_avx");
69         flags.cpu.sse41 = get_boolean(cscene, "debug_use_cpu_sse41");
70         flags.cpu.sse3 = get_boolean(cscene, "debug_use_cpu_sse3");
71         flags.cpu.sse2 = get_boolean(cscene, "debug_use_cpu_sse2");
72         flags.cpu.bvh_layout = (BVHLayout)get_enum(cscene, "debug_bvh_layout");
73         flags.cpu.split_kernel = get_boolean(cscene, "debug_use_cpu_split_kernel");
74         /* Synchronize CUDA flags. */
75         flags.cuda.adaptive_compile = get_boolean(cscene, "debug_use_cuda_adaptive_compile");
76         flags.cuda.split_kernel = get_boolean(cscene, "debug_use_cuda_split_kernel");
77         /* Synchronize OpenCL kernel type. */
78         switch(get_enum(cscene, "debug_opencl_kernel_type")) {
79                 case 0:
80                         flags.opencl.kernel_type = DebugFlags::OpenCL::KERNEL_DEFAULT;
81                         break;
82                 case 1:
83                         flags.opencl.kernel_type = DebugFlags::OpenCL::KERNEL_MEGA;
84                         break;
85                 case 2:
86                         flags.opencl.kernel_type = DebugFlags::OpenCL::KERNEL_SPLIT;
87                         break;
88         }
89         /* Synchronize OpenCL device type. */
90         switch(get_enum(cscene, "debug_opencl_device_type")) {
91                 case 0:
92                         flags.opencl.device_type = DebugFlags::OpenCL::DEVICE_NONE;
93                         break;
94                 case 1:
95                         flags.opencl.device_type = DebugFlags::OpenCL::DEVICE_ALL;
96                         break;
97                 case 2:
98                         flags.opencl.device_type = DebugFlags::OpenCL::DEVICE_DEFAULT;
99                         break;
100                 case 3:
101                         flags.opencl.device_type = DebugFlags::OpenCL::DEVICE_CPU;
102                         break;
103                 case 4:
104                         flags.opencl.device_type = DebugFlags::OpenCL::DEVICE_GPU;
105                         break;
106                 case 5:
107                         flags.opencl.device_type = DebugFlags::OpenCL::DEVICE_ACCELERATOR;
108                         break;
109         }
110         /* Synchronize other OpenCL flags. */
111         flags.opencl.debug = get_boolean(cscene, "debug_use_opencl_debug");
112         flags.opencl.mem_limit = ((size_t)get_int(cscene, "debug_opencl_mem_limit"))*1024*1024;
113         flags.opencl.single_program = get_boolean(cscene, "debug_opencl_kernel_single_program");
114         return flags.opencl.device_type != opencl_device_type ||
115                flags.opencl.kernel_type != opencl_kernel_type;
116 }
117
118 /* Reset debug flags to default values.
119  * Return truth when device list needs invalidation.
120  */
121 bool debug_flags_reset()
122 {
123         DebugFlagsRef flags = DebugFlags();
124         /* Backup some settings for comparison. */
125         DebugFlags::OpenCL::DeviceType opencl_device_type = flags.opencl.device_type;
126         DebugFlags::OpenCL::KernelType opencl_kernel_type = flags.opencl.kernel_type;
127         flags.reset();
128         return flags.opencl.device_type != opencl_device_type ||
129                flags.opencl.kernel_type != opencl_kernel_type;
130 }
131
132 }  /* namespace */
133
134 void python_thread_state_save(void **python_thread_state)
135 {
136         *python_thread_state = (void*)PyEval_SaveThread();
137 }
138
139 void python_thread_state_restore(void **python_thread_state)
140 {
141         PyEval_RestoreThread((PyThreadState*)*python_thread_state);
142         *python_thread_state = NULL;
143 }
144
145 static const char *PyC_UnicodeAsByte(PyObject *py_str, PyObject **coerce)
146 {
147         const char *result = _PyUnicode_AsString(py_str);
148         if(result) {
149                 /* 99% of the time this is enough but we better support non unicode
150                  * chars since blender doesnt limit this.
151                  */
152                 return result;
153         }
154         else {
155                 PyErr_Clear();
156                 if(PyBytes_Check(py_str)) {
157                         return PyBytes_AS_STRING(py_str);
158                 }
159                 else if((*coerce = PyUnicode_EncodeFSDefault(py_str))) {
160                         return PyBytes_AS_STRING(*coerce);
161                 }
162                 else {
163                         /* Clear the error, so Cycles can be at leadt used without
164                          * GPU and OSL support,
165                          */
166                         PyErr_Clear();
167                         return "";
168                 }
169         }
170 }
171
172 static PyObject *init_func(PyObject * /*self*/, PyObject *args)
173 {
174         PyObject *path, *user_path;
175         int headless;
176
177         if(!PyArg_ParseTuple(args, "OOi", &path, &user_path, &headless)) {
178                 return NULL;
179         }
180
181         PyObject *path_coerce = NULL, *user_path_coerce = NULL;
182         path_init(PyC_UnicodeAsByte(path, &path_coerce),
183                   PyC_UnicodeAsByte(user_path, &user_path_coerce));
184         Py_XDECREF(path_coerce);
185         Py_XDECREF(user_path_coerce);
186
187         BlenderSession::headless = headless;
188
189         VLOG(2) << "Debug flags initialized to:\n"
190                 << DebugFlags();
191
192         Py_RETURN_NONE;
193 }
194
195
196 static PyObject *exit_func(PyObject * /*self*/, PyObject * /*args*/)
197 {
198         ShaderManager::free_memory();
199         TaskScheduler::free_memory();
200         Device::free_memory();
201         Py_RETURN_NONE;
202 }
203
204 static PyObject *create_func(PyObject * /*self*/, PyObject *args)
205 {
206         PyObject *pyengine, *pyuserpref, *pydata, *pyscene, *pyregion, *pyv3d, *pyrv3d;
207         int preview_osl;
208
209         if(!PyArg_ParseTuple(args, "OOOOOOOi", &pyengine, &pyuserpref, &pydata, &pyscene,
210                              &pyregion, &pyv3d, &pyrv3d, &preview_osl))
211         {
212                 return NULL;
213         }
214
215         /* RNA */
216         PointerRNA engineptr;
217         RNA_pointer_create(NULL, &RNA_RenderEngine, (void*)PyLong_AsVoidPtr(pyengine), &engineptr);
218         BL::RenderEngine engine(engineptr);
219
220         PointerRNA userprefptr;
221         RNA_pointer_create(NULL, &RNA_UserPreferences, (void*)PyLong_AsVoidPtr(pyuserpref), &userprefptr);
222         BL::UserPreferences userpref(userprefptr);
223
224         PointerRNA dataptr;
225         RNA_main_pointer_create((Main*)PyLong_AsVoidPtr(pydata), &dataptr);
226         BL::BlendData data(dataptr);
227
228         PointerRNA sceneptr;
229         RNA_id_pointer_create((ID*)PyLong_AsVoidPtr(pyscene), &sceneptr);
230         BL::Scene scene(sceneptr);
231
232         PointerRNA regionptr;
233         RNA_pointer_create(NULL, &RNA_Region, pylong_as_voidptr_typesafe(pyregion), &regionptr);
234         BL::Region region(regionptr);
235
236         PointerRNA v3dptr;
237         RNA_pointer_create(NULL, &RNA_SpaceView3D, pylong_as_voidptr_typesafe(pyv3d), &v3dptr);
238         BL::SpaceView3D v3d(v3dptr);
239
240         PointerRNA rv3dptr;
241         RNA_pointer_create(NULL, &RNA_RegionView3D, pylong_as_voidptr_typesafe(pyrv3d), &rv3dptr);
242         BL::RegionView3D rv3d(rv3dptr);
243
244         /* create session */
245         BlenderSession *session;
246
247         if(rv3d) {
248                 /* interactive viewport session */
249                 int width = region.width();
250                 int height = region.height();
251
252                 session = new BlenderSession(engine, userpref, data, scene, v3d, rv3d, width, height);
253         }
254         else {
255                 /* override some settings for preview */
256                 if(engine.is_preview()) {
257                         PointerRNA cscene = RNA_pointer_get(&sceneptr, "cycles");
258
259                         RNA_boolean_set(&cscene, "shading_system", preview_osl);
260                         RNA_boolean_set(&cscene, "use_progressive_refine", true);
261                 }
262
263                 /* offline session or preview render */
264                 session = new BlenderSession(engine, userpref, data, scene);
265         }
266
267         python_thread_state_save(&session->python_thread_state);
268
269         session->create();
270
271         python_thread_state_restore(&session->python_thread_state);
272
273         return PyLong_FromVoidPtr(session);
274 }
275
276 static PyObject *free_func(PyObject * /*self*/, PyObject *value)
277 {
278         delete (BlenderSession*)PyLong_AsVoidPtr(value);
279
280         Py_RETURN_NONE;
281 }
282
283 static PyObject *render_func(PyObject * /*self*/, PyObject *args)
284 {
285         PyObject *pysession, *pydepsgraph;
286
287         if(!PyArg_ParseTuple(args, "OO", &pysession, &pydepsgraph))
288                 return NULL;
289
290         BlenderSession *session = (BlenderSession*)PyLong_AsVoidPtr(pysession);
291
292         PointerRNA depsgraphptr;
293         RNA_pointer_create(NULL, &RNA_Depsgraph, (ID*)PyLong_AsVoidPtr(pydepsgraph), &depsgraphptr);
294         BL::Depsgraph b_depsgraph(depsgraphptr);
295
296         python_thread_state_save(&session->python_thread_state);
297
298         session->render(b_depsgraph);
299
300         python_thread_state_restore(&session->python_thread_state);
301
302         Py_RETURN_NONE;
303 }
304
305 /* pixel_array and result passed as pointers */
306 static PyObject *bake_func(PyObject * /*self*/, PyObject *args)
307 {
308         PyObject *pysession, *pydepsgraph, *pyobject;
309         PyObject *pypixel_array, *pyresult;
310         const char *pass_type;
311         int num_pixels, depth, object_id, pass_filter;
312
313         if(!PyArg_ParseTuple(args, "OOOsiiOiiO", &pysession, &pydepsgraph, &pyobject, &pass_type, &pass_filter, &object_id, &pypixel_array, &num_pixels, &depth, &pyresult))
314                 return NULL;
315
316         BlenderSession *session = (BlenderSession*)PyLong_AsVoidPtr(pysession);
317
318         PointerRNA depsgraphptr;
319         RNA_id_pointer_create((ID*)PyLong_AsVoidPtr(pydepsgraph), &depsgraphptr);
320         BL::Depsgraph b_depsgraph(depsgraphptr);
321
322         PointerRNA objectptr;
323         RNA_id_pointer_create((ID*)PyLong_AsVoidPtr(pyobject), &objectptr);
324         BL::Object b_object(objectptr);
325
326         void *b_result = PyLong_AsVoidPtr(pyresult);
327
328         PointerRNA bakepixelptr;
329         RNA_pointer_create(NULL, &RNA_BakePixel, PyLong_AsVoidPtr(pypixel_array), &bakepixelptr);
330         BL::BakePixel b_bake_pixel(bakepixelptr);
331
332         python_thread_state_save(&session->python_thread_state);
333
334         session->bake(b_depsgraph, b_object, pass_type, pass_filter, object_id, b_bake_pixel, (size_t)num_pixels, depth, (float *)b_result);
335
336         python_thread_state_restore(&session->python_thread_state);
337
338         Py_RETURN_NONE;
339 }
340
341 static PyObject *draw_func(PyObject * /*self*/, PyObject *args)
342 {
343         PyObject *pysession, *pygraph, *pyv3d, *pyrv3d;
344
345         if(!PyArg_ParseTuple(args, "OOOO", &pysession, &pygraph, &pyv3d, &pyrv3d))
346                 return NULL;
347         
348         BlenderSession *session = (BlenderSession*)PyLong_AsVoidPtr(pysession);
349
350         if(PyLong_AsVoidPtr(pyrv3d)) {
351                 /* 3d view drawing */
352                 int viewport[4];
353                 glGetIntegerv(GL_VIEWPORT, viewport);
354
355                 session->draw(viewport[2], viewport[3]);
356         }
357
358         Py_RETURN_NONE;
359 }
360
361 static PyObject *reset_func(PyObject * /*self*/, PyObject *args)
362 {
363         PyObject *pysession, *pydata, *pyscene;
364
365         if(!PyArg_ParseTuple(args, "OOO", &pysession, &pydata, &pyscene))
366                 return NULL;
367
368         BlenderSession *session = (BlenderSession*)PyLong_AsVoidPtr(pysession);
369
370         PointerRNA dataptr;
371         RNA_main_pointer_create((Main*)PyLong_AsVoidPtr(pydata), &dataptr);
372         BL::BlendData b_data(dataptr);
373
374         PointerRNA sceneptr;
375         RNA_id_pointer_create((ID*)PyLong_AsVoidPtr(pyscene), &sceneptr);
376         BL::Scene b_scene(sceneptr);
377
378         python_thread_state_save(&session->python_thread_state);
379
380         session->reset_session(b_data, b_scene);
381
382         python_thread_state_restore(&session->python_thread_state);
383
384         Py_RETURN_NONE;
385 }
386
387 static PyObject *sync_func(PyObject * /*self*/, PyObject *args)
388 {
389         PyObject *pysession, *pydepsgraph;
390
391         if(!PyArg_ParseTuple(args, "OO", &pysession, &pydepsgraph))
392                 return NULL;
393
394         BlenderSession *session = (BlenderSession*)PyLong_AsVoidPtr(pysession);
395
396         PointerRNA depsgraphptr;
397         RNA_id_pointer_create((ID*)PyLong_AsVoidPtr(pydepsgraph), &depsgraphptr);
398         BL::Depsgraph b_depsgraph(depsgraphptr);
399
400         python_thread_state_save(&session->python_thread_state);
401
402         session->synchronize(b_depsgraph);
403
404         python_thread_state_restore(&session->python_thread_state);
405
406         Py_RETURN_NONE;
407 }
408
409 static PyObject *available_devices_func(PyObject * /*self*/, PyObject * /*args*/)
410 {
411         vector<DeviceInfo>& devices = Device::available_devices();
412         PyObject *ret = PyTuple_New(devices.size());
413
414         for(size_t i = 0; i < devices.size(); i++) {
415                 DeviceInfo& device = devices[i];
416                 string type_name = Device::string_from_type(device.type);
417                 PyObject *device_tuple = PyTuple_New(3);
418                 PyTuple_SET_ITEM(device_tuple, 0, PyUnicode_FromString(device.description.c_str()));
419                 PyTuple_SET_ITEM(device_tuple, 1, PyUnicode_FromString(type_name.c_str()));
420                 PyTuple_SET_ITEM(device_tuple, 2, PyUnicode_FromString(device.id.c_str()));
421                 PyTuple_SET_ITEM(ret, i, device_tuple);
422         }
423
424         return ret;
425 }
426
427 #ifdef WITH_OSL
428
429 static PyObject *osl_update_node_func(PyObject * /*self*/, PyObject *args)
430 {
431         PyObject *pynodegroup, *pynode;
432         const char *filepath = NULL;
433
434         if(!PyArg_ParseTuple(args, "OOs", &pynodegroup, &pynode, &filepath))
435                 return NULL;
436
437         /* RNA */
438         PointerRNA nodeptr;
439         RNA_pointer_create((ID*)PyLong_AsVoidPtr(pynodegroup), &RNA_ShaderNodeScript, (void*)PyLong_AsVoidPtr(pynode), &nodeptr);
440         BL::ShaderNodeScript b_node(nodeptr);
441
442         /* update bytecode hash */
443         string bytecode = b_node.bytecode();
444
445         if(!bytecode.empty()) {
446                 MD5Hash md5;
447                 md5.append((const uint8_t*)bytecode.c_str(), bytecode.size());
448                 b_node.bytecode_hash(md5.get_hex().c_str());
449         }
450         else
451                 b_node.bytecode_hash("");
452
453         /* query from file path */
454         OSL::OSLQuery query;
455
456         if(!OSLShaderManager::osl_query(query, filepath))
457                 Py_RETURN_FALSE;
458
459         /* add new sockets from parameters */
460         set<void*> used_sockets;
461
462         for(int i = 0; i < query.nparams(); i++) {
463                 const OSL::OSLQuery::Parameter *param = query.getparam(i);
464
465                 /* skip unsupported types */
466                 if(param->varlenarray || param->isstruct || param->type.arraylen > 1)
467                         continue;
468
469                 /* determine socket type */
470                 string socket_type;
471                 BL::NodeSocket::type_enum data_type = BL::NodeSocket::type_VALUE;
472                 float4 default_float4 = make_float4(0.0f, 0.0f, 0.0f, 1.0f);
473                 float default_float = 0.0f;
474                 int default_int = 0;
475                 string default_string = "";
476
477                 if(param->isclosure) {
478                         socket_type = "NodeSocketShader";
479                         data_type = BL::NodeSocket::type_SHADER;
480                 }
481                 else if(param->type.vecsemantics == TypeDesc::COLOR) {
482                         socket_type = "NodeSocketColor";
483                         data_type = BL::NodeSocket::type_RGBA;
484
485                         if(param->validdefault) {
486                                 default_float4[0] = param->fdefault[0];
487                                 default_float4[1] = param->fdefault[1];
488                                 default_float4[2] = param->fdefault[2];
489                         }
490                 }
491                 else if(param->type.vecsemantics == TypeDesc::POINT ||
492                         param->type.vecsemantics == TypeDesc::VECTOR ||
493                         param->type.vecsemantics == TypeDesc::NORMAL)
494                 {
495                         socket_type = "NodeSocketVector";
496                         data_type = BL::NodeSocket::type_VECTOR;
497
498                         if(param->validdefault) {
499                                 default_float4[0] = param->fdefault[0];
500                                 default_float4[1] = param->fdefault[1];
501                                 default_float4[2] = param->fdefault[2];
502                         }
503                 }
504                 else if(param->type.aggregate == TypeDesc::SCALAR) {
505                         if(param->type.basetype == TypeDesc::INT) {
506                                 socket_type = "NodeSocketInt";
507                                 data_type = BL::NodeSocket::type_INT;
508                                 if(param->validdefault)
509                                         default_int = param->idefault[0];
510                         }
511                         else if(param->type.basetype == TypeDesc::FLOAT) {
512                                 socket_type = "NodeSocketFloat";
513                                 data_type = BL::NodeSocket::type_VALUE;
514                                 if(param->validdefault)
515                                         default_float = param->fdefault[0];
516                         }
517                         else if(param->type.basetype == TypeDesc::STRING) {
518                                 socket_type = "NodeSocketString";
519                                 data_type = BL::NodeSocket::type_STRING;
520                                 if(param->validdefault)
521                                         default_string = param->sdefault[0];
522                         }
523                         else
524                                 continue;
525                 }
526                 else
527                         continue;
528
529                 /* find socket socket */
530                 BL::NodeSocket b_sock(PointerRNA_NULL);
531                 if(param->isoutput) {
532                         b_sock = b_node.outputs[param->name.string()];
533                         /* remove if type no longer matches */
534                         if(b_sock && b_sock.bl_idname() != socket_type) {
535                                 b_node.outputs.remove(b_sock);
536                                 b_sock = BL::NodeSocket(PointerRNA_NULL);
537                         }
538                 }
539                 else {
540                         b_sock = b_node.inputs[param->name.string()];
541                         /* remove if type no longer matches */
542                         if(b_sock && b_sock.bl_idname() != socket_type) {
543                                 b_node.inputs.remove(b_sock);
544                                 b_sock = BL::NodeSocket(PointerRNA_NULL);
545                         }
546                 }
547
548                 if(!b_sock) {
549                         /* create new socket */
550                         if(param->isoutput)
551                                 b_sock = b_node.outputs.create(socket_type.c_str(), param->name.c_str(), param->name.c_str());
552                         else
553                                 b_sock = b_node.inputs.create(socket_type.c_str(), param->name.c_str(), param->name.c_str());
554
555                         /* set default value */
556                         if(data_type == BL::NodeSocket::type_VALUE) {
557                                 set_float(b_sock.ptr, "default_value", default_float);
558                         }
559                         else if(data_type == BL::NodeSocket::type_INT) {
560                                 set_int(b_sock.ptr, "default_value", default_int);
561                         }
562                         else if(data_type == BL::NodeSocket::type_RGBA) {
563                                 set_float4(b_sock.ptr, "default_value", default_float4);
564                         }
565                         else if(data_type == BL::NodeSocket::type_VECTOR) {
566                                 set_float3(b_sock.ptr, "default_value", float4_to_float3(default_float4));
567                         }
568                         else if(data_type == BL::NodeSocket::type_STRING) {
569                                 set_string(b_sock.ptr, "default_value", default_string);
570                         }
571                 }
572
573                 used_sockets.insert(b_sock.ptr.data);
574         }
575
576         /* remove unused parameters */
577         bool removed;
578
579         do {
580                 BL::Node::inputs_iterator b_input;
581                 BL::Node::outputs_iterator b_output;
582
583                 removed = false;
584
585                 for(b_node.inputs.begin(b_input); b_input != b_node.inputs.end(); ++b_input) {
586                         if(used_sockets.find(b_input->ptr.data) == used_sockets.end()) {
587                                 b_node.inputs.remove(*b_input);
588                                 removed = true;
589                                 break;
590                         }
591                 }
592
593                 for(b_node.outputs.begin(b_output); b_output != b_node.outputs.end(); ++b_output) {
594                         if(used_sockets.find(b_output->ptr.data) == used_sockets.end()) {
595                                 b_node.outputs.remove(*b_output);
596                                 removed = true;
597                                 break;
598                         }
599                 }
600         } while(removed);
601
602         Py_RETURN_TRUE;
603 }
604
605 static PyObject *osl_compile_func(PyObject * /*self*/, PyObject *args)
606 {
607         const char *inputfile = NULL, *outputfile = NULL;
608
609         if(!PyArg_ParseTuple(args, "ss", &inputfile, &outputfile))
610                 return NULL;
611         
612         /* return */
613         if(!OSLShaderManager::osl_compile(inputfile, outputfile))
614                 Py_RETURN_FALSE;
615
616         Py_RETURN_TRUE;
617 }
618 #endif
619
620 static PyObject *system_info_func(PyObject * /*self*/, PyObject * /*value*/)
621 {
622         string system_info = Device::device_capabilities();
623         return PyUnicode_FromString(system_info.c_str());
624 }
625
626 #ifdef WITH_OPENCL
627 static PyObject *opencl_disable_func(PyObject * /*self*/, PyObject * /*value*/)
628 {
629         VLOG(2) << "Disabling OpenCL platform.";
630         DebugFlags().opencl.device_type = DebugFlags::OpenCL::DEVICE_NONE;
631         Py_RETURN_NONE;
632 }
633 #endif
634
635 static PyObject *debug_flags_update_func(PyObject * /*self*/, PyObject *args)
636 {
637         PyObject *pyscene;
638         if(!PyArg_ParseTuple(args, "O", &pyscene)) {
639                 return NULL;
640         }
641
642         PointerRNA sceneptr;
643         RNA_id_pointer_create((ID*)PyLong_AsVoidPtr(pyscene), &sceneptr);
644         BL::Scene b_scene(sceneptr);
645
646         if(debug_flags_sync_from_scene(b_scene)) {
647                 VLOG(2) << "Tagging device list for update.";
648                 Device::tag_update();
649         }
650
651         VLOG(2) << "Debug flags set to:\n"
652                 << DebugFlags();
653
654         debug_flags_set = true;
655
656         Py_RETURN_NONE;
657 }
658
659 static PyObject *debug_flags_reset_func(PyObject * /*self*/, PyObject * /*args*/)
660 {
661         if(debug_flags_reset()) {
662                 VLOG(2) << "Tagging device list for update.";
663                 Device::tag_update();
664         }
665         if(debug_flags_set) {
666                 VLOG(2) << "Debug flags reset to:\n"
667                         << DebugFlags();
668                 debug_flags_set = false;
669         }
670         Py_RETURN_NONE;
671 }
672
673 static PyObject *set_resumable_chunk_func(PyObject * /*self*/, PyObject *args)
674 {
675         int num_resumable_chunks, current_resumable_chunk;
676         if(!PyArg_ParseTuple(args, "ii",
677                              &num_resumable_chunks,
678                              &current_resumable_chunk)) {
679                 Py_RETURN_NONE;
680         }
681
682         if(num_resumable_chunks <= 0) {
683                 fprintf(stderr, "Cycles: Bad value for number of resumable chunks.\n");
684                 abort();
685                 Py_RETURN_NONE;
686         }
687         if(current_resumable_chunk < 1 ||
688            current_resumable_chunk > num_resumable_chunks)
689         {
690                 fprintf(stderr, "Cycles: Bad value for current resumable chunk number.\n");
691                 abort();
692                 Py_RETURN_NONE;
693         }
694
695         VLOG(1) << "Initialized resumable render: "
696                 << "num_resumable_chunks=" << num_resumable_chunks << ", "
697                 << "current_resumable_chunk=" << current_resumable_chunk;
698         BlenderSession::num_resumable_chunks = num_resumable_chunks;
699         BlenderSession::current_resumable_chunk = current_resumable_chunk;
700
701         printf("Cycles: Will render chunk %d of %d\n",
702                current_resumable_chunk,
703                num_resumable_chunks);
704
705         Py_RETURN_NONE;
706 }
707
708 static PyObject *set_resumable_chunk_range_func(PyObject * /*self*/, PyObject *args)
709 {
710         int num_chunks, start_chunk, end_chunk;
711         if(!PyArg_ParseTuple(args, "iii",
712                              &num_chunks,
713                              &start_chunk,
714                              &end_chunk)) {
715                 Py_RETURN_NONE;
716         }
717
718         if(num_chunks <= 0) {
719                 fprintf(stderr, "Cycles: Bad value for number of resumable chunks.\n");
720                 abort();
721                 Py_RETURN_NONE;
722         }
723         if(start_chunk < 1 || start_chunk > num_chunks) {
724                 fprintf(stderr, "Cycles: Bad value for start chunk number.\n");
725                 abort();
726                 Py_RETURN_NONE;
727         }
728         if(end_chunk < 1 || end_chunk > num_chunks) {
729                 fprintf(stderr, "Cycles: Bad value for start chunk number.\n");
730                 abort();
731                 Py_RETURN_NONE;
732         }
733         if(start_chunk > end_chunk) {
734                 fprintf(stderr, "Cycles: End chunk should be higher than start one.\n");
735                 abort();
736                 Py_RETURN_NONE;
737         }
738
739         VLOG(1) << "Initialized resumable render: "
740                 << "num_resumable_chunks=" << num_chunks << ", "
741                 << "start_resumable_chunk=" << start_chunk
742                 << "end_resumable_chunk=" << end_chunk;
743         BlenderSession::num_resumable_chunks = num_chunks;
744         BlenderSession::start_resumable_chunk = start_chunk;
745         BlenderSession::end_resumable_chunk = end_chunk;
746
747         printf("Cycles: Will render chunks %d to %d of %d\n",
748                start_chunk,
749                end_chunk,
750                num_chunks);
751
752         Py_RETURN_NONE;
753 }
754
755 static PyObject *get_device_types_func(PyObject * /*self*/, PyObject * /*args*/)
756 {
757         vector<DeviceInfo>& devices = Device::available_devices();
758         bool has_cuda = false, has_opencl = false;
759         for(int i = 0; i < devices.size(); i++) {
760                 has_cuda   |= (devices[i].type == DEVICE_CUDA);
761                 has_opencl |= (devices[i].type == DEVICE_OPENCL);
762         }
763         PyObject *list = PyTuple_New(2);
764         PyTuple_SET_ITEM(list, 0, PyBool_FromLong(has_cuda));
765         PyTuple_SET_ITEM(list, 1, PyBool_FromLong(has_opencl));
766         return list;
767 }
768
769 static PyMethodDef methods[] = {
770         {"init", init_func, METH_VARARGS, ""},
771         {"exit", exit_func, METH_VARARGS, ""},
772         {"create", create_func, METH_VARARGS, ""},
773         {"free", free_func, METH_O, ""},
774         {"render", render_func, METH_VARARGS, ""},
775         {"bake", bake_func, METH_VARARGS, ""},
776         {"draw", draw_func, METH_VARARGS, ""},
777         {"sync", sync_func, METH_VARARGS, ""},
778         {"reset", reset_func, METH_VARARGS, ""},
779 #ifdef WITH_OSL
780         {"osl_update_node", osl_update_node_func, METH_VARARGS, ""},
781         {"osl_compile", osl_compile_func, METH_VARARGS, ""},
782 #endif
783         {"available_devices", available_devices_func, METH_NOARGS, ""},
784         {"system_info", system_info_func, METH_NOARGS, ""},
785 #ifdef WITH_OPENCL
786         {"opencl_disable", opencl_disable_func, METH_NOARGS, ""},
787 #endif
788
789         /* Debugging routines */
790         {"debug_flags_update", debug_flags_update_func, METH_VARARGS, ""},
791         {"debug_flags_reset", debug_flags_reset_func, METH_NOARGS, ""},
792
793         /* Resumable render */
794         {"set_resumable_chunk", set_resumable_chunk_func, METH_VARARGS, ""},
795         {"set_resumable_chunk_range", set_resumable_chunk_range_func, METH_VARARGS, ""},
796
797         /* Compute Device selection */
798         {"get_device_types", get_device_types_func, METH_VARARGS, ""},
799
800         {NULL, NULL, 0, NULL},
801 };
802
803 static struct PyModuleDef module = {
804         PyModuleDef_HEAD_INIT,
805         "_cycles",
806         "Blender cycles render integration",
807         -1,
808         methods,
809         NULL, NULL, NULL, NULL
810 };
811
812 CCL_NAMESPACE_END
813
814 void *CCL_python_module_init()
815 {
816         PyObject *mod = PyModule_Create(&ccl::module);
817
818 #ifdef WITH_OSL
819         /* TODO(sergey): This gives us library we've been linking against.
820          *               In theory with dynamic OSL library it might not be
821          *               accurate, but there's nothing in OSL API which we
822          *               might use to get version in runtime.
823          */
824         int curversion = OSL_LIBRARY_VERSION_CODE;
825         PyModule_AddObject(mod, "with_osl", Py_True);
826         Py_INCREF(Py_True);
827         PyModule_AddObject(mod, "osl_version",
828                            Py_BuildValue("(iii)",
829                                           curversion / 10000, (curversion / 100) % 100, curversion % 100));
830         PyModule_AddObject(mod, "osl_version_string",
831                            PyUnicode_FromFormat("%2d, %2d, %2d",
832                                                 curversion / 10000, (curversion / 100) % 100, curversion % 100));
833 #else
834         PyModule_AddObject(mod, "with_osl", Py_False);
835         Py_INCREF(Py_False);
836         PyModule_AddStringConstant(mod, "osl_version", "unknown");
837         PyModule_AddStringConstant(mod, "osl_version_string", "unknown");
838 #endif
839
840 #ifdef WITH_CYCLES_DEBUG
841         PyModule_AddObject(mod, "with_cycles_debug", Py_True);
842         Py_INCREF(Py_True);
843 #else
844         PyModule_AddObject(mod, "with_cycles_debug", Py_False);
845         Py_INCREF(Py_False);
846 #endif
847
848 #ifdef WITH_NETWORK
849         PyModule_AddObject(mod, "with_network", Py_True);
850         Py_INCREF(Py_True);
851 #else /* WITH_NETWORK */
852         PyModule_AddObject(mod, "with_network", Py_False);
853         Py_INCREF(Py_False);
854 #endif /* WITH_NETWORK */
855
856         return (void*)mod;
857 }