edd5f292c790c94092dee4d571ee0d3a920f6a69
[blender.git] / intern / cycles / blender / blender_object.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 "camera.h"
18 #include "integrator.h"
19 #include "graph.h"
20 #include "light.h"
21 #include "mesh.h"
22 #include "object.h"
23 #include "scene.h"
24 #include "nodes.h"
25 #include "particles.h"
26 #include "shader.h"
27
28 #include "blender_sync.h"
29 #include "blender_util.h"
30
31 #include "util_foreach.h"
32 #include "util_hash.h"
33 #include "util_logging.h"
34
35 CCL_NAMESPACE_BEGIN
36
37 /* Utilities */
38
39 bool BlenderSync::BKE_object_is_modified(BL::Object& b_ob)
40 {
41         /* test if we can instance or if the object is modified */
42         if(b_ob.type() == BL::Object::type_META) {
43                 /* multi-user and dupli metaballs are fused, can't instance */
44                 return true;
45         }
46         else if(ccl::BKE_object_is_modified(b_ob, b_scene, preview)) {
47                 /* modifiers */
48                 return true;
49         }
50         else {
51                 /* object level material links */
52                 BL::Object::material_slots_iterator slot;
53                 for(b_ob.material_slots.begin(slot); slot != b_ob.material_slots.end(); ++slot)
54                         if(slot->link() == BL::MaterialSlot::link_OBJECT)
55                                 return true;
56         }
57
58         return false;
59 }
60
61 bool BlenderSync::object_is_mesh(BL::Object& b_ob)
62 {
63         BL::ID b_ob_data = b_ob.data();
64
65         return (b_ob_data && (b_ob_data.is_a(&RNA_Mesh) ||
66                 b_ob_data.is_a(&RNA_Curve) || b_ob_data.is_a(&RNA_MetaBall)));
67 }
68
69 bool BlenderSync::object_is_light(BL::Object& b_ob)
70 {
71         BL::ID b_ob_data = b_ob.data();
72
73         return (b_ob_data && b_ob_data.is_a(&RNA_Lamp));
74 }
75
76 static uint object_ray_visibility(BL::Object& b_ob)
77 {
78         PointerRNA cvisibility = RNA_pointer_get(&b_ob.ptr, "cycles_visibility");
79         uint flag = 0;
80
81         flag |= get_boolean(cvisibility, "camera")? PATH_RAY_CAMERA: 0;
82         flag |= get_boolean(cvisibility, "diffuse")? PATH_RAY_DIFFUSE: 0;
83         flag |= get_boolean(cvisibility, "glossy")? PATH_RAY_GLOSSY: 0;
84         flag |= get_boolean(cvisibility, "transmission")? PATH_RAY_TRANSMIT: 0;
85         flag |= get_boolean(cvisibility, "shadow")? PATH_RAY_SHADOW: 0;
86         flag |= get_boolean(cvisibility, "scatter")? PATH_RAY_VOLUME_SCATTER: 0;
87
88         return flag;
89 }
90
91 /* Light */
92
93 void BlenderSync::sync_light(BL::Object& b_parent,
94                              int persistent_id[OBJECT_PERSISTENT_ID_SIZE],
95                              BL::Object& b_ob,
96                              Transform& tfm,
97                              bool *use_portal)
98 {
99         /* test if we need to sync */
100         Light *light;
101         ObjectKey key(b_parent, persistent_id, b_ob);
102
103         if(!light_map.sync(&light, b_ob, b_parent, key)) {
104                 if(light->is_portal)
105                         *use_portal = true;
106                 return;
107         }
108         
109         BL::Lamp b_lamp(b_ob.data());
110
111         /* type */
112         switch(b_lamp.type()) {
113                 case BL::Lamp::type_POINT: {
114                         BL::PointLamp b_point_lamp(b_lamp);
115                         light->size = b_point_lamp.shadow_soft_size();
116                         light->type = LIGHT_POINT;
117                         break;
118                 }
119                 case BL::Lamp::type_SPOT: {
120                         BL::SpotLamp b_spot_lamp(b_lamp);
121                         light->size = b_spot_lamp.shadow_soft_size();
122                         light->type = LIGHT_SPOT;
123                         light->spot_angle = b_spot_lamp.spot_size();
124                         light->spot_smooth = b_spot_lamp.spot_blend();
125                         break;
126                 }
127                 case BL::Lamp::type_HEMI: {
128                         light->type = LIGHT_DISTANT;
129                         light->size = 0.0f;
130                         break;
131                 }
132                 case BL::Lamp::type_SUN: {
133                         BL::SunLamp b_sun_lamp(b_lamp);
134                         light->size = b_sun_lamp.shadow_soft_size();
135                         light->type = LIGHT_DISTANT;
136                         break;
137                 }
138                 case BL::Lamp::type_AREA: {
139                         BL::AreaLamp b_area_lamp(b_lamp);
140                         light->size = 1.0f;
141                         light->axisu = transform_get_column(&tfm, 0);
142                         light->axisv = transform_get_column(&tfm, 1);
143                         light->sizeu = b_area_lamp.size();
144                         if(b_area_lamp.shape() == BL::AreaLamp::shape_RECTANGLE)
145                                 light->sizev = b_area_lamp.size_y();
146                         else
147                                 light->sizev = light->sizeu;
148                         light->type = LIGHT_AREA;
149                         break;
150                 }
151         }
152
153         /* location and (inverted!) direction */
154         light->co = transform_get_column(&tfm, 3);
155         light->dir = -transform_get_column(&tfm, 2);
156
157         /* shader */
158         vector<Shader*> used_shaders;
159         find_shader(b_lamp, used_shaders, scene->default_light);
160         light->shader = used_shaders[0];
161
162         /* shadow */
163         PointerRNA cscene = RNA_pointer_get(&b_scene.ptr, "cycles");
164         PointerRNA clamp = RNA_pointer_get(&b_lamp.ptr, "cycles");
165         light->cast_shadow = get_boolean(clamp, "cast_shadow");
166         light->use_mis = get_boolean(clamp, "use_multiple_importance_sampling");
167         
168         int samples = get_int(clamp, "samples");
169         if(get_boolean(cscene, "use_square_samples"))
170                 light->samples = samples * samples;
171         else
172                 light->samples = samples;
173
174         light->max_bounces = get_int(clamp, "max_bounces");
175
176         if(light->type == LIGHT_AREA)
177                 light->is_portal = get_boolean(clamp, "is_portal");
178         else
179                 light->is_portal = false;
180
181         if(light->is_portal)
182                 *use_portal = true;
183
184         /* visibility */
185         uint visibility = object_ray_visibility(b_ob);
186         light->use_diffuse = (visibility & PATH_RAY_DIFFUSE) != 0;
187         light->use_glossy = (visibility & PATH_RAY_GLOSSY) != 0;
188         light->use_transmission = (visibility & PATH_RAY_TRANSMIT) != 0;
189         light->use_scatter = (visibility & PATH_RAY_VOLUME_SCATTER) != 0;
190
191         /* tag */
192         light->tag_update(scene);
193 }
194
195 void BlenderSync::sync_background_light(bool use_portal)
196 {
197         BL::World b_world = b_scene.world();
198
199         if(b_world) {
200                 PointerRNA cscene = RNA_pointer_get(&b_scene.ptr, "cycles");
201                 PointerRNA cworld = RNA_pointer_get(&b_world.ptr, "cycles");
202                 bool sample_as_light = get_boolean(cworld, "sample_as_light");
203
204                 if(sample_as_light || use_portal) {
205                         /* test if we need to sync */
206                         Light *light;
207                         ObjectKey key(b_world, 0, b_world);
208
209                         if(light_map.sync(&light, b_world, b_world, key) ||
210                             world_recalc ||
211                             b_world.ptr.data != world_map)
212                         {
213                                 light->type = LIGHT_BACKGROUND;
214                                 light->map_resolution  = get_int(cworld, "sample_map_resolution");
215                                 light->shader = scene->default_background;
216                                 light->use_mis = sample_as_light;
217                                 light->max_bounces = get_int(cworld, "max_bounces");
218
219                                 int samples = get_int(cworld, "samples");
220                                 if(get_boolean(cscene, "use_square_samples"))
221                                         light->samples = samples * samples;
222                                 else
223                                         light->samples = samples;
224
225                                 light->tag_update(scene);
226                                 light_map.set_recalc(b_world);
227                         }
228                 }
229         }
230
231         world_map = b_world.ptr.data;
232         world_recalc = false;
233 }
234
235 /* Object */
236
237 /* TODO(sergey): Not really optimal, consider approaches based on k-DOP in order
238  * to reduce number of objects which are wrongly considered visible.
239  */
240 static bool object_boundbox_clip(Scene *scene,
241                                  BL::Object& b_ob,
242                                  Transform& tfm,
243                                  float margin)
244 {
245         Camera *cam = scene->camera;
246         Transform& worldtondc = cam->worldtondc;
247         BL::Array<float, 24> boundbox = b_ob.bound_box();
248         float3 bb_min = make_float3(FLT_MAX, FLT_MAX, FLT_MAX),
249                bb_max = make_float3(-FLT_MAX, -FLT_MAX, -FLT_MAX);
250         bool all_behind = true;
251         for(int i = 0; i < 8; ++i) {
252                 float3 p = make_float3(boundbox[3 * i + 0],
253                                        boundbox[3 * i + 1],
254                                        boundbox[3 * i + 2]);
255                 p = transform_point(&tfm, p);
256                 p = transform_point(&worldtondc, p);
257                 if(p.z >= -margin) {
258                         all_behind = false;
259                 }
260                 p /= p.z;
261                 bb_min = min(bb_min, p);
262                 bb_max = max(bb_max, p);
263         }
264         if(!all_behind) {
265                 if(bb_min.x >= 1.0f + margin ||
266                    bb_min.y >= 1.0f + margin ||
267                    bb_max.x <= -margin ||
268                    bb_max.y <= -margin)
269                 {
270                         return true;
271                 }
272                 return false;
273         }
274         return true;
275 }
276
277 Object *BlenderSync::sync_object(BL::Object& b_parent,
278                                  int persistent_id[OBJECT_PERSISTENT_ID_SIZE],
279                                  BL::DupliObject& b_dupli_ob,
280                                  Transform& tfm,
281                                  uint layer_flag,
282                                  float motion_time,
283                                  bool hide_tris,
284                                  bool use_camera_cull,
285                                  float camera_cull_margin,
286                                  bool *use_portal)
287 {
288         BL::Object b_ob = (b_dupli_ob ? b_dupli_ob.object() : b_parent);
289         bool motion = motion_time != 0.0f;
290         
291         /* light is handled separately */
292         if(object_is_light(b_ob)) {
293                 /* don't use lamps for excluded layers used as mask layer */
294                 if(!motion && !((layer_flag & render_layer.holdout_layer) && (layer_flag & render_layer.exclude_layer)))
295                         sync_light(b_parent, persistent_id, b_ob, tfm, use_portal);
296
297                 return NULL;
298         }
299
300         /* only interested in object that we can create meshes from */
301         if(!object_is_mesh(b_ob))
302                 return NULL;
303
304         /* Perform camera space culling. */
305         if(use_camera_cull && object_boundbox_clip(scene, b_ob, tfm, camera_cull_margin)) {
306                 return NULL;
307         }
308
309         /* key to lookup object */
310         ObjectKey key(b_parent, persistent_id, b_ob);
311         Object *object;
312
313         /* motion vector case */
314         if(motion) {
315                 object = object_map.find(key);
316
317                 if(object && (scene->need_motion() == Scene::MOTION_PASS ||
318                               object_use_motion(b_parent, b_ob)))
319                 {
320                         /* object transformation */
321                         if(tfm != object->tfm) {
322                                 VLOG(1) << "Object " << b_ob.name() << " motion detected.";
323                                 if(motion_time == -1.0f) {
324                                         object->motion.pre = tfm;
325                                         object->use_motion = true;
326                                 }
327                                 else if(motion_time == 1.0f) {
328                                         object->motion.post = tfm;
329                                         object->use_motion = true;
330                                 }
331                         }
332
333                         /* mesh deformation */
334                         if(object->mesh)
335                                 sync_mesh_motion(b_ob, object, motion_time);
336                 }
337
338                 return object;
339         }
340
341         /* test if we need to sync */
342         bool object_updated = false;
343
344         if(object_map.sync(&object, b_ob, b_parent, key))
345                 object_updated = true;
346         
347         bool use_holdout = (layer_flag & render_layer.holdout_layer) != 0;
348         
349         /* mesh sync */
350         object->mesh = sync_mesh(b_ob, object_updated, hide_tris);
351
352         /* special case not tracked by object update flags */
353
354         /* holdout */
355         if(use_holdout != object->use_holdout) {
356                 object->use_holdout = use_holdout;
357                 scene->object_manager->tag_update(scene);
358                 object_updated = true;
359         }
360
361         /* visibility flags for both parent and child */
362         uint visibility = object_ray_visibility(b_ob) & PATH_RAY_ALL_VISIBILITY;
363         if(b_parent.ptr.data != b_ob.ptr.data) {
364                 visibility &= object_ray_visibility(b_parent);
365         }
366
367         /* make holdout objects on excluded layer invisible for non-camera rays */
368         if(use_holdout && (layer_flag & render_layer.exclude_layer))
369                 visibility &= ~(PATH_RAY_ALL_VISIBILITY - PATH_RAY_CAMERA);
370
371         /* camera flag is not actually used, instead is tested against render layer
372          * flags */
373         if(visibility & PATH_RAY_CAMERA) {
374                 visibility |= layer_flag << PATH_RAY_LAYER_SHIFT;
375                 visibility &= ~PATH_RAY_CAMERA;
376         }
377
378         if(visibility != object->visibility) {
379                 object->visibility = visibility;
380                 object_updated = true;
381         }
382
383         /* object sync
384          * transform comparison should not be needed, but duplis don't work perfect
385          * in the depsgraph and may not signal changes, so this is a workaround */
386         if(object_updated || (object->mesh && object->mesh->need_update) || tfm != object->tfm) {
387                 object->name = b_ob.name().c_str();
388                 object->pass_id = b_ob.pass_index();
389                 object->tfm = tfm;
390                 object->motion.pre = tfm;
391                 object->motion.post = tfm;
392                 object->use_motion = false;
393
394                 /* motion blur */
395                 if(scene->need_motion() == Scene::MOTION_BLUR && object->mesh) {
396                         Mesh *mesh = object->mesh;
397
398                         mesh->use_motion_blur = false;
399
400                         if(object_use_motion(b_parent, b_ob)) {
401                                 if(object_use_deform_motion(b_parent, b_ob)) {
402                                         mesh->motion_steps = object_motion_steps(b_ob);
403                                         mesh->use_motion_blur = true;
404                                 }
405
406                                 vector<float> times = object->motion_times();
407                                 foreach(float time, times)
408                                         motion_times.insert(time);
409                         }
410                 }
411
412                 /* random number */
413                 object->random_id = hash_string(object->name.c_str());
414
415                 if(persistent_id) {
416                         for(int i = 0; i < OBJECT_PERSISTENT_ID_SIZE; i++)
417                                 object->random_id = hash_int_2d(object->random_id, persistent_id[i]);
418                 }
419                 else
420                         object->random_id = hash_int_2d(object->random_id, 0);
421
422                 if(b_parent.ptr.data != b_ob.ptr.data)
423                         object->random_id ^= hash_int(hash_string(b_parent.name().c_str()));
424
425                 /* dupli texture coordinates */
426                 if(b_dupli_ob) {
427                         object->dupli_generated = 0.5f*get_float3(b_dupli_ob.orco()) - make_float3(0.5f, 0.5f, 0.5f);
428                         object->dupli_uv = get_float2(b_dupli_ob.uv());
429                 }
430                 else {
431                         object->dupli_generated = make_float3(0.0f, 0.0f, 0.0f);
432                         object->dupli_uv = make_float2(0.0f, 0.0f);
433                 }
434
435                 object->tag_update(scene);
436         }
437
438         return object;
439 }
440
441 static bool object_render_hide_original(BL::Object::type_enum ob_type,
442                                         BL::Object::dupli_type_enum dupli_type)
443 {
444         /* metaball exception, they duplicate self */
445         if(ob_type == BL::Object::type_META)
446                 return false;
447
448         return (dupli_type == BL::Object::dupli_type_VERTS ||
449                 dupli_type == BL::Object::dupli_type_FACES ||
450                 dupli_type == BL::Object::dupli_type_FRAMES);
451 }
452
453 static bool object_render_hide(BL::Object& b_ob,
454                                bool top_level,
455                                bool parent_hide,
456                                bool& hide_triangles)
457 {
458         /* check if we should render or hide particle emitter */
459         BL::Object::particle_systems_iterator b_psys;
460
461         bool hair_present = false;
462         bool show_emitter = false;
463         bool hide_emitter = false;
464         bool hide_as_dupli_parent = false;
465         bool hide_as_dupli_child_original = false;
466
467         for(b_ob.particle_systems.begin(b_psys); b_psys != b_ob.particle_systems.end(); ++b_psys) {
468                 if((b_psys->settings().render_type() == BL::ParticleSettings::render_type_PATH) &&
469                    (b_psys->settings().type()==BL::ParticleSettings::type_HAIR))
470                         hair_present = true;
471
472                 if(b_psys->settings().use_render_emitter())
473                         show_emitter = true;
474                 else
475                         hide_emitter = true;
476         }
477
478         if(show_emitter)
479                 hide_emitter = false;
480
481         /* duplicators hidden by default, except dupliframes which duplicate self */
482         if(b_ob.is_duplicator())
483                 if(top_level || b_ob.dupli_type() != BL::Object::dupli_type_FRAMES)
484                         hide_as_dupli_parent = true;
485
486         /* hide original object for duplis */
487         BL::Object parent = b_ob.parent();
488         while(parent) {
489                 if(object_render_hide_original(b_ob.type(),
490                                                parent.dupli_type()))
491                 {
492                         if(parent_hide) {
493                                 hide_as_dupli_child_original = true;
494                                 break;
495                         }
496                 }
497                 parent = parent.parent();
498         }
499         
500         hide_triangles = hide_emitter;
501
502         if(show_emitter) {
503                 return false;
504         }
505         else if(hair_present) {
506                 return hide_as_dupli_child_original;
507         }
508         else {
509                 return (hide_as_dupli_parent || hide_as_dupli_child_original);
510         }
511 }
512
513 static bool object_render_hide_duplis(BL::Object& b_ob)
514 {
515         BL::Object parent = b_ob.parent();
516
517         return (parent && object_render_hide_original(b_ob.type(), parent.dupli_type()));
518 }
519
520 /* Object Loop */
521
522 void BlenderSync::sync_objects(BL::SpaceView3D& b_v3d, float motion_time)
523 {
524         /* layer data */
525         uint scene_layer = render_layer.scene_layer;
526         bool motion = motion_time != 0.0f;
527         
528         if(!motion) {
529                 /* prepare for sync */
530                 light_map.pre_sync();
531                 mesh_map.pre_sync();
532                 object_map.pre_sync();
533                 particle_system_map.pre_sync();
534                 motion_times.clear();
535         }
536         else {
537                 mesh_motion_synced.clear();
538         }
539
540         bool allow_camera_cull = false;
541         float camera_cull_margin = 0.0f;
542         if(b_scene.render().use_simplify()) {
543                 PointerRNA cscene = RNA_pointer_get(&b_scene.ptr, "cycles");
544                 allow_camera_cull = scene->camera->type != CAMERA_PANORAMA &&
545                                     !b_scene.render().use_multiview() &&
546                                     get_boolean(cscene, "use_camera_cull");
547                 if(allow_camera_cull) {
548                         camera_cull_margin = get_float(cscene, "camera_cull_margin");
549                 }
550         }
551
552         /* object loop */
553         BL::Scene::object_bases_iterator b_base;
554         BL::Scene b_sce = b_scene;
555         /* modifier result type (not exposed as enum in C++ API)
556          * 1 : DAG_EVAL_PREVIEW
557          * 2 : DAG_EVAL_RENDER
558          */
559         int dupli_settings = preview ? 1 : 2;
560
561         bool cancel = false;
562         bool use_portal = false;
563
564         uint layer_override = get_layer(b_engine.layer_override());
565         for(; b_sce && !cancel; b_sce = b_sce.background_set()) {
566                 /* Render layer's scene_layer is affected by local view already,
567                  * which is not a desired behavior here.
568                  */
569                 uint scene_layers = layer_override ? layer_override : get_layer(b_scene.layers());
570                 for(b_sce.object_bases.begin(b_base); b_base != b_sce.object_bases.end() && !cancel; ++b_base) {
571                         BL::Object b_ob = b_base->object();
572                         bool hide = (render_layer.use_viewport_visibility)? b_ob.hide(): b_ob.hide_render();
573                         uint ob_layer = get_layer(b_base->layers(),
574                                                   b_base->layers_local_view(),
575                                                   render_layer.use_localview,
576                                                   object_is_light(b_ob),
577                                                   scene_layers);
578                         hide = hide || !(ob_layer & scene_layer);
579
580                         if(!hide) {
581                                 progress.set_sync_status("Synchronizing object", b_ob.name());
582
583                                 PointerRNA cobject = RNA_pointer_get(&b_ob.ptr, "cycles");
584                                 bool use_camera_cull = allow_camera_cull && get_boolean(cobject, "use_camera_cull");
585                                 if(use_camera_cull) {
586                                         /* Need to have proper projection matrix. */
587                                         scene->camera->update();
588                                 }
589                                 if(b_ob.is_duplicator() && !object_render_hide_duplis(b_ob)) {
590                                         /* dupli objects */
591                                         b_ob.dupli_list_create(b_scene, dupli_settings);
592
593                                         BL::Object::dupli_list_iterator b_dup;
594
595                                         for(b_ob.dupli_list.begin(b_dup); b_dup != b_ob.dupli_list.end(); ++b_dup) {
596                                                 Transform tfm = get_transform(b_dup->matrix());
597                                                 BL::Object b_dup_ob = b_dup->object();
598                                                 bool dup_hide = (b_v3d)? b_dup_ob.hide(): b_dup_ob.hide_render();
599                                                 bool in_dupli_group = (b_dup->type() == BL::DupliObject::type_GROUP);
600                                                 bool hide_tris;
601
602                                                 if(!(b_dup->hide() || dup_hide || object_render_hide(b_dup_ob, false, in_dupli_group, hide_tris))) {
603                                                         /* the persistent_id allows us to match dupli objects
604                                                          * between frames and updates */
605                                                         BL::Array<int, OBJECT_PERSISTENT_ID_SIZE> persistent_id = b_dup->persistent_id();
606
607                                                         /* sync object and mesh or light data */
608                                                         Object *object = sync_object(b_ob,
609                                                                                      persistent_id.data,
610                                                                                      *b_dup,
611                                                                                      tfm,
612                                                                                      ob_layer,
613                                                                                      motion_time,
614                                                                                      hide_tris,
615                                                                                      use_camera_cull,
616                                                                                      camera_cull_margin,
617                                                                                      &use_portal);
618
619                                                         /* sync possible particle data, note particle_id
620                                                          * starts counting at 1, first is dummy particle */
621                                                         if(!motion && object) {
622                                                                 sync_dupli_particle(b_ob, *b_dup, object);
623                                                         }
624
625                                                 }
626                                         }
627
628                                         b_ob.dupli_list_clear();
629                                 }
630
631                                 /* test if object needs to be hidden */
632                                 bool hide_tris;
633
634                                 if(!object_render_hide(b_ob, true, true, hide_tris)) {
635                                         /* object itself */
636                                         Transform tfm = get_transform(b_ob.matrix_world());
637                                         BL::DupliObject b_empty_dupli_ob(PointerRNA_NULL);
638                                         sync_object(b_ob,
639                                                     NULL,
640                                                     b_empty_dupli_ob,
641                                                     tfm,
642                                                     ob_layer,
643                                                     motion_time,
644                                                     hide_tris,
645                                                     use_camera_cull,
646                                                     camera_cull_margin,
647                                                     &use_portal);
648                                 }
649                         }
650
651                         cancel = progress.get_cancel();
652                 }
653         }
654
655         progress.set_sync_status("");
656
657         if(!cancel && !motion) {
658                 sync_background_light(use_portal);
659
660                 /* handle removed data and modified pointers */
661                 if(light_map.post_sync())
662                         scene->light_manager->tag_update(scene);
663                 if(mesh_map.post_sync())
664                         scene->mesh_manager->tag_update(scene);
665                 if(object_map.post_sync())
666                         scene->object_manager->tag_update(scene);
667                 if(particle_system_map.post_sync())
668                         scene->particle_system_manager->tag_update(scene);
669         }
670
671         if(motion)
672                 mesh_motion_synced.clear();
673 }
674
675 void BlenderSync::sync_motion(BL::RenderSettings& b_render,
676                               BL::SpaceView3D& b_v3d,
677                               BL::Object& b_override,
678                               int width, int height,
679                               void **python_thread_state)
680 {
681         if(scene->need_motion() == Scene::MOTION_NONE)
682                 return;
683
684         /* get camera object here to deal with camera switch */
685         BL::Object b_cam = b_scene.camera();
686         if(b_override)
687                 b_cam = b_override;
688
689         Camera prevcam = *(scene->camera);
690
691         int frame_center = b_scene.frame_current();
692         float frame_center_delta = 0.0f;
693
694         if(scene->need_motion() != Scene::MOTION_PASS &&
695            scene->camera->motion_position != Camera::MOTION_POSITION_CENTER)
696         {
697                 float shuttertime = scene->camera->shuttertime;
698                 if(scene->camera->motion_position == Camera::MOTION_POSITION_END) {
699                         frame_center_delta = -shuttertime * 0.5f;
700                 }
701                 else {
702                         assert(scene->camera->motion_position == Camera::MOTION_POSITION_START);
703                         frame_center_delta = shuttertime * 0.5f;
704                 }
705                 float time = frame_center + frame_center_delta;
706                 int frame = (int)floorf(time);
707                 float subframe = time - frame;
708                 python_thread_state_restore(python_thread_state);
709                 b_engine.frame_set(frame, subframe);
710                 python_thread_state_save(python_thread_state);
711                 sync_camera_motion(b_render, b_cam, width, height, 0.0f);
712                 sync_objects(b_v3d, 0.0f);
713         }
714
715         /* always sample these times for camera motion */
716         motion_times.insert(-1.0f);
717         motion_times.insert(1.0f);
718
719         /* note iteration over motion_times set happens in sorted order */
720         foreach(float relative_time, motion_times) {
721                 VLOG(1) << "Synchronizing motion for the relative time "
722                         << relative_time << ".";
723
724                 /* fixed shutter time to get previous and next frame for motion pass */
725                 float shuttertime;
726
727                 if(scene->need_motion() == Scene::MOTION_PASS)
728                         shuttertime = 2.0f;
729                 else
730                         shuttertime = scene->camera->shuttertime;
731
732                 /* compute frame and subframe time */
733                 float time = frame_center + frame_center_delta + relative_time * shuttertime * 0.5f;
734                 int frame = (int)floorf(time);
735                 float subframe = time - frame;
736
737                 /* change frame */
738                 python_thread_state_restore(python_thread_state);
739                 b_engine.frame_set(frame, subframe);
740                 python_thread_state_save(python_thread_state);
741
742                 /* sync camera, only supports two times at the moment */
743                 if(relative_time == -1.0f || relative_time == 1.0f) {
744                         sync_camera_motion(b_render,
745                                            b_cam,
746                                            width, height,
747                                            relative_time);
748                 }
749
750                 /* sync object */
751                 sync_objects(b_v3d, relative_time);
752         }
753
754         /* we need to set the python thread state again because this
755          * function assumes it is being executed from python and will
756          * try to save the thread state */
757         python_thread_state_restore(python_thread_state);
758         b_engine.frame_set(frame_center, 0.0f);
759         python_thread_state_save(python_thread_state);
760
761         /* tag camera for motion update */
762         if(scene->camera->motion_modified(prevcam))
763                 scene->camera->tag_update();
764 }
765
766 CCL_NAMESPACE_END
767