Custom Group Node type for extending existing nodes from python scripts. This is...
[blender.git] / intern / cycles / blender / blender_object.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 "camera.h"
20 #include "integrator.h"
21 #include "graph.h"
22 #include "light.h"
23 #include "mesh.h"
24 #include "object.h"
25 #include "scene.h"
26 #include "nodes.h"
27 #include "particles.h"
28 #include "shader.h"
29
30 #include "blender_sync.h"
31 #include "blender_util.h"
32
33 #include "util_foreach.h"
34 #include "util_hash.h"
35
36 CCL_NAMESPACE_BEGIN
37
38 /* Utilities */
39
40 bool BlenderSync::BKE_object_is_modified(BL::Object b_ob)
41 {
42         /* test if we can instance or if the object is modified */
43         if(ccl::BKE_object_is_modified(b_ob, b_scene, preview)) {
44                 /* modifiers */
45                 return true;
46         }
47         else {
48                 /* object level material links */
49                 BL::Object::material_slots_iterator slot;
50                 for(b_ob.material_slots.begin(slot); slot != b_ob.material_slots.end(); ++slot)
51                         if(slot->link() == BL::MaterialSlot::link_OBJECT)
52                                 return true;
53         }
54
55         return false;
56 }
57
58 bool BlenderSync::object_is_mesh(BL::Object b_ob)
59 {
60         BL::ID b_ob_data = b_ob.data();
61
62         return (b_ob_data && (b_ob_data.is_a(&RNA_Mesh) ||
63                 b_ob_data.is_a(&RNA_Curve) || b_ob_data.is_a(&RNA_MetaBall)));
64 }
65
66 bool BlenderSync::object_is_light(BL::Object b_ob)
67 {
68         BL::ID b_ob_data = b_ob.data();
69
70         return (b_ob_data && b_ob_data.is_a(&RNA_Lamp));
71 }
72
73 static uint object_ray_visibility(BL::Object b_ob)
74 {
75         PointerRNA cvisibility = RNA_pointer_get(&b_ob.ptr, "cycles_visibility");
76         uint flag = 0;
77
78         flag |= get_boolean(cvisibility, "camera")? PATH_RAY_CAMERA: 0;
79         flag |= get_boolean(cvisibility, "diffuse")? PATH_RAY_DIFFUSE: 0;
80         flag |= get_boolean(cvisibility, "glossy")? PATH_RAY_GLOSSY: 0;
81         flag |= get_boolean(cvisibility, "transmission")? PATH_RAY_TRANSMIT: 0;
82         flag |= get_boolean(cvisibility, "shadow")? PATH_RAY_SHADOW: 0;
83
84         return flag;
85 }
86
87 /* Light */
88
89 void BlenderSync::sync_light(BL::Object b_parent, int persistent_id[OBJECT_PERSISTENT_ID_SIZE], BL::Object b_ob, Transform& tfm)
90 {
91         /* test if we need to sync */
92         Light *light;
93         ObjectKey key(b_parent, persistent_id, b_ob);
94
95         if(!light_map.sync(&light, b_ob, b_parent, key))
96                 return;
97         
98         BL::Lamp b_lamp(b_ob.data());
99
100         /* type */
101         switch(b_lamp.type()) {
102                 case BL::Lamp::type_POINT: {
103                         BL::PointLamp b_point_lamp(b_lamp);
104                         light->size = b_point_lamp.shadow_soft_size();
105                         light->type = LIGHT_POINT;
106                         break;
107                 }
108                 case BL::Lamp::type_SPOT: {
109                         BL::SpotLamp b_spot_lamp(b_lamp);
110                         light->size = b_spot_lamp.shadow_soft_size();
111                         light->type = LIGHT_SPOT;
112                         light->spot_angle = b_spot_lamp.spot_size();
113                         light->spot_smooth = b_spot_lamp.spot_blend();
114                         break;
115                 }
116                 case BL::Lamp::type_HEMI: {
117                         light->type = LIGHT_DISTANT;
118                         light->size = 0.0f;
119                         break;
120                 }
121                 case BL::Lamp::type_SUN: {
122                         BL::SunLamp b_sun_lamp(b_lamp);
123                         light->size = b_sun_lamp.shadow_soft_size();
124                         light->type = LIGHT_DISTANT;
125                         break;
126                 }
127                 case BL::Lamp::type_AREA: {
128                         BL::AreaLamp b_area_lamp(b_lamp);
129                         light->size = 1.0f;
130                         light->axisu = transform_get_column(&tfm, 0);
131                         light->axisv = transform_get_column(&tfm, 1);
132                         light->sizeu = b_area_lamp.size();
133                         if(b_area_lamp.shape() == BL::AreaLamp::shape_RECTANGLE)
134                                 light->sizev = b_area_lamp.size_y();
135                         else
136                                 light->sizev = light->sizeu;
137                         light->type = LIGHT_AREA;
138                         break;
139                 }
140         }
141
142         /* location and (inverted!) direction */
143         light->co = transform_get_column(&tfm, 3);
144         light->dir = -transform_get_column(&tfm, 2);
145
146         /* shader */
147         vector<uint> used_shaders;
148
149         find_shader(b_lamp, used_shaders, scene->default_light);
150
151         if(used_shaders.size() == 0)
152                 used_shaders.push_back(scene->default_light);
153
154         light->shader = used_shaders[0];
155
156         /* shadow */
157         PointerRNA clamp = RNA_pointer_get(&b_lamp.ptr, "cycles");
158         light->cast_shadow = get_boolean(clamp, "cast_shadow");
159         light->use_mis = get_boolean(clamp, "use_multiple_importance_sampling");
160         light->samples = get_int(clamp, "samples");
161
162         /* visibility */
163         uint visibility = object_ray_visibility(b_ob);
164         light->use_diffuse = (visibility & PATH_RAY_DIFFUSE) != 0;
165         light->use_glossy = (visibility & PATH_RAY_GLOSSY) != 0;
166         light->use_transmission = (visibility & PATH_RAY_TRANSMIT) != 0;
167
168         /* tag */
169         light->tag_update(scene);
170 }
171
172 void BlenderSync::sync_background_light()
173 {
174         BL::World b_world = b_scene.world();
175
176         if(b_world) {
177                 PointerRNA cworld = RNA_pointer_get(&b_world.ptr, "cycles");
178                 bool sample_as_light = get_boolean(cworld, "sample_as_light");
179
180                 if(sample_as_light) {
181                         /* test if we need to sync */
182                         Light *light;
183                         ObjectKey key(b_world, 0, b_world);
184
185                         if(light_map.sync(&light, b_world, b_world, key) ||
186                            world_recalc ||
187                            b_world.ptr.data != world_map)
188                         {
189                                 light->type = LIGHT_BACKGROUND;
190                                 light->map_resolution  = get_int(cworld, "sample_map_resolution");
191                                 light->samples  = get_int(cworld, "samples");
192                                 light->shader = scene->default_background;
193
194                                 light->tag_update(scene);
195                                 light_map.set_recalc(b_world);
196                         }
197                 }
198         }
199
200         world_map = b_world.ptr.data;
201         world_recalc = false;
202 }
203
204 /* Object */
205
206 Object *BlenderSync::sync_object(BL::Object b_parent, int persistent_id[OBJECT_PERSISTENT_ID_SIZE], BL::DupliObject b_dupli_ob, Transform& tfm, uint layer_flag, int motion, bool hide_tris)
207 {
208         BL::Object b_ob = (b_dupli_ob ? b_dupli_ob.object() : b_parent);
209         
210         /* light is handled separately */
211         if(object_is_light(b_ob)) {
212                 if(!motion)
213                         sync_light(b_parent, persistent_id, b_ob, tfm);
214
215                 return NULL;
216         }
217
218         /* only interested in object that we can create meshes from */
219         if(!object_is_mesh(b_ob))
220                 return NULL;
221
222         /* key to lookup object */
223         ObjectKey key(b_parent, persistent_id, b_ob);
224         Object *object;
225
226         /* motion vector case */
227         if(motion) {
228                 object = object_map.find(key);
229
230                 if(object) {
231                         if(tfm != object->tfm) {
232                                 if(motion == -1)
233                                         object->motion.pre = tfm;
234                                 else
235                                         object->motion.post = tfm;
236
237                                 object->use_motion = true;
238                         }
239
240                         /* mesh deformation blur not supported yet */
241                         if(!scene->integrator->motion_blur)
242                                 sync_mesh_motion(b_ob, object->mesh, motion);
243                 }
244
245                 return object;
246         }
247
248         /* test if we need to sync */
249         bool object_updated = false;
250
251         if(object_map.sync(&object, b_ob, b_parent, key))
252                 object_updated = true;
253         
254         bool use_holdout = (layer_flag & render_layer.holdout_layer) != 0;
255         
256         /* mesh sync */
257         object->mesh = sync_mesh(b_ob, object_updated, hide_tris);
258
259         /* sspecial case not tracked by object update flags */
260         if(use_holdout != object->use_holdout) {
261                 object->use_holdout = use_holdout;
262                 scene->object_manager->tag_update(scene);
263                 object_updated = true;
264         }
265
266         /* object sync
267          * transform comparison should not be needed, but duplis don't work perfect
268          * in the depsgraph and may not signal changes, so this is a workaround */
269         if(object_updated || (object->mesh && object->mesh->need_update) || tfm != object->tfm) {
270                 object->name = b_ob.name().c_str();
271                 object->pass_id = b_ob.pass_index();
272                 object->tfm = tfm;
273                 object->motion.pre = tfm;
274                 object->motion.post = tfm;
275                 object->use_motion = false;
276
277                 /* random number */
278                 object->random_id = hash_string(object->name.c_str());
279
280                 if(persistent_id) {
281                         for(int i = 0; i < OBJECT_PERSISTENT_ID_SIZE; i++)
282                                 object->random_id = hash_int_2d(object->random_id, persistent_id[i]);
283                 }
284                 else
285                         object->random_id = hash_int_2d(object->random_id, 0);
286
287                 /* visibility flags for both parent */
288                 object->visibility = object_ray_visibility(b_ob) & PATH_RAY_ALL;
289                 if(b_parent.ptr.data != b_ob.ptr.data) {
290                         object->visibility &= object_ray_visibility(b_parent);
291                         object->random_id ^= hash_int(hash_string(b_parent.name().c_str()));
292                 }
293
294                 /* make holdout objects on excluded layer invisible for non-camera rays */
295                 if(use_holdout && (layer_flag & render_layer.exclude_layer))
296                         object->visibility &= ~(PATH_RAY_ALL - PATH_RAY_CAMERA);
297
298                 /* camera flag is not actually used, instead is tested
299                  * against render layer flags */
300                 if(object->visibility & PATH_RAY_CAMERA) {
301                         object->visibility |= layer_flag << PATH_RAY_LAYER_SHIFT;
302                         object->visibility &= ~PATH_RAY_CAMERA;
303                 }
304
305                 if (b_dupli_ob) {
306                         object->dupli_generated = get_float3(b_dupli_ob.orco());
307                         object->dupli_uv = get_float2(b_dupli_ob.uv());
308                 }
309                 else {
310                         object->dupli_generated = make_float3(0.0f, 0.0f, 0.0f);
311                         object->dupli_uv = make_float2(0.0f, 0.0f);
312                 }
313
314                 object->tag_update(scene);
315         }
316
317         return object;
318 }
319
320 static bool object_render_hide_original(BL::Object::dupli_type_enum dupli_type)
321 {
322         return (dupli_type == BL::Object::dupli_type_VERTS ||
323                 dupli_type == BL::Object::dupli_type_FACES ||
324                 dupli_type == BL::Object::dupli_type_FRAMES);
325 }
326
327 static bool object_render_hide(BL::Object b_ob, bool top_level, bool parent_hide, bool& hide_triangles)
328 {
329         /* check if we should render or hide particle emitter */
330         BL::Object::particle_systems_iterator b_psys;
331
332         bool hair_present = false;
333         bool show_emitter = false;
334         bool hide = false;
335
336         for(b_ob.particle_systems.begin(b_psys); b_psys != b_ob.particle_systems.end(); ++b_psys) {
337                 if((b_psys->settings().render_type() == BL::ParticleSettings::render_type_PATH) &&
338                    (b_psys->settings().type()==BL::ParticleSettings::type_HAIR))
339                         hair_present = true;
340
341                 if(b_psys->settings().use_render_emitter()) {
342                         hide = false;
343                         show_emitter = true;
344                 }
345         }
346
347         /* duplicators hidden by default, except dupliframes which duplicate self */
348         if(b_ob.is_duplicator())
349                 if(top_level || b_ob.dupli_type() != BL::Object::dupli_type_FRAMES)
350                         hide = true;
351
352         /* hide original object for duplis */
353         BL::Object parent = b_ob.parent();
354         if(parent && object_render_hide_original(parent.dupli_type()))
355                 if(parent_hide)
356                         hide = true;
357
358         hide_triangles = (hair_present && !show_emitter);
359         return hide && !show_emitter;
360 }
361
362 static bool object_render_hide_duplis(BL::Object b_ob)
363 {
364         BL::Object parent = b_ob.parent();
365
366         return (parent && object_render_hide_original(parent.dupli_type()));
367 }
368
369 /* Object Loop */
370
371 void BlenderSync::sync_objects(BL::SpaceView3D b_v3d, int motion)
372 {
373         /* layer data */
374         uint scene_layer = render_layer.scene_layer;
375         
376         if(!motion) {
377                 /* prepare for sync */
378                 light_map.pre_sync();
379                 mesh_map.pre_sync();
380                 object_map.pre_sync();
381                 mesh_synced.clear();
382                 particle_system_map.pre_sync();
383         }
384
385         /* object loop */
386         BL::Scene::objects_iterator b_ob;
387         BL::Scene b_sce = b_scene;
388
389         /* global particle index counter */
390         int particle_id = 1;
391
392         bool cancel = false;
393
394         for(; b_sce && !cancel; b_sce = b_sce.background_set()) {
395                 for(b_sce.objects.begin(b_ob); b_ob != b_sce.objects.end() && !cancel; ++b_ob) {
396                         bool hide = (render_layer.use_viewport_visibility)? b_ob->hide(): b_ob->hide_render();
397                         uint ob_layer = get_layer(b_ob->layers(), b_ob->layers_local_view(), render_layer.use_localview, object_is_light(*b_ob));
398                         hide = hide || !(ob_layer & scene_layer);
399
400                         if(!hide) {
401                                 progress.set_sync_status("Synchronizing object", (*b_ob).name());
402
403                                 if(b_ob->is_duplicator() && !object_render_hide_duplis(*b_ob)) {
404                                         /* dupli objects */
405                                         b_ob->dupli_list_create(b_scene, 2);
406
407                                         BL::Object::dupli_list_iterator b_dup;
408
409                                         for(b_ob->dupli_list.begin(b_dup); b_dup != b_ob->dupli_list.end(); ++b_dup) {
410                                                 Transform tfm = get_transform(b_dup->matrix());
411                                                 BL::Object b_dup_ob = b_dup->object();
412                                                 bool dup_hide = (b_v3d)? b_dup_ob.hide(): b_dup_ob.hide_render();
413                                                 bool in_dupli_group = (b_dup->type() == BL::DupliObject::type_GROUP);
414                                                 bool hide_tris;
415
416                                                 if(!(b_dup->hide() || dup_hide || object_render_hide(b_dup_ob, false, in_dupli_group, hide_tris))) {
417                                                         /* the persistent_id allows us to match dupli objects
418                                                          * between frames and updates */
419                                                         BL::Array<int, OBJECT_PERSISTENT_ID_SIZE> persistent_id = b_dup->persistent_id();
420
421                                                         /* sync object and mesh or light data */
422                                                         Object *object = sync_object(*b_ob, persistent_id.data, *b_dup, tfm, ob_layer, motion, hide_tris);
423
424                                                         /* sync possible particle data, note particle_id
425                                                          * starts counting at 1, first is dummy particle */
426                                                         if(!motion && object && sync_dupli_particle(*b_ob, *b_dup, object)) {
427                                                                 if(particle_id != object->particle_id) {
428                                                                         object->particle_id = particle_id;
429                                                                         scene->object_manager->tag_update(scene);
430                                                                 }
431
432                                                                 particle_id++;
433                                                         }
434
435                                                 }
436                                         }
437
438                                         b_ob->dupli_list_clear();
439                                 }
440
441                                 /* test if object needs to be hidden */
442                                 bool hide_tris;
443
444                                 if(!object_render_hide(*b_ob, true, true, hide_tris)) {
445                                         /* object itself */
446                                         Transform tfm = get_transform(b_ob->matrix_world());
447                                         sync_object(*b_ob, NULL, PointerRNA_NULL, tfm, ob_layer, motion, hide_tris);
448                                 }
449                         }
450
451                         cancel = progress.get_cancel();
452                 }
453         }
454
455         progress.set_sync_status("");
456
457         if(!cancel && !motion) {
458                 sync_background_light();
459
460                 /* handle removed data and modified pointers */
461                 if(light_map.post_sync())
462                         scene->light_manager->tag_update(scene);
463                 if(mesh_map.post_sync())
464                         scene->mesh_manager->tag_update(scene);
465                 if(object_map.post_sync())
466                         scene->object_manager->tag_update(scene);
467                 if(particle_system_map.post_sync())
468                         scene->particle_system_manager->tag_update(scene);
469                 mesh_synced.clear();
470         }
471 }
472
473 void BlenderSync::sync_motion(BL::SpaceView3D b_v3d, BL::Object b_override)
474 {
475         if(scene->need_motion() == Scene::MOTION_NONE)
476                 return;
477
478         /* get camera object here to deal with camera switch */
479         BL::Object b_cam = b_scene.camera();
480         if(b_override)
481                 b_cam = b_override;
482
483         Camera prevcam = *(scene->camera);
484         
485         /* go back and forth one frame */
486         int frame = b_scene.frame_current();
487
488         for(int motion = -1; motion <= 1; motion += 2) {
489                 b_scene.frame_set(frame + motion, 0.0f);
490
491                 /* camera object */
492                 if(b_cam)
493                         sync_camera_motion(b_cam, motion);
494
495                 /* mesh objects */
496                 sync_objects(b_v3d, motion);
497         }
498
499         b_scene.frame_set(frame, 0.0f);
500
501         /* tag camera for motion update */
502         if(scene->camera->motion_modified(prevcam))
503                 scene->camera->tag_update();
504 }
505
506 CCL_NAMESPACE_END
507