forked from johnzero7/XNALaraMesh
-
Notifications
You must be signed in to change notification settings - Fork 0
/
export_xnalara_pose.py
179 lines (128 loc) · 4.52 KB
/
export_xnalara_pose.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
# <pep8 compliant>
from math import degrees
import os
import re
from . import write_ascii_xps
from . import xps_types
from .timing import timing
import bpy
from mathutils import Vector
def getOutputPoseSequence(filename):
filepath, file = os.path.split(filename)
basename, ext = os.path.splitext(file)
poseSuffix = re.sub('\d+$', '', basename)
startFrame = bpy.context.scene.frame_start
endFrame = bpy.context.scene.frame_end
initialFrame = bpy.context.scene.frame_current
for currFrame in range(startFrame, endFrame + 1):
bpy.context.scene.frame_set(currFrame)
numSuffix = '{:0>3d}'.format(currFrame)
name = poseSuffix + numSuffix + ext
newPoseFilename = os.path.join(filepath, name)
getOutputFilename(newPoseFilename)
bpy.context.scene.frame_current = initialFrame
def getOutputFilename(filename):
blenderExportSetup()
xpsExport(filename)
blenderExportFinalize()
def blenderExportSetup():
pass
def blenderExportFinalize():
pass
def saveXpsFile(filename, xpsPoseData):
# dirpath, file = os.path.split(filename)
# basename, ext = os.path.splitext(file)
write_ascii_xps.writeXpsPose(filename, xpsPoseData)
@timing
def xpsExport(filename):
global rootDir
global xpsData
print("------------------------------------------------------------")
print("---------------EXECUTING XPS PYTHON EXPORTER----------------")
print("------------------------------------------------------------")
print("Exporting Pose: ", filename)
rootDir, file = os.path.split(filename)
print('rootDir: {}'.format(rootDir))
xpsPoseData = exportPose()
saveXpsFile(filename, xpsPoseData)
def exportPose():
armature = next((obj for obj in bpy.context.selected_objects
if obj.type == 'ARMATURE'), None)
boneCount = len(armature.data.bones)
print('Exporting Pose', str(boneCount), 'bones')
return xpsPoseData(armature)
def xpsPoseData(armature):
context = bpy.context
currentMode = bpy.context.mode
currentObj = bpy.context.active_object
context.view_layer.objects.active = armature
bpy.ops.object.mode_set(mode='OBJECT', toggle=False)
bpy.ops.object.mode_set(mode='POSE')
bpy.ops.pose.select_all(action='DESELECT')
bones = armature.pose.bones
objectMatrix = armature.matrix_world
xpsPoseData = {}
for poseBone in bones:
boneName = poseBone.name
boneData = xpsPoseBone(poseBone, objectMatrix)
xpsPoseData[boneName] = boneData
bpy.ops.object.posemode_toggle()
context.view_layer.objects.active = currentObj
bpy.ops.object.mode_set(mode=currentMode)
return xpsPoseData
def xpsPoseBone(poseBone, objectMatrix):
boneName = poseBone.name
boneRotDelta = xpsBoneRotate(poseBone)
boneCoordDelta = xpsBoneTranslate(poseBone, objectMatrix)
boneScale = xpsBoneScale(poseBone)
boneData = xps_types.XpsBonePose(boneName, boneCoordDelta, boneRotDelta,
boneScale)
return boneData
def eulerToXpsBoneRot(rotEuler):
xDeg = degrees(rotEuler.x)
yDeg = degrees(rotEuler.y)
zDeg = degrees(rotEuler.z)
return Vector((xDeg, yDeg, zDeg))
def vectorTransform(vec):
x = vec.x
y = vec.y
z = vec.z
y = -y
newVec = Vector((x, z, y))
return newVec
def vectorTransformTranslate(vec):
x = vec.x
y = vec.y
z = vec.z
y = -y
newVec = Vector((x, z, y))
return newVec
def vectorTransformScale(vec):
x = vec.x
y = vec.y
z = vec.z
newVec = Vector((x, y, z))
return newVec
def xpsBoneRotate(poseBone):
# LOCAL PoseBone
poseMatGlobal = poseBone.matrix_basis.to_quaternion()
# LOCAL EditBoneRot
editMatLocal = poseBone.bone.matrix_local.to_quaternion()
rotQuat = editMatLocal @ poseMatGlobal @ editMatLocal.inverted()
rotEuler = rotQuat.to_euler('YXZ')
xpsRot = eulerToXpsBoneRot(rotEuler)
rot = vectorTransform(xpsRot)
return rot
def xpsBoneTranslate(poseBone, objectMatrix):
translate = poseBone.location
# LOCAL EditBoneRot
editMatLocal = poseBone.bone.matrix_local.to_quaternion()
vector = editMatLocal @ translate
return vectorTransformTranslate(objectMatrix.to_3x3() @ vector)
def xpsBoneScale(poseBone):
scale = poseBone.scale
return vectorTransformScale(scale)
if __name__ == "__main__":
writePosefilename0 = (r"G:\3DModeling\XNALara\XNALara_XPS\dataTest\Models"
r"\Queen's Blade\echidna pose - copy.pose")
getOutputFilename(writePosefilename0)