Code Junkie…

on

A buddy at work has been motivating me to try to script more things as I am going through my daily routine and I’m starting to realize the power that is there. We have a number of minor repetitive tasks that we do every day in our jobs as tech artists, and as one-offs they might take 30 secs or a minute and seem trivial. Now take that ‘one-off’ and think about how many times we actually do it in any given day…10, 20, 50? That time savings adds up exponentially over the course of a week, month, year…What can one say, except, the AWESOME power of scripting continues to amaze!

red coolness

A small task that we need to do is assign all of our physics objects a physics shader. Now normally you have to create the shader, change it’s color to red, and then assign it to the physics objects in your scene. Not exactly rocket-science, nor does it take an hour to do, nonetheless it IS repetitive. Well I started thinking about it and found out it’s not too difficult to do with script. With a bit o’ help from a tutorial I found here: I was quickly able to create a script to do exactly what I was looking for.


import maya.cmds as cmds
import maya.mel as mel

def createMaterial( name, color, type ):
cmds.sets( renderable=True, noSurfaceShader=True, empty=True, name=name + 'SG' )
cmds.shadingNode( type, asShader=True, name=name )
cmds.setAttr( name + ".color", color[0], color[1], color[2], 'double3')
cmds.connectAttr( name + ".outColor", name+"SG.surfaceShader" )

def assignMaterial (name, object):
cmds.sets(object, edit=True, forceElement=name+'SG')

def assignNewMaterial( name, color, type, object):
createMaterial (name, color, type)
assignMaterial (name, object)

cmds.select("*_physobj*")
mrSelection = cmds.ls(sl=True)

assignNewMaterial( 'Physobj' , (1, 0, 0), 'lambert', mrSelection )

Give it a try. This will toss all of your named meshes of _physobj in your scene into an array and assign them with a shader that is red with the name Physobj.

Cool!

Step 2 will then be to mark the faces as Physics and NoDraw, but I’ll save that for when I’m actually at work 😀

Leave a Reply

Your email address will not be published. Required fields are marked *