Maya Python中应用文件作为纹理

2024-10-01 13:40:50 发布

您现在位置:Python中文网/ 问答频道 /正文

我正在尝试获取我先前在代码中添加的文件纹理,并将其添加到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”的传入连接。 回溯(最近一次呼叫): “文件”,第52行,在“应用纹理”中 运行时错误:'textureMaterialGroup.surfaceShader“”已经有来自“lambert1.outColor”的传入连接。在

我不确定lambert和它有什么关系,因为它根本没有考虑到我的代码中。有什么想法吗?在


Tags: 代码nameselftrueifdefitemsbutton
1条回答
网友
1楼 · 发布于 2024-10-01 13:40:50

您已经有一个连接,所以如果您想强制它连接,您需要添加force参数,如下所示:

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

connectAttr docsforce

Forces the connection. If the destination is already connected, the old connection is broken and the new one made.

相关问题 更多 >