Skip to content

Commit 5f7f47c

Browse files
committed
Make sure export always uses the rest pose
Fixes mrwonko#52
1 parent 11fa586 commit 5f7f47c

File tree

1 file changed

+16
-0
lines changed

1 file changed

+16
-0
lines changed

JAG2GLM.py

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -991,6 +991,9 @@ def loadFromBlender(self, glm_filepath_rel: str, gla_filepath_rel: str, basepath
991991
"" or gla_filepath_rel == "*default")
992992
skeleton_object: Optional[bpy.types.Object] = None
993993
boneIndexMap: Optional[BoneIndexMap] = None
994+
skeleton_armature : Optional[bpy.types.Armature] = None
995+
# Assume people usually use the "rest pose" before exporting
996+
old_pose = "REST"
994997
if defaultSkeleton:
995998
# no skeleton available, generate default/unit skeleton instead
996999
self.header.numBones = 1
@@ -1004,17 +1007,22 @@ def loadFromBlender(self, glm_filepath_rel: str, gla_filepath_rel: str, basepath
10041007
if obj.type != 'ARMATURE':
10051008
return False, ErrorMessage("skeleton_root is no Armature!")
10061009
skeleton_armature = downcast(bpy.types.Armature, obj.data)
1010+
# Exporting in pose position will lead to incorrect results, make sure to reset to users last position!
1011+
old_pose = skeleton_armature.pose_position
1012+
skeleton_armature.pose_position = "REST"
10071013

10081014
boneIndexMap, message = buildBoneIndexLookupMap(JAFilesystem.RemoveExtension(
10091015
JAFilesystem.AbsPath(gla_filepath_rel, basepath)) + ".gla")
10101016
if boneIndexMap is None:
1017+
skeleton_armature.pose_position = old_pose
10111018
return False, message
10121019

10131020
self.header.numBones = len(boneIndexMap)
10141021

10151022
# check if skeleton matches the specified one
10161023
for bone in skeleton_armature.bones:
10171024
if bone.name not in boneIndexMap:
1025+
skeleton_armature.pose_position = old_pose
10181026
return False, ErrorMessage(f"skeleton_root does not match specified gla, could not find bone {bone.name}")
10191027

10201028
# load from Blender
@@ -1030,13 +1038,17 @@ def loadFromBlender(self, glm_filepath_rel: str, gla_filepath_rel: str, basepath
10301038
f"Found {self.header.numLODs} model_root objects, i.e. LOD levels")
10311039

10321040
if self.header.numLODs == 0:
1041+
if skeleton_armature:
1042+
skeleton_armature.pose_position = old_pose
10331043
return False, ErrorMessage("Could not find model_root_0 object")
10341044

10351045
# build hierarchy from first LOD
10361046
surfaceIndexMap: Dict[str, int] = {} # surface name -> index
10371047
success, message = self.surfaceDataCollection.loadFromBlender(
10381048
rootObjects[0], surfaceIndexMap)
10391049
if not success:
1050+
if skeleton_armature:
1051+
skeleton_armature.pose_position = old_pose
10401052
return False, message
10411053
self.surfaceDataOffsets.calculateOffsets(self.surfaceDataCollection)
10421054

@@ -1047,12 +1059,16 @@ def loadFromBlender(self, glm_filepath_rel: str, gla_filepath_rel: str, basepath
10471059
success, message = self.LODCollection.loadFromBlender(
10481060
rootObjects, surfaceIndexMap, self.surfaceDataCollection, boneIndexMap, skeleton_object)
10491061
if not success:
1062+
if skeleton_armature:
1063+
skeleton_armature.pose_position = old_pose
10501064
return False, message
10511065

10521066
self.LODCollection.calculateOffsets(self.header.ofsLODs)
10531067

10541068
# calculate offsets etc.4
10551069
self._calculateHeaderOffsets()
1070+
if skeleton_armature:
1071+
skeleton_armature.pose_position = old_pose
10561072
return True, NoError
10571073

10581074
def saveToFile(self, filepath_abs: str) -> Tuple[bool, ErrorMessage]:

0 commit comments

Comments
 (0)