2016-11-02 86 views
1

我一直在關於數字導師腳本的教程,在一些視頻中,導師制作了一個工具,使用MEL爲任何選定的着色器添加伽馬校正節點,因爲我的學習我想我會嘗試重寫Python中的代碼,但我努力將一段MEL代碼轉換爲Python。梅爾到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代碼,我認爲這個問題是是:

if(`attributeExists "color" $mat`){ 
    string $connection_to_mat[] = `listConnections($mat + ".color")`; 
    if(size($connection_to_mat) == 1){ 
     connectAttr -f ($connection_to_mat[0] + ".outColor") ($rename_gamma + ".value"); 
     connectAttr -f ($rename_gamma + ".outValue") ($mat + ".color"); 

我不知道如何轉換和使用python中的「attributeQuery」命令來代替MEL中的「attributeExists」,T他的導師也定義了預置的變量「$ connection_to_mat []」,但這不適用於Python。

+0

你有原始碼,17號線上有什麼?它對我來說看起來像一條空白的線,所以發佈的代碼不完全是你所運行的,或者我錯誤的 - 但是當你已經知道它是哪一個,並且可以首先把它放在你的問題中。 – Useless

+0

嗨,對不起,隊友我是新來的,它沒有在論壇上正確發帖,在我的腳本編輯器中的第17行代碼是「如果cmds.attributeQuery('color',mat):」 –

+1

否問題,我剛剛編輯它到你的問題 – Useless

回答

2

attributeQuery只取一個未命名的參數,屬性。您必須指定具有節點標誌的節點,與MEL版本相同。

cmds.attributeQuery('color', n=mat, exists=True) 

listConnections返回一個數組。你需要檢查有一些連接,如果是使用第一個連接:connection_to_mat[0]

順便說一句,如果你指定你想要的插件,那麼你不會有「.outColor」

來連接字符串
cmds.listConnections(mat + '.color', p=True) 
// result ["someNode.outColor"] 

這比較好,因爲傳入屬性的命名可能不同,或者是化合物的子項。例如:someNode.colors.outColor1。不管它是什麼,你都可以餵它到connectAttr