List the members of a shading group.

2,265 views
Skip to first unread message

johan Borgström

unread,
Aug 25, 2014, 3:14:58 AM8/25/14
to python_in...@googlegroups.com
Hi,

I am using the following snippet (pymel) to list the objects that are connected to the selected material. What would be the cmds version of doing the same? (listing both meshes and mesh faces)

import pymel.core as pm

# list the materials
mat_list = pm.ls(mat=True, sl=True)

for mat in mat_list:
    
    # list the shading groups
    sg_list = mat.listConnections(type='shadingEngine')
    
    for sg in sg_list:
        
        # get the members of the sg
        member_list = sg.members()
        
        for member in member_list:
            
            print(member)

Cheers,
Johan

Justin Israel

unread,
Aug 25, 2014, 5:20:17 AM8/25/14
to python_in...@googlegroups.com
mat_list = cmds.ls(mat=True, sl=True)
for mat in mat_list:
    sg_list = cmds.listConnections(type='shadingEngine')

    for sg in sg_list:
        member_list = cmds.sets(sg, q=True)
        if not member_list:
            continue

        for member in member_list:
            print member





--
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/355d3b26-4e3e-46ca-9344-691bf8cc5140%40googlegroups.com.
For more options, visit https://groups.google.com/d/optout.

johan Borgström

unread,
Aug 25, 2014, 5:54:52 AM8/25/14
to python_in...@googlegroups.com
Thanks Justin,

I would like to filter the members based on the type. So for instance separate meshes from meshFaces. The if statement below evaluates to true for both meshes and faces. I guess a face is a "subclass" of mesh. How could I separate the two?
if cmds.objectType(member, isType='mesh'):
                print('mesh and face')

Cheers,
Johan


To unsubscribe from this group and stop receiving emails from it, send an email to python_inside_maya+unsub...@googlegroups.com.

Justin Israel

unread,
Aug 25, 2014, 6:39:05 AM8/25/14
to python_in...@googlegroups.com
There are probably a bunch of ways to do this, but here is one that might work:
mat_list = cmds.ls(mat=True, sl=True)
for mat in
 mat_list:
    sg_list = cmds.listConnections(mat, type='shadingEngine')
    if not sg_list:
        continue

    for sg in sg_list:
        if not cmds.sets(sg, q=True, size=True):
            continue

        print "[set]", sg
        cmds.select(sg, replace=True)

        members = cmds.ls(sl=True, exactType="mesh") or []
        for member in members:
            print "[mesh]", member

        members = cmds.filterExpand(sm=34, expand=False) or []
        for member in members:
            print "[shape]", member

ls() can filter on inherited or exact node types, and filterExpand can filter the existing selection for faces





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/3905d09e-0499-47d7-8d36-d1e9d5621506%40googlegroups.com.

johan Borgström

unread,
Aug 25, 2014, 3:23:06 PM8/25/14
to python_in...@googlegroups.com
Great, thank you!

Cheers,
Johan
To unsubscribe from this group and stop receiving emails from it, send an email to python_inside_maya+unsubscribe@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.

Roy Nieterau

unread,
Aug 26, 2014, 3:50:38 AM8/26/14
to python_in...@googlegroups.com
Just to add on top of Justin's version. You don't need to perform the actual selection, both ls() and filterExpand() can operate on input objects. Like so:

mat_list = cmds.ls(mat=True, sl=True)

for mat in mat_list:
    sg_list
= cmds.listConnections(mat, type='shadingEngine')
   
if not sg_list:
       
continue

   
for sg in sg_list:

        members
= cmds.sets(sg, q=True)

        meshes
= cmds.ls(members, exactType="mesh")
       
for mesh in meshes:
           
print "[mesh]", mesh

        faces
= cmds.filterExpand(members, sm=34, expand=False) or []
       
for face in faces:
           
print "[face]", faces

Of course this would be a micro-optimization I assume. :)

-Roy

Justin Israel

unread,
Aug 26, 2014, 5:01:13 AM8/26/14
to python_in...@googlegroups.com
Naw I would give it much more credit than a "micro-optimization". It is definitely better to leave the selection list as it is, if you can help it, in addition to getting a bump for not having to change it :-)

Thanks!


--
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/b0898a82-b8bf-4d7a-97c0-0d26e03272f8%40googlegroups.com.

johan Borgström

unread,
Aug 26, 2014, 5:19:57 AM8/26/14
to python_in...@googlegroups.com
Thanks Roy!

I agree with Justin, great tip :)



On Tuesday, August 26, 2014 11:01:13 AM UTC+2, Justin Israel wrote:
Naw I would give it much more credit than a "micro-optimization". It is definitely better to leave the selection list as it is, if you can help it, in addition to getting a bump for not having to change it :-)

Thanks!
On Tue, Aug 26, 2014 at 7:50 PM, Roy Nieterau <royni...@gmail.com> wrote:
Just to add on top of Justin's version. You don't need to perform the actual selection, both ls() and filterExpand() can operate on input objects. Like so:

mat_list = cmds.ls(mat=True, sl=True)

for mat in mat_list:
    sg_list
= cmds.listConnections(mat, type='shadingEngine')
   
if not sg_list:
       
continue

   
for sg in sg_list:

        members
= cmds.sets(sg, q=True)

        meshes
= cmds.ls(members, exactType="mesh")
       
for mesh in meshes:
           
print "[mesh]", mesh

        faces
= cmds.filterExpand(members, sm=34, expand=False) or []
       
for face in faces:
           
print "[face]", faces

Of course this would be a micro-optimization I assume. :)

-Roy

--
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.
Reply all
Reply to author
Forward
0 new messages