Created
November 14, 2022 01:47
-
-
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
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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