Python and Mel

Hi All,

I have been busy re learning Python and Mel in Maya.

Below, is a script which makes a material in Maya that has a texture applied to it.


import maya.cmds as cmds
#create a counter and set it to zero
i = 0
#create a shader
shader=cmds.shadingNode("lambert",asShader=True, n='Red')
#a file texture node
file_node=cmds.shadingNode("file",asTexture=True, n = "text_%s" % i)
# defines location where texture is
file = ("textures"+ "/" +"red.jpg")
#a shading group
shading_group = cmds.sets(renderable=True,noSurfaceShader=True,empty=True)
#connect texture to sg surface shader
cmds.setAttr( '%s.fileTextureName' % file_node, file, type = "string")
#connect shader to sg surface shader
cmds.connectAttr('%s.outColor' %shader ,'%s.surfaceShader' %shading_group)
#connect file texture node to shader's color
cmds.connectAttr('%s.outColor' %file_node,'%s.color' %shader)
#increases the counter
i += 1

This code is based on the following reference: http://mayapy.wordpress.com/2011/11/04/create-shaders-through-python/

This entry was posted in Code. Bookmark the permalink.

3 Responses to Python and Mel

Leave a Reply

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