Skip to content

Instantly share code, notes, and snippets.

@Lightnet
Created May 24, 2025 04:48
Show Gist options
  • Save Lightnet/8772ea291309bf00c0e55985261f010f to your computer and use it in GitHub Desktop.
Save Lightnet/8772ea291309bf00c0e55985261f010f to your computer and use it in GitHub Desktop.
action list test and armture pose bones.
bl_info = {
"name": "Test Build",
"category": "3D View",
"author": "lightnet",
"blender": (4, 4, 0),
"version": (0, 0, 1),
"description": "Test Script",
"doc_url": "",
"tracker_url": "",
}
import time
import bpy
from bpy.types import Panel, Operator
def main(context):
for ob in context.scene.objects:
print(ob)
class SimpleOperator(Operator):
"""Tooltip"""
bl_idname = "object.simple_operator"
bl_label = "Simple Object Operator"
@classmethod
def poll(cls, context):
return context.active_object is not None
def execute(self, context):
main(context)
return {'FINISHED'}
class ActionsOperator(Operator):
"""Tooltip"""
bl_idname = "object.actions_operator"
bl_label = "Actions Object Operator"
@classmethod
def poll(cls, context):
return context.active_object is not None
def execute(self, context):
print("test")
obj = context.active_object
ad = context.active_object.animation_data
if ad:
if ad.action:
print(obj.name, 'uses', ad.action.name)
for t in ad.nla_tracks:
for s in t.strips:
print(obj.name, 'uses', s.action.name)
return {'FINISHED'}
class ActionListOperator(Operator):
"""Tooltip"""
bl_idname = "object.actionlist_operator"
bl_label = "Action List Object Operator"
@classmethod
def poll(cls, context):
return context.active_object is not None
def execute(self, context):
print("action list")
# bpy.data.actions
for action in bpy.data.actions:
print(action)
# print(dir(action))
for slot in action.slots:
# print(slot)
print(slot.name_display)
# print(dir(slot))
# print(slot)
# obj = context.active_object
# ad = context.active_object.animation_data
# if ad:
# if ad.action:
# print(obj.name, 'uses', ad.action.name)
# for t in ad.nla_tracks:
# for s in t.strips:
# print(obj.name, 'uses', s.action.name)
return {'FINISHED'}
class ActionFrameOperator(Operator):
"""action frame test print"""
bl_idname = "object.actionframe_operator"
bl_label = "Action Frame Object Operator x"
@classmethod
def poll(cls, context):
return context.active_object is not None
def execute(self, context):
bpy.ops.object.mode_set(mode='POSE')
print("action frame", context.scene.frame_current)
# time.sleep(1) # Pauses execution for 1 second
start_frame = 0
end_frame = 0
obj = context.active_object
# print("obj")
# print(dir(obj))
obj_pose = obj.pose #object > pose
print("obj_pose: ")
print(dir(obj_pose))
ad = context.active_object.animation_data
if ad:# animation data
if ad.action: # check if action exist
print(obj.name, 'uses', ad.action.name)# action name
# print("0:", dir(ad.action)) # display action functions
# print("frame_start:", ad.action.frame_start) # none need to set mannual
# print("frame_end:", ad.action.frame_end) # none need to set mannual
# print("frame_range:", ad.action.frame_range) # show the ranage from start and end base on key set
# print("frame_range", dir(ad.action.frame_range))
print("frame_range[0]:", ad.action.frame_range[0])
print("frame_range[1]:", ad.action.frame_range[1])
start_frame = ad.action.frame_range[0]
end_frame = ad.action.frame_range[1]
# for t in ad.nla_tracks:
# for s in t.strips:
# print(obj.name, 'uses', s.action.name)
# print("0:", dir(s.action))
# bpy.ops.object.mode_set(mode='OBJECT')
# print(dir(obj))
armature = obj.data #armture
# print("ARMATURE")
# print(dir(armature))
# animation data...
# print(dir(bpy.context.object.data))
for pose_bone in bpy.context.object.pose.bones: #current select armature
# print(pose_bone)
print(pose_bone.name)
# print(dir(pose_bone))
print("bone.matrix:", pose_bone.matrix)
print("bone.matrix.translation:", pose_bone.matrix.translation)
# print("bone.location:", pose_bone.location)
# print("bone.head:", pose_bone.head)
# print("bone.tail:", pose_bone.tail)
# print("pose_bone")
# for pose_bone in bpy.context.object.pose.bones: #current select armature
# print(pose_bone)
# print(dir(pose_bone))
if start_frame != 0 and end_frame != 0:
print("START FRAME TEST")
frames = range(int(start_frame), int(end_frame) + 1, 1)
# context.scene.frame_current
print(frames)
for f in frames:
# print(f)
# context.scene.frame_current = f
bpy.context.scene.frame_set(f)
# time.sleep(1)
# for bone in armature.edit_bones: # only edit mode
# print("bone Name:")
# print(dir(bone))
# move its head and tail
# print("bone.head:", bone.head)
# print("bone.tail:", bone.tail)
# bone.head.x += 1.5
# bone.tail.x += 1.5
# for bone in armature.bones: # only pose mode
# # print(dir(bone))
# print(bone.matrix['ARMATURESPACE'])
# print("bone.head:", bone.head)
# print("bone.tail:", bone.tail)
for pose_bone in bpy.context.object.pose.bones: #current select armature
print(pose_bone.name)
# print(pose_bone)
# print(dir(pose_bone))
# print("bone.matrix:", pose_bone.matrix)
print("bone.matrix.translation:", pose_bone.matrix.translation)
# print("bone.location:", pose_bone.location)
# print("bone.head:", pose_bone.head)
# print("bone.tail:", pose_bone.tail)
# pos = pose_bone.matrix['ARMATURESPACE'].translationPart()
# print("bone.translationPart:", pos)
pass
return {'FINISHED'}
class PANEL_PT_TestCustom(Panel):
bl_label = "Test Custom Panel"
bl_idname = "OBJECT_PT_TestCustom"
bl_space_type = "VIEW_3D"
bl_region_type = "UI"
bl_category = "TestCustom"
def draw(self, context):
layout = self.layout
layout.label(text="Test Custom UI in 3D Viewport")
layout.prop(context.scene, "frame_current", text="Current Frame")
layout.operator(SimpleOperator.bl_idname, text=SimpleOperator.bl_label)
layout.operator(ActionsOperator.bl_idname, text=ActionsOperator.bl_label)
layout.operator(ActionListOperator.bl_idname, text=ActionListOperator.bl_label)
layout.operator(ActionFrameOperator.bl_idname, text=ActionFrameOperator.bl_label)
# List of classes for registration
classes = [
SimpleOperator,
ActionsOperator,
ActionListOperator,
ActionFrameOperator,
PANEL_PT_TestCustom
]
def unregister():
print("Unregistering classes...")
for cls in reversed(classes):
try:
# Attempt to unregister regardless of hasattr check to force cleanup
bpy.utils.unregister_class(cls)
print(f"Unregistered {cls.__name__}")
except Exception as e:
print(f"Failed to unregister {cls.__name__}: {e}")
def register():
print("Registering classes...")
for cls in classes:
try:
# Force unregister before registering to handle any stale registrations
try:
bpy.utils.unregister_class(cls)
print(f"Force unregistered {cls.__name__} before registering")
except Exception:
pass # Ignore if not registered
bpy.utils.register_class(cls)
print(f"Registered {cls.__name__}")
except Exception as e:
print(f"Failed to register {cls.__name__}: {e}")
if __name__ == "__main__":
print("Executing script...")
unregister()
register()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment