1
$\begingroup$

In an add-on I'm developing, I'd like to draw elements (using the GPU module) to a N-key panel. I've looked extensively, but I haven't found a correct answer.

https://blender.stackexchange.com/a/155692/23372 In the "Hard Way" section, it shows a way to draw an image to the screen, but it draws it to the Properties panel, which is not a desired effect.

Is there a way to make elements show/hide (when panels are shown/hidden) and move (when panels are rearranged and not have a fixed position)?

I've seen this done a lot in the node editor (see https://blendermarket.com/products/node-preview, https://docs.animation-nodes.com/release_notes/v2_0/interpolation/#mix-interpolation (with the Interpolation Viewer node)).

Can this be achieved using the N-key panel?

$\endgroup$

1 Answer 1

0
$\begingroup$

enter image description here

import bpy
import gpu
from gpu_extras.batch import batch_for_shader

vertices = (
    (10, 10), (300, 10),
    (10, 200), (300, 200))

indices = (
    (0, 1, 2), (2, 1, 3))

shader = gpu.shader.from_builtin('2D_UNIFORM_COLOR')
batch = batch_for_shader(shader, 'TRIS', {"pos": vertices}, indices=indices)


def draw():
    shader.bind()
    shader.uniform_float("color", (0, 0.5, 0.5, 1.0))
    batch.draw(shader)

class ModalDrawOperator(bpy.types.Operator):
    bl_idname = "view3d.modal_operator"
    bl_label = "Test Operator"

    def invoke(self, context, event):
        if context.area.type != 'VIEW_3D':
            print("Viewport not found")
            return {'CANCELLED'}

        bpy.types.SpaceView3D.draw_handler_add(draw, (), 'UI', 'POST_PIXEL')
        return {'FINISHED'}

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

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

if __name__ == "__main__":
    register()

Is there a way to make elements show/hide (when panels are shown/hidden) and move (when panels are rearranged and not have a fixed position)?
Yes, it do auto.

$\endgroup$
3
  • $\begingroup$ Definitely more the direction I'm looking for! However, it's not quite fixed onto a specific tab and subpanel. Could it be implemented with those features, or is it not possible? $\endgroup$ Aug 6, 2022 at 2:12
  • $\begingroup$ If you can get the name of current category and get the tab position, it can do that. glScissor can cut the drawing area according to the given position. Unfortunately, I can't figure out how to get the current category. $\endgroup$
    – X Y
    Aug 6, 2022 at 8:58
  • $\begingroup$ If there are more than two viewports on the workspace, it also can draw on specific viewports, see line 84 in: blender.stackexchange.com/questions/270382/… $\endgroup$
    – X Y
    Aug 6, 2022 at 9:05

You must log in to answer this question.

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