UPDATED MARCH 18, 2016 – Output script information to the bottom command line after realizing there was a way to print there using Python.
One of the commands I heavily rely on aside from the much abused toggle isolate for selections is the Select Objects With Material command of the Hypershade. I usually hide/isolate stuff based on their material while modeling and unwrapping. Sometimes I even assign temporary materials to have selection sets/groups (There are better ways but it works and it became a habit, and having them on bright colors helps visually). It’s really such a convenient tool, but what I’m not a fan of is having the Hypershade open, right clicking the Material of your preference, and clicking on the command. When you work with a laptop like me, you’ll know that real estate is very precious.
In addition, I prefer to have my screen clean and minimal (in full screen mode using my own custom Marking Menu). While a friend and I were working on a build machine for our casual indie android game, he used Python and I decided to give it a try.
Anyway, the command for Selecting Objects With Material is hyperShade(objects = “”) and below is what I came up with. Basically what this does is it selects all objects with the same material as the object you selected. This works with multiple selections, so if you select, say a couple of meshes with “Metal” and “Wood” assigned to them, then all objects sharing those two materials will be selected. Now you can easily select and hide/isolate all objects with the same material with one command and without the Hypershade open. Add this to your shelf/Hotkey/Marking Menu for an easy life. Just remember to have the script on Python, not Mel.
# Select Objects By Material(s)
# nerdocs - Just nerdy stuff that no one really cares about
# created: 2015-02-19
# last updated: 2016-03-18 - //output info to the bottom command line
import maya.cmds as mC
import maya.OpenMaya as oM
messageOutput = "\n\n[nerdocs]: "
objectsSelected = mC.ls(dag = True, o = True, s = True, sl = True)
if objectsSelected == :
messageOutput += "No Objects Selected."
shadingGroups = mC.listConnections(objectsSelected,type='shadingEngine')
selectionMaterial = mC.ls(mC.listConnections(shadingGroups),materials = True)
materialCount = 0
previousMaterial = " "
mC.select(clear = True)
selected = mC.ls(sl = True)
for object in selectionMaterial:
if (previousMaterial != object):
materialCount += 1
previousMaterial = object
mC.hyperShade(objects = object)
selected += mC.ls(sl = True)
messageOutput += str(len(selected)) + " object(s) selected based on the material of " + str(materialCount) + " selection(s)."
How this works, with pictures in case you didn’t read through my novel:
STEP 1: Select an object or multiple objects. In this example, yellow was assigned for wood, magenta for metal, and cyan for leather. I selected an object with leather as well as an object with wood material.
STEP 2: Run script and Celebrate! Now all the materials with wood and leather are selected!
As of the March 18, 2016 update, the script outputs to the bottom command line, providing a more visible feedback.