Skip to content

Commit

Permalink
Merge pull request #12 from ynput/bugfix/AY-5757_Maya-JSON-layout-for…
Browse files Browse the repository at this point in the history
…-Unreal-using-local-transform-matrix-instead-of-world-matrix

Maya: JSON layout for Unreal using local transform matrix instead of world matrix
  • Loading branch information
antirotor committed Aug 7, 2024
2 parents 31d8e73 + 9c83b38 commit a0d1933
Showing 1 changed file with 60 additions and 47 deletions.
107 changes: 60 additions & 47 deletions client/ayon_maya/plugins/publish/extract_layout.py
Original file line number Diff line number Diff line change
@@ -1,13 +1,59 @@
import json
import math
import os

from typing import List
from ayon_api import get_representation_by_id
from ayon_maya.api import plugin
from maya import cmds
from maya.api import OpenMaya as om


def convert_matrix_to_4x4_list(
value) -> List[List[float]]:
"""Convert matrix or flat list to 4x4 matrix list
Example:
>>> convert_matrix_to_4x4_list(om.MMatrix())
[[1, 0, 0, 0], [0, 1, 0, 0], [0, 0, 1, 0], [0, 0, 0, 1]]
>>> convert_matrix_to_4x4_list(
... [1, 0, 0, 0,
... 0, 1, 0, 0,
... 0, 0, 1, 0,
... 0, 0, 0, 1]
... )
[[1, 0, 0, 0], [0, 1, 0, 0], [0, 0, 1, 0], [0, 0, 0, 1]]
"""
result = []
value = list(value)
for i in range(0, len(value), 4):
result.append(list(value[i:i + 4]))
return result


def convert_transformation_matrix(transform_mm: om.MMatrix, rotation: list) -> om.MMatrix:
"""Convert matrix to list of transformation matrix for Unreal Engine import.
Args:
transform_mm (om.MMatrix): Local Matrix for the asset
rotation (list): Rotations of the asset
Returns:
List[om.MMatrix]: List of transformation matrix of the asset
"""
convert_transform = om.MTransformationMatrix(transform_mm)

convert_translation = convert_transform.translation(om.MSpace.kWorld)
convert_translation = om.MVector(convert_translation.x, convert_translation.z, convert_translation.y)
convert_scale = convert_transform.scale(om.MSpace.kObject)
convert_transform.setTranslation(convert_translation, om.MSpace.kWorld)
converted_rotation = om.MEulerRotation(
math.radians(rotation[0]), math.radians(rotation[2]), math.radians(rotation[1])
)
convert_transform.setRotation(converted_rotation)
convert_transform.setScale([convert_scale[0], convert_scale[2], convert_scale[1]], om.MSpace.kObject)

return convert_transform.asMatrix()


class ExtractLayout(plugin.MayaExtractorPlugin):
"""Extract a layout."""

Expand Down Expand Up @@ -82,46 +128,13 @@ def process(self, instance):
"version": str(version_id)
}

loc = cmds.xform(asset, query=True, translation=True)
rot = cmds.xform(asset, query=True, rotation=True, euler=True)
scl = cmds.xform(asset, query=True, relative=True, scale=True)

json_element["transform"] = {
"translation": {
"x": loc[0],
"y": loc[1],
"z": loc[2]
},
"rotation": {
"x": math.radians(rot[0]),
"y": math.radians(rot[1]),
"z": math.radians(rot[2])
},
"scale": {
"x": scl[0],
"y": scl[1],
"z": scl[2]
}
}

row_length = 4
t_matrix_list = cmds.xform(asset, query=True, matrix=True)

transform_mm = om.MMatrix(t_matrix_list)
transform = om.MTransformationMatrix(transform_mm)

t = transform.translation(om.MSpace.kWorld)
t = om.MVector(t.x, t.z, -t.y)
transform.setTranslation(t, om.MSpace.kWorld)
transform.rotateBy(
om.MEulerRotation(math.radians(-90), 0, 0), om.MSpace.kWorld)
transform.scaleBy([1.0, 1.0, -1.0], om.MSpace.kObject)
local_matrix = cmds.xform(asset, query=True, matrix=True)
local_rotation = cmds.xform(asset, query=True, rotation=True, euler=True)

t_matrix_list = list(transform.asMatrix())

t_matrix = []
for i in range(0, len(t_matrix_list), row_length):
t_matrix.append(t_matrix_list[i:i + row_length])
matrix = om.MMatrix(local_matrix)
matrix = convert_transformation_matrix(matrix, local_rotation)
t_matrix = convert_matrix_to_4x4_list(matrix)

json_element["transform_matrix"] = [
list(row)
Expand All @@ -131,23 +144,22 @@ def process(self, instance):
basis_list = [
1, 0, 0, 0,
0, 1, 0, 0,
0, 0, -1, 0,
0, 0, 1, 0,
0, 0, 0, 1
]

basis_mm = om.MMatrix(basis_list)
basis = om.MTransformationMatrix(basis_mm)

b_matrix_list = list(basis.asMatrix())
b_matrix = []

for i in range(0, len(b_matrix_list), row_length):
b_matrix.append(b_matrix_list[i:i + row_length])
b_matrix = convert_matrix_to_4x4_list(basis_mm)

json_element["basis"] = []
for row in b_matrix:
json_element["basis"].append(list(row))

json_element["rotation"] = {
"x": local_rotation[0],
"y": local_rotation[1],
"z": local_rotation[2]
}
json_data.append(json_element)

json_filename = "{}.json".format(instance.name)
Expand All @@ -162,6 +174,7 @@ def process(self, instance):
'files': json_filename,
"stagingDir": stagingdir,
}

instance.data["representations"].append(json_representation)

self.log.debug("Extracted instance '%s' to: %s",
Expand Down

0 comments on commit a0d1933

Please sign in to comment.