1
$\begingroup$

I am trying to draw some guide-lines on top of my image in my image-view using bgl.

But when I draw the coordinates of my shape/line is relative to the screen/view when I actually want what I draw to always be on top of my image in the image-view.

Is there any way to extract the image position to transform my shape to always be on my image or is there any other way to do this?

$\endgroup$
0
$\begingroup$

So I solved this, it appears that the image editor is also like a 3d coordinate system. So I followed a tut for 3D and then just changed the view to SpaceImageEditor and instead of drawing on 'POST_PIXEL' I drew to 'POST_VIEW'. Code:

def __init__(self):
    self.draw_handle = None
    self.draw_event = None

    self.widgets = []


def invoke(self, context, event):

    self.create_batch()

    args = (self, context)
    self.register_handlers(args, context)

    context.window_manager.modal_handler_add(self)
    return {'RUNNING_MODAL'}

def register_handlers(self, args, context):
    self.draw_handle = bpy.types.SpaceImageEditor.draw_handler_add(self.draw_callback, args,'WINDOW', 'POST_VIEW')

    self.draw_event = context.window_manager.event_timer_add(0.1, window=context.window)

def unregister_handlers(self, context):
    context.window_manager.event_timer_remove(self.draw_event)

    bpy.types.SpaceImageEditor.draw_handler_remove(self.draw_handle, 'WINDOW')

    self.draw_handle = None
    self.draw_event = None

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

    if event.type in {'ESC'}:
        self.unregister_handlers(context)
        return {'CANCELLED'}

    return {'PASS_THROUGH'}

def finish(self):
    self.unregister_handlers(context)
    return {'FINISHED'}

def create_batch(self):

    vertices = [(0, 0.5, 0), (1, 0.5, 0)]

    self.shader = gpu.shader.from_builtin('3D_UNIFORM_COLOR')
    self.batch = batch_for_shader(self.shader, 'LINE_STRIP', {'pos': vertices})

def draw_callback(self, op, context):

    self.shader.bind()
    self.shader.uniform_float('color', (1,0,0,1))
    self.batch.draw(self.shader)
|improve this answer|||||
$\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.