6
$\begingroup$

I am looking to overlay a sound vis, using bgl, over strips in the NLA, VSE, like

Active Strip with Overlay

How can I find the area.region.view2d x,y, width and height of strips in the NLA and VSE editors using python?

$\endgroup$
5
+100
$\begingroup$

Yes, this can be done, using the API call View2D.view_to_region.

Heres a "hello world" example, drawing Hello World under the active strip.

import bpy
import bgl
import blf


def draw_callback_px(self, context):
    print("mouse points", len(self.mouse_path))

    region = context.region
    strip = context.scene.sequence_editor.active_strip
    if strip is None:
        return

    x = strip.frame_final_start
    y = strip.channel
    x, y = region.view2d.view_to_region(x, y)

    font_id = 0  # XXX, need to find out how best to get this.

    # draw some text
    blf.position(font_id, x, y, 0)
    blf.size(font_id, 20, 72)
    blf.draw(font_id, "Hello Word " + str(len(self.mouse_path)))


class ModalDrawOperator(bpy.types.Operator):
    """Draw a line with the mouse"""
    bl_idname = "sequencer.modal_operator"
    bl_label = "Simple Modal Sequencer Operator"

    def modal(self, context, event):
        context.area.tag_redraw()
        region = context.region

        if event.type == 'MOUSEMOVE':
            self.mouse_path.append((event.mouse_region_x, event.mouse_region_y))

        elif event.type == 'LEFTMOUSE':
            bpy.types.SpaceSequenceEditor.draw_handler_remove(self._handle, 'WINDOW')
            return {'FINISHED'}

        elif event.type in {'RIGHTMOUSE', 'ESC'}:
            bpy.types.SpaceSequenceEditor.draw_handler_remove(self._handle, 'WINDOW')
            return {'CANCELLED'}

        return {'RUNNING_MODAL'}

    def invoke(self, context, event):
        if context.area.type == 'SEQUENCE_EDITOR':
            # the arguments we pass the the callback
            args = (self, context)
            # Add the region OpenGL drawing callback
            # draw in view space with 'POST_VIEW' and 'PRE_VIEW'
            self._handle = bpy.types.SpaceSequenceEditor.draw_handler_add(draw_callback_px, args, 'WINDOW', 'POST_PIXEL')

            self.mouse_path = []

            context.window_manager.modal_handler_add(self)
            return {'RUNNING_MODAL'}
        else:
            self.report({'WARNING'}, "SequenceEditor not found, cannot run operator")
            return {'CANCELLED'}


def register():
    bpy.utils.register_class(ModalDrawOperator)


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

if __name__ == "__main__":
    register()

Note that this is the template operator_modal_draw.py with minor edits.

The draw_callback_px can be added without having to be connected to a modal operator, if you want this to display continuously.

Also see: https://github.com/armadillica/attract-blender-addon/blob/master/draw.py for example use cases.

$\endgroup$
  • $\begingroup$ Cheers for that example use case scroller_width = 16 * dpi_fac was particularly handy $\endgroup$ – batFINGER Oct 25 '15 at 8:41

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.