Level One Magic
"Any sufficiently advanced technology is indistinguishable from magic." - Arthur C. Clarke

shader.py

try:
    import maya.cmds as cmds
except:
    print('# ERROR: Maya module could not be loaded.')

def connectNodes(outPath, inPath):
    '''
    Connect two node paths together, disconnecting any existing
    input into the target path first.
    '''
    source = cmds.connectionInfo(inPath, sfd=True) 
    if not source == '' and not source == outPath:
        cmds.disconnectAttr(source, inPath)
        print('cmds.disconnectAttr("%s", "%s")' % (source, inPath))
    cmds.connectAttr(outPath, inPath, f=True)
    print('cmds.connectAttr("%s", "%s", f=True)' % (outPath, inPath))
    
def createShaderNode(node):
    '''
    Create a new Shading Group node with a specific name
    Returns the new node, or the same node if it already exists
    '''
    if not cmds.objExists(node):
        node = cmds.createNode('shadingEngine', n=node)
        print('cmds.createNode("shadingEngine", n="%s")' % node)
    return node
        
def createMaterialNode(type, node):
    '''
    Create a new Material node with a specific name
    Returns the new node, or the same node if it already exists
    '''
    if not cmds.objExists(node):
        node = cmds.shadingNode(type, asShader=True, n=node)
        print('cmds.shadingNode("%s", asShader=True, n="%s")' % (type, node))
    return node
        
def createUtilityNode(type, node):
    '''
    Create a new Utility node with a specific name
    Returns the new node, or the same node if it already exists
    '''
    if not cmds.objExists(node):
        node = cmds.shadingNode(type, asUtility=True, n=node)
        print('cmds.shadingNode("%s", asUtility=True, n="%s")' % (type, node))
    return node

def createFileTexture(path):
    '''
    Create a new File Texture node and place2dTexture node, and connects
    all default attributes between the two.
    Returns the new File Texture node, or the same node if it already exists
    '''
    print('# Creating file and place2dTexture nodes for texture: ' + path)
    name = path.split('/')[-1].split('.')[0]
    fn = '%s_file' % name
    un = '%s_place2d' % name
    if not cmds.objExists(fn):
        fn = cmds.shadingNode('file', asTexture=True, n=fn)
        print('cmds.shadingNode("file", asTexture=True, n="%s")' % fn)
    un = createUtilityNode('place2dTexture', '%s_place2d' % name)
    attrs = ['coverage', 'translateFrame', 'rotateFrame', 'mirrorU', 'mirrorV',
             'stagger', 'wrapU', 'wrapV', 'repeatUV', 'offset', 'rotateUV',
             'noiseUV', 'vertexUvOne', 'vertexUvTwo', 'vertexUvThree',
             'vertexCameraOne']
    for attr in attrs:
        connectNodes('%s.%s' % (un, attr), '%s.%s' % (fn, attr))
    connectNodes('%s.outUV' % un, '%s.uv' % fn)
    connectNodes('%s.outUvFilterSize' % un, '%s.uvFilterSize' % fn)
    cmds.setAttr('%s.fileTextureName' % fn, path, type='string')
    return fn
    
def createGameShadingNetwork(path):
    '''
    Create a new shading network with nodes for color, specular, normal and
    glow maps
    '''
    print('# createGameShadingNetwork("%s")' % path)
    name = path.split('/')[-1].split('.')[0]
    sg = createShaderNode('%s_SG' % name)
    mat = createMaterialNode('blinn', '%s_MAT' % name)
    connectNodes('%s.outColor' % mat, '%s.surfaceShader' % sg)
    # Color
    colorPath = path.replace('.psd', '_col.png')
    colorNode = createFileTexture(colorPath)
    connectNodes('%s.outColor' % colorNode, '%s.color' % mat)
    # Specular
    specPath = path.replace('.psd', '_spc.png')
    specNode = createFileTexture(specPath)
    connectNodes('%s.outColor' % specNode, '%s.specularColor' % mat)
    # Normal
    normPath = path.replace('.psd', '_nml.png')
    normNode = createFileTexture(normPath)
    cmds.setAttr('%s.alphaIsLuminance' % normNode, 1)
    bn = createUtilityNode('bump2d', '%s_bump' % name)
    cmds.setAttr('%s.bumpInterp' % bn, 1)
    connectNodes('%s.outAlpha' % normNode, '%s.bumpValue' % bn)
    connectNodes('%s.outNormal' % bn, '%s.normalCamera' % mat)
    # Glow
    glowPath = path.replace('.psd', '_glo.png')
    glowNode = createFileTexture(glowPath)
    connectNodes('%s.outColor' % glowNode, '%s.ambientColor' % mat)

Back to Main

© 2003-2013 Andrew Kelts