Maya Python – Select By Volume

posted in: Uncategorized | 1

I wrote up this quick script in response to a forum post requested help on this selection-helper method. The below script should properly select all objects in a scene that are at least partially contained within the specified object’s bounding box volume.  This script is currently limited by the fact that the bounding box calculation performed by the xform command returns an axis-aligned box. To improve this, calculate the boundingbox with respect to the object’s local transformation – see This will allow you to rotate your bounding volume object and still return visually expected results.

import maya.cmds as cmds

class BoundingBox():
''' Helper class for bounding box-related calculations. '''

    def FromShape(cls, shapeObj):
    ''' Constructor method to create a bounding box from a shape. '''

        boundingBox = BoundingBox()
        bb = cmds.xform(shapeObj, q=True, bb=True)
        boundingBox.minX = bb[0]
        boundingBox.minY = bb[1]
        boundingBox.minZ = bb[2]
        boundingBox.maxX = bb[3]
        boundingBox.maxY = bb[4]
        boundingBox.maxZ = bb[5]
        return boundingBox

    def ContainsPoint(self, point):
    ''' Returns whether or not a point is contained in this bounding box. '''
        return (point[0] > self.minX and point[0] < self.maxX and point[1] > self.minY and point[1] < self.maxY and point[2] > self.minZ and point[2] < self.maxZ)

    def ContainsShape(self, shape):
    ''' Returns whether or not a shape is intersecting with this bounding box. '''
        shapeBB = BoundingBox.FromShape(shape)
        return (shapeBB.minX < self.maxX and shapeBB.maxX > self.minX) and (shapeBB.minY < self.maxY and shapeBB.maxY > self.minY) and(shapeBB.maxZ < self.maxZ and shapeBB.maxZ > self.minZ)

def SelectByVolume(volumeObj):
 ''' Selects all transforms in the scene that are within the specified argument's bounding box volume. '''

    # Create bounding box class from object
    boundingBox = BoundingBox.FromShape(volumeObj)

    # Get all scene objects asides from bounding box
    sceneObjs = [obj for obj in if obj != volumeObj]

    # Compare against every object in our scene to determine what is in our volume
    newSelection = list()
    for obj in sceneObjs:
        objPos = cmds.xform(obj, q=True, rotatePivot=True, ws=True)

        # If the shape's bounding box intersects with the volume's bounding box
        if boundingBox.ContainsShape(obj):
            print "%s is contained."%obj

        # NOTE - Optional alternative means to determine objects within volume.
        # If the center of the object is contained
        #if boundingBox.ContainsPoint(objPos):
            # print "%s is contained."%obj
            # newSelection.append(obj)

    # Silly but compact list comprehension way of getting all our objects
    #newSelection = [obj for obj in [obj for obj in if obj != volumeObj] if boundingBox.ContainsPoint( cmds.xform(obj, q=True, rotatePivot=True, ws=True))]

    # Update selection
    if newSelection:

# Usage
yourVolumeObject = 'pCube1'

Select By Material – Python Snippet

posted in: Uncategorized | 0

Here’s another one-line snippet that selects all faces and objects in a scene based on shading group set. Material assignment in Maya is driven by “sets”. According to the Maya documentation, “a set is a logical grouping of an arbitrary collection of objects, attributes, or components of object.”  We’ll be querying a set to find everything assigned by a specific material and then selecting it.;initialShadingGroup&quot;, q=True))

This line selects faces and objects assigned with lambert1, a helpful check I perform in scenes before exporting. (We never want artists to export default Maya materials.) It shows the artist where in the scene the lambert1 lives by highlighting it in the selection.

If you don’t know the name of the set associated with the material off hand (you probably won’t), you can easily translate from material name to set:

materialName = &quot;lambert1&quot;
shadingGroup = cmds.listConnections(materialName, type=&quot;shadingEngine&quot;)
componentsWithMaterial = cmds.sets(shadingGroup, q=True)


List All Texture Files In Scene – Python Method

posted in: Uncategorized | 0

Here’s another Python snippet you can use in Maya to, in one line, gather a list of all the files referenced in your scene. This is of great use whenever you need to copy, export, or do other actions on textures associated with a particular Maya scene.

allFiles = [cmds.getAttr(&quot;%s.fileTextureName&quot;%file) for file in;file&quot;)]

This Python list comprehension iterates over every file found using the ls command and then gets the attribute that points to the file node’s texture path. Easy!


As an alternative, if you’re concerned about only transferring specific file types, or handling certain file types differently, you can use the following extended version of the above line.

fileFilter = &quot;.jpg&quot;
typedFiles = [cmds.getAttr(&quot;%s.fileTextureName&quot;%file) for file in;file&quot;) if fileFilter in cmds.getAttr(&quot;%s.fileTextureName&quot;%file)]

Save To Clipboard Python Method

posted in: Uncategorized | 0

Here’s a super useful snippet you can use to copy any string to the user’s OS clipboard. We use this for any manual data-entry into databases or spreadsheets.

def addToClipboard(text):
    command = 'echo ' + text.strip() + '| clip'
    mel.eval(&quot;system(&quot;%s&quot;) &quot;%command)

You’ll notice I wrap the system call with MEL. This is just a personal touch and definitely not needed. I’ve noticed the usual python call to os.system displays an empty command line window for a split second while executing the command. This is a bit distracting to me.. and  the MEL version seems to avoid that. :]