Finding utility nodes through script

My buddy Toby approached me this morning with an interesting question: is there a way to list all utility nodes currently in the scene through code? I assumed he already Googled it himself, but I nevertheless did the same thing. As it turns out, either no one has really asked the question or nobody came up with a very good answer. In a thread from 8 years ago on CGTalk someone suggests making a helper function, check every node in the scene against this helper function and return the type of node. It requires you to hardcode every type of utility node and doesn’t allow for much flexibility. This was probably around the time that Maya 7.0 was the latest version and I really don’t remember much from those days, so maybe this was the most efficient solution back in the day.

While this looks like a good solution, it does have one major flow. If you’re going to do it like this, you have to assume that the user didn’t rename their utility nodes. Which could be the case, but it’s really not something you can guarantee. If the artist is building a complex network, he will probably rename his nodes to something a little more meaningful. I remember an Earth shading network I built years ago (as part of the 3Dbuzz.com Mastering Maya course) where I renamed my nodes. I’m glad I did, because when I look back at the network now, I can actually understand whatever the hell I was taught back then 🙂

earth_network

And I too was thinking along the same lines as the CGtalk answer. I figured that if you just rename every utility node you make and have it start with “U_”, you can easily search through the scene and list everything starting with “U_”. But that also doesn’t help you if you’re not adhering to that naming convention.

So I dug a little deeper and found that every utility node has a standard output connection to “defaultRenderUtilityList1”. As far as I can tell, a connection to this node makes a node show up in the hyper shade. Which is not necessarily something you want if you’re creating nodes yourself, but I digress. Listing all connections made to “defaultRenderUtilityList1” gives us exactly what we need if we want to loop over all utility nodes.

Some code:

import maya.cmds as cmds

utilityNodes = cmds.listConnections("defaultRenderUtilityList1")
print utilityNodes

In my example, this outputs the following:
[u'blendDayAndNightFile', u'findSunLight', u'place2dTexture1', u'bump2d1', u'findDarkSide_multiplyByLightSideIllumination', u'multiplyColorMapByLightInformation', u'CityLightsIntensity', u'cityLightsBlender', u'plusMinusAverage1', u'setRange', u'bumpHeightDivider', u'brightenClouds2', u'brightenClouds1', u'cloudBumpDivider', u'bump2d2', u'samplerInfo', u'reverse', u'atmoCondition', u'intensityMultiplier']

(The extra nodes you see which are not on the screenshot are part of the controls I’ve made available in the scene.)

This helps us a little bit, but it’s not a home run. Getting the connections from “defaultRenderUtilityList1” gives you the names, but not the type. So you might find “findSunLight”, but you wouldn’t necessarily know that this is a surfaceLuminance node. Luckily, there’s an easy way to find out using the “nodeType” command. This returns which the node type (whodathunkit, right? :))

import maya.cmds as cmds

utilityNodes = cmds.listConnections("defaultRenderUtilityList1")
print utilityNodes

for utilityNode in utilityNodes:
	print utilityNode + " is a " + cmds.nodeType(utilityNode)

This provides the following output:

blendDayAndNightFile is a blendColors
findSunLight is a surfaceLuminance
place2dTexture1 is a place2dTexture
bump2d1 is a bump2d
findDarkSide_multiplyByLightSideIllumination is a multiplyDivide
multiplyColorMapByLightInformation is a multiplyDivide
CityLightsIntensity is a multiplyDivide
cityLightsBlender is a blendColors
plusMinusAverage1 is a plusMinusAverage
setRange is a setRange
bumpHeightDivider is a multiplyDivide
brightenClouds2 is a multiplyDivide
brightenClouds1 is a multiplyDivide
cloudBumpDivider is a multiplyDivide
bump2d2 is a bump2d
samplerInfo is a samplerInfo
reverse is a reverse
atmoCondition is a condition
intensityMultiplier is a multiplyDivide

Knowing this, you should be able to do whatever you want to because you can now always find the node name and the type.

By the way, Toby is an AMAZING artist and you should really check out his work. If you’ve seen the Avengers or Prometheus, you’ve already seen some of his stuff. It’s ball-tingly awesome 🙂

http://www.toastfx.com/