Skip to content

Instantly share code, notes, and snippets.

@zeffii
Created June 11, 2020 11:25
Show Gist options
  • Save zeffii/1416b4638b5a6db255a2ea5d08aeef6b to your computer and use it in GitHub Desktop.
Save zeffii/1416b4638b5a6db255a2ea5d08aeef6b to your computer and use it in GitHub Desktop.
test_handler with out remove before repeat-running
import bpy
import gpu
import bgl
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')
poly_indices = np.empty((len(mesh.loop_triangles), 3), 'i')
edge_indices = np.array(mesh.edge_keys)
mesh.vertices.foreach_get("co", np.reshape(vertices, len(mesh.vertices) * 3))
mesh.loop_triangles.foreach_get("vertices", np.reshape(poly_indices, len(mesh.loop_triangles) * 3))
shader = gpu.shader.from_builtin('3D_UNIFORM_COLOR')
batch = batch_for_shader(shader, 'TRIS', {"pos": vertices}, indices=poly_indices)
wire_shader = gpu.shader.from_builtin('3D_UNIFORM_COLOR')
wire_batch = batch_for_shader(shader, 'LINES', {"pos": vertices}, indices=edge_indices)
def draw():
bgl.glEnable(bgl.GL_DEPTH_TEST)
bgl.glEnable(bgl.GL_POLYGON_OFFSET_FILL)
bgl.glPolygonOffset(1.0, 1.0)
batch.draw(shader)
bgl.glDisable(bgl.GL_POLYGON_OFFSET_FILL)
wire_shader.bind()
wire_shader.uniform_float("color", (1, 0, 0, 1))
wire_batch.draw(wire_shader)
bgl.glDisable(bgl.GL_DEPTH_TEST)
if hasattr(bpy, 'test_handler'):
bpy.types.SpaceView3D.draw_handler_remove(bpy.test_handler, 'WINDOW')
bpy.test_handler = bpy.types.SpaceView3D.draw_handler_add(draw, (), 'WINDOW', 'POST_VIEW')
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment