從Blender裡萃取模組資料(5)
前言
續前篇從Blender裡萃取模組資料(4),這次會說明Skeleton的transform如何取得,在此做個紀錄。內容
在上一篇從Blender裡萃取模組資料(4)中提到了Hierarchy的取得,但Skeleton的Bone還必須包含"Position"與"Rotation"的資訊,這次就坐明這兩種資訊如何取得。先來看看只考慮"Position"的部分如何取得,範例如下
import bpy
def ShowBonePosition(bone):
if bone.parent != None:
boneWorldPos = bone.head_local
parentBoneWorldPos = bone.parent.head_local
print(' Bone pos:',boneWorldPos - parentBoneWorldPos,end='')
else:
print(' Bone pos:',bone.head_local,end='')
def ShowBoneInfo(bone,levelCon=0):
for i in range(levelCon):
print(" ")
#
print('Bone name:',bone.name,end='')
ShowBonePosition(bone)
print('')
#
for childBone in bone.children:
ShowBoneInfo(childBone ,levelCon+1)
tagObj = bpy.data.objects["metarig"]
if type(tagObj.data) == bpy.types.Armature:
armatureData = tagObj.data
rootBoneList = []
for bone in armatureData.bones:
if bone.parent == None:
rootBoneList.append(bone)
#
for rootBone in rootBoneList:
ShowBoneInfo(rootBone)
搭配前一篇的範例,這次新增"ShowBonePosition()",取得的方法並不困難,Bone的"head_local"代表的是世界座標,所以在有Parent的狀況時,要將它轉成本地座標。
接著來看看"Position"與"Rotation"如何取得,範例如下
import bpy
def ShowBonePostionAndRotation(bone):
quat = bone.matrix.to_quaternion()
if bone.parent != None:
boneWorldPos = bone.head_local
parentBoneWorldPos = bone.parent.head_local
localPos=boneWorldPos - parentBoneWorldPos
parentRot = bone.parent.matrix_local.to_3x3().to_quaternion()
invParentRot = parentRot.inverted()
print('Bone pos:',invParentQuat.to_matrix() * localPos,end='')
else:
print(' Bone pos:',bone.head_local,end='')
print(' Bone rot:',quat,end='')
def ShowBoneInfo(bone,levelCon=0):
for i in range(levelCon):
print(" ")
#
print('Bone name:',bone.name,end='')
ShowBonePostionAndRotation(bone)
print('')
#
for childBone in bone.children:
ShowBoneInfo(childBone ,levelCon+1)
tagObj = bpy.data.objects["metarig"]
if type(tagObj.data) == bpy.types.Armature:
armatureData = tagObj.data
rootBoneList = []
for bone in armatureData.bones:
if bone.parent == None:
rootBoneList.append(bone)
#
for rootBone in rootBoneList:
ShowBoneInfo(rootBone)
這次的做法很不一樣,理由很簡單,如果只是單單需要知道Bone的旋轉,利用Bone的matrix直接取得就是Local的旋轉,不需要再轉換,但"Position"是會被Parent的旋轉引響的,所以要透過Bone的matrix_local(這個是世界旋轉)的"逆旋轉"來反算"Position"。
參考資料
Blender Documentation Contents相關文章
從Blender裡萃取模組資料(4)從Blender裡萃取模組資料(6)
沒有留言:
張貼留言