5
$\begingroup$

A small edit of test code from GPU Shader Module (gpu) "Mesh with Random Vertex Colors" to save the handle to the driver namespace ...

import bpy
import gpu
import numpy as np
from random import random
from gpu_extras.batch import batch_for_shader

mesh = bpy.context.active_object.data
mesh.calc_loop_triangles()

vertices = np.empty((len(mesh.vertices), 3), 'f')
indices = np.empty((len(mesh.loop_triangles), 3), 'i')

mesh.vertices.foreach_get(
    "co", np.reshape(vertices, len(mesh.vertices) * 3))
mesh.loop_triangles.foreach_get(
    "vertices", np.reshape(indices, len(mesh.loop_triangles) * 3))

vertex_colors = [(random(), random(), random(), 1) for _ in range(len(mesh.vertices))]

shader = gpu.shader.from_builtin('3D_SMOOTH_COLOR')
batch = batch_for_shader(
    shader, 'TRIS',
    {"pos": vertices, "color": vertex_colors},
    indices=indices,
)


def draw():
    batch.draw(shader)


h = bpy.types.SpaceView3D.draw_handler_add(draw, (), 'WINDOW', 'POST_VIEW')

bpy.app.driver_namespace["foo"] = h

Such that we can remove via the python console

>>> dns = bpy.app.driver_namespace
>>> h = dns['foo']
>>> h
<capsule object "RNA_HANDLE" at 0x7f4841cdbf90>

>>> bpy.types.SpaceView3D.draw_handler_remove(h, 'WINDOW')

... otherwise it is drawn on the screen until blender is shut.

Or is it? Is there a way, via python API, to remove all or any draw callbacks without the handle?

Related re making a class to "handle the handles"

I can't get a SpaceView3D draw callback to work

How do you remove a draw handler after it's been added?

handle vertex property changes with msgbus.subscribe_rna

$\endgroup$
4
  • $\begingroup$ I don't know too much what this question was about, but was it necessary to create the 'gpu-module' tag? Are there many other questions that would benefit from this tag? $\endgroup$ Commented Jan 19, 2020 at 17:03
  • 1
    $\begingroup$ There is a new gpu python module in 2.8, replacing the bgl module to draw on the viewport. Feel the gpu tag refers more to the hardware. bmesh as a standalone module has a tag, yet again blf doesn't. $\endgroup$
    – batFINGER
    Commented Jan 19, 2020 at 17:24
  • $\begingroup$ Ah ok. Seems appropriate then. $\endgroup$ Commented Jan 19, 2020 at 17:25
  • $\begingroup$ Suggest adding a definition for the 'gpu-module' tag. Already seen the first misuse of it as a generic gpu tag. $\endgroup$ Commented Jan 21, 2020 at 16:32

0

You must log in to answer this question.

Browse other questions tagged .