4
$\begingroup$

My current workflow for a project I am working on uses empties in the 3D-View to more easily control properties of strips in the VSE via drivers. However sometimes it gets a little confusing which empties control which strips and I sometimes manipulate the wrong empty.

Is there a way using Python to automatically select the proper empty when I select a strip in the VSE? For instance when I select the strip in channel 4 of the VSE I would like it to select empty Fader_4.

I tried a little bit myself but I'm not too swift with Blender Python and I couldn't figure out how to tell when a strip is selected in the VSE.

$\endgroup$
5
$\begingroup$

A strip (sequence) is selected in the VSE when its select property is True. The selected strips are available via the context.selected_sequences list, and the active strip is context.scene.sequence_editor.active_strip

Test code using modal timer operator. Prepended a toggle button to turn autoselect on / off, to the Sequence Editor header.

enter image description here

import bpy
from bpy.props import BoolProperty
from bl_ui.space_sequencer import act_strip

class SeqModalTimerOperator(bpy.types.Operator):
    """Operator which runs its self from a timer"""
    bl_idname = "wm.modal_timer_operator"
    bl_label = "Modal Timer Operator"

    _timer = None

    def modal(self, context, event):
        scene = context.scene
        active_strip = act_strip(context)

        if event.type in {'ESC'} or not scene.auto_select:
            self.cancel(context)
            return {'CANCELLED'}

        if event.type == 'TIMER' and active_strip:

            # deselect everything
            for o in scene.objects:
                o.select = False
            # select empties, make active active.
            for s in context.selected_sequences:                
                empty = scene.objects.get("Fader_%d" % s.channel)
                if empty:
                    empty.select = True
                    if s == active_strip:
                        scene.objects.active = empty

        return {'PASS_THROUGH'}

    def execute(self, context):
        wm = context.window_manager
        self._timer = wm.event_timer_add(0.1, context.window)
        wm.modal_handler_add(self)
        return {'RUNNING_MODAL'}

    def cancel(self, context):
        wm = context.window_manager
        wm.event_timer_remove(self._timer)

def toggle_auto_select(self, context):
    scene = context.scene
    layout = self.layout
    #layout.label("Auto Select")
    layout.prop(scene, "auto_select", toggle=True)

def auto_select(self, context):
    if self.auto_select:
        bpy.ops.wm.modal_timer_operator()


def register():
    bpy.utils.register_class(SeqModalTimerOperator)
    bpy.types.Scene.auto_select = BoolProperty(
                               default=False,
                               name="Auto Select",
                               description="Turn on to Auto select Empties",
                               update=auto_select)
    bpy.types.SEQUENCER_HT_header.prepend(toggle_auto_select)


def unregister():
    bpy.utils.unregister_class(SeqModalTimerOperator)


if __name__ == "__main__":
    register()
$\endgroup$

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.