<?xml version="1.0" encoding="UTF-8"?>
<rss xmlns:content="http://purl.org/rss/1.0/modules/content/" xmlns:dc="http://purl.org/dc/elements/1.1/" xmlns:rdf="http://www.w3.org/1999/02/22-rdf-syntax-ns#" xmlns:taxo="http://purl.org/rss/1.0/modules/taxonomy/" version="2.0">
  <channel>
    <title>topic Re: MFnMesh.getFloatPoints() inconsistent return values? in Maya Programming Forum</title>
    <link>https://forums.autodesk.com/t5/maya-programming-forum/mfnmesh-getfloatpoints-inconsistent-return-values/m-p/8016797#M9843</link>
    <description>&lt;P&gt;Hi&amp;nbsp;@Anonymous&lt;/P&gt;&lt;P&gt;Thanks for the in-depth explanation, and reference to the relevant documentation that illustrates the crux of the issue.&lt;/P&gt;&lt;P&gt;I sort of figured something was happening with referencing behind-the-scenes as a performance optimization when dealing with these types.&amp;nbsp; I suppose it's may not be possible for operations like&lt;/P&gt;&lt;PRE&gt;x = sphereMeshFn.getFloatPoints()[10]&lt;/PRE&gt;&lt;P&gt;&amp;nbsp;to realize that a copy should be made of that reference, without having to explicitly make a copy using the MFloatPoint constructor.&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
    <pubDate>Tue, 22 May 2018 01:31:35 GMT</pubDate>
    <dc:creator>Anonymous</dc:creator>
    <dc:date>2018-05-22T01:31:35Z</dc:date>
    <item>
      <title>MFnMesh.getFloatPoints() inconsistent return values?</title>
      <link>https://forums.autodesk.com/t5/maya-programming-forum/mfnmesh-getfloatpoints-inconsistent-return-values/m-p/7988055#M9838</link>
      <description>&lt;DIV&gt;So, I'm seeing some very odd behavior in Maya 2017&lt;/DIV&gt;&lt;DIV&gt;(Not sure if this affects other versions, but this is the version I'm testing with)&lt;/DIV&gt;&lt;DIV&gt;&amp;nbsp;&lt;/DIV&gt;&lt;DIV&gt;&lt;FONT face="courier new, monospace"&gt;import maya.api.OpenMaya as om&lt;BR /&gt;&lt;/FONT&gt;&lt;/DIV&gt;&lt;DIV&gt;&lt;FONT face="courier new, monospace"&gt;import maya.cmds as cmds&lt;/FONT&gt;&lt;/DIV&gt;&lt;DIV&gt;&lt;FONT face="courier new, monospace"&gt;sphere = cmds.polySphere(name="sphere")&lt;/FONT&gt;&lt;/DIV&gt;&lt;DIV&gt;&lt;FONT face="courier new, monospace"&gt;compare = cmds.polySphere(name="compare")&lt;/FONT&gt;&lt;/DIV&gt;&lt;DIV&gt;&lt;FONT face="courier new, monospace"&gt;cmds.xform("{}.vtx[10]".format(sphere[0]), translation=(1,1,1))&lt;/FONT&gt;&lt;/DIV&gt;&lt;DIV&gt;&lt;FONT face="courier new, monospace"&gt;&amp;nbsp;&lt;/FONT&gt;&lt;/DIV&gt;&lt;DIV&gt;&lt;FONT face="courier new, monospace"&gt;selectionList = om.MSelectionList()&lt;/FONT&gt;&lt;/DIV&gt;&lt;DIV&gt;&lt;FONT face="courier new, monospace"&gt;selectionList.add(sphere[0])&lt;/FONT&gt;&lt;/DIV&gt;&lt;DIV&gt;&lt;FONT face="courier new, monospace"&gt;selectionList.add(compare[0])&lt;/FONT&gt;&lt;/DIV&gt;&lt;DIV&gt;&lt;FONT face="courier new, monospace"&gt;sphereDagPath = selectionList.getDagPath(0)&lt;/FONT&gt;&lt;/DIV&gt;&lt;DIV&gt;&lt;FONT face="courier new, monospace"&gt;compareDagPath = selectionList.getDagPath(1)&lt;/FONT&gt;&lt;/DIV&gt;&lt;DIV&gt;&lt;FONT face="courier new, monospace"&gt;sphereMeshFn = om.MFnMesh(sphereDagPath)&lt;/FONT&gt;&lt;/DIV&gt;&lt;DIV&gt;&lt;FONT face="courier new, monospace"&gt;compareMeshFn = om.MFnMesh(compareDagPath)&lt;/FONT&gt;&lt;/DIV&gt;&lt;DIV&gt;&lt;FONT face="courier new, monospace"&gt;sphereMeshFn.getFloatPoints()[10].distanceTo(compareMeshFn.getFloatPoints()[10])&lt;/FONT&gt;&lt;/DIV&gt;&lt;DIV&gt;&amp;nbsp;&lt;/DIV&gt;&lt;DIV&gt;So, if you enter all the above in the Script Editor, and execute the last line, you'll sometimes get the correct answer:&amp;nbsp;2.485206365585327, and other times 0.0&lt;/DIV&gt;&lt;DIV&gt;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.&lt;/DIV&gt;&lt;DIV&gt;&amp;nbsp;&lt;/DIV&gt;&lt;DIV&gt;If you select just the&amp;nbsp;&amp;nbsp;&lt;SPAN&gt;sphereMeshFn.getFloatPoints()[10]&lt;/SPAN&gt;part, and press-and-hold CTRL-ENTER, the vertex position will not change.&amp;nbsp; Similarly, with the&amp;nbsp;&lt;SPAN&gt;compareMeshFn.getFloatPoints()[10])&lt;/SPAN&gt;part&amp;nbsp;&lt;/DIV&gt;&lt;DIV&gt;&amp;nbsp;&lt;/DIV&gt;&lt;DIV&gt;If we enter these lines&lt;/DIV&gt;&lt;DIV&gt;&lt;DIV&gt;&lt;FONT face="courier new, monospace"&gt;x = sphereMeshFn.getFloatPoints()[10]&lt;/FONT&gt;&lt;/DIV&gt;&lt;DIV&gt;&lt;FONT face="courier new, monospace"&gt;y = compareMeshFn.getFloatPoints()[10]&lt;/FONT&gt;&lt;/DIV&gt;&lt;/DIV&gt;&lt;DIV&gt;&amp;nbsp;&lt;/DIV&gt;&lt;DIV&gt;Sometimes, the value of&lt;SPAN&gt;&amp;nbsp;&lt;/SPAN&gt;&lt;FONT face="courier new, monospace"&gt;x&lt;/FONT&gt;&lt;SPAN&gt;&amp;nbsp;&lt;/SPAN&gt;will change when&lt;SPAN&gt;&amp;nbsp;&lt;/SPAN&gt;&lt;FONT face="courier new, monospace"&gt;y&lt;/FONT&gt;&lt;SPAN&gt;&amp;nbsp;&lt;/SPAN&gt;is assigned.&lt;/DIV&gt;&lt;DIV&gt;&amp;nbsp;&lt;/DIV&gt;&lt;DIV&gt;I really can't comprehend this particular behavior, and it seems like a bug to me.&lt;/DIV&gt;&lt;DIV&gt;&amp;nbsp;&lt;/DIV&gt;&lt;DIV&gt;I also posted this here:&amp;nbsp;&lt;A href="https://groups.google.com/forum/#!topic/python_inside_maya/27-qBEWCvEI" target="_blank"&gt;https://groups.google.com/forum/#!topic/python_inside_maya/27-qBEWCvEI&lt;/A&gt;&lt;/DIV&gt;</description>
      <pubDate>Tue, 08 May 2018 18:00:32 GMT</pubDate>
      <guid>https://forums.autodesk.com/t5/maya-programming-forum/mfnmesh-getfloatpoints-inconsistent-return-values/m-p/7988055#M9838</guid>
      <dc:creator>Anonymous</dc:creator>
      <dc:date>2018-05-08T18:00:32Z</dc:date>
    </item>
    <item>
      <title>Re: MFnMesh.getFloatPoints() inconsistent return values?</title>
      <link>https://forums.autodesk.com/t5/maya-programming-forum/mfnmesh-getfloatpoints-inconsistent-return-values/m-p/7992111#M9839</link>
      <description>&lt;P&gt;Hi,&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;I think it is a bug and logged in our system on your behalf. I've sent you defect id via email earlier. Please check it out.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;Yours,&lt;/P&gt;
&lt;P&gt;Li&lt;/P&gt;</description>
      <pubDate>Thu, 10 May 2018 07:35:26 GMT</pubDate>
      <guid>https://forums.autodesk.com/t5/maya-programming-forum/mfnmesh-getfloatpoints-inconsistent-return-values/m-p/7992111#M9839</guid>
      <dc:creator>cheng_xi_li</dc:creator>
      <dc:date>2018-05-10T07:35:26Z</dc:date>
    </item>
    <item>
      <title>Re: MFnMesh.getFloatPoints() inconsistent return values?</title>
      <link>https://forums.autodesk.com/t5/maya-programming-forum/mfnmesh-getfloatpoints-inconsistent-return-values/m-p/7994024#M9840</link>
      <description>&lt;P&gt;Cheng Xi Li,&lt;/P&gt;&lt;P&gt;Thank you for the prompt response.&amp;nbsp; I've replied to the e-mail with the defect id with additional information.&lt;/P&gt;&lt;P&gt;Regards,&lt;/P&gt;&lt;P&gt;Nate&lt;/P&gt;</description>
      <pubDate>Thu, 10 May 2018 21:37:06 GMT</pubDate>
      <guid>https://forums.autodesk.com/t5/maya-programming-forum/mfnmesh-getfloatpoints-inconsistent-return-values/m-p/7994024#M9840</guid>
      <dc:creator>Anonymous</dc:creator>
      <dc:date>2018-05-10T21:37:06Z</dc:date>
    </item>
    <item>
      <title>Re: MFnMesh.getFloatPoints() inconsistent return values?</title>
      <link>https://forums.autodesk.com/t5/maya-programming-forum/mfnmesh-getfloatpoints-inconsistent-return-values/m-p/7998757#M9841</link>
      <description>&lt;P&gt;It's part of the documented behaviour of M*Array types in the Maya Python API. A careful reading of &lt;A href="http://help.autodesk.com/view/MAYAUL/2016/ENU/?guid=__py_ref__m_array_html" target="_blank"&gt;this page&lt;/A&gt; should help you out. In particular:&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;"Maya's array types do not provide reference counting for individual elements. This means that a Python script which holds a reference to an array element may suddenly find that reference invalid if the corresponding array element is destroyed."&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;When you do this:&lt;/P&gt;&lt;PRE&gt;x = sphereMeshFn.getFloatPoints()[10]&lt;/PRE&gt;&lt;P&gt;The following happens:&lt;/P&gt;&lt;OL&gt;&lt;LI&gt;getFloatPoints() returns an MFloatPointArray.&lt;/LI&gt;&lt;LI&gt;x is assigned a reference to element 10 of the returned array.&lt;/LI&gt;&lt;LI&gt;Since there are no longer any references to the MFloatPointArray itself (as opposed to its elements) Python destroys it.&lt;/LI&gt;&lt;LI&gt;x is now left with a reference to a destroyed object.&lt;/LI&gt;&lt;/OL&gt;&lt;P&gt;To resolve the problem you need to hold a reference to the entire MFloatPointArray so long as you hold references to any of its elements. E.g:&lt;/P&gt;&lt;PRE&gt;spherePoints = sphereMeshFn.getFloatPoints()
comparePoints = compareMeshFn.getFloatPoints()

x = spherePoints[10];
y = comparePoints[10]
dist = y.distanceTo(x)

x = None

// It's now safe to destroy spherePoints, but we must hang onto comparePoints&lt;BR /&gt;// until the reference held by y is destroyed.
spherePoints = None&lt;/PRE&gt;&lt;P&gt;The document I linked to above explains why it works this way. As is the case with most such oddities, the answer is "performance".&lt;/P&gt;&lt;P&gt;An alternative approach is to copy the value of the array element so that you're not left holding a reference to it. E.g:&lt;/P&gt;&lt;PRE&gt;x = om.MFloatPoint(sphereMeshFn.getPoints()[10])
y = om.MFloatPoint(compareMeshFn.getPoints()[10])&lt;/PRE&gt;</description>
      <pubDate>Sun, 13 May 2018 22:57:35 GMT</pubDate>
      <guid>https://forums.autodesk.com/t5/maya-programming-forum/mfnmesh-getfloatpoints-inconsistent-return-values/m-p/7998757#M9841</guid>
      <dc:creator>Anonymous</dc:creator>
      <dc:date>2018-05-13T22:57:35Z</dc:date>
    </item>
    <item>
      <title>Re: MFnMesh.getFloatPoints() inconsistent return values?</title>
      <link>https://forums.autodesk.com/t5/maya-programming-forum/mfnmesh-getfloatpoints-inconsistent-return-values/m-p/7998858#M9842</link>
      <description>&lt;P&gt;Hi,&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;Yes, you are right. I totally overlooked referencing mechanism. Thanks for pointing out.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;Yours,&lt;/P&gt;
&lt;P&gt;Li&lt;/P&gt;</description>
      <pubDate>Mon, 14 May 2018 01:22:14 GMT</pubDate>
      <guid>https://forums.autodesk.com/t5/maya-programming-forum/mfnmesh-getfloatpoints-inconsistent-return-values/m-p/7998858#M9842</guid>
      <dc:creator>cheng_xi_li</dc:creator>
      <dc:date>2018-05-14T01:22:14Z</dc:date>
    </item>
    <item>
      <title>Re: MFnMesh.getFloatPoints() inconsistent return values?</title>
      <link>https://forums.autodesk.com/t5/maya-programming-forum/mfnmesh-getfloatpoints-inconsistent-return-values/m-p/8016797#M9843</link>
      <description>&lt;P&gt;Hi&amp;nbsp;@Anonymous&lt;/P&gt;&lt;P&gt;Thanks for the in-depth explanation, and reference to the relevant documentation that illustrates the crux of the issue.&lt;/P&gt;&lt;P&gt;I sort of figured something was happening with referencing behind-the-scenes as a performance optimization when dealing with these types.&amp;nbsp; I suppose it's may not be possible for operations like&lt;/P&gt;&lt;PRE&gt;x = sphereMeshFn.getFloatPoints()[10]&lt;/PRE&gt;&lt;P&gt;&amp;nbsp;to realize that a copy should be made of that reference, without having to explicitly make a copy using the MFloatPoint constructor.&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Tue, 22 May 2018 01:31:35 GMT</pubDate>
      <guid>https://forums.autodesk.com/t5/maya-programming-forum/mfnmesh-getfloatpoints-inconsistent-return-values/m-p/8016797#M9843</guid>
      <dc:creator>Anonymous</dc:creator>
      <dc:date>2018-05-22T01:31:35Z</dc:date>
    </item>
  </channel>
</rss>

