在Maya python中将文件应用为纹理

时间:2014-01-25 00:29:27

标签: python maya

我正在尝试获取我之前在代码中添加的文件纹理,并将其添加到maya中的选定网格中。我对python和脚本编写起来很陌生,现在我已经苦苦挣扎了几天,所以我的代码可能很乱。

import maya.cmds as cmds
from os import listdir

class TextureImport():
    def __init__(self):
        if cmds.window(TextureImport, q=True, exists=True):
            cmds.deleteUI(TextureImport)
        GUI=cmds.window(title="Texture Import Tool", widthHeight=(250,160), s=True, tlb=True)
        cmds.rowColumnLayout(numberOfColumns=1, columnAlign=(1, 'center'), columnAttach=(1, 'both', 0), cw=(1,250))
        cmds.button(label="Select Directory", command=self.select_dir)
        cmds.separator(style='in', h=20)
        cmds.optionMenu('optionMenu', label="File List")
        cmds.button(label="Clear List", command=self.clear_list)
        cmds.separator(style='in', h=20)
        cmds.text('Select your object, then:', h=25)
        cmds.button(label="Apply Texture", command=self.apply_texture)
        cmds.setParent('..')
        cmds.showWindow()

    def select_dir(self, *args):
        basicFilter = "Image Files (*.jpg *.jpeg *.tga *.png *.tiff *.bmp *.psd)"
        self.myDir = cmds.fileDialog2 (fileFilter=basicFilter, dialogStyle=2, fm=3)
        myFiles = listdir(self.myDir[0])

        for items in myFiles:
            fileEndings = ('.psd','.PSD','.jpg','JPG','.jpeg','.JPEG','.tga','.TGA','.png','.PNG','.tiff','.TIFF','.bmp','.BMP')
            if items.endswith(fileEndings):
                cmds.menuItem(items)
            else:
                cmds.warning(items + 'This is not a valid image type, you fool.')
        print myFiles

    def clear_list(self, *args):
        fileList = cmds.optionMenu('optionMenu', q=True, itemListLong=True)
        if fileList:
            cmds.deleteUI(fileList)

    def apply_texture(self, *args):
        object = cmds.ls(sl=True)
        selectedMenuItem = cmds.optionMenu('optionMenu', q=True, value=True)
        cmds.sets(name='imageMaterialGroup', renderable=True, empty=True)
        shaderNode = cmds.shadingNode('phong', name='shaderNode', asShader=True)
        fileNode = cmds.shadingNode('file', name='fileTexture', asTexture=True)
        cmds.setAttr('fileTexture'+'.fileTextureName', self.myDir[0]+'/'+selectedMenuItem, type="string")
        shadingGroup = cmds.sets(name='textureMaterialGroup', renderable=True, empty=True)
        cmds.connectAttr('shaderNode'+'.outColor','textureMaterialGroup'+'.surfaceShader')
        cmds.connectAttr('fileTexture'+'.outColor','shaderNode'+'.color')
        cmds.surfaceShaderList('shaderNode', add='imageMaterialGroup')
        cmds.sets(object, e=True, forceElement='imageMaterialGroup')
TextureImport()

当我尝试使用最后一个按钮时,问题就出现了。它抛出错误:

错误:'textureMaterialGroup.surfaceShader'已经有来自'lambert1.outColor'的传入连接。 Traceback(最近一次调用最后一次): 在apply_texture中输入第52行文件“” RuntimeError:'textureMaterialGroup.surfaceShader'已经有来自'lambert1.outColor'的传入连接。

我不确定朗伯与它有什么关系,因为它根本不考虑我的代码。有什么想法吗?

1 个答案:

答案 0 :(得分:5)

你已经有了连接,所以如果要强制连接,你需要像这样添加force参数:

cmds.connectAttr('shaderNode'+'.outColor','textureMaterialGroup'+'.surfaceShader', force=True)
cmds.connectAttr('fileTexture'+'.outColor','shaderNode'+'.color', force=True)

来自connectAttr docsforce

  

强制连接。如果目的地已经连接,则   旧连接断了,新连接坏了。