Skip to content

Instantly share code, notes, and snippets.

@Lightnet
Last active April 13, 2020 19:34
Show Gist options
  • Save Lightnet/715cf31cee3e901476927bc6a0f809d8 to your computer and use it in GitHub Desktop.
Save Lightnet/715cf31cee3e901476927bc6a0f809d8 to your computer and use it in GitHub Desktop.
blender armature copy bones to armature bone
import bpy
#source_rig = 'MBlab_sk'
#dest_rigs = ['Armature']
############
bone_data = {}
selectobjname = None
def edit_rig(rigname):
bpy.ops.object.mode_set(mode='OBJECT')
bpy.ops.object.select_all(action='DESELECT')
bpy.data.objects[rigname].select_set(state=True)
bpy.context.view_layer.objects.active = bpy.data.objects[rigname]
bpy.ops.object.mode_set(mode='EDIT')
def build_rig():
#Get Armature bone to add bone list
for b in bpy.data.objects[selectobjname].data.edit_bones:
#print(b.name)
#print(dir(b))
#print((b.parent))
if b.parent != None:
#Store data, parent name as string
bone_data[b.name] = (b.head.copy(), b.tail.copy(), b.roll, b.parent.name)
else:
#None if case there bone parent to store in the bone data
bone_data[b.name] = (b.head.copy(), b.tail.copy(), b.roll, None)
bpy.ops.object.mode_set(mode='OBJECT')
#Create Armture
nameobj = "skel"
pivot_bone = bpy.data.armatures.new(nameobj)
#Create Object Armture
object = bpy.data.objects.new(nameobj, pivot_bone)
#Add Object to Scene Collection
bpy.context.scene.collection.objects.link(object)
#deselct all object
bpy.ops.object.select_all(action='DESELECT')
#Set active object
bpy.context.view_layer.objects.active = object
bpy.ops.object.mode_set(mode='EDIT')
edit_bones = pivot_bone.edit_bones
#print(dir(pivot_bone))
for b in bone_data:
#print(b)
#Create Bone
bone = edit_bones.new(b)
head, tail, roll, parent = bone_data[b]
bone.head = head
bone.tail = tail
bone.roll = roll
#print("Bone:", b)
#print("PARENT:", parent)
if parent != None:
#print(parent)
#print(edit_bones[parent])
#print(pivot_bone.bones[parent])
#bone.parent = pivot_bone.bones[parent]
bone.parent = edit_bones[parent]
bpy.ops.object.mode_set(mode='OBJECT')
#print(bpy.context.view_layer.objects.active.name)
selectobjname = bpy.context.view_layer.objects.active.name
edit_rig(selectobjname)
build_rig()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment