If I understand you correctly:

   1. You’ve got a dictionary of worldMatrices associated to names of
   controls
   2. They are unordered, e.g. could be pinky and then shoulder.
   3. You’d like these assigned to their associated control

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 cmdsfrom maya.api import OpenMaya as omimport 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)

[image: applymatrices]


On Sun, 14 Feb 2021 at 22:50, Eric Bates <backwheelba...@gmail.com> wrote:

> Hi all,
>
> Really great group you guys have here! Some awesome stuff :)
>
> I'm looking for some advice about a posing script I'm working on. Thanks
> for any ideas!
>
> I have world space matrices that represent control positions on a rig,
> lots of them. My goal is to snap all of the rig controls to these points.
>
> Unfortunately, since I don't know the evaluation order ahead of time, when
> the positions are aligned out of order, things get ugly!
>
> This terrifying pose should look like a hand, the locators above show the
> "correct" points.
>
> [image: Screenshot 2021-02-14 234510.png]
> I was thinking that if there was a way to lookup the evaluation order of
> nodes, that took into account hierarchy, constraints, etc, it would solve
> the problem. But so far, I havent had much luck.
>
> Of if there is another approach out there, Id love to hear.
>
> Thanks for your thoughts everyone!
>
> Eric
>
> --
> 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_maya+unsubscr...@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
> <https://groups.google.com/d/msgid/python_inside_maya/b8af2735-f81a-4991-af84-8b12448b06d8n%40googlegroups.com?utm_medium=email&utm_source=footer>
> .
>

-- 
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_maya+unsubscr...@googlegroups.com.
To view this discussion on the web visit 
https://groups.google.com/d/msgid/python_inside_maya/CAFRtmOCY7y0HPxHafUppAcSGdUohanHVc%3DXSuieUu_E%2B_Xti_Q%40mail.gmail.com.

Reply via email to