Link procedure to each button command maya

折月煮酒 提交于 2019-12-25 12:03:30

问题


I made this script that creates two material with different color values.How can I attach this procedure so that when I click color 1 button it creates material color 1 and color 2 when I click second button.If there is a much better way to achieve this is python.Please let me know

global string $list_of_names[];
global float $matColor[];
$list_of_names = {"color1","color2"};
$matColor = { 1.0,0.355,0.5,0.545,0.5,1.0};


global proc create() {
global string $list_of_names[];
global float $matColor[];

   for ($i=0; $i<`size $list_of_names`; ++$i){
        shadingNode -asShader VRayMtl -n $list_of_names;
        setAttr ($list_of_names[$i] + ".color") -type double3 $matColor[($i*3)] $matColor[($i*3)+1] $matColor[($i*3)+2];                   
    } 

}


window -width 150;
columnLayout -adjustableColumn true;

for ($i=0; $i<`size $list_of_names`; ++$i){
    button -label $list_of_names[$i] -command "create()";
}

showWindow;

回答1:


Based on the comments on your question, this should be what you are looking for:

import functools

import maya.cmds

buttons = {
    'color1': (1.0, 0.0, 0.0),
    'color2': (0.0, 1.0, 0.0),
}

def button_callback(shader_color):
    print shader_color
    maya.cmds.shadingNode(...)
    maya.cmds.setAttr(...)


w = maya.cmds.window(width=150)
maya.cmds.columnLayout( adjustableColumn=True)

for btn in buttons:
    maya.cmds.button(
        label=btn,
        command=functools.partial(button_callback, buttons[btn])
    )

maya.cmds.showWindow()

Basically this code does the same thing you are doing in MEL, but using python "structure" (because python is an indipendent language from Maya, has some libraries like functools, that helps you doing more complex things that the one that MEL lets you do).

To understand more about it, just look into google for python 2.7.10+ documentation (that is the one used from the last version of Maya).

Also Maya has some documentation about the python function you can use, instead of the MEL ones.



来源:https://stackoverflow.com/questions/46530691/link-procedure-to-each-button-command-maya

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!