Python API

82 views
Skip to first unread message

Nico Klaassen

unread,
May 28, 2014, 10:28:06 AM5/28/14
to python_in...@googlegroups.com
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? 

Paul Molodowitch

unread,
May 28, 2014, 11:57:23 AM5/28/14
to python_inside_maya
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.


--
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/a8d10aae-31a4-4134-b4c7-e28c593d205d%40googlegroups.com.
For more options, visit https://groups.google.com/d/optout.

Nico Klaassen

unread,
May 28, 2014, 2:05:13 PM5/28/14
to python_in...@googlegroups.com
Thank you so much for your reply and the in-depth info! Everything is clear now :)

The reason I just want the MPoints, is because I need to feed the points as start points into the MFnMesh.closestIntersections() method. So no reason to get the points in a list.

Cheers, have a nice day!

Op woensdag 28 mei 2014 17:57:23 UTC+2 schreef elrond79:
To unsubscribe from this group and stop receiving emails from it, send an email to python_inside_maya+unsub...@googlegroups.com.

ha...@haggi.de

unread,
May 28, 2014, 3:42:51 PM5/28/14
to python_in...@googlegroups.com

A nice one!

have a look at these:

def faceCenter():

    faceCenter = []

I suppose it is not really a good idea to name the function exactly like a variable.

haggi

--
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.

Justin Israel

unread,
May 28, 2014, 3:54:59 PM5/28/14
to python_in...@googlegroups.com


On May 29, 2014 7:42 AM, <ha...@haggi.de> wrote:
>
> A nice one!
>
> have a look at these:
>
> def faceCenter():
>
>     faceCenter = []
>
> I suppose it is not really a good idea to name the function exactly like a variable.
>

Unless you don't care about making recursive calls withing the function.

> To view this discussion on the web visit https://groups.google.com/d/msgid/python_inside_maya/4f99ccdc07d5f05571d6617b9867bf90%40haggi.de.

Reply all
Reply to author
Forward
0 new messages