簡體   English   中英

為Maya中導入的.OBJ文件指定多個着色器

[英]Assign multiple shaders to imported .OBJ files in 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()

使用此代碼,我只能為整個幾何體分配一個着色器。

是否可以為同一塊幾何體分配不同的着色器?

當然,您可以為同一塊幾何體分配不同的着色器,也可以為多個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

在此輸入圖像描述

您還可以使用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))] )

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM