10
$\begingroup$

For further reconstruction of a shot, I'd like to access the 3d location of the projected tracks by the camera solver constraint in the 3d View for all frames via python.

While searching through the Datablocks I've only found a read only bundle attribute per track: bpy.data.movieclips["clip_name"].tracking.tracks["track_name"].bundle, but unfortunately the given coordinates are totally off:

enter image description here

Is there any way to access the properties of the 3d tracks at the moment?

$\endgroup$

1 Answer 1

4
+100
$\begingroup$

Copy matrix_world uninfluenced by "Camera Solver" constraint, ie set constraint influence to 0, set the frame to the first frame of the simulation, save a matrix_world copy, then reset the influence.

The track positions are mworldcopy * Track.bundle

Camera tracking points in red, object tracking points in green, resultant object tracking points in yellow.

import bpy

from sound_drivers.BGL_draw_visualiser import BGLWidget

context = bpy.context
scene = context.scene
scene.frame_set(1)
camera = cam = scene.camera
cam_const = cam.constraints[0]
cam_const.influence = 0
mw = cam.matrix_world.copy()
cam_const.influence = 1

class ShowTracks(BGLWidget):
    def draw_region(self, context):
        clip = context.scene.active_clip
        tracking = clip.tracking                
        tracking_object = clip.tracking.objects.active

        # match to object tracking
        # from bl_operators/clip.py
        if camera:
            reconstruction = tracking_object.reconstruction
            framenr = scene.frame_current - clip.frame_start + 1
            reconstructed_matrix = reconstruction.cameras.matrix_from_frame(framenr)
            matrix = camera.matrix_world * reconstructed_matrix.inverted()

        else:
            return

        tracks = tracking.objects

        # track points in global space current frame         
        pts = [matrix  * t.bundle for t in tracks['Object'].tracks]    
        pts = self.points3d_points2d(context, pts)
        self.draw_points(pts, lw=2, color=(1, 1, 0, 1))

        # tracking data 
        pts = [mw * t.bundle for t in tracks['Camera'].tracks]
        pts = self.points3d_points2d(context, pts)
        self.draw_points(pts, lw=1, color=(1, 0, 0, 1))

        pts = [mw * t.bundle for t in tracks['Object'].tracks]
        pts = self.points3d_points2d(context, pts)
        self.draw_points(pts, lw=1, color=(0, 1, 0, 1))

v = ShowTracks(None, bpy.context, bpy.types.SpaceView3D)

enter image description here

$\endgroup$
1
  • 2
    $\begingroup$ Found code required in bl_operators/clip.py, fortunately cos the source code had me a bit stumped on what is readily available thru the API with reconstruction.cameras.matrix_from_frame(framenr) Edited the answer to show yellow lines connecting the solved object tracks, in the same locations as empties created with bpy.ops.track_to_empty and setting the generated empty's constraint use_3d_position to True would be. $\endgroup$
    – batFINGER
    Commented Nov 15, 2015 at 9:02

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .