Search code examples
pythonmaya

how should i get the shape node of imagePlane connected to camera in maya


def connectedImgPlanes(self,dagNode):
    print "dagNode ",dagNode ,type(dagNode)
    sourceConnections = cmds.listConnections(dagNode, source = True) or []
    if len(sourceConnections) != 0:
       lc = sourceConnections[0].split("->")[1]
       atribVal=cmds.getAttr(lc+".imageName")
       return atribVal
    else:
        return ""

the above function works and returns the path with file name from the imageName attribute of shape node of imageplane, but if top camera has imageplane set then the above function doesnt work, in that case i get error saying # Error: line 1: IndexError: file line 1: list index out of range # due to

newStr=str(sourceConnections[0]).split("->")[1]

and then I tried different way to get the shape node of the imageplanes and return an attribute from it,

    lc=""
    try:
        lc=cmds.listRelatives(cmds.listRelatives(dagNode)[0])[0]
    except TypeError:
           return ""
    print lc
    atribVal=cmds.getAttr(lc+".imageName")
    return atribVal

this one also works until we add the top camera and the code starts giving a different type of error saying More than one object matches name: imagePlane1 #

please some one help me out get the shape nodes of each camera and return empty string if camera has no imageplane set...


Solution

  • Since an imagePlanes(dependNode) cannot have any shape node attached with therefore I am assuming it's the imageName attr of the imagePlane that you want.

    The following function, if passed a camera, will return the imageName attribute of any imagePlane attached to it. And if a transform is passed will look for any camera connections to it and then return the imageName attribute of any imagePlanes attached to the camera that was found.

    import maya.cmds as cmds 
    
    def connectedImgPlanes(dagNode):
        # if we have camera transform then go to camera shape 
        if cmds.nodeType(dagNode) == 'transform':
            dagNode = cmds.listRelatives(dagNode, s=True, c=True)[0]
        if cmds.nodeType(dagNode) != 'camera': 
            cmds.error("%s is not a camera node" %dagNode)
    
        # get all the imageplane nodes connected to image plane
        sourceConnections = cmds.listConnections(dagNode + '.imagePlane', source = True, type = 'imagePlane') or []
    
        # collect all the 'imageName(s)' into a list and return it
        imageNames = []
        for lc in sourceConnections:
           atribVal=cmds.getAttr(lc+".imageName")
           imageNames.append(atribVal)
        return imageNames