First - if center were an MPoint*, yes, that would create problems, because
the only way you can deal with pointers in the python api is generally to
use MScriptUtil, and it doesn't have support for anything but basic pointer
types (int*, float*, etc).

However - the docs for MItMeshPolygon.center say it returns an MPoint, not
an MPoint*, so that's not your problem.  The issue here is that faceCenter
is a list, and you're trying to add to it with "+=".  This will only work
if the thing on the right side of the += is a list or some other sort of
iterable - what you have is an MPoint.  You get into the infinite loops
because of an unfortunate feature of the maya-python api v1, which makes
python think that objects like MPoints can be used as iterables, when they
really can't.**

The best solution is something along the lines of what you've already done,
which is to "manually" extract each member of the MPoint, and and them into
your faceCenter list yourself.  (Unless what you really want to build up is
a list of MPoints, in which case you can just use faceCenter.append).

- Paul

**For the details of why this is - python assumes that any class which
implements __getitem__ can be treated as an iterable. Unfortunately, in
order for this to work, the class needs to raise an IndexError once you try
to get an item out of bounds.  MPoint, and most other maya api classes,
however, will happily let you index myPoint[327], even though this is
nonsense (and potentially unsafe).  Also, note that even if this did work,
it may not be what you would want, since MPoints have four members - the
last is a "w" coordinate, which is generally just to make internal
translation math easier, and is nearly always 1.


On Wed, May 28, 2014 at 7:28 AM, Nico Klaassen <[email protected]>wrote:

> import maya.OpenMaya as OM
> import math
>
> def faceCenter():
>     faceCenter = []
>
>     selection = OM.MSelectionList()
>     OM.MGlobal.getActiveSelectionList(selection)
>     print ("Number of objects in selection: %s " % selection.length())
>
>     iter = OM.MItSelectionList(selection, OM.MFn.kMeshPolygonComponent)
>
>     while not iter.isDone():
>         dagPath = OM.MDagPath()
>         component = OM.MObject()
>
>         iter.getDagPath(dagPath, component)
>
>         polyIter = OM.MItMeshPolygon(dagPath, component)
>
>         while not polyIter.isDone():
>             center = OM.MPoint
>             center = polyIter.center(OM.MSpace.kWorld)
>
>             #method A. (works fine)
>             point = [center.x, center.y, center.z]
>             faceCenter += point # <<< works fine
>
>             #method B. (makes it run into an infinite loop / memory leak)
>             #faceCenter += center
>
>             polyIter.next()
>
>         iter.next()
>
>     return faceCenter
>
> Hi there! First post!
>
> Based upon some code that I've found (in this group) about looping over
> faces and getting their centers, I ran into the following problem.
> I've highlighted 2 methods in the polyIter section where I try to store
> the center as an MPoint.
>
> center is already an MPoint*, but apparently I'm not allowed to store the
> point directly into a list. Anybody who can point out why this is? Because
> it's an MPoint reference?
>
> --
> 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 [email protected].
> To view this discussion on the web visit
> https://groups.google.com/d/msgid/python_inside_maya/a8d10aae-31a4-4134-b4c7-e28c593d205d%40googlegroups.com<https://groups.google.com/d/msgid/python_inside_maya/a8d10aae-31a4-4134-b4c7-e28c593d205d%40googlegroups.com?utm_medium=email&utm_source=footer>
> .
> For more options, visit https://groups.google.com/d/optout.
>

-- 
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 [email protected].
To view this discussion on the web visit 
https://groups.google.com/d/msgid/python_inside_maya/CAAssL7YwwzoOZBLztuKsS7A0vOSh4sG3%2B6pkTNZuoDLtk63qcg%40mail.gmail.com.
For more options, visit https://groups.google.com/d/optout.

Reply via email to