Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Maya: JSON layout for Unreal using local transform matrix instead of world matrix #12

Merged
Changes from all commits
Commits
Show all changes
46 commits
Select commit Hold shift + click to select a range
1962336
align with the unreal settings on MayaImportAbc Presets
moonyuet Jul 8, 2024
e5513c6
the original radian number -90 for rotation is correct
moonyuet Jul 8, 2024
ebb4dc9
use default MEulerRotation
moonyuet Jul 8, 2024
162d507
fix the world transform issue
moonyuet Jul 10, 2024
41d002f
Merge branch 'develop' into bugfix/AY-5757_Maya-JSON-layout-for-Unrea…
moonyuet Jul 10, 2024
c8aac7f
make sure the rotation is correct
moonyuet Jul 10, 2024
fe6d36a
fix the scale y alignment
moonyuet Jul 10, 2024
f950bd7
make sure the rotation is correct
moonyuet Jul 11, 2024
d1ae9bf
improve the rotation calculation method
moonyuet Jul 15, 2024
7bb991b
ruff cosmetic fix
moonyuet Jul 15, 2024
2725072
converting the code into UE transforms
moonyuet Jul 16, 2024
783fcc9
use cmds.select instead of cmds.ls
moonyuet Jul 16, 2024
dd30bc1
Simplify setting basis list + fix using example @moonyuet provided
BigRoy Jul 16, 2024
829de26
Do not write JSON file on each iteration
BigRoy Jul 16, 2024
109f01d
Cosmetics
BigRoy Jul 16, 2024
60cca63
Fix access to `json_path`
BigRoy Jul 16, 2024
f6f5854
Move logic closer together
BigRoy Jul 16, 2024
72ab320
Simplify range
BigRoy Jul 16, 2024
1b09793
Simplify getting transform matrix
BigRoy Jul 16, 2024
dc54810
Remove unused context manager
BigRoy Jul 16, 2024
40dd6c6
Add line break to have less changes in PR
BigRoy Jul 16, 2024
784f3ed
Fix conversion for `MMatrix`
BigRoy Jul 16, 2024
855b3b2
Move Maya to UE matrix conversion to dedicated function for clarity
BigRoy Jul 16, 2024
f16ddae
Add docstring
BigRoy Jul 16, 2024
09b5b33
Allow easier access into the matrix
BigRoy Jul 16, 2024
48cdbf1
Implement coordinate system rotation
BigRoy Jul 16, 2024
8ced52b
Fix
BigRoy Jul 16, 2024
47cdda3
Fix error
BigRoy Jul 16, 2024
55e55f3
Implement the last bit of conversion that Unreal Live Link seems to do
BigRoy Jul 16, 2024
6809a48
Add `# noqa`
BigRoy Jul 16, 2024
e5be13a
Make sure the values are radians
BigRoy Jul 16, 2024
58ea550
Change basis to identity
BigRoy Jul 16, 2024
8ecbc75
Update client/ayon_maya/plugins/publish/extract_layout.py
BigRoy Jul 16, 2024
feb159f
revert the changes
moonyuet Jul 16, 2024
60a1602
clean up the code
moonyuet Jul 17, 2024
bcee798
cosmetic
moonyuet Jul 17, 2024
04cf137
make sure the extract layout can follow scanerios purposed by Ondrej
moonyuet Jul 23, 2024
5405eee
add condition check on the rotation
moonyuet Jul 23, 2024
85764bc
big roy comment - code clean up
moonyuet Jul 23, 2024
cba7667
add two more conditions base on discussion with @antirotor
moonyuet Jul 24, 2024
de7db5d
Merge branch 'develop' into bugfix/AY-5757_Maya-JSON-layout-for-Unrea…
moonyuet Jul 30, 2024
e8d9c99
add rotation data as being part of the json_element
moonyuet Aug 2, 2024
33af830
Merge branch 'develop' into bugfix/AY-5757_Maya-JSON-layout-for-Unrea…
moonyuet Aug 2, 2024
3878462
make sure the basis matrix is identity matrix
moonyuet Aug 6, 2024
7d88542
make sure not negative number in z
moonyuet Aug 7, 2024
9c83b38
Merge branch 'develop' into bugfix/AY-5757_Maya-JSON-layout-for-Unrea…
moonyuet Aug 7, 2024
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
moonyuet marked this conversation as resolved.
Show resolved Hide resolved

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