Skip to content

Instantly share code, notes, and snippets.

@shmolyneaux
Last active March 30, 2021 18:12
Show Gist options
  • Save shmolyneaux/73d718d20f48a453ab084797141cd387 to your computer and use it in GitHub Desktop.
Save shmolyneaux/73d718d20f48a453ab084797141cd387 to your computer and use it in GitHub Desktop.
Blender Onion Skinning (WIP)
import bpy
import gpu
from gpu_extras.batch import batch_for_shader
from collections import defaultdict
from time import time
from math import sin
shader = gpu.shader.from_builtin('3D_UNIFORM_COLOR')
mesh_cache = defaultdict(lambda: defaultdict(lambda: ([],[])))
def update_mesh_cache(scene):
current_frame = scene.frame_current
deps_graph = bpy.context.evaluated_depsgraph_get()
for obj in bpy.context.selected_objects:
evaluated_obj = obj.evaluated_get(deps_graph)
obj_data = evaluated_obj.to_mesh()
vertices = [
(v.co.x, v.co.y, v.co.z)
for v in obj_data.vertices
]
indices=[
(e.vertices[0], e.vertices[1])
for e in obj_data.edges
]
mesh_cache[current_frame][obj.name_full] = (vertices, indices)
def draw():
shader.bind()
shader.uniform_float("color", (1, 0, 0, 1))
for per_frame_info in mesh_cache.values():
for vertices, indices in per_frame_info.values():
batch = batch_for_shader(
shader,
'LINES',
{
"pos": vertices
},
indices=indices
)
batch.draw(shader)
bpy.types.SpaceView3D.draw_handler_add(draw, (), 'WINDOW', 'POST_VIEW')
bpy.app.handlers.frame_change_post.append(update_mesh_cache)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment