为Maya中导入的.OBJ文件指定多个着色器

时间:2016-09-30 07:10:04

标签: python import textures maya

我需要将多个文件导入Maya并为每个文件分配多个材料。

我在Python中有以下代码:

import maya.cmds as cmds
import glob

def importFile(i):
    cmds.file(i, i=True, groupReference=True, groupName="myobj")

def moveFile():
    cmds.select("myobj")

    cmds.scale(1,1,1, r=True)
    cmds.move (0, 14, 0, r=True)
    cmds.rotate (-90, 0, 0, r=True)

def materialFile():
    cmds.select("myobj")
    myMaterial = "blinn1"
    cmds.sets( e=True, forceElement= myMaterial + 'SG' ) 

def renderFile(i):
    cmds.setAttr("defaultRenderGlobals.imageFilePrefix", i, type="string")
    cmds.render(batch=True)

def deleteFile():
    cmds.select("myobj")
    cmds.delete()

myglob = glob.glob("/The/path/of/your/Obj_files/*.obj") 

for i in myglob:
    importFile(i)
    moveFile()
    materialFile()
    renderFile(i)
    deleteFile()

使用此代码,我只能为整个几何体分配一个着色器。

是否可以为同一块几何体分配不同的着色器?

1 个答案:

答案 0 :(得分:0)

当然,您可以为同一块几何体分配不同的着色器,也可以为多个3d模型分配任何可访问的着色器。

import maya.cmds as mat

d = 0

shader_node1 = mat.shadingNode( "anisotropic", asShader = True, n = 'ani' )
shader_node2 = mat.shadingNode( "phong", asShader = True, n = 'pho' )
shader_node3 = mat.shadingNode( "lambert", asShader = True, n = 'lam' )
mat.setAttr( (shader_node1 + '.color'), 1.0, 0.0, 0.0, type = 'double3' )
mat.setAttr( (shader_node2 + '.color'), 0.0, 1.0, 0.0, type = 'double3' )
mat.setAttr( (shader_node2 + '.transparency'), 0.25, 0.25, 0.25, type = 'double3' )
mat.setAttr( (shader_node3 + '.color'), 0.0, 0.0, 1.0, type = 'double3' )

shading_group1 = mat.sets( renderable = True, noSurfaceShader = True, empty = True )
shading_group2 = mat.sets( renderable = True, noSurfaceShader = True, empty = True )
shading_group3 = mat.sets( renderable = True, noSurfaceShader = True, empty = True )

mat.connectAttr( '%s.outColor' %shader_node1, '%s.surfaceShader' %shading_group1 )
mat.connectAttr( '%s.outColor' %shader_node2, '%s.surfaceShader' %shading_group2 )
mat.connectAttr( '%s.outColor' %shader_node3, '%s.surfaceShader' %shading_group3 )

mat.polySphere( radius = 5 )
mat.polySphere( radius = 7 )
mat.polySphere( radius = 3 )

mat.select( 'pSphere1' )
mat.move( 7, 6, 0, r = True )
mat.hyperShade( assign = shader_node1 )

mat.select( 'pSphere2' )
mat.hyperShade( assign = shader_node2 )

mat.select( 'pSphere3' )
mat.move( -7, -2, 0, r = True )
mat.hyperShade( assign = shader_node3 )

d += 1

enter image description here

您还可以使用random函数访问数组中的着色器:

import random as random

myArray = [shader_node1, shader_node2, shader_node3]

for i in myArray :

    mat.select( 'pSphere1' )
    mat.move( 7, 6, 0, r = True )
    mat.hyperShade( assign = myArray[int(random.uniform(0,3))] )

    mat.select( 'pSphere2' )
    mat.hyperShade( assign = myArray[int(random.uniform(0,3))] )

    mat.select( 'pSphere3' )
    mat.move( -7, -2, 0, r = True )
    mat.hyperShade( assign = myArray[int(random.uniform(0,3))] )