Thanks Justin and Ian,
violationMesh = [ ] was indeed missing. My mistake not copying it over.
Regarding profiling, I'm now ashamed to say that I did not do a line by
line profile because doing so just now I found that looping over and
re-creating "mIntArray = om.MIntArray()" was accounting for 80+% of the
runtime. I switched to creating it first and clearing it in every loop and
got... 80+% time reduction.
With Ian's suggestion I assume runtime will only get better.
Thanks for all the other optimization tips too.
import maya.cmds as cmds
import maya.OpenMaya as om
violationMesh = [ ]
violationPolygons = [ ]
mIt_kMesh = om.MItDependencyNodes( om.MFn.kMesh )
while not mIt_kMesh.isDone():
mObject = mIt_kMesh.thisNode()
dagPath = om.MDagPath.getAPathTo( mObject )
mFnMesh = om.MFnMesh( dagPath )
mObject_numFaces = mFnMesh.numPolygons()
mIntArray = om.MIntArray()
for faceID in range(0, mObject_numFaces ):
mIntArray.clear()
mFnMesh.getPolygonVertices( faceID, mIntArray )
if mIntArray.__len__() > 4:
if not dagPath.fullPathName() in violationMesh:
violationMesh.append( dagPath.fullPathName() )
violationPolygons.append( '%s.f[%d]' %( dagPath.fullPathName(),
faceID ) )
mIt_kMesh.next()
cmds.select(violationPolygons)
On Tuesday, September 6, 2016 at 5:00:01 PM UTC-7, Ian Jones wrote:
>
> Two recommendations: One I would use selection lists as they'll be faster
> to interact with during your loops and you can convert them to strings if
> needed. Second I would recommend looking at the mesh iterators as there
> are convenient access to things like vertex count per face found there that
> will save you querying an array just to then check the length. We recently
> wrote something similar at work like this:
>
> bad_stuff = OpenMaya.MSelectionList()
> meshIt = OpenMaya.MItMeshPolygon(dagPath)
> while !meshIt.isDone:
> if meshIt.polygonVertexCount > 4:
> bad_stuff.add(dagPath,meshIt.currentItem())
> meshIt.next()
>
> OpenMaya.MGlobal.setActiveSelectionList(bad_stuff)
>
> Ian
>
>
> On Tue, Sep 6, 2016 at 4:28 PM, Justin Israel <[email protected]
> <javascript:>> wrote:
>
>> Have you done any profiling to determine the slowest parts of this code?
>> Beyond that, I could just throw out some suggestions from a pure python
>> code review standapoint as opposed to the best way to approach this
>> problem..
>>
>> Where is violationMesh defined? Is it a typo and actually meant to be
>> violationPolygons?
>> If so, you are doing a membership test in a list that is growing over time.
>> Your lookups will get longer and longer because it has to do a linear
>> search to match. You may consider switching to using a set(), as membership
>> tests will be much faster.
>>
>> You could switch to use xrange() instead of range(), because in Python
>> 2.x the range() command will expand to a complete list, whereas xrange()
>> will be a generator. If you have tons of faces, this could make a
>> difference.
>>
>> This may get you nothing in return, but you could also create your
>> mFnMesh just once before the loop, and change the object each time with
>> mfnMesh.setObject(dagPath). It would save you whatever the cost is of
>> calling the constructor and then throwing it away on each loop.
>>
>> Justin
>>
>> On Wed, Sep 7, 2016 at 11:08 AM Chad_Fox <[email protected]
>> <javascript:>> wrote:
>>
>>> Hi All
>>>
>>> I recently started diving into OpenMaya to write a faster NGon check,
>>> but my results are still too slow @ 5.5 seconds in a 500k Polygon scene.
>>>
>>> I'd like to ask for any recommendations you can offer on how to improve
>>> this check script, if possible.
>>>
>>> Side note: the script cannot select anything. I previously used a
>>> command to run the PolyCleanup tool. Although it finds NGons in a
>>> microsecond, it doesnt give me a list, it selects them.
>>>
>>> Thoughts?
>>>
>>> import maya.cmds as cmds
>>> import maya.OpenMaya as om
>>>
>>> violationPolygons = []
>>>
>>> mIt_kMesh = om.MItDependencyNodes( om.MFn.kMesh )
>>>
>>> while not mIt_kMesh.isDone():
>>> mObject = mIt_kMesh.thisNode()
>>>
>>> dagPath = om.MDagPath.getAPathTo( mObject )
>>>
>>> mFnMesh = om.MFnMesh( dagPath )
>>> mObject_numFaces = mFnMesh.numPolygons()
>>>
>>> for faceID in range(0, mObject_numFaces ):
>>> mIntArray = om.MIntArray()
>>> mFnMesh.getPolygonVertices( faceID, mIntArray )
>>>
>>> if mIntArray.__len__() > 4:
>>> if not dagPath.fullPathName() in violationMesh:
>>> violationMesh.append( dagPath.fullPathName() )
>>> violationPolygons.append( '%s.f[%d]' %( dagPath.fullPathName
>>> (), faceID ) )
>>>
>>> mIt_kMesh.next()
>>>
>>> cmds.select(violationPolygons)
>>>
>>> --
>>> 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]
>>> <javascript:>.
>>> To view this discussion on the web visit
>>> https://groups.google.com/d/msgid/python_inside_maya/6e6e3e86-ed5d-4335-9c10-352696e8701e%40googlegroups.com
>>>
>>> <https://groups.google.com/d/msgid/python_inside_maya/6e6e3e86-ed5d-4335-9c10-352696e8701e%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] <javascript:>.
>> To view this discussion on the web visit
>> https://groups.google.com/d/msgid/python_inside_maya/CAPGFgA2r5rbjv1XZpDKYRBm39DvsnAehMrUykyBW%3DxLeuaav-g%40mail.gmail.com
>>
>> <https://groups.google.com/d/msgid/python_inside_maya/CAPGFgA2r5rbjv1XZpDKYRBm39DvsnAehMrUykyBW%3DxLeuaav-g%40mail.gmail.com?utm_medium=email&utm_source=footer>
>> .
>> For more options, visit https://groups.google.com/d/optout.
>>
>
>
On Tuesday, September 6, 2016 at 5:00:01 PM UTC-7, Ian Jones wrote:
>
> Two recommendations: One I would use selection lists as they'll be faster
> to interact with during your loops and you can convert them to strings if
> needed. Second I would recommend looking at the mesh iterators as there
> are convenient access to things like vertex count per face found there that
> will save you querying an array just to then check the length. We recently
> wrote something similar at work like this:
>
> bad_stuff = OpenMaya.MSelectionList()
> meshIt = OpenMaya.MItMeshPolygon(dagPath)
> while !meshIt.isDone:
> if meshIt.polygonVertexCount > 4:
> bad_stuff.add(dagPath,meshIt.currentItem())
> meshIt.next()
>
> OpenMaya.MGlobal.setActiveSelectionList(bad_stuff)
>
> Ian
>
>
> On Tue, Sep 6, 2016 at 4:28 PM, Justin Israel <[email protected]
> <javascript:>> wrote:
>
>> Have you done any profiling to determine the slowest parts of this code?
>> Beyond that, I could just throw out some suggestions from a pure python
>> code review standapoint as opposed to the best way to approach this
>> problem..
>>
>> Where is violationMesh defined? Is it a typo and actually meant to be
>> violationPolygons?
>> If so, you are doing a membership test in a list that is growing over time.
>> Your lookups will get longer and longer because it has to do a linear
>> search to match. You may consider switching to using a set(), as membership
>> tests will be much faster.
>>
>> You could switch to use xrange() instead of range(), because in Python
>> 2.x the range() command will expand to a complete list, whereas xrange()
>> will be a generator. If you have tons of faces, this could make a
>> difference.
>>
>> This may get you nothing in return, but you could also create your
>> mFnMesh just once before the loop, and change the object each time with
>> mfnMesh.setObject(dagPath). It would save you whatever the cost is of
>> calling the constructor and then throwing it away on each loop.
>>
>> Justin
>>
>> On Wed, Sep 7, 2016 at 11:08 AM Chad_Fox <[email protected]
>> <javascript:>> wrote:
>>
>>> Hi All
>>>
>>> I recently started diving into OpenMaya to write a faster NGon check,
>>> but my results are still too slow @ 5.5 seconds in a 500k Polygon scene.
>>>
>>> I'd like to ask for any recommendations you can offer on how to improve
>>> this check script, if possible.
>>>
>>> Side note: the script cannot select anything. I previously used a
>>> command to run the PolyCleanup tool. Although it finds NGons in a
>>> microsecond, it doesnt give me a list, it selects them.
>>>
>>> Thoughts?
>>>
>>> import maya.cmds as cmds
>>> import maya.OpenMaya as om
>>>
>>> violationPolygons = []
>>>
>>> mIt_kMesh = om.MItDependencyNodes( om.MFn.kMesh )
>>>
>>> while not mIt_kMesh.isDone():
>>> mObject = mIt_kMesh.thisNode()
>>>
>>> dagPath = om.MDagPath.getAPathTo( mObject )
>>>
>>> mFnMesh = om.MFnMesh( dagPath )
>>> mObject_numFaces = mFnMesh.numPolygons()
>>>
>>> for faceID in range(0, mObject_numFaces ):
>>> mIntArray = om.MIntArray()
>>> mFnMesh.getPolygonVertices( faceID, mIntArray )
>>>
>>> if mIntArray.__len__() > 4:
>>> if not dagPath.fullPathName() in violationMesh:
>>> violationMesh.append( dagPath.fullPathName() )
>>> violationPolygons.append( '%s.f[%d]' %( dagPath.fullPathName
>>> (), faceID ) )
>>>
>>> mIt_kMesh.next()
>>>
>>> cmds.select(violationPolygons)
>>>
>>> --
>>> 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]
>>> <javascript:>.
>>> To view this discussion on the web visit
>>> https://groups.google.com/d/msgid/python_inside_maya/6e6e3e86-ed5d-4335-9c10-352696e8701e%40googlegroups.com
>>>
>>> <https://groups.google.com/d/msgid/python_inside_maya/6e6e3e86-ed5d-4335-9c10-352696e8701e%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] <javascript:>.
>> To view this discussion on the web visit
>> https://groups.google.com/d/msgid/python_inside_maya/CAPGFgA2r5rbjv1XZpDKYRBm39DvsnAehMrUykyBW%3DxLeuaav-g%40mail.gmail.com
>>
>> <https://groups.google.com/d/msgid/python_inside_maya/CAPGFgA2r5rbjv1XZpDKYRBm39DvsnAehMrUykyBW%3DxLeuaav-g%40mail.gmail.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/590be83e-46db-4364-b144-438000b6c812%40googlegroups.com.
For more options, visit https://groups.google.com/d/optout.