如何更改着色器颜色设置trhough Python-Maya

时间:2017-03-23 17:03:23

标签: python maya

我有这个代码...我不明白为什么,每当我做for循环时它都不会改变立方体的颜色......我的意思是,在这种情况下我调用函数“BookGen”5时间...每次我随机分配3个数字,但颜色不会改变....

import maya.cmds as cmds
def BookGen(myBlinn,myBlinn2,myBlinn3):
    myBook=cmds.polyCube(name='Book',h=8,w=4,d=2) #Create a testCube with that Name

    #Modeling that I didn't include
    #//Cover Color
    Colors=[]
    for i in range(3):
        tmp=random.uniform(0.0,1.0) #Uniform function allow me to find a random FLOAT number between a range(not like randint!)
        Colors.append(tmp)  
    cmds.setAttr ( (myBlinn + '.color'), Colors[0],Colors[1],Colors[2], type = 'double3' )   
    cmds.select(myBook[0]+'.f[0:4]')
    cmds.hyperShade( assign=myBlinn )


def ShaderGen():
        #Add Color
        #//Pages Color
        myBlinn = cmds.shadingNode( 'blinn', asShader=True) 
        #cmds.setAttr ( (myBlinn + '.color'), 1.0,0.8,0.44, type = 'double3' ) 

        #//Cover Color   
        myBlinn2 = cmds.shadingNode( 'blinn', asShader=True)
        #cmds.setAttr ( (myBlinn2 + '.color'), 1.0,1.0,1.0, type = 'double3' )

        #//Cover Details Color
        myBlinn3 = cmds.shadingNode( 'blinn', asShader=True)
        #cmds.setAttr ( (myBlinn3 + '.color'), 1.0,1.0,1.0, type = 'double3' )

        return myBlinn,myBlinn2,myBlinn3

def BookShelf():

    NumberOfBooks=5 #Inserted from user
    myBlinn=''
    myBlinn2=''
    myBlinn3=''
    myBlinn,myBlinn2,myBlinn3=ShaderGen()


    myRowShelfGroup=cmds.group(em=True,n='RowShelfGroup')
    for i in range(NumberOfBooks):
        BookGen(myBlinn,myBlinn2,myBlinn3)


BookShelf()

1 个答案:

答案 0 :(得分:2)

你一遍又一遍地使用相同的shadernode,最后都使用相同的shadernode,这样你就可以在所有对象上获得相同的颜色。你需要在BookGen函数中移动myBlinn,myBlinn2,myBlinn3 = ShaderGen()以获得你想要的东西..这是一个工作版本

import maya.cmds as cmds
import random 
def BookGen():
    myBook=cmds.polyCube(name='Book',h=8,w=4,d=2) #Create a testCube with that Name
    myBlinn,myBlinn2,myBlinn3=ShaderGen()
    #Modeling that I didn't include
    #//Cover Color
    Colors=[]
    for i in range(3):
        tmp=random.uniform(0.0,1.0) #Uniform function allow me to find a random FLOAT number between a range(not like randint!)
        Colors.append(tmp)  
    cmds.setAttr ( (myBlinn + '.color'), Colors[0],Colors[1],Colors[2], type = 'double3' )   
    cmds.select(myBook[0]+'.f[0:4]')
    cmds.hyperShade( assign=myBlinn )


def ShaderGen():
        #Add Color
        #//Pages Color
        myBlinn = cmds.shadingNode( 'blinn', asShader=True) 
        #cmds.setAttr ( (myBlinn + '.color'), 1.0,0.8,0.44, type = 'double3' ) 

        #//Cover Color   
        myBlinn2 = cmds.shadingNode( 'blinn', asShader=True)
        #cmds.setAttr ( (myBlinn2 + '.color'), 1.0,1.0,1.0, type = 'double3' )

        #//Cover Details Color
        myBlinn3 = cmds.shadingNode( 'blinn', asShader=True)
        #cmds.setAttr ( (myBlinn3 + '.color'), 1.0,1.0,1.0, type = 'double3' )

        return myBlinn,myBlinn2,myBlinn3

def BookShelf():

    NumberOfBooks=5 #Inserted from user
    myBlinn=''
    myBlinn2=''
    myBlinn3=''

    myRowShelfGroup=cmds.group(em=True,n='RowShelfGroup')
    for i in range(NumberOfBooks):
        BookGen()


BookShelf()