需要帮助将着色器应用于场景中的选定对象(Maya)

时间:2019-05-01 05:24:58

标签: python maya

我正在根据文件选择创建着色器,并且需要将着色器应用于场景中选择的任何单个对象。我在下方显示的完整工具中对分组进行了注释。问题出在def apply_texture()

这是我的旧代码,我不希望它对所有内容进行分组,因为这会将纹理应用于所有选定对象。

def apply_texture(self, *args):
    object = cmds.ls(sl=True)
    selectedMenuItem = cmds.optionMenu('optionMenu', q=True, value=True)
    cmds.sets(name='iG', renderable=True, empty=True)
    shader=cmds.shadingNode("blinn", name='shaderNode', asShader=True)
    file_node=cmds.shadingNode("file",asTexture=True)
    cmds.setAttr('.fileTextureName', self.myDir[0]+'/'+selectedMenuItem, type="string")
    shading_group= cmds.sets(renderable=True,noSurfaceShader=True,empty=True)
    cmds.connectAttr('%s.outColor' %shader ,'%s.surfaceShader' %shading_group)
    cmds.connectAttr('%s.outColor' %file_node, '%s.color' %shader)
    cmds.surfaceShaderList(shader, add='iG')
    cmds.sets(object, e=True, forceElement='iG') 

新代码:

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='iG', renderable=True, empty=True)
        shader=cmds.shadingNode("blinn", name='shaderNode', asShader=True)
        file_node=cmds.shadingNode("file",asTexture=True)
        cmds.setAttr('.fileTextureName', self.myDir[0]+'/'+selectedMenuItem, type="string")
        shading_group= cmds.sets(renderable=True,noSurfaceShader=True,empty=True)
        cmds.connectAttr('%s.outColor' %shader ,'%s.surfaceShader' %shading_group)
        cmds.connectAttr('%s.outColor' %file_node, '%s.color' %shader)
        #cmds.surfaceShaderList(shader, add='iG')
        #cmds.sets(object, e=True, forceElement='iG')        
TextureImport()

我应该能够选择一个对象并将着色器应用于该对象,而无需将其他选定对象添加到组中。如果选择另一个对象,则应该能够对该对象应用其他着色器,而无需进行原始更改。

1 个答案:

答案 0 :(得分:0)

您尝试过做过吗:cmds.sets(* object,e = True,forceElement = shading_group)– DrWeeny在18个小时前就赞扬了这个人!