0
$\begingroup$

I've been trying to create a script to allow me track the camera in a video in blender. The reason for this is such that I can later add a 3d model in the video. I want to do all of this with scripts. I have the following script:

import pathlib
import bpy

BASE_DIR = pathlib.Path(__file__).parent.resolve()
VIDEO_PATH = BASE_DIR / "videos" / "video.mp4" # TODO: Figure out a better way to load videos...
OUPUT_DIR = BASE_DIR / "scenes" / "result.blend" # TODO: Fix me


def import_video(video_path):
    return bpy.data.movieclips.load(str(video_path))

def setup_scene(video_clip):
    scene = bpy.context.scene
    scene.frame_start = 1
    scene.frame_end = video_clip.frame_duration
    scene.render.fps = int(video_clip.fps)

    # Set the active movie clip for tracking
    bpy.context.scene.active_clip = video_clip

    # Setup camera settings based on the video clip
    tracking = video_clip.tracking
    tracking.camera.sensor_width = 36.0  # Example sensor width in mm
    tracking.camera.focal_length = 50.0   # Example focal length in mm
    return scene, tracking

def get_clip_editor_override(clip):
    # Create a temporary area in the current screen with a Movie Clip Editor
    for area in bpy.context.window.screen.areas:
        if area.type == 'CLIP_EDITOR':
            for space in area.spaces:
                if space.type == 'CLIP_EDITOR':
                    space.clip = clip
                    # Return a context override
                    return {
                        'area': area,
                        'region': area.regions[-1],
                        'space_data': space,
                        'scene': bpy.context.scene
                    }
    # If no CLIP_EDITOR area exists, create one (in background this might not exist)
    raise RuntimeError("No CLIP_EDITOR area found for context override")

# === Main script ===
# Load the video and prepare the scene for motion tracking
clip = import_video(VIDEO_PATH)

# Setup the scene
scene, tracking = setup_scene(clip)

bpy.ops.wm.save_mainfile(filepath=str(OUPUT_DIR))
print("Scene saved to:", OUPUT_DIR)

# --- SET ACTIVE CLIP FOR TRACKING ---
bpy.context.scene.active_clip = clip

override = get_clip_editor_override(clip)
# --- DETECT FEATURES WITH OVERRIDE ---
with bpy.context.temp_override(**override):
    bpy.ops.clip.detect_features(threshold=0.1, margin=30)

but it's erroring out when I call it from my terminal with:

blender .\tracking_startpoint.blend --python .\auto_track.py

Also note that tracking_startpoint.blend is literally an empty new blender scene with focus on the Motion tracking workspace where I have a light and a camera. Screenshot below: tracking_startpoint.blend

The error I am getting:

blender .\tracking_startpoint.blend --background --python .\auto_track.py
TBBmalloc: skip allocation functions replacement in ucrtbase.dll: unknown prologue for function _msize
Blender 4.2.1 LTS (hash 396f546c9d82 built 2024-08-19 11:29:52)
Read blend: "C:\...\tracking_startpoint.blend"
Warning: File written by newer Blender binary (404.30), expect loss of data!
Loaded video clip: video.mp4
Info: Total files 1 | Changed 0 | Failed 0
Info: Saved "solved_tracking.blend"
Saved blend to: C:\...\solved_tracking.blend
Traceback (most recent call last):
  File "C:...\auto_track.py", line 60, in <module>
    bpy.ops.clip.detect_features(override, threshold=0.1, margin=30)
  File "C:\...\blender\blender-4.2.1-windows-x64\blender-4.2.1-windows-x64\4.2\scripts\modules\bpy\ops.py", line 106, in __call__
    C_exec, C_undo = _BPyOpsSubModOp._parse_args(args)
                     ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "C:\...\blender\blender-4.2.1-windows-x64\blender-4.2.1-windows-x64\4.2\scripts\modules\bpy\ops.py", line 60, in _parse_args
    raise ValueError("1-2 args execution context is supported")
ValueError: 1-2 args execution context is supported

Blender quit

Any idea what I am doing wrong?

As shown in the error I am on blender 4.2 LTS on my terminal/command line and on Windows (though I guess the OS shouldn't matter). Thank you in advance!

$\endgroup$

0

You must log in to answer this question.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.