What is Maya API doing here?

108 views
Skip to first unread message

Nate Reid

unread,
May 8, 2018, 1:52:22 PM5/8/18
to Python Programming for Autodesk Maya
So, I'm seeing some very odd behavior in Maya 2017
(Not sure if this affects other versions, but this is the version I'm testing with)

import maya.api.OpenMaya as om
import maya.cmds as cmds
sphere = cmds.polySphere(name="sphere")
compare = cmds.polySphere(name="compare")
cmds.xform("{}.vtx[10]".format(sphere[0]), translation=(1,1,1))

selectionList = om.MSelectionList()
selectionList.add(sphere[0])
selectionList.add(compare[0])
sphereDagPath = selectionList.getDagPath(0)
compareDagPath = selectionList.getDagPath(1)
sphereMeshFn = om.MFnMesh(sphereDagPath)
compareMeshFn = om.MFnMesh(compareDagPath)
sphereMeshFn.getFloatPoints()[10].distanceTo(compareMeshFn.getFloatPoints()[10])

So, if you enter all the above in the Script Editor, and execute the last line, you'll sometimes get the correct answer: 2.485206365585327, and other times 0.0
So, you can test this by selecting the last line in the Script Editor, and press-and-hold CTRL-ENTER and watching the results go by in the Script Editor.

If you select just the  sphereMeshFn.getFloatPoints()[10]part, and press-and-hold CTRL-ENTER, the vertex position will not change.  Similarly, with the compareMeshFn.getFloatPoints()[10])part 

If we enter these lines
x = sphereMeshFn.getFloatPoints()[10]
y = compareMeshFn.getFloatPoints()[10]

Sometimes, the value of x will change when y is assigned.

I really can't comprehend this particular behavior, and it seems like a bug to me.

Justin Israel

unread,
May 8, 2018, 6:44:37 PM5/8/18
to python_in...@googlegroups.com
I think what you are seeing is the temporary reference to the MFloatPointArray being tossed away and making your MFloatPoint reference invalid.
Try saving the array data structure:

sphereMeshPts = sphereMeshFn.getFloatPoints()
compareMeshPts = compareMeshFn.getFloatPoints()
sphereMeshPts[10].distanceTo(compareMeshPts[10])


--
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/292f92da-6f30-4a2b-9206-20eaa106a494%40googlegroups.com.
For more options, visit https://groups.google.com/d/optout.

Nate Reid

unread,
May 10, 2018, 5:00:04 PM5/10/18
to Python Programming for Autodesk Maya
Hi Justin,
Thanks for responding.  Since I also filed this on Autodesk's Maya Programming forum, I've gotten a response:

And they say it's a bug ;)

You're right that if I save the return result from getFloatPoints() into a list, the MFloatPoint instances do not get changed.
However, something is wrong in the way Autodesk has wrapped these methods that is incorrectly throwing away temporaries.
This is especially evident in the example where I assign x, and then assign y, which changes x.  


To unsubscribe from this group and stop receiving emails from it, send an email to python_inside_maya+unsub...@googlegroups.com.

Justin Israel

unread,
May 10, 2018, 5:29:11 PM5/10/18
to python_in...@googlegroups.com
Good information. I agree it seems undesirable that you should have to know to keep around the intermediate container object reference. It only made sense because other parts of the API specifically work that way, such as MScriptUtil.

To unsubscribe from this group and stop receiving emails from it, send an email to python_inside_m...@googlegroups.com.

--
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/a0038e3d-184e-4d0a-aebd-f0b9ddd8f586%40googlegroups.com.

Nate Reid

unread,
May 22, 2018, 2:42:13 PM5/22/18
to Python Programming for Autodesk Maya
Looks like it's actually a documented "feature" of the Python API and how it works with references as you suggested in your initial response:

Thanks for the help,
-Nate
To unsubscribe from this group and stop receiving emails from it, send an email to python_inside_maya+unsub...@googlegroups.com.

--
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+unsub...@googlegroups.com.
Reply all
Reply to author
Forward
0 new messages