[PyMEL] Sparse Component List handling

184 views
Skip to first unread message

Felix Ulber

unread,
Nov 17, 2017, 6:52:49 AM11/17/17
to python_inside_maya

Hey List :)

I am struggeling in the process of handling lists of mesh components (mesh faces, from shadingGroups, per-face material assignment).
Using PyMEL, there are two ways to query:

sets(shadingGroup, q = True)

returns:

[MeshFace(u'pSphereShape1.f[0:6]'),
 MeshFace(u'pSphereShape1.f[17:26]'),
 MeshFace(u'pSphereShape1.f[37:46]'),
...]

whereas

shadingGroup.members()

gives me:

[MeshFace(u'pSphereShape1.f[0:6,17:26,37:46,57:66,77:86,...]')]

Which is more what I need. But in this case, I cannot use the PyMEL OO variant, for  in case of quering a shadingGroups it under certain circumstances the result contains the shaderBallGeom object (from the hyperShade material preview).

So I need a way to convert the multiple MeshFace objects into one, i.e. the second kind of representation.

I rember long time ago, in mel there was something, i think in the use of certain commands, to convert the representation of sparse component lists - its just that I did not have to work with this for so long I just cannont remember it.

I tried to look up the internals of the PyMEL "ObjectSet" members method but also failed :(

Seems like a rather simple thing, sorry, I'm just a blockhead right now...

BTW: how do you format/beautify your code for mailing it here?

Thanks
Felix

Paul Molodowitch

unread,
Nov 17, 2017, 5:56:14 PM11/17/17
to python_in...@googlegroups.com

Hi - so, currently, I think the only way to combine two component objs would be to just create a new object with their indices, ie:

sel = [pm.PyNode('myCube.f[0:2]'), pm.PyNode('myCube.f[4]')
indices = itertools.chain.from_iterable(x.indicesIter() for x in sel)
indices = list(indices)

singleComp = pm.PyNode('myCube').f[indices]

Note that you have to cast the iterator to a list for current pymel releases. However, I just pushed a change to the github repo (https://github.com/LumaPictures/pymel) which will fix this, and allow you to pass an iterator in as the indices for a new component object.

I also added in the ability to add two component objects, so you could just do:

pm.PyNode('myCube').f[0:2] + pm.PyNode('myCube.f[4]')

or

faces = pm.PyNode('myCube').f[0:2]
faces += pm.PyNode('myCube.f[4]')

With my latest changes, you can also do:

faces = pm.PyNode('myCube').f[0:2, 4]
  • Paul

PS: I personally use a chrome plugin called “Markdown here” to do my code formatting..

--
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/45b11971-1940-b071-496f-a07d3d0409a6%40web.de.
For more options, visit https://groups.google.com/d/optout.

Paul Molodowitch

unread,
Nov 17, 2017, 5:57:33 PM11/17/17
to python_in...@googlegroups.com
I should note that, going forward, adding the two component objects directly is probably the preferred way of doing things, as it will use the underlying API objects, and should be considerably faster for large component selections.

Felix Ulber

unread,
Nov 24, 2017, 9:28:43 AM11/24/17
to python_in...@googlegroups.com
Thanks Paul! Very informative.

Just for personal interest - is there any particular reason why using sets(q) and ObjectSet.members() give different results? I tried to understand it from the pymel source code but got stucked.

best
Felix

Paul Molodowitch

unread,
Nov 24, 2017, 12:44:35 PM11/24/17
to python_in...@googlegroups.com
Don't know off hand, but likely it's because pymel is wrapping MFnSet... sometimes the API commands give slightly different results than the maya.cmds equivalents

Angelo Sta. Catalina

unread,
Nov 24, 2017, 1:06:34 PM11/24/17
to python_in...@googlegroups.com
I'm not sure if this will help but I use the flatten flag in the mel command ls.

pm.ls(sl=1, fl=1)
This will return a list of selected components flattened.

I believe sets also has a flatten flag that you can use to flatten components.

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.

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 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.
To view this discussion on the web visit https://groups.google.com/d/msgid/python_inside_maya/CAAssL7b0NFJSpMxS%3Dc%3Dya8CgyBZdEJ3ZpS%3DioyY%3D8yUZRujM5w%40mail.gmail.com.

Felix Ulber

unread,
Nov 27, 2017, 6:39:20 AM11/27/17
to python_in...@googlegroups.com
Hey Angelo,
thanks, but I already considered both options:

I'm not sure if this will help but I use the flatten flag in the mel command ls.

pm.ls(sl=1, fl=1)
This will return a list of selected components flattened.
This gives me:

[MeshFace(u'pSphereShape1.f[0],
 MeshFace(u'pSphereShape1.f[1],
 MeshFace(u'pSphereShape1.f[2],
....]

While this is correct in the sense of 'flattening', it is quite  the oposite from what I asked for :)


I believe sets also has a flatten flag that you can use to flatten components.
I also recognized that, but this flag has a different meaning, it has something to to with the actual assignment of set memberships in case of nestes sets if I remember right.

Thanks anyway

best
Felix

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/CAJAkR62PygEnZvNOLHDxi1UBtMzf6CRhV3tm%2BEiaUS5QeG5okg%40mail.gmail.com.
Reply all
Reply to author
Forward
0 new messages