Maya Python - Get renderable nodes

Maya Python - Get renderable nodes

pkp.lsa
Explorer Explorer
548 Views
2 Replies
Message 1 of 3

Maya Python - Get renderable nodes

pkp.lsa
Explorer
Explorer

Is there a way to get a list of renderable nodes in the scene that matches nodes that will get exported via Maya's Alembic Export -renderableOnly flag?

 

maya.cmds.ls(visible=True) does not return nodes that have their LOD Visibility turned off while having their visibility on, so that doesn't work for what I'm trying to do. Nodes with their LOD visibility turned off are flagged as non-renderable for the purposes of Maya's Alembic Export, so these nodes get ignored during export.

 

 

0 Likes
549 Views
2 Replies
Replies (2)
Message 2 of 3

Kahylan
Advisor
Advisor

Hi!

 

If I understand your question correctly, you need a list of all dag objects that have both LOD Visibility and Visibility turned on and have no parents with either turned off. You could list them as follows:

 

import maya.cmds as mc

#getting visible List
vis = mc.ls(v= True, dag= True, tr = True, l= True)
#creating list to store truly visible objects
renderableList = []
#going through visible list
for vo in vis:
    
    check = 1
    #splitting the long name into each object
    objects = vo.split("|")
    objects.pop(0)
    storedString = None
    for o in objects:
        #reformatting path to avoid problems with identical names
        o = "|" + o
        if storedString == None:
            storedString = o
        else:
            o = storedString + o
            storedString = o
        #checking if each object in path has both LOD Visibility and Visibility turned on
        v = mc.getAttr("{0}.visibility".format(o))
        lv = mc.getAttr("{0}.lodVisibility".format(o))
        #excluding objects that don't have both attributes set to True
        if v == False or lv == False:
            check = 0
            break
    #adding objects that are truly visible to list
    if check == 1:
        renderableList.append(vo)
        
print(renderableList)      

 

Now, this script still isn't perfect, since it doesn't account for other ways to turn off an objects visibility. For example using a Layer with visibility turned off.

 

Another way to approach it could be to use the python API to select everything in view.

This script creates a new camera with the widest angle of view possible, sets it as the camera in the active view, then selects everything it sees. rotates the camera, selects again, rotates again, and so on:

import maya.OpenMaya as om
import maya.OpenMayaUI as omu
import maya.cmds as mc

initSel = mc.ls(sl = True)
cam = mc.camera(hfv = 160, vfv = 160, ar = 1)

mE = mc.getPanel(type = 'modelPanel')
for e in mE:
    av = mc.modelEditor(e,q= True, av = True)
    if av == True:
        currentCam = mc.modelEditor(e,q= True, cam = True)
        mc.modelEditor(e,e = True, cam = cam[0])
        break

values = [90,180,-90,0]
channels =["rx","ry"]
renderableList = []
for c in channels:
    for v in values:
        mc.setAttr("{0}.{1}".format(cam[0], c), v)
        
        view = omu.M3dView.active3dView()
        om.MGlobal.selectFromScreen(0, 0, view.portWidth(), view.portHeight(), om.MGlobal.kReplaceList)
        sel = mc.ls(sl= True)
        renderableList = renderableList + sel

mc.select(initSel, r = True)
mc.delete(cam[0])       
mc.modelEditor(e, e=True, cam = currentCam)
renderableList = list(set(renderableList))
print(renderableList)

Now this is quite hacky, since it needs to create a new cam and delete it after it is finished. Also it works using selection, which isn't optimal...

 

I have the feeling that there is a more elegant way to do this, but I couldn't find it...

 

Anyways, these two scripts should do what you want for now, maybe someone will post a more elegant way to do it.^^

 

I hope it helps!

Message 3 of 3

pkp.lsa
Explorer
Explorer

Hey, thank you so much for your response! Your second code snippet taught me some stuff about the API I didn't even know.

Your first snippet is similar to how I'm achieving this for now, but was wondering if there is a Maya command or a flag to `maya.cmds.ls` that I was missing which would give me the list directly.

Thanks again!