Skip to content

Instantly share code, notes, and snippets.

@sjkillen
Created November 14, 2022 01:47
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save sjkillen/92fe4229148f474a627ac9f63156e5d3 to your computer and use it in GitHub Desktop.
Save sjkillen/92fe4229148f474a627ac9f63156e5d3 to your computer and use it in GitHub Desktop.
Create a simple mesh from an armature that can could create a similar armature using the skin modifier
import bpy
from bpy.types import Scene, Armature, Mesh
from operator import itemgetter
from itertools import chain
def create_geometry(armature: Armature) -> Mesh:
roots = tuple(bone for bone in armature.bones if bone.parent is None)
mesh = bpy.data.meshes.new(armature.name)
bone_tail_indices = {
bone: i for i, bone in enumerate(armature.bones, start=len(roots))
}
verts = tuple(root.head_local for root in roots) + tuple(bone.tail_local for bone in armature.bones)
edges = []
for i, root in enumerate(roots):
edges.append((i, bone_tail_indices[root]))
working_roots = list(roots)
while working_roots:
root = working_roots.pop()
for child in root.children:
edges.append((bone_tail_indices[root], bone_tail_indices[child]))
working_roots.append(child)
mesh.from_pydata(verts, edges, ())
return mesh
armature = bpy.context.active_object.data
if type(armature) is Armature:
mesh = create_geometry(armature)
obj = bpy.data.objects.new(bpy.context.active_object.name, mesh)
bpy.context.scene.collection.objects.link(obj)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment