5 Useful Python Commands


In this post I will share 5 python commands that you can use in your scripts or in your daily tasks inside Maya. Things like renaming transform and shape nodes, query material connections or manage your lights attributes. Really simple to understand and ready to use in the script editor.

Simple Light Manager

for i in cmds.sets('LIGHTS_SET',q=1):

	cmds.setAttr(cmds.listRelatives(i,s=1)[0]+'.intensity', 2)

	cmds.setAttr(cmds.listRelatives(i,s=1)[0]+'.aiSamples', 3)

So, let’s say you have an array of lights, and you need to change the sampling, intensity or any other attribute on all at the same time. You can simply create a set of those lights (create > set > quick selection set), and query it in a for loop. Then simply use the setAttr command, selecting the shape node with the listRelatives command, and change the attribute in all of the lights, sampling or intensity for instance. You can have this script in a tab of the script editor, and just add the setAttr command you need to change. This can also be used as a material or geometry attribute manager.

sRGB to linear (gamma 2.2)

pow (sRGBValue, 2.2)

example usage:

sRGB = cmds.getAttr('VRayMtl3.color')[0]
# Result: (0.5, 0.5, 0.5) #

#convert to linear (2.2 gamma)
cmds.setAttr('VRayMtl3.color', pow(sRGB[0],2.2), pow(sRGB[1],2.2), pow(sRGB[2],2.2))

Here we have the simple formula to convert any rgb value to linear, applying the gamma 2.2 correction. Instead of having a bunch of gammaCorrect nodes floating around in your scene, You could easily create s simple script to convert any rgb values to linear. In the sRGB variable we get the current rgb values of the vray material, and then simply use the gamma 2.2 function in a setAttr command.

Check material connections

# Is material attribute mapped?
cmds.connectionInfo( 'aiStandard2.color' ,id=1 )
# Result: True/False #

#if true, what is the source:
	cmds.connectionInfo( 'aiStandard2.color' ,sfd=1 )
	# Result: u'file2.outColor' #

In this block of code we use the connectionInfo maya command with the sdf(sourceFromDestination) flag, to ask if the material attribute, in this instance the color, has any input connections, like a file or a ramp. This will return true or false.

If the result is True, we can ask what is the source of the connection, in this example a file node is connected.

cg generalist course therenderblog

Get object(s) with material

# get object with material

Material = cmds.ls(sl=1)
# Result: [u'VRayMtl3'] #

ObjsWithMatSG = cmds.listConnections(Material,type='shadingEngine')
ObjsWithMat = cmds.listConnections(ObjsWithMatSG,type='shape')
# Result: [u'pSphere1'] #

Getting the objects with a given material is really easy inside the hypershade or the node editor. In your python scripts, if you don’t want to use any “execute mel command” you will need to get the shading node of the material with the listConnections command, and using the same command again you can grab the object(s) with the material applied.

Rename duplicated shapes

def renameDup():
	for i, obj in enumerate( cmds.ls(sl=1) ):
		if obj[-1] != cmds.listRelatives(obj,s=1)[0][-1]:


Sometimes we end up with different shape and transform names, something like “polySurface33, polySurfaceShape88″, and that can give you some issues with shape attributes and when executing scripts. In this simple function we go through the objects selected with a for loop, compare the suffix number of the object, and if it doesn’t match, we replace it with the default name plus the counter iterator. Maya will rename any duplicated name, so we don’t need to query the other objects in the scene.

Thank you for reading and please leave a comment if this has been useful to you. If you enjoy reading our blog, you might be interested in our course, have a look.

  • Zeno Pelgrims

    Great little list! Love your articles :)

  • David vH

    Great tips! Thanks a lor for sharing!

    Do you happen to know how to export Arnold Standins with python in combination with a variable for the export path? Since a decent Arnold MtoA python reference is basicaly non excistent.

    For example:

    mypath =”/home/user/desktop/mystandin.ass”

    and use that as the path in the arnoldExportAss command.

    I just can’t figure out how to use it properly.



    • therenderblog

      Does this helps:

      def aiExportFrame( self, frame, objFilename ):

      # Export a single mental ray archive frame.

      cmds.currentTime( frame )

      cmds.arnoldExportAss(f=objFilename+”.ass.gz”, s=True, c=True, bb=True, mask=56)

      #cmds.Mayatomr( mi=True, exportFilter=7020488,

      # active=True, binary=True, compression=9,

      # fragmentExport=True, fragmentChildDag =True, passContributionMaps =True,

      # assembly=True, assemblyRootName=”obj”, exportPathNames=”n”,

      # file=objFilename + “.mi” )

      self.log( “assExport ” + objFilename + “.ass.gz”)

      • David vH

        Absolutely! Thanks for taking the time to explain! Very much appreciated!