1

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?

1 Answers1

0

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.

X Y
  • 5,234
  • 1
  • 6
  • 20
  • 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? – i7_macintosh Aug 06 '22 at 02:12
  • 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. – X Y Aug 06 '22 at 08:58
  • If there are more than two viewports on the workspace, it also can draw on specific viewports, see line 84 in: https://blender.stackexchange.com/questions/270382/how-can-i-make-a-2d-gpu-polygon-draggable/271032#271032 – X Y Aug 06 '22 at 09:05