我一直在遵循有关脚本的数字教程的教程,在一些视频中,导师制作了一个工具,可以使用MEL将gamma校正节点添加到任何选定的着色器中,为了学习,我想尝试用Python重写代码,但我正努力将一段MEL代码转换为Python。在
到目前为止,我得到的代码是:
import maya.cmds as cmds
selMat = cmds.ls(sl=True, mat=True)
if len(selMat) < 1:
cmds.warning('Select at least one Maya or Mental Ray Shader to apply gamma correct node to.')
for mat in selMat:
gammaCorrect_util = cmds.shadingNode('gammaCorrect', asUtility=True)
rename_gamma = cmds.rename(gammaCorrect_util, ('gamma_' + mat))
cmds.setAttr((rename_gamma + '.gammaX'), 0.45)
cmds.setAttr((rename_gamma + '.gammaY'), 0.45)
cmds.setAttr((rename_gamma + '.gammaZ'), 0.45)
if cmds.attributeQuery('color', mat): # << error here
connection_to_mat = cmds.listConnections(mat + '.color')
if len(connection_to_mat) == 1:
cmds.connectAttr ((connection_to_mat + '.outColor'), (rename_gamma + '.value'), f=True)
cmds.connectAttr ((rename_gamma + '.outValue'), (mat + '.color'), f=True)
运行此程序时,我收到以下错误:
Error: Too many objects or values.Traceback (most recent call last): File "", line 17, in TypeError: Too many objects or values.
我认为问题所在的MEL代码是:
^{pr2}$我不知道如何转换和使用python中的“attributeQuery”命令来代替MEL中的“attributeExists”,导师还定义了前面的变量“$connection_to_mat[]”,但这在python中不起作用。在
attributeQuery
只接受一个未命名的参数,即属性。必须使用节点标志指定节点,与MEL版本相同。在listConnections返回一个数组。您需要检查是否有一些连接,如果有,请使用第一个连接:
connection_to_mat[0]
顺便说一句,如果您指定要插入,则不必将字符串与“.outColor”连接起来
^{pr2}$这是更好的,因为传入属性有可能以不同的名称命名,或者是一个复合的子级。示例:
someNode.colors.outColor1
。不管它是什么,你只要把它喂给connectAttr
。在相关问题 更多 >
编程相关推荐