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", 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 = "lambert1"
shadingGroup = cmds.listConnections(materialName, type="shadingEngine")
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("%s.fileTextureName"%file) for file in"file")]

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 = ".jpg"
typedFiles = [cmds.getAttr("%s.fileTextureName"%file) for file in"file") if fileFilter in cmds.getAttr("%s.fileTextureName"%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("system("%s") "%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. :]