Merge branch 'master' into blender2.8
[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         bool hide_as_dupli_parent = false;
458         bool hide_as_dupli_child_original = false;
459
460         /* duplicators hidden by default, except dupliframes which duplicate self */
461         if(b_ob.is_duplicator())
462                 if(top_level || b_ob.dupli_type() != BL::Object::dupli_type_FRAMES)
463                         hide_as_dupli_parent = true;
464
465         /* hide original object for duplis */
466         BL::Object parent = b_ob.parent();
467         while(parent) {
468                 if(object_render_hide_original(b_ob.type(),
469                                                parent.dupli_type()))
470                 {
471                         if(parent_hide) {
472                                 hide_as_dupli_child_original = true;
473                                 break;
474                         }
475                 }
476                 parent = parent.parent();
477         }
478         
479         hide_triangles = false;
480
481         return (hide_as_dupli_parent || hide_as_dupli_child_original);
482 }
483
484 static bool object_render_hide_duplis(BL::Object& b_ob)
485 {
486         BL::Object parent = b_ob.parent();
487
488         return (parent && object_render_hide_original(b_ob.type(), parent.dupli_type()));
489 }
490
491 /* Object Loop */
492
493 void BlenderSync::sync_objects(BL::SpaceView3D& b_v3d, float motion_time)
494 {
495         /* layer data */
496         uint scene_layer = render_layer.scene_layer;
497         bool motion = motion_time != 0.0f;
498         
499         if(!motion) {
500                 /* prepare for sync */
501                 light_map.pre_sync();
502                 mesh_map.pre_sync();
503                 object_map.pre_sync();
504                 motion_times.clear();
505         }
506         else {
507                 mesh_motion_synced.clear();
508         }
509
510         bool allow_camera_cull = false;
511         float camera_cull_margin = 0.0f;
512         if(b_scene.render().use_simplify()) {
513                 PointerRNA cscene = RNA_pointer_get(&b_scene.ptr, "cycles");
514                 allow_camera_cull = scene->camera->type != CAMERA_PANORAMA &&
515                                     !b_scene.render().use_multiview() &&
516                                     get_boolean(cscene, "use_camera_cull");
517                 if(allow_camera_cull) {
518                         camera_cull_margin = get_float(cscene, "camera_cull_margin");
519                 }
520         }
521
522         /* object loop */
523         BL::Scene::object_bases_iterator b_base;
524         BL::Scene b_sce = b_scene;
525         /* modifier result type (not exposed as enum in C++ API)
526          * 1 : DAG_EVAL_PREVIEW
527          * 2 : DAG_EVAL_RENDER
528          */
529         int dupli_settings = preview ? 1 : 2;
530
531         bool cancel = false;
532         bool use_portal = false;
533
534         uint layer_override = get_layer(b_engine.layer_override());
535         for(; b_sce && !cancel; b_sce = b_sce.background_set()) {
536                 /* Render layer's scene_layer is affected by local view already,
537                  * which is not a desired behavior here.
538                  */
539                 uint scene_layers = layer_override ? layer_override : get_layer(b_scene.layers());
540                 for(b_sce.object_bases.begin(b_base); b_base != b_sce.object_bases.end() && !cancel; ++b_base) {
541                         BL::Object b_ob = b_base->object();
542                         bool hide = (render_layer.use_viewport_visibility)? b_ob.hide(): b_ob.hide_render();
543                         uint ob_layer = get_layer(b_base->layers(),
544                                                   b_base->layers_local_view(),
545                                                   object_is_light(b_ob),
546                                                   scene_layers);
547                         hide = hide || !(ob_layer & scene_layer);
548
549                         if(!hide) {
550                                 progress.set_sync_status("Synchronizing object", b_ob.name());
551
552                                 PointerRNA cobject = RNA_pointer_get(&b_ob.ptr, "cycles");
553                                 bool use_camera_cull = allow_camera_cull && get_boolean(cobject, "use_camera_cull");
554                                 if(use_camera_cull) {
555                                         /* Need to have proper projection matrix. */
556                                         scene->camera->update();
557                                 }
558                                 if(b_ob.is_duplicator() && !object_render_hide_duplis(b_ob)) {
559                                         /* dupli objects */
560                                         b_ob.dupli_list_create(b_scene, dupli_settings);
561
562                                         BL::Object::dupli_list_iterator b_dup;
563
564                                         for(b_ob.dupli_list.begin(b_dup); b_dup != b_ob.dupli_list.end(); ++b_dup) {
565                                                 Transform tfm = get_transform(b_dup->matrix());
566                                                 BL::Object b_dup_ob = b_dup->object();
567                                                 bool dup_hide = (b_v3d)? b_dup_ob.hide(): b_dup_ob.hide_render();
568                                                 bool in_dupli_group = (b_dup->type() == BL::DupliObject::type_GROUP);
569                                                 bool hide_tris;
570
571                                                 if(!(b_dup->hide() || dup_hide || object_render_hide(b_dup_ob, false, in_dupli_group, hide_tris))) {
572                                                         /* the persistent_id allows us to match dupli objects
573                                                          * between frames and updates */
574                                                         BL::Array<int, OBJECT_PERSISTENT_ID_SIZE> persistent_id = b_dup->persistent_id();
575
576                                                         /* sync object and mesh or light data */
577                                                         sync_object(b_ob,
578                                                                     persistent_id.data,
579                                                                     *b_dup,
580                                                                     tfm,
581                                                                     ob_layer,
582                                                                     motion_time,
583                                                                     hide_tris,
584                                                                     use_camera_cull,
585                                                                     camera_cull_margin,
586                                                                     &use_portal);
587                                                 }
588                                         }
589
590                                         b_ob.dupli_list_clear();
591                                 }
592
593                                 /* test if object needs to be hidden */
594                                 bool hide_tris;
595
596                                 if(!object_render_hide(b_ob, true, true, hide_tris)) {
597                                         /* object itself */
598                                         Transform tfm = get_transform(b_ob.matrix_world());
599                                         BL::DupliObject b_empty_dupli_ob(PointerRNA_NULL);
600                                         sync_object(b_ob,
601                                                     NULL,
602                                                     b_empty_dupli_ob,
603                                                     tfm,
604                                                     ob_layer,
605                                                     motion_time,
606                                                     hide_tris,
607                                                     use_camera_cull,
608                                                     camera_cull_margin,
609                                                     &use_portal);
610                                 }
611                         }
612
613                         cancel = progress.get_cancel();
614                 }
615         }
616
617         progress.set_sync_status("");
618
619         if(!cancel && !motion) {
620                 sync_background_light(use_portal);
621
622                 /* handle removed data and modified pointers */
623                 if(light_map.post_sync())
624                         scene->light_manager->tag_update(scene);
625                 if(mesh_map.post_sync())
626                         scene->mesh_manager->tag_update(scene);
627                 if(object_map.post_sync())
628                         scene->object_manager->tag_update(scene);
629         }
630
631         if(motion)
632                 mesh_motion_synced.clear();
633 }
634
635 void BlenderSync::sync_motion(BL::RenderSettings& b_render,
636                               BL::SpaceView3D& b_v3d,
637                               BL::Object& b_override,
638                               int width, int height,
639                               void **python_thread_state)
640 {
641         if(scene->need_motion() == Scene::MOTION_NONE)
642                 return;
643
644         /* get camera object here to deal with camera switch */
645         BL::Object b_cam = b_scene.camera();
646         if(b_override)
647                 b_cam = b_override;
648
649         Camera prevcam = *(scene->camera);
650
651         int frame_center = b_scene.frame_current();
652         float frame_center_delta = 0.0f;
653
654         if(scene->need_motion() != Scene::MOTION_PASS &&
655            scene->camera->motion_position != Camera::MOTION_POSITION_CENTER)
656         {
657                 float shuttertime = scene->camera->shuttertime;
658                 if(scene->camera->motion_position == Camera::MOTION_POSITION_END) {
659                         frame_center_delta = -shuttertime * 0.5f;
660                 }
661                 else {
662                         assert(scene->camera->motion_position == Camera::MOTION_POSITION_START);
663                         frame_center_delta = shuttertime * 0.5f;
664                 }
665                 float time = frame_center + frame_center_delta;
666                 int frame = (int)floorf(time);
667                 float subframe = time - frame;
668                 python_thread_state_restore(python_thread_state);
669                 b_engine.frame_set(frame, subframe);
670                 python_thread_state_save(python_thread_state);
671                 sync_camera_motion(b_render, b_cam, width, height, 0.0f);
672                 sync_objects(b_v3d, 0.0f);
673         }
674
675         /* always sample these times for camera motion */
676         motion_times.insert(-1.0f);
677         motion_times.insert(1.0f);
678
679         /* note iteration over motion_times set happens in sorted order */
680         foreach(float relative_time, motion_times) {
681                 VLOG(1) << "Synchronizing motion for the relative time "
682                         << relative_time << ".";
683
684                 /* fixed shutter time to get previous and next frame for motion pass */
685                 float shuttertime;
686
687                 if(scene->need_motion() == Scene::MOTION_PASS)
688                         shuttertime = 2.0f;
689                 else
690                         shuttertime = scene->camera->shuttertime;
691
692                 /* compute frame and subframe time */
693                 float time = frame_center + frame_center_delta + relative_time * shuttertime * 0.5f;
694                 int frame = (int)floorf(time);
695                 float subframe = time - frame;
696
697                 /* change frame */
698                 python_thread_state_restore(python_thread_state);
699                 b_engine.frame_set(frame, subframe);
700                 python_thread_state_save(python_thread_state);
701
702                 /* sync camera, only supports two times at the moment */
703                 if(relative_time == -1.0f || relative_time == 1.0f) {
704                         sync_camera_motion(b_render,
705                                            b_cam,
706                                            width, height,
707                                            relative_time);
708                 }
709
710                 /* sync object */
711                 sync_objects(b_v3d, relative_time);
712         }
713
714         /* we need to set the python thread state again because this
715          * function assumes it is being executed from python and will
716          * try to save the thread state */
717         python_thread_state_restore(python_thread_state);
718         b_engine.frame_set(frame_center, 0.0f);
719         python_thread_state_save(python_thread_state);
720
721         /* tag camera for motion update */
722         if(scene->camera->motion_modified(prevcam))
723                 scene->camera->tag_update();
724 }
725
726 CCL_NAMESPACE_END
727