Skip to content

Commit

Permalink
Make sure export always uses the rest pose
Browse files Browse the repository at this point in the history
  • Loading branch information
SomaZ committed Aug 31, 2024
1 parent 11fa586 commit 5f7f47c
Showing 1 changed file with 16 additions and 0 deletions.
16 changes: 16 additions & 0 deletions JAG2GLM.py
Original file line number Diff line number Diff line change
Expand Up @@ -991,6 +991,9 @@ def loadFromBlender(self, glm_filepath_rel: str, gla_filepath_rel: str, basepath
"" or gla_filepath_rel == "*default")
skeleton_object: Optional[bpy.types.Object] = None
boneIndexMap: Optional[BoneIndexMap] = None
skeleton_armature : Optional[bpy.types.Armature] = None
# Assume people usually use the "rest pose" before exporting
old_pose = "REST"
if defaultSkeleton:
# no skeleton available, generate default/unit skeleton instead
self.header.numBones = 1
Expand All @@ -1004,17 +1007,22 @@ def loadFromBlender(self, glm_filepath_rel: str, gla_filepath_rel: str, basepath
if obj.type != 'ARMATURE':
return False, ErrorMessage("skeleton_root is no Armature!")
skeleton_armature = downcast(bpy.types.Armature, obj.data)
# Exporting in pose position will lead to incorrect results, make sure to reset to users last position!
old_pose = skeleton_armature.pose_position
skeleton_armature.pose_position = "REST"

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

self.header.numBones = len(boneIndexMap)

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

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

if self.header.numLODs == 0:
if skeleton_armature:
skeleton_armature.pose_position = old_pose
return False, ErrorMessage("Could not find model_root_0 object")

# build hierarchy from first LOD
surfaceIndexMap: Dict[str, int] = {} # surface name -> index
success, message = self.surfaceDataCollection.loadFromBlender(
rootObjects[0], surfaceIndexMap)
if not success:
if skeleton_armature:
skeleton_armature.pose_position = old_pose
return False, message
self.surfaceDataOffsets.calculateOffsets(self.surfaceDataCollection)

Expand All @@ -1047,12 +1059,16 @@ def loadFromBlender(self, glm_filepath_rel: str, gla_filepath_rel: str, basepath
success, message = self.LODCollection.loadFromBlender(
rootObjects, surfaceIndexMap, self.surfaceDataCollection, boneIndexMap, skeleton_object)
if not success:
if skeleton_armature:
skeleton_armature.pose_position = old_pose
return False, message

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

# calculate offsets etc.4
self._calculateHeaderOffsets()
if skeleton_armature:
skeleton_armature.pose_position = old_pose
return True, NoError

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

0 comments on commit 5f7f47c

Please sign in to comment.