Skip to content

Commit

Permalink
Fixed marker rotation
Browse files Browse the repository at this point in the history
  • Loading branch information
amorgun committed Nov 23, 2024
1 parent 2bba671 commit a40991f
Show file tree
Hide file tree
Showing 3 changed files with 26 additions and 13 deletions.
2 changes: 1 addition & 1 deletion blender_manifest.toml
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
schema_version = "1.0.0"

id = "blender_dow"
version = "0.14.0-alpha8"
version = "0.14.0-alpha9"
name = "Dawn of War Import/Export"
tagline = "Import and export of Dawn of War models"
maintainer = "amorgun"
Expand Down
15 changes: 10 additions & 5 deletions exporter.py
Original file line number Diff line number Diff line change
Expand Up @@ -856,10 +856,13 @@ def write_marks(self, writer: ChunkWriter):
markers = [b for b in armature.bones if self.is_marker(b)]
if not markers:
return

coord_transform = mathutils.Matrix([[-1, 0, 0], [0, 1, 0], [0, 0, 1]]).to_4x4()
coord_transform_inv = coord_transform.inverted()

with self.start_chunk(writer, ExportFormat.WHM, 'DATAMARK'):
if self.format is ExportFormat.WHM:
writer.write_struct('<l', len(markers))
delta = mathutils.Matrix.Rotation(math.radians(-90.0), 4, 'Z')
for marker in markers:
with self.start_chunk(writer, ExportFormat.SGM, 'DATAMARK', name=marker.name):
if self.format is ExportFormat.WHM:
Expand All @@ -869,11 +872,13 @@ def write_marks(self, writer: ChunkWriter):
parent_mat = self.bone_transforms[marker.parent]
else:
parent_mat = self.armature_obj.matrix_world.inverted() @ mathutils.Matrix.Rotation(math.radians(90.0), 4, 'X')
transform = parent_mat.inverted() @ marker.matrix_local @ delta.inverted()
transform = coord_transform @ parent_mat.inverted() @ marker.matrix_local @ coord_transform_inv
loc, rot, _ = transform.decompose()
rot = rot.to_matrix().transposed()
for row_idx in range(3):
writer.write_struct('<3f', *transform[row_idx][:3])
writer.write_struct('<3f', -transform[0][3], transform[1][3], transform[2][3])
self.bone_transforms[marker] = marker.matrix_local @ delta.inverted()
writer.write_struct('<3f', *rot[row_idx])
writer.write_struct('<3f', *loc)
self.bone_transforms[marker] = marker.matrix_local

def write_cams(self, writer: ChunkWriter):
cameras = [i for i in bpy.data.objects if i.type == 'CAMERA']
Expand Down
22 changes: 15 additions & 7 deletions importer.py
Original file line number Diff line number Diff line change
Expand Up @@ -456,16 +456,23 @@ def CH_DATAMARK(self, reader: ChunkReader):
bpy.ops.object.mode_set(mode='EDIT', toggle=True)
bone_collection = self.armature.collections.new('Markers')

coord_transform = mathutils.Matrix([[-1, 0, 0], [0, 1, 0], [0, 0, 1]]).to_4x4()
coord_transform_inv = coord_transform.inverted()

num_markers = reader.read_one('<l') # -- Read Number Of Markers
for i in range(num_markers): # -- Read All Markers
marker_name = reader.read_str() # -- Read Marker Name
parent_name = reader.read_str() # -- Read Parent Name
transform = mathutils.Matrix().to_3x3()
rot = mathutils.Matrix().to_3x3()
for row_idx in range(3): # -- Read Matrix
transform[row_idx][:3] = reader.read_struct('<3f')
x, y, z = reader.read_struct('<3f')
rot[row_idx][:3] = reader.read_struct('<3f')
pos = reader.read_struct('<3f')

transform = mathutils.Matrix.LocRotScale([-x, y, z], transform, None)
transform = coord_transform_inv @ mathutils.Matrix.LocRotScale(
mathutils.Vector(pos),
rot.transposed(),
None,
) @ coord_transform

marker = self.armature.edit_bones.new(marker_name) # -- Create Bone and Set Name
marker.head = (0, 0, 0)
Expand All @@ -481,11 +488,13 @@ def CH_DATAMARK(self, reader: ChunkReader):

parent = self.armature.edit_bones.get(parent_name)
if parent is None:
if parent_name.strip():
self.messages.append(('WARNING', f'Marker "{marker_name}" is attached to non-existent bone "{parent_name}"'))
parent_mat = mathutils.Matrix.Rotation(math.radians(90.0), 4, 'X')
else:
marker.parent = parent # -- Set Parent Of New Marker
parent_mat = self.bone_transform[parent_name]
marker.matrix = parent_mat @ transform @ mathutils.Matrix.Rotation(math.radians(-90.0), 4, 'Z')
marker.matrix = parent_mat @ transform
self.bone_transform[marker_name] = parent_mat @ transform
bpy.ops.object.mode_set(mode='EDIT', toggle=True)

Expand All @@ -494,8 +503,7 @@ def CH_DATAMARK(self, reader: ChunkReader):
for bone in bone_collection.bones:
pose_bone = self.armature_obj.pose.bones[bone.name]
pose_bone.custom_shape = custom_shape_template
pose_bone.custom_shape_rotation_euler = 0, math.pi / 2, math.pi / 2
pose_bone.custom_shape_scale_xyz = 1, 1, -1
pose_bone.custom_shape_scale_xyz = -1, 1, 1

def CH_DATACAMS(self, reader: ChunkReader):
cameras_collection = bpy.data.collections.new('Cameras')
Expand Down

0 comments on commit a40991f

Please sign in to comment.