Fix issue in recent bugfix, did not work with multiple sessions (preview render).
authorBrecht Van Lommel <brechtvanlommel@gmail.com>
Wed, 12 Feb 2014 22:13:45 +0000 (23:13 +0100)
committerBrecht Van Lommel <brechtvanlommel@gmail.com>
Wed, 12 Feb 2014 22:14:57 +0000 (23:14 +0100)
intern/cycles/blender/blender_object.cpp
intern/cycles/blender/blender_python.cpp
intern/cycles/blender/blender_session.cpp
intern/cycles/blender/blender_session.h
intern/cycles/blender/blender_sync.cpp
intern/cycles/blender/blender_sync.h
intern/cycles/blender/blender_util.h

index c8a2f080bbad64b5b7ed389bc25ec9d028dc887e..cc52717fdb651a00119b9ed5d02a60e3170c202c 100644 (file)
@@ -516,7 +516,7 @@ void BlenderSync::sync_objects(BL::SpaceView3D b_v3d, int motion)
                mesh_motion_synced.clear();
 }
 
-void BlenderSync::sync_motion(BL::SpaceView3D b_v3d, BL::Object b_override)
+void BlenderSync::sync_motion(BL::SpaceView3D b_v3d, BL::Object b_override, void **python_thread_state)
 {
        if(scene->need_motion() == Scene::MOTION_NONE)
                return;
@@ -535,9 +535,9 @@ void BlenderSync::sync_motion(BL::SpaceView3D b_v3d, BL::Object b_override)
                /* we need to set the python thread state again because this
                 * function assumes it is being executed from python and will
                 * try to save the thread state */
-               python_thread_state_restore();
+               python_thread_state_restore(python_thread_state);
                b_scene.frame_set(frame + motion, 0.0f);
-               python_thread_state_save();
+               python_thread_state_save(python_thread_state);
 
                /* camera object */
                if(b_cam)
@@ -550,9 +550,9 @@ void BlenderSync::sync_motion(BL::SpaceView3D b_v3d, BL::Object b_override)
        /* we need to set the python thread state again because this
         * function assumes it is being executed from python and will
         * try to save the thread state */
-       python_thread_state_restore();
+       python_thread_state_restore(python_thread_state);
        b_scene.frame_set(frame, 0.0f);
-       python_thread_state_save();
+       python_thread_state_save(python_thread_state);
 
        /* tag camera for motion update */
        if(scene->camera->motion_modified(prevcam))
index 6c9f1dc080fe91338391d69ee776ceb01332590e..e08b7980e7890ea59e786d78e7dd9a6446227197 100644 (file)
 
 CCL_NAMESPACE_BEGIN
 
-static PyThreadState *python_thread_state = NULL;
-
-void python_thread_state_save()
+void python_thread_state_save(void **python_thread_state)
 {
-       python_thread_state = PyEval_SaveThread();
+       *python_thread_state = (void*)PyEval_SaveThread();
 }
 
-void python_thread_state_restore()
+void python_thread_state_restore(void **python_thread_state)
 {
-       PyEval_RestoreThread(python_thread_state);
-       python_thread_state = NULL;
+       PyEval_RestoreThread((PyThreadState*)*python_thread_state);
+       *python_thread_state = NULL;
 }
 
 static PyObject *init_func(PyObject *self, PyObject *args)
@@ -100,8 +98,6 @@ static PyObject *create_func(PyObject *self, PyObject *args)
        /* create session */
        BlenderSession *session;
 
-       python_thread_state_save();
-
        if(rv3d) {
                /* interactive viewport session */
                int width = region.width();
@@ -122,7 +118,11 @@ static PyObject *create_func(PyObject *self, PyObject *args)
                session = new BlenderSession(engine, userpref, data, scene);
        }
 
-       python_thread_state_restore();
+       python_thread_state_save(&session->python_thread_state);
+
+       session->create();
+
+       python_thread_state_restore(&session->python_thread_state);
 
        return PyLong_FromVoidPtr(session);
 }
@@ -136,12 +136,13 @@ static PyObject *free_func(PyObject *self, PyObject *value)
 
 static PyObject *render_func(PyObject *self, PyObject *value)
 {
-       python_thread_state_save();
-
        BlenderSession *session = (BlenderSession*)PyLong_AsVoidPtr(value);
+
+       python_thread_state_save(&session->python_thread_state);
+
        session->render();
 
-       python_thread_state_restore();
+       python_thread_state_restore(&session->python_thread_state);
 
        Py_RETURN_NONE;
 }
@@ -183,23 +184,24 @@ static PyObject *reset_func(PyObject *self, PyObject *args)
        RNA_id_pointer_create((ID*)PyLong_AsVoidPtr(pyscene), &sceneptr);
        BL::Scene b_scene(sceneptr);
 
-       python_thread_state_save();
+       python_thread_state_save(&session->python_thread_state);
 
        session->reset_session(b_data, b_scene);
 
-       python_thread_state_restore();
+       python_thread_state_restore(&session->python_thread_state);
 
        Py_RETURN_NONE;
 }
 
 static PyObject *sync_func(PyObject *self, PyObject *value)
 {
-       python_thread_state_save();
-
        BlenderSession *session = (BlenderSession*)PyLong_AsVoidPtr(value);
+
+       python_thread_state_save(&session->python_thread_state);
+
        session->synchronize();
 
-       python_thread_state_restore();
+       python_thread_state_restore(&session->python_thread_state);
 
        Py_RETURN_NONE;
 }
index 1d3c49b519c59fd464759160f9d02fc8523465c5..ef578493901f63337a1b91fad6664f62444f4c0e 100644 (file)
@@ -40,7 +40,7 @@ CCL_NAMESPACE_BEGIN
 BlenderSession::BlenderSession(BL::RenderEngine b_engine_, BL::UserPreferences b_userpref_,
        BL::BlendData b_data_, BL::Scene b_scene_)
 : b_engine(b_engine_), b_userpref(b_userpref_), b_data(b_data_), b_render(b_engine_.render()), b_scene(b_scene_),
-  b_v3d(PointerRNA_NULL), b_rv3d(PointerRNA_NULL)
+  b_v3d(PointerRNA_NULL), b_rv3d(PointerRNA_NULL), python_thread_state(NULL)
 {
        /* offline render */
 
@@ -50,15 +50,13 @@ BlenderSession::BlenderSession(BL::RenderEngine b_engine_, BL::UserPreferences b
        background = true;
        last_redraw_time = 0.0;
        start_resize_time = 0.0;
-
-       create_session();
 }
 
 BlenderSession::BlenderSession(BL::RenderEngine b_engine_, BL::UserPreferences b_userpref_,
        BL::BlendData b_data_, BL::Scene b_scene_,
        BL::SpaceView3D b_v3d_, BL::RegionView3D b_rv3d_, int width_, int height_)
 : b_engine(b_engine_), b_userpref(b_userpref_), b_data(b_data_), b_render(b_scene_.render()), b_scene(b_scene_),
-  b_v3d(b_v3d_), b_rv3d(b_rv3d_)
+  b_v3d(b_v3d_), b_rv3d(b_rv3d_), python_thread_state(NULL)
 {
        /* 3d view render */
 
@@ -67,9 +65,6 @@ BlenderSession::BlenderSession(BL::RenderEngine b_engine_, BL::UserPreferences b
        background = false;
        last_redraw_time = 0.0;
        start_resize_time = 0.0;
-
-       create_session();
-       session->start();
 }
 
 BlenderSession::~BlenderSession()
@@ -77,6 +72,14 @@ BlenderSession::~BlenderSession()
        free_session();
 }
 
+void BlenderSession::create()
+{
+       create_session();
+
+       if(b_v3d)
+               session->start();
+}
+
 void BlenderSession::create_session()
 {
        SceneParams scene_params = BlenderSync::get_scene_params(b_scene, background);
@@ -102,7 +105,7 @@ void BlenderSession::create_session()
 
        if(b_v3d) {
                /* full data sync */
-               sync->sync_data(b_v3d, b_engine.camera_override());
+               sync->sync_data(b_v3d, b_engine.camera_override(), &python_thread_state);
                sync->sync_view(b_v3d, b_rv3d, width, height);
        }
        else {
@@ -389,7 +392,7 @@ void BlenderSession::render()
 
                /* update scene */
                sync->sync_camera(b_render, b_engine.camera_override(), width, height);
-               sync->sync_data(b_v3d, b_engine.camera_override(), b_rlay_name.c_str());
+               sync->sync_data(b_v3d, b_engine.camera_override(), &python_thread_state, b_rlay_name.c_str());
 
                /* update number of samples per layer */
                int samples = sync->get_layer_samples();
@@ -506,7 +509,7 @@ void BlenderSession::synchronize()
        }
 
        /* data and camera synchronize */
-       sync->sync_data(b_v3d, b_engine.camera_override());
+       sync->sync_data(b_v3d, b_engine.camera_override(), &python_thread_state);
 
        if(b_rv3d)
                sync->sync_view(b_v3d, b_rv3d, width, height);
index 86b221650846b1e3af341e494b0a9e6642a6ea67..0568fb291d061bab918049e6460c3cfd37d9ab15 100644 (file)
@@ -40,6 +40,8 @@ public:
 
        ~BlenderSession();
 
+       void create();
+
        /* session */
        void create_session();
        void free_session();
@@ -90,6 +92,8 @@ public:
        int width, height;
        double start_resize_time;
 
+       void *python_thread_state;
+
 protected:
        void do_write_update_render_result(BL::RenderResult b_rr, BL::RenderLayer b_rlay, RenderTile& rtile, bool do_update_only);
        void do_write_update_render_tile(RenderTile& rtile, bool do_update_only);
index d21ffdec0d1944435d93fbad280d7e6c391d560e..8e2197a2aa6a04aadaa8e9569b998e6bd842aa3e 100644 (file)
@@ -135,7 +135,7 @@ bool BlenderSync::sync_recalc()
        return recalc;
 }
 
-void BlenderSync::sync_data(BL::SpaceView3D b_v3d, BL::Object b_override, const char *layer)
+void BlenderSync::sync_data(BL::SpaceView3D b_v3d, BL::Object b_override, void **python_thread_state, const char *layer)
 {
        sync_render_layers(b_v3d, layer);
        sync_integrator();
@@ -143,7 +143,7 @@ void BlenderSync::sync_data(BL::SpaceView3D b_v3d, BL::Object b_override, const
        sync_shaders();
        sync_curve_settings();
        sync_objects(b_v3d);
-       sync_motion(b_v3d, b_override);
+       sync_motion(b_v3d, b_override, python_thread_state);
 }
 
 /* Integrator */
index 3d2a3ae5aac4670e371a619ccfb263352b539d5d..205761ad302949f7edeb165ba0064830efccf556 100644 (file)
@@ -53,7 +53,7 @@ public:
 
        /* sync */
        bool sync_recalc();
-       void sync_data(BL::SpaceView3D b_v3d, BL::Object b_override, const char *layer = 0);
+       void sync_data(BL::SpaceView3D b_v3d, BL::Object b_override, void **python_thread_state, const char *layer = 0);
        void sync_render_layers(BL::SpaceView3D b_v3d, const char *layer);
        void sync_integrator();
        void sync_camera(BL::RenderSettings b_render, BL::Object b_override, int width, int height);
@@ -72,7 +72,7 @@ private:
        void sync_lamps(bool update_all);
        void sync_materials(bool update_all);
        void sync_objects(BL::SpaceView3D b_v3d, int motion = 0);
-       void sync_motion(BL::SpaceView3D b_v3d, BL::Object b_override);
+       void sync_motion(BL::SpaceView3D b_v3d, BL::Object b_override, void **python_thread_state);
        void sync_film();
        void sync_view();
        void sync_world(bool update_all);
index c9048cd8a282861211ae68c3e4a4b7a5f1eaa852..58e523d7fc234b85d20d62035a3f8d189750c013 100644 (file)
@@ -37,8 +37,8 @@ float *BKE_image_get_float_pixels_for_frame(void *image, int frame);
 
 CCL_NAMESPACE_BEGIN
 
-void python_thread_state_save();
-void python_thread_state_restore();
+void python_thread_state_save(void **python_thread_state);
+void python_thread_state_restore(void **python_thread_state);
 
 static inline BL::Mesh object_to_mesh(BL::BlendData data, BL::Object object, BL::Scene scene, bool apply_modifiers, bool render, bool calc_undeformed)
 {