Camera tracking integration
authorSergey Sharybin <sergey.vfx@gmail.com>
Mon, 10 Oct 2011 17:42:04 +0000 (17:42 +0000)
committerSergey Sharybin <sergey.vfx@gmail.com>
Mon, 10 Oct 2011 17:42:04 +0000 (17:42 +0000)
===========================

- Added space_view3d.background_images.add() function to add new background
  image from scripts.
- Added operator to set current clip in clip editor as background in 3d viewport.
- Do not show reconstruction if camera hasn't got camera solver constraint.
- Added button to convert tracking-related constraint to fcurves.

release/scripts/startup/bl_operators/clip.py
release/scripts/startup/bl_ui/properties_object_constraint.py
release/scripts/startup/bl_ui/space_clip.py
source/blender/blenkernel/BKE_object.h
source/blender/blenkernel/intern/object.c
source/blender/blenkernel/intern/tracking.c
source/blender/editors/space_view3d/drawobject.c
source/blender/editors/space_view3d/view3d_draw.c
source/blender/editors/space_view3d/view3d_select.c
source/blender/editors/space_view3d/view3d_snap.c
source/blender/makesrna/intern/rna_space.c

index 1b904910576497fbdef9725c81dbe338aa2e4a29..efdacdd24597b92510f3dcbf9fdbd1089685a687 100644 (file)
@@ -139,3 +139,137 @@ class CLIP_OT_delete_proxy(Operator):
             pass
 
         return {'FINISHED'}
+
+
+class CLIP_OT_set_viewport_background(Operator):
+    bl_idname = "clip.set_viewport_background"
+    bl_label = "Set as Background"
+    bl_options = {'UNDO', 'REGISTER'}
+
+    @classmethod
+    def poll(cls, context):
+        if context.space_data.type != 'CLIP_EDITOR':
+            return False
+
+        sc = context.space_data
+
+        return sc.clip
+
+    def _set_background(self, space_v3d, clip, user):
+        bgpic = None
+
+        for x in space_v3d.background_images:
+            if x.source == 'MOVIE':
+                bgpic = x
+                break
+
+        if not bgpic:
+            bgpic = space_v3d.background_images.add()
+
+        bgpic.source = 'MOVIE'
+        bgpic.clip = clip
+        bgpic.clip_user.proxy_render_size = user.proxy_render_size
+        bgpic.clip_user.use_render_undistorted = user.use_render_undistorted
+        bgpic.use_camera_clip = False
+
+    def execute(self, context):
+        sc = context.space_data
+        clip = sc.clip
+
+        for area in context.window.screen.areas:
+            if area.type == 'VIEW_3D':
+                for space in area.spaces:
+                    if space.type == 'VIEW_3D':
+                        self._set_background(space, clip, sc.clip_user)
+
+        return {'FINISHED'}
+
+
+class CLIP_OT_constraint_to_fcurve(Operator):
+    bl_idname = "clip.constraint_to_fcurve"
+    bl_label = "Constraint to F-Curve"
+    bl_options = {'UNDO', 'REGISTER'}
+
+    def _bake_object(self, scene, ob):
+        con = None
+        clip = None
+        sfra = None
+        efra = None
+        frame_current = scene.frame_current
+        matrices = []
+
+        # Find constraint which would eb converting
+        # TODO: several camera solvers and track followers would fail,
+        #       but can't think about eal workflow where it'll be useful
+        for x in ob.constraints:
+            if x.type in ('CAMERA_SOLVER', 'FOLLOW_TRACK'):
+                con = x
+
+        if not con:
+            return
+
+        if con.type == 'FOLLOW_TRACK' and con.reference == 'BUNDLE':
+            mat = ob.matrix_world.copy()
+            ob.constraints.remove(con)
+            ob.matrix_world = mat
+
+            return
+
+        # Get clip used for parenting
+        if con.use_default_clip:
+            clip = scene.clip
+        else:
+            clip = con.clip
+
+        if not clip:
+            return
+
+        # Find start and end frames
+        for track in clip.tracking.tracks:
+            if sfra is None:
+                sfra = track.markers[0].frame
+            else:
+                sfra = min(sfra, track.markers[0].frame)
+
+            if efra is None:
+                efra = track.markers[-1].frame
+            else:
+                efra = max(efra, track.markers[-1].frame)
+
+        if sfra is None or efra is None:
+           return
+
+        # Store object matrices
+        for x in range(sfra, efra+1):
+            scene.frame_set(x)
+            matrices.append(ob.matrix_world.copy())
+
+        ob.animation_data_create()
+
+        # Apply matrices on object and insert keyframes
+        i = 0
+        for x in range(sfra, efra+1):
+            scene.frame_set(x)
+            ob.matrix_world = matrices[i]
+
+            ob.keyframe_insert("location")
+
+            if ob.rotation_mode == 'QUATERNION':
+                ob.keyframe_insert("rotation_quaternion")
+            else:
+                ob.keyframe_insert("rotation_euler")
+
+            i += 1
+
+        ob.constraints.remove(con)
+
+        scene.frame_set(frame_current)
+
+    def execute(self, context):
+        scene = context.scene
+
+        for ob in scene.objects:
+            if ob.select:
+                self._bake_object(scene, ob)
+
+        return {'FINISHED'}
index 101f7b6b7e07a17a1a4a1565aefbb3da3776caab..69fc51145a781feb613a569fdb9d81dc86879e19 100644 (file)
@@ -764,12 +764,16 @@ class ConstraintButtonsPanel():
         row = layout.row()
         row.prop(con, "reference", expand=True)
 
+        layout.operator("clip.constraint_to_fcurve")
+
     def CAMERA_SOLVER(self, context, layout, con):
         layout.prop(con, "use_default_clip")
 
         if not con.use_default_clip:
             layout.prop(con, "clip")
 
+        layout.operator("clip.constraint_to_fcurve")
+
     def SCRIPT(self, context, layout, con):
         layout.label("Blender 2.5 has no py-constraints")
 
index 09d692cd344e0e9f337761b04dbe74b43535d6f4..9b3fdc8772be7d308b76f7cc5eb105fba8565699 100644 (file)
@@ -619,6 +619,18 @@ class CLIP_PT_footage(Panel):
             layout.operator("clip.open", icon='FILESEL')
 
 
+class CLIP_PT_tools_clip(Panel):
+    bl_space_type = 'CLIP_EDITOR'
+    bl_region_type = 'TOOLS'
+    bl_label = "CLip"
+
+    def draw(self, context):
+        layout = self.layout
+        clip = context.space_data.clip
+
+        layout.operator("clip.set_viewport_background")
+
+
 class CLIP_MT_view(Menu):
     bl_label = "View"
 
index ce630afc180fc77a15e923cc10b5654ee819689f..353a08ede7f98e599eec3179d362cd26a5adce95 100644 (file)
@@ -155,7 +155,7 @@ void camera_view_frame(struct Scene *scene, struct Camera *camera, float r_vec[4
 
 void object_relink(struct Object *ob);
 
-struct MovieClip *object_get_movieclip(struct Scene *scene, struct Object *ob);
+struct MovieClip *object_get_movieclip(struct Scene *scene, struct Object *ob, int use_default);
 
 #ifdef __cplusplus
 }
index a18a777fd73b6b57df92ac35d86c3aa8bd64dcfe..e8f70ff87d09dae1eaf855c6841f3756db171122 100644 (file)
@@ -3361,9 +3361,9 @@ void object_relink(Object *ob)
        ID_NEW(ob->proxy_group);
 }
 
-MovieClip *object_get_movieclip(Scene *scene, Object *ob)
+MovieClip *object_get_movieclip(Scene *scene, Object *ob, int use_default)
 {
-       MovieClip *clip= scene->clip;
+       MovieClip *clip= use_default ? scene->clip : NULL;
        bConstraint *con= ob->constraints.first, *scon= NULL;
 
        while(con){
@@ -3379,6 +3379,8 @@ MovieClip *object_get_movieclip(Scene *scene, Object *ob)
                bCameraSolverConstraint *solver= scon->data;
                if((solver->flag&CAMERASOLVER_DEFAULTCLIP)==0)
                        clip= solver->clip;
+               else
+                       clip= scene->clip;
        }
 
        return clip;
index 36f3f51a03babc5590506a294c21c9d876538257..9ad7b9634473b9ef786362a59c463910cc3388df 100644 (file)
@@ -1413,7 +1413,7 @@ void BKE_tracking_get_interpolated_camera(MovieTracking *tracking, int framenr,
 void BKE_get_tracking_mat(Scene *scene, Object *ob, float mat[4][4])
 {
        if(!ob) {
-               if(!scene->camera) ob= scene->camera;
+               if(scene->camera) ob= scene->camera;
                else ob= scene_find_camera(scene);
        }
 
index ba3eb0b47894629296c96ff11c547c5f90bed5f8..1ff04b7286fe1d9b3b1b8eaba13feb1d09ff7dc8 100644 (file)
@@ -1598,7 +1598,7 @@ static void drawcamera(Scene *scene, View3D *v3d, RegionView3D *rv3d, Base *base
        int i;
        float drawsize;
        const short is_view= (rv3d->persp==RV3D_CAMOB && ob==v3d->camera);
-       MovieClip *clip= object_get_movieclip(scene, base->object);
+       MovieClip *clip= object_get_movieclip(scene, base->object, 0);
 
        /* draw data for movie clip set as active for scene */
        if(clip)
index 59a142a784ec7091a367e147f9456c072d954212..854bd3cdb249f1f9ae04b7e38453f782cf7acca4 100644 (file)
@@ -1494,7 +1494,7 @@ static void draw_bgpic(Scene *scene, ARegion *ar, View3D *v3d)
                                                scene->camera= scene_find_camera(scene);
 
                                        if(scene->camera)
-                                               clip= object_get_movieclip(scene, scene->camera);
+                                               clip= object_get_movieclip(scene, scene->camera, 1);
                                } else clip= bgpic->clip;
 
                                if(clip==NULL)
index c5c39ed334cddaf9bd3974daea81b48f23a4e847..4d75d481c47262787a55b86ace6bd9d7ffae12d6 100644 (file)
@@ -1406,7 +1406,7 @@ static int mouse_select(bContext *C, const int mval[2], short extend, short obce
                                                        /* index of bundle is 1<<16-based. if there's no "bone" index
                                                           in hight word, this buffer value belongs to camera,. not to bundle */
                                                        if(buffer[4*i+3] & 0xFFFF0000) {
-                                                               MovieClip *clip= object_get_movieclip(scene, basact->object);
+                                                               MovieClip *clip= object_get_movieclip(scene, basact->object, 0);
                                                                int selected;
                                                                track= BKE_tracking_indexed_bundle(&clip->tracking, hitresult >> 16);
 
index 47504a1333c37b628dff38243f3ff81f91f0f8de..5e7f7b8d77b3fc2380a5541151f4e4483935bd9d 100644 (file)
@@ -759,7 +759,7 @@ void VIEW3D_OT_snap_cursor_to_grid(wmOperatorType *ot)
 static void bundle_midpoint(Scene *scene, Object *ob, float vec[3])
 {
        MovieTrackingTrack *track;
-       MovieClip *clip= object_get_movieclip(scene, ob);
+       MovieClip *clip= object_get_movieclip(scene, ob, 0);
        int ok= 0;
        float min[3], max[3], mat[4][4], pos[3];
 
index a0b9c63dedf097f2009704905d4dbea3fdccdc08..247b1153a59e9b963239cadc577856651105a391 100644 (file)
@@ -853,6 +853,23 @@ static void rna_BackgroundImage_opacity_set(PointerRNA *ptr, float value)
        bgpic->blend = 1.0f - value;
 }
 
+static BGpic *rna_BackgroundImage_add(View3D *v3d)
+{
+       BGpic *bgpic= MEM_callocN(sizeof(BGpic), "Background Image");
+
+       bgpic->size= 5.0;
+       bgpic->blend= 0.5;
+       bgpic->iuser.fie_ima= 2;
+       bgpic->iuser.ok= 1;
+       bgpic->view= 0; /* 0 for all */
+
+       BLI_addtail(&v3d->bgpicbase, bgpic);
+
+       WM_main_add_notifier(NC_SPACE|ND_SPACE_VIEW3D, NULL);
+
+       return bgpic;
+}
+
 /* Space Node Editor */
 
 static int rna_SpaceNodeEditor_node_tree_poll(PointerRNA *ptr, PointerRNA value)
@@ -1235,6 +1252,24 @@ static void rna_def_background_image(BlenderRNA *brna)
        RNA_def_property_update(prop, NC_SPACE|ND_SPACE_VIEW3D, NULL);
 }
 
+static void rna_def_backgroundImages(BlenderRNA *brna, PropertyRNA *cprop)
+{
+       StructRNA *srna;
+       FunctionRNA *func;
+       PropertyRNA *parm;
+
+       RNA_def_property_srna(cprop, "BackgroundImages");
+       srna= RNA_def_struct(brna, "BackgroundImages", NULL);
+       RNA_def_struct_sdna(srna, "View3D");
+       RNA_def_struct_ui_text(srna, "Background Images", "Collection of background images");
+
+       func= RNA_def_function(srna, "add", "rna_BackgroundImage_add");
+       RNA_def_function_ui_description(func, "Add new background image");
+
+       parm= RNA_def_pointer(func, "image", "BackgroundImage", "", "Image displayed as viewport background");
+       RNA_def_function_return(func, parm);
+}
+
 static void rna_def_space_view3d(BlenderRNA *brna)
 {
        StructRNA *srna;
@@ -1408,6 +1443,7 @@ static void rna_def_space_view3d(BlenderRNA *brna)
        RNA_def_property_struct_type(prop, "BackgroundImage");
        RNA_def_property_ui_text(prop, "Background Images", "List of background images");
        RNA_def_property_update(prop, NC_SPACE|ND_SPACE_VIEW3D, NULL);
+       rna_def_backgroundImages(brna, prop);
 
        prop= RNA_def_property(srna, "show_background_images", PROP_BOOLEAN, PROP_NONE);
        RNA_def_property_boolean_sdna(prop, NULL, "flag", V3D_DISPBGPICS);