If I understand you correctly:
If so, then what you’re looking for isn’t evaluation order per-se, but hierarchical order. By assigning to shoulder first, you change where pinky is. So what you need to do is assign your worldMatrices in the order they are parented.
from maya import cmds
from maya.api import OpenMaya as om
import math # degrees
# Make something to export and import, you'll already have these
shoulder, _ = cmds.polyCube()
shoulder = cmds.rename(shoulder, "shoulder")
cmds.move(1, 3, 6)
cmds.rotate(15)
pinky, _ = cmds.polyCube()
pinky = cmds.rename(pinky, "pinky")
cmds.parent(pinky, shoulder)
cmds.move(0, 5, 0)
cmds.rotate(45, 20)
# Next, serialise them. You'll have these too
matrices = {
"pinky": cmds.getAttr("pinky.worldMatrix[0]"),
"shoulder": cmds.getAttr("shoulder.worldMatrix[0]"),
}
# Now, anything below will restore the above to their original world matrix transformations
selected_root = cmds.ls(selection=True)[0]
hierarchy = cmds.listRelatives(selected_root, allDescendents=True)
hierarchy += [selected_root] # This one isn't included in the above
# allDescendents returns not only transforms, but shapes too. We don't need those
hierarchy = cmds.ls(hierarchy, type="transform")
# allDescendents returns the order reversed
hierarchy.reverse()
for control in hierarchy:
matrix = matrices.get(control)
# If there's something in the hierarchy not present in your list of matrices, that's fine
if not matrix:
continue
# A world matrix can't be applied directly, because every control is relative their parent
# So we'll convert world to local by multiplying it with the parent inverse matrix
parent_inverse_matrix = cmds.getAttr(control + ".parentInverseMatrix[0]")
# To multiply, we need to call on the API
matrix = om.MMatrix(matrix)
parent_inverse_matrix = om.MMatrix(parent_inverse_matrix)
local_matrix = matrix * parent_inverse_matrix
# Next we need translate/rotate/scale from this matrix
local_matrix = om.MTransformationMatrix(local_matrix)
translate = local_matrix.translation(om.MSpace.kTransform)
rotate = local_matrix.rotation()
scale = local_matrix.scale(om.MSpace.kTransform)
# API rotations are in radians
rotate = [math.degrees(r) for r in rotate]
# Now we're got the local values ready to apply
cmds.setAttr(control + ".translate", *translate)
cmds.setAttr(control + ".rotate", *rotate)
cmds.setAttr(control + ".scale", *scale)
--
You received this message because you are subscribed to the Google Groups "Python Programming for Autodesk Maya" group.
To unsubscribe from this group and stop receiving emails from it, send an email to python_inside_m...@googlegroups.com.
To view this discussion on the web visit https://groups.google.com/d/msgid/python_inside_maya/b8af2735-f81a-4991-af84-8b12448b06d8n%40googlegroups.com.
You received this message because you are subscribed to a topic in the Google Groups "Python Programming for Autodesk Maya" group.
To unsubscribe from this topic, visit https://groups.google.com/d/topic/python_inside_maya/7gqUkM2-IT8/unsubscribe.
To unsubscribe from this group and all its topics, send an email to python_inside_m...@googlegroups.com.
To view this discussion on the web visit https://groups.google.com/d/msgid/python_inside_maya/75933eb6-6bc5-4511-a09d-fe0e84fbfac0n%40googlegroups.com.
For more elegance, have you considered storing the local matrix, instead of the world? Or better yet, store the translate/rotate values? Is it not safe to assume that wherever you export these from will have a similar - if not identical - hierarchy?
@marcus, you will have to share your secret :)
Hehehehe, sure. It’s called MarkdownHere
To view this discussion on the web visit https://groups.google.com/d/msgid/python_inside_maya/b53df90d-ee5c-4c30-9758-cde4f7e3d72cn%40googlegroups.com.
To view this discussion on the web visit https://groups.google.com/d/msgid/python_inside_maya/CAFRtmODK3widYoLJn3W_bDG6nS%3DefDX--5UydB%2BHTvZxq0WF%3DQ%40mail.gmail.com.
Well Im happy to have at least found a solution that is working, will call this done for now, unless anyone has another idea.
The last idea you had of iterating over it multiple times seems fine to me. So long as it doesn’t bother you performance wise, it is very robust. I would probably do something similar. Down the road, you might be able to sort the initial list with the help of cmds.cycleCheck or other evaluation-related tools. Maya is able to generate this dependency hierarchy up-front for you via the Evaluation Toolkit. It is likely you could leverage that to get a near perfect sort here.
To view this discussion on the web visit https://groups.google.com/d/msgid/python_inside_maya/CANaeObKi_eek6NVfec0swKK8FYJKLvC9YEDRBq%2B5sgqwyFv1eg%40mail.gmail.com.