Skip to content

Instantly share code, notes, and snippets.

@Lightnet
Created April 17, 2020 17:40
Show Gist options
  • Save Lightnet/1672f5ac295ed5f25255636d581edac8 to your computer and use it in GitHub Desktop.
Save Lightnet/1672f5ac295ed5f25255636d581edac8 to your computer and use it in GitHub Desktop.
SImple rig and object rebuilds.
bl_info = {
"name": "Simple My Tools b28x",
"author":"Lightnet",
"version":(0,0,1),
"blender": (2,82,0),
"location": "Object",
"category": "Object",
"warning": "",
"wiki_url": "",
}
import bpy
from bpy.props import (StringProperty,
BoolProperty,
IntProperty,
FloatProperty,
EnumProperty,
PointerProperty,
)
from bpy.types import (Panel,
Operator,
PropertyGroup,
)
def eraseAllKeyframes(scene, passedOB = None):
print("init checks...")
print(passedOB)
if passedOB != None:
print("animation data")
ad = passedOB.animation_data
if ad != None:
print ('ad=',ad)
passedOB.animation_data_clear()
#scene.update()
class OBJECT_ClearAnimationDatas(bpy.types.Operator):
"""description"""
bl_idname = "object.clearanimationdatas"
bl_label = "Clear Animation Datas"
def execute(self, context):
scene = bpy.context.scene
passedOB = bpy.context.view_layer.objects.active
bpy.ops.object.mode_set(mode='OBJECT')
for ob in bpy.context.scene.objects:
eraseAllKeyframes(scene,ob)
bpy.ops.object.mode_set(mode='OBJECT')
print("finish...")
return {'FINISHED'}
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(rigname):
#Get Armature bone to add bone list
for b in bpy.data.objects[rigname].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')
class OBJECT_ArmRebuildrig(bpy.types.Operator):
"""description"""
bl_idname = "object.armrebuildrig"
bl_label = "Rebuild Armature Rig"
def execute(self, context):
#print(bpy.context.view_layer.objects.active.name)
selectobjname = bpy.context.view_layer.objects.active.name
edit_rig(selectobjname)
build_rig(selectobjname)
print("finish...")
return {'FINISHED'}
class OBJECT_PT_mytools(bpy.types.Panel):
"""Creates a Panel in the Object properties window"""
bl_label = "My Custom Tools"
bl_idname = "OBJECT_PT_mytools"
bl_space_type = 'PROPERTIES'
bl_region_type = 'WINDOW'
bl_context = "object"
def draw(self, context):
layout = self.layout
scene = context.scene
layout.label(text="My Tools")
mycustomtools = scene.mycustomtools
col = layout.column(align=True)
row = col.row(align=True)
row.prop(mycustomtools, "mytoolshowobjects")
row.prop(mycustomtools, "mytoolshowarmatures")
layout.prop(mycustomtools, "mytoolshowexports")
if mycustomtools.mytoolshowobjects:
layout.label(text="My Objects")
pass
if mycustomtools.mytoolshowarmatures:
layout.operator('object.clearanimationdatas')
layout.operator('object.armrebuildrig')
pass
if mycustomtools.mytoolshowexports:
layout.label(text="Exports")
pass
#self.layout.operator(displaydialog_op.bl_idname)
return
class MyCustomTools(PropertyGroup):
mytoolshowobjects= BoolProperty(name="Show Objects",default=True) # show mesh
mytoolshowarmatures= BoolProperty(name="Armatures",default=True) # show armature
mytoolshowexports= BoolProperty(name="Exports",default=True) # export to file
classes = (
OBJECT_PT_mytools,
OBJECT_ArmRebuildrig,
OBJECT_ClearAnimationDatas,
MyCustomTools,
)
def register():
print("register hi")
for cls in classes:
bpy.utils.register_class(cls)
bpy.types.Scene.mycustomtools= PointerProperty(type=MyCustomTools)
#bpy.types.TOPBAR_MT_file.append(draw_item_submenu)# File Menu Top Left
def unregister():
print("register Goodbye")
for cls in classes:
bpy.utils.unregister_class(cls)
#bpy.types.TOPBAR_MT_file.remove(draw_item_submenu)# File Menu Top Left
# This allows you to run the script directly from Blender's Text editor
# to test the add-on without having to install it.
if __name__ == "__main__":
register()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment