Merge branch 'master' into blender2.8
[blender.git] / intern / cycles / blender / blender_session.cpp
index ecc668af7829ce3a1ca9d6c0677d6500330a4ef4..46f32fe816d4bfc45d629876d49f3318fe53e666 100644 (file)
@@ -569,8 +569,6 @@ void BlenderSession::bake(BL::Object& b_object,
                           float result[])
 {
        ShaderEvalType shader_type = get_shader_type(pass_type);
-       size_t object_index = OBJECT_NONE;
-       int tri_offset = 0;
 
        /* Set baking flag in advance, so kernel loading can check if we need
         * any baking capabilities.
@@ -580,9 +578,6 @@ void BlenderSession::bake(BL::Object& b_object,
        /* ensure kernels are loaded before we do any scene updates */
        session->load_kernels();
 
-       if(session->progress.get_cancel())
-               return;
-
        if(shader_type == SHADER_EVAL_UV) {
                /* force UV to be available */
                Pass::add(PASS_UV, scene->film->passes);
@@ -600,50 +595,61 @@ void BlenderSession::bake(BL::Object& b_object,
        scene->film->tag_update(scene);
        scene->integrator->tag_update(scene);
 
-       /* update scene */
-       BL::Object b_camera_override(b_engine.camera_override());
-       sync->sync_camera(b_render, b_camera_override, width, height, "");
-       sync->sync_data(b_render,
-                       b_v3d,
-                       b_camera_override,
-                       width, height,
-                       &python_thread_state,
-                       b_rlay_name.c_str());
+       if(!session->progress.get_cancel()) {
+               /* update scene */
+               BL::Object b_camera_override(b_engine.camera_override());
+               sync->sync_camera(b_render, b_camera_override, width, height, "");
+               sync->sync_data(b_render,
+                                               b_v3d,
+                                               b_camera_override,
+                                               width, height,
+                                               &python_thread_state,
+                                               b_rlay_name.c_str());
+       }
 
-       /* get buffer parameters */
-       SessionParams session_params = BlenderSync::get_session_params(b_engine, b_userpref, b_scene, background);
-       BufferParams buffer_params = BlenderSync::get_buffer_params(b_render, b_v3d, b_rv3d, scene->camera, width, height);
+       BakeData *bake_data = NULL;
 
-       scene->bake_manager->set_shader_limit((size_t)b_engine.tile_x(), (size_t)b_engine.tile_y());
+       if(!session->progress.get_cancel()) {
+               /* get buffer parameters */
+               SessionParams session_params = BlenderSync::get_session_params(b_engine, b_userpref, b_scene, background);
+               BufferParams buffer_params = BlenderSync::get_buffer_params(b_render, b_v3d, b_rv3d, scene->camera, width, height);
 
-       /* set number of samples */
-       session->tile_manager.set_samples(session_params.samples);
-       session->reset(buffer_params, session_params.samples);
-       session->update_scene();
+               scene->bake_manager->set_shader_limit((size_t)b_engine.tile_x(), (size_t)b_engine.tile_y());
 
-       /* find object index. todo: is arbitrary - copied from mesh_displace.cpp */
-       for(size_t i = 0; i < scene->objects.size(); i++) {
-               if(strcmp(scene->objects[i]->name.c_str(), b_object.name().c_str()) == 0) {
-                       object_index = i;
-                       tri_offset = scene->objects[i]->mesh->tri_offset;
-                       break;
-               }
-       }
+               /* set number of samples */
+               session->tile_manager.set_samples(session_params.samples);
+               session->reset(buffer_params, session_params.samples);
+               session->update_scene();
+
+               /* find object index. todo: is arbitrary - copied from mesh_displace.cpp */
+               size_t object_index = OBJECT_NONE;
+               int tri_offset = 0;
 
-       int object = object_index;
+               for(size_t i = 0; i < scene->objects.size(); i++) {
+                       if(strcmp(scene->objects[i]->name.c_str(), b_object.name().c_str()) == 0) {
+                               object_index = i;
+                               tri_offset = scene->objects[i]->mesh->tri_offset;
+                               break;
+                       }
+               }
 
-       BakeData *bake_data = scene->bake_manager->init(object, tri_offset, num_pixels);
+               int object = object_index;
 
-       populate_bake_data(bake_data, object_id, pixel_array, num_pixels);
+               bake_data = scene->bake_manager->init(object, tri_offset, num_pixels);
+               populate_bake_data(bake_data, object_id, pixel_array, num_pixels);
 
-       /* set number of samples */
-       session->tile_manager.set_samples(session_params.samples);
-       session->reset(buffer_params, session_params.samples);
-       session->update_scene();
+               /* set number of samples */
+               session->tile_manager.set_samples(session_params.samples);
+               session->reset(buffer_params, session_params.samples);
+               session->update_scene();
 
-       session->progress.set_update_callback(function_bind(&BlenderSession::update_bake_progress, this));
+               session->progress.set_update_callback(function_bind(&BlenderSession::update_bake_progress, this));
+       }
 
-       scene->bake_manager->bake(scene->device, &scene->dscene, scene, session->progress, shader_type, bake_pass_filter, bake_data, result);
+       /* Perform bake. Check cancel to avoid crash with incomplete scene data. */
+       if(!session->progress.get_cancel()) {
+               scene->bake_manager->bake(scene->device, &scene->dscene, scene, session->progress, shader_type, bake_pass_filter, bake_data, result);
+       }
 
        /* free all memory used (host and device), so we wouldn't leave render
         * engine with extra memory allocated