1
$\begingroup$

I am attempting to programmatically use motion tracking in Blender. My script successfully adds the image strip to a clip editor but not the one in the motion tracking workspace.

bpy.ops.workspace.append_activate(idname="Motion Tracking")
bpy.context.area.type = 'CLIP_EDITOR'

Making the context a clip editor using the code above only makes the local area within the scripting workspace become a clip editor, so when I switch to motion tracking that workspace is still empty. I used a similar sequence of commands to activate and add to the sequence editor in the VSE but it does not seem to work with motion tracking. Does anyone have any suggestions or is this possibly a bug that I will have to manually workaround?

UPDATE 1

Thanks to some help from another user, I was able to change the workspace to motion tracking but I do not think it is active as the clip editor in that workspace is not used. Instead, it changes the text editor area in the scripting workspace. Here is my code with some comments to hopefully clarify my issue.

import bpy

# ImportHelper is a helper class, defines filename and
# invoke() function which calls the file selector.
from bpy.props import StringProperty, CollectionProperty
from bpy.types import Operator
from bpy_extras.io_utils import ImportHelper
from pathlib import Path
from bpy import data as D

class Motion_OT_Tracking(Operator, ImportHelper):
    """Imports rendered images and tracks cells"""
    bl_idname = "import_rendered.images"  # important since its how bpy.ops.import_test.some_data is constructed
    bl_label = "Import Images"
    bl_options = {"REGISTER", "UNDO"}
    
    directory: StringProperty(
        options={'HIDDEN'}
    )
    
    files: CollectionProperty(type=bpy.types.PropertyGroup)

    # ImportHelper mixin class uses this
    filename_ext = bpy.path.extensions_image

    # List of operator properties, the attributes will be assigned
    # to the class instance from the operator settings before calling.

    @classmethod
    def poll(cls, context):
        return True
        ##use filename_ext to verify correct file type (acceptable image) how??

    def execute(self, context):
        for ws in D.workspaces:                 ##changes workspace to motion tracking
            if ws.name == "Motion Tracking":
                bpy.context.window.workspace = ws
#       bpy.ops.workspace.append_activate(idname="Motion Tracking")   ##not sure if i need this somewhere
        bpy.context.area.type = 'CLIP_EDITOR'
        directory_path = Path(self.filepath).parent
        file_list = []
        for file in self.files: #creates list of file names selected from file selector po up
            file_list.append({"name":file.name})
        bpy.ops.clip.open(                      ##adds files to clip editor
            directory=str(directory_path),
            files=file_list,
            relative_path=True
        )
        bpy.context.area.type = 'TEXT_EDITOR'
        return {'FINISHED'}

def invoke(self, context, event):
        context.window_manager.fileselect_add(self)
        return {'RUNNING_MODAL'}

def register():
    bpy.utils.register_class(Motion_OT_Tracking)
        
def unregister():
    bpy.utils.unregister_class(Motion_OT_Tracking)

if __name__ == "__main__":
    register()

    # test call
    bpy.ops.import_rendered.images('INVOKE_DEFAULT')

Any help is greatly appreciated, thank you!

$\endgroup$
6
  • $\begingroup$ So, just to be clear the intent of this operator is essentially to bulk import and dump clips into the clip editor correct? $\endgroup$
    – Jakemoyo
    Commented Jul 8, 2022 at 18:51
  • $\begingroup$ Yes but they are not clips but rather individual frames (images) that were rendered into an image strip the VSE from a previous class I wrote. For some reason I found that importing raw unrendered images into the clip editor did not work and I had to go through the VSE first @Jakemoyo $\endgroup$
    – se2000
    Commented Jul 8, 2022 at 19:17
  • $\begingroup$ Then I think you should just take an entirely different approach. It seems like you're trying to automate navigating to the window and opening the file browser and dragging them into the clip editor. You should try just creating the datablocks directly in Python and then just adding them to the database with ` bpy.data.scenes['Scene'].sequence_editor.sequences.new_image()` or something like that. $\endgroup$
    – Jakemoyo
    Commented Jul 8, 2022 at 19:23
  • $\begingroup$ Classic XY problem $\endgroup$
    – Jakemoyo
    Commented Jul 8, 2022 at 19:28
  • $\begingroup$ So I am very new to Blender scripting and was sure there was an easier way to go about my problem. The goal of my algorithm is to auto detect and track the motion of points so I need to use the Motion Tracking workspace. However, when I try to even manually add the images (unrendered PNG, JPEG, TIF, etc.) to the clip editor they do not appear. This is why I first rendered them in the sequence editor. I'm sorry, I am brand new to this API it has been very tough since it is not my main focus. Could you possibly explain a little more about your suggestion? @Jakemoyo $\endgroup$
    – se2000
    Commented Jul 8, 2022 at 20:04

1 Answer 1

2
$\begingroup$

Area is the data type for the different chunks of a workspace. So when you're looking at the default "Scripting" workspace there's like 4 areas, "VIEW_3D", "CONSOLE", "INFO", "TEXT_EDITOR" etc. Those are what areas are. Switching the context.area.type will only change the active windowpane you're working in.

If you want to switch to a different workspace you have to get the actual workspace datablock and then set the context.workspace to that, you can't just do it with type.

Like:

import bpy
from bpy import data as D

for ws in D.workspaces:
    if ws.name == "Motion Tracking":
        bpy.context.window.workspace = ws
$\endgroup$
1
  • 1
    $\begingroup$ So if I run this script, it does change the window (thank you!) but still does not activate the clip editor in the motion tracking workspace and instead just changes the text editor in the scripting workspace to the clip editor. Is there some way to activate the clip editor in only the motion tracking workspace? $\endgroup$
    – se2000
    Commented Jul 8, 2022 at 16:05

You must log in to answer this question.

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