0

I am creating a shader from a file selection and need to apply the shader to any single object that I select in the scene. I commented out the grouping in the full tool shown down below. The problem is in the def apply_texture()

This was my old code, I do not want it to group everything because that will apply the texture to all the selected objects.:

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') 

NEW CODE:

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()

I should be able to select an object and have the shader apply to it without other selected objects being added to a group. If I select another object I should be able to apply a different shader to that object and not have the original change.

petezurich
  • 9,280
  • 9
  • 43
  • 57
Kudos
  • 3
  • 2

1 Answers1

0

have you tried to do : cmds.sets(*object , e=True, forceElement=shading_group) – DrWeeny 18 hours ago credits to this man!

Kudos
  • 3
  • 2