Cleanup: remove Cycles layer bits checking in the kernel.
[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
372         /* hide objects not on render layer from camera rays */
373         if(!(layer_flag & render_layer.layer)) {
374                 visibility &= ~PATH_RAY_CAMERA;
375         }
376
377         if(visibility != object->visibility) {
378                 object->visibility = visibility;
379                 object_updated = true;
380         }
381
382         /* object sync
383          * transform comparison should not be needed, but duplis don't work perfect
384          * in the depsgraph and may not signal changes, so this is a workaround */
385         if(object_updated || (object->mesh && object->mesh->need_update) || tfm != object->tfm) {
386                 object->name = b_ob.name().c_str();
387                 object->pass_id = b_ob.pass_index();
388                 object->tfm = tfm;
389                 object->motion.pre = tfm;
390                 object->motion.post = tfm;
391                 object->use_motion = false;
392
393                 /* motion blur */
394                 if(scene->need_motion() == Scene::MOTION_BLUR && object->mesh) {
395                         Mesh *mesh = object->mesh;
396
397                         mesh->use_motion_blur = false;
398
399                         if(object_use_motion(b_parent, b_ob)) {
400                                 if(object_use_deform_motion(b_parent, b_ob)) {
401                                         mesh->motion_steps = object_motion_steps(b_ob);
402                                         mesh->use_motion_blur = true;
403                                 }
404
405                                 vector<float> times = object->motion_times();
406                                 foreach(float time, times)
407                                         motion_times.insert(time);
408                         }
409                 }
410
411                 /* random number */
412                 object->random_id = hash_string(object->name.c_str());
413
414                 if(persistent_id) {
415                         for(int i = 0; i < OBJECT_PERSISTENT_ID_SIZE; i++)
416                                 object->random_id = hash_int_2d(object->random_id, persistent_id[i]);
417                 }
418                 else
419                         object->random_id = hash_int_2d(object->random_id, 0);
420
421                 if(b_parent.ptr.data != b_ob.ptr.data)
422                         object->random_id ^= hash_int(hash_string(b_parent.name().c_str()));
423
424                 /* dupli texture coordinates */
425                 if(b_dupli_ob) {
426                         object->dupli_generated = 0.5f*get_float3(b_dupli_ob.orco()) - make_float3(0.5f, 0.5f, 0.5f);
427                         object->dupli_uv = get_float2(b_dupli_ob.uv());
428                 }
429                 else {
430                         object->dupli_generated = make_float3(0.0f, 0.0f, 0.0f);
431                         object->dupli_uv = make_float2(0.0f, 0.0f);
432                 }
433
434                 object->tag_update(scene);
435         }
436
437         return object;
438 }
439
440 static bool object_render_hide_original(BL::Object::type_enum ob_type,
441                                         BL::Object::dupli_type_enum dupli_type)
442 {
443         /* metaball exception, they duplicate self */
444         if(ob_type == BL::Object::type_META)
445                 return false;
446
447         return (dupli_type == BL::Object::dupli_type_VERTS ||
448                 dupli_type == BL::Object::dupli_type_FACES ||
449                 dupli_type == BL::Object::dupli_type_FRAMES);
450 }
451
452 static bool object_render_hide(BL::Object& b_ob,
453                                bool top_level,
454                                bool parent_hide,
455                                bool& hide_triangles)
456 {
457         /* check if we should render or hide particle emitter */
458         BL::Object::particle_systems_iterator b_psys;
459
460         bool hair_present = false;
461         bool show_emitter = false;
462         bool hide_emitter = false;
463         bool hide_as_dupli_parent = false;
464         bool hide_as_dupli_child_original = false;
465
466         for(b_ob.particle_systems.begin(b_psys); b_psys != b_ob.particle_systems.end(); ++b_psys) {
467                 if((b_psys->settings().render_type() == BL::ParticleSettings::render_type_PATH) &&
468                    (b_psys->settings().type()==BL::ParticleSettings::type_HAIR))
469                         hair_present = true;
470
471                 if(b_psys->settings().use_render_emitter())
472                         show_emitter = true;
473                 else
474                         hide_emitter = true;
475         }
476
477         if(show_emitter)
478                 hide_emitter = false;
479
480         /* duplicators hidden by default, except dupliframes which duplicate self */
481         if(b_ob.is_duplicator())
482                 if(top_level || b_ob.dupli_type() != BL::Object::dupli_type_FRAMES)
483                         hide_as_dupli_parent = true;
484
485         /* hide original object for duplis */
486         BL::Object parent = b_ob.parent();
487         while(parent) {
488                 if(object_render_hide_original(b_ob.type(),
489                                                parent.dupli_type()))
490                 {
491                         if(parent_hide) {
492                                 hide_as_dupli_child_original = true;
493                                 break;
494                         }
495                 }
496                 parent = parent.parent();
497         }
498         
499         hide_triangles = hide_emitter;
500
501         if(show_emitter) {
502                 return false;
503         }
504         else if(hair_present) {
505                 return hide_as_dupli_child_original;
506         }
507         else {
508                 return (hide_as_dupli_parent || hide_as_dupli_child_original);
509         }
510 }
511
512 static bool object_render_hide_duplis(BL::Object& b_ob)
513 {
514         BL::Object parent = b_ob.parent();
515
516         return (parent && object_render_hide_original(b_ob.type(), parent.dupli_type()));
517 }
518
519 /* Object Loop */
520
521 void BlenderSync::sync_objects(BL::SpaceView3D& b_v3d, float motion_time)
522 {
523         /* layer data */
524         uint scene_layer = render_layer.scene_layer;
525         bool motion = motion_time != 0.0f;
526         
527         if(!motion) {
528                 /* prepare for sync */
529                 light_map.pre_sync();
530                 mesh_map.pre_sync();
531                 object_map.pre_sync();
532                 particle_system_map.pre_sync();
533                 motion_times.clear();
534         }
535         else {
536                 mesh_motion_synced.clear();
537         }
538
539         bool allow_camera_cull = false;
540         float camera_cull_margin = 0.0f;
541         if(b_scene.render().use_simplify()) {
542                 PointerRNA cscene = RNA_pointer_get(&b_scene.ptr, "cycles");
543                 allow_camera_cull = scene->camera->type != CAMERA_PANORAMA &&
544                                     !b_scene.render().use_multiview() &&
545                                     get_boolean(cscene, "use_camera_cull");
546                 if(allow_camera_cull) {
547                         camera_cull_margin = get_float(cscene, "camera_cull_margin");
548                 }
549         }
550
551         /* object loop */
552         BL::Scene::object_bases_iterator b_base;
553         BL::Scene b_sce = b_scene;
554         /* modifier result type (not exposed as enum in C++ API)
555          * 1 : DAG_EVAL_PREVIEW
556          * 2 : DAG_EVAL_RENDER
557          */
558         int dupli_settings = preview ? 1 : 2;
559
560         bool cancel = false;
561         bool use_portal = false;
562
563         uint layer_override = get_layer(b_engine.layer_override());
564         for(; b_sce && !cancel; b_sce = b_sce.background_set()) {
565                 /* Render layer's scene_layer is affected by local view already,
566                  * which is not a desired behavior here.
567                  */
568                 uint scene_layers = layer_override ? layer_override : get_layer(b_scene.layers());
569                 for(b_sce.object_bases.begin(b_base); b_base != b_sce.object_bases.end() && !cancel; ++b_base) {
570                         BL::Object b_ob = b_base->object();
571                         bool hide = (render_layer.use_viewport_visibility)? b_ob.hide(): b_ob.hide_render();
572                         uint ob_layer = get_layer(b_base->layers(),
573                                                   b_base->layers_local_view(),
574                                                   object_is_light(b_ob),
575                                                   scene_layers);
576                         hide = hide || !(ob_layer & scene_layer);
577
578                         if(!hide) {
579                                 progress.set_sync_status("Synchronizing object", b_ob.name());
580
581                                 PointerRNA cobject = RNA_pointer_get(&b_ob.ptr, "cycles");
582                                 bool use_camera_cull = allow_camera_cull && get_boolean(cobject, "use_camera_cull");
583                                 if(use_camera_cull) {
584                                         /* Need to have proper projection matrix. */
585                                         scene->camera->update();
586                                 }
587                                 if(b_ob.is_duplicator() && !object_render_hide_duplis(b_ob)) {
588                                         /* dupli objects */
589                                         b_ob.dupli_list_create(b_scene, dupli_settings);
590
591                                         BL::Object::dupli_list_iterator b_dup;
592
593                                         for(b_ob.dupli_list.begin(b_dup); b_dup != b_ob.dupli_list.end(); ++b_dup) {
594                                                 Transform tfm = get_transform(b_dup->matrix());
595                                                 BL::Object b_dup_ob = b_dup->object();
596                                                 bool dup_hide = (b_v3d)? b_dup_ob.hide(): b_dup_ob.hide_render();
597                                                 bool in_dupli_group = (b_dup->type() == BL::DupliObject::type_GROUP);
598                                                 bool hide_tris;
599
600                                                 if(!(b_dup->hide() || dup_hide || object_render_hide(b_dup_ob, false, in_dupli_group, hide_tris))) {
601                                                         /* the persistent_id allows us to match dupli objects
602                                                          * between frames and updates */
603                                                         BL::Array<int, OBJECT_PERSISTENT_ID_SIZE> persistent_id = b_dup->persistent_id();
604
605                                                         /* sync object and mesh or light data */
606                                                         Object *object = sync_object(b_ob,
607                                                                                      persistent_id.data,
608                                                                                      *b_dup,
609                                                                                      tfm,
610                                                                                      ob_layer,
611                                                                                      motion_time,
612                                                                                      hide_tris,
613                                                                                      use_camera_cull,
614                                                                                      camera_cull_margin,
615                                                                                      &use_portal);
616
617                                                         /* sync possible particle data, note particle_id
618                                                          * starts counting at 1, first is dummy particle */
619                                                         if(!motion && object) {
620                                                                 sync_dupli_particle(b_ob, *b_dup, object);
621                                                         }
622
623                                                 }
624                                         }
625
626                                         b_ob.dupli_list_clear();
627                                 }
628
629                                 /* test if object needs to be hidden */
630                                 bool hide_tris;
631
632                                 if(!object_render_hide(b_ob, true, true, hide_tris)) {
633                                         /* object itself */
634                                         Transform tfm = get_transform(b_ob.matrix_world());
635                                         BL::DupliObject b_empty_dupli_ob(PointerRNA_NULL);
636                                         sync_object(b_ob,
637                                                     NULL,
638                                                     b_empty_dupli_ob,
639                                                     tfm,
640                                                     ob_layer,
641                                                     motion_time,
642                                                     hide_tris,
643                                                     use_camera_cull,
644                                                     camera_cull_margin,
645                                                     &use_portal);
646                                 }
647                         }
648
649                         cancel = progress.get_cancel();
650                 }
651         }
652
653         progress.set_sync_status("");
654
655         if(!cancel && !motion) {
656                 sync_background_light(use_portal);
657
658                 /* handle removed data and modified pointers */
659                 if(light_map.post_sync())
660                         scene->light_manager->tag_update(scene);
661                 if(mesh_map.post_sync())
662                         scene->mesh_manager->tag_update(scene);
663                 if(object_map.post_sync())
664                         scene->object_manager->tag_update(scene);
665                 if(particle_system_map.post_sync())
666                         scene->particle_system_manager->tag_update(scene);
667         }
668
669         if(motion)
670                 mesh_motion_synced.clear();
671 }
672
673 void BlenderSync::sync_motion(BL::RenderSettings& b_render,
674                               BL::SpaceView3D& b_v3d,
675                               BL::Object& b_override,
676                               int width, int height,
677                               void **python_thread_state)
678 {
679         if(scene->need_motion() == Scene::MOTION_NONE)
680                 return;
681
682         /* get camera object here to deal with camera switch */
683         BL::Object b_cam = b_scene.camera();
684         if(b_override)
685                 b_cam = b_override;
686
687         Camera prevcam = *(scene->camera);
688
689         int frame_center = b_scene.frame_current();
690         float frame_center_delta = 0.0f;
691
692         if(scene->need_motion() != Scene::MOTION_PASS &&
693            scene->camera->motion_position != Camera::MOTION_POSITION_CENTER)
694         {
695                 float shuttertime = scene->camera->shuttertime;
696                 if(scene->camera->motion_position == Camera::MOTION_POSITION_END) {
697                         frame_center_delta = -shuttertime * 0.5f;
698                 }
699                 else {
700                         assert(scene->camera->motion_position == Camera::MOTION_POSITION_START);
701                         frame_center_delta = shuttertime * 0.5f;
702                 }
703                 float time = frame_center + frame_center_delta;
704                 int frame = (int)floorf(time);
705                 float subframe = time - frame;
706                 python_thread_state_restore(python_thread_state);
707                 b_engine.frame_set(frame, subframe);
708                 python_thread_state_save(python_thread_state);
709                 sync_camera_motion(b_render, b_cam, width, height, 0.0f);
710                 sync_objects(b_v3d, 0.0f);
711         }
712
713         /* always sample these times for camera motion */
714         motion_times.insert(-1.0f);
715         motion_times.insert(1.0f);
716
717         /* note iteration over motion_times set happens in sorted order */
718         foreach(float relative_time, motion_times) {
719                 VLOG(1) << "Synchronizing motion for the relative time "
720                         << relative_time << ".";
721
722                 /* fixed shutter time to get previous and next frame for motion pass */
723                 float shuttertime;
724
725                 if(scene->need_motion() == Scene::MOTION_PASS)
726                         shuttertime = 2.0f;
727                 else
728                         shuttertime = scene->camera->shuttertime;
729
730                 /* compute frame and subframe time */
731                 float time = frame_center + frame_center_delta + relative_time * shuttertime * 0.5f;
732                 int frame = (int)floorf(time);
733                 float subframe = time - frame;
734
735                 /* change frame */
736                 python_thread_state_restore(python_thread_state);
737                 b_engine.frame_set(frame, subframe);
738                 python_thread_state_save(python_thread_state);
739
740                 /* sync camera, only supports two times at the moment */
741                 if(relative_time == -1.0f || relative_time == 1.0f) {
742                         sync_camera_motion(b_render,
743                                            b_cam,
744                                            width, height,
745                                            relative_time);
746                 }
747
748                 /* sync object */
749                 sync_objects(b_v3d, relative_time);
750         }
751
752         /* we need to set the python thread state again because this
753          * function assumes it is being executed from python and will
754          * try to save the thread state */
755         python_thread_state_restore(python_thread_state);
756         b_engine.frame_set(frame_center, 0.0f);
757         python_thread_state_save(python_thread_state);
758
759         /* tag camera for motion update */
760         if(scene->camera->motion_modified(prevcam))
761                 scene->camera->tag_update();
762 }
763
764 CCL_NAMESPACE_END
765