def doCheckTextures(arg=None): '''Button: Check Textures. ''' sys.stdout.write("Checking textures... \n") cmds.undoInfo(openChunk=True) currentAssetCategory = getAssetCategory() nodes = getSelectionList() for node in nodes: texturesPublished = False validTextures, override = ddCheckTextures.do(node=node, override=False, publish=False) if validTextures: sys.stdout.write("Textures are valid for %s.\n" % node.rpartition("|")[2]) texturesPublished = ddCheckTexturePublished.do(nodes=node, verbose=True, assetCategory=currentAssetCategory) if validTextures and texturesPublished: sys.stdout.write("Textures are valid and published for %s.\n" % node.rpartition("|")[2]) elif not validTextures: sys.stdout.write("Textures are not valid for %s.\n" % node.rpartition("|")[2]) cmds.undoInfo(closeChunk=True)
def doImportShaders(arg=None): ''' Button: Import Shader. Imports selected shader from library if does not already exist in file and assigns shader to selected objects. ''' selectedCategory = cmds.textScrollList("shaderCategoriesSL", query=True, selectItem=True) selection = cmds.ls(selection=True, long=True) or [] tempGrp = "tempImportedGrp" if cmds.objExists(tempGrp): cmds.delete(tempGrp) # Get selected swatch. swatch = cmds.textField("currentlySelectedTFD", query=True, text=True) if not swatch: sys.stdout.write("Select a shader\n") return # Get selected category. directory = getCategoryFolder() # Check if selected swatch shader already exists in file. shadingEngine = "%s_SG" % swatch shader = "%s_SHD" % swatch validTexture = False if cmds.objExists(shadingEngine): meshList = [x for x in (cmds.listHistory(shadingEngine) or []) if cmds.nodeType(x) == "mesh"] if meshList: meshTransform = cmds.listRelatives(meshList[0], path=True, parent=True)[0] validTexture = ddCheckTexturePublished.do(nodes=meshTransform, confirm=False, assetCategory=selectedCategory) if cmds.objExists(shadingEngine) and cmds.objExists(shader) and validTexture: if selection: # Apply existing shader to selection. sys.stdout.write('Shader "%s_SG" already exists. Applying to selection... \n' % swatch) for sel in selection: selShape = sel if not (cmds.nodeType(sel) == "mesh"): selShape = cmds.listRelatives(sel, shapes=True, path=True) if selShape: selShape = selShape[0] if selShape: cmds.sets(selShape, forceElement=shadingEngine) else: sys.stdout.write('Shader "%s_SG" already exists. Skipping... \n' % swatch) else: meshList = list() connectedSurfaceShader = None # If shader has been deleted from Hypershade, shading engine and other nodes must also be deleted. if cmds.objExists(shadingEngine): meshList = [x for x in (cmds.listHistory(shadingEngine) or []) if cmds.nodeType(x) == "mesh"] connectedSurfaceShader = cmds.listConnections("%s.surfaceShader" % shadingEngine) cmds.delete(shadingEngine) if cmds.objExists(shader): connectedShadingEngine = cmds.listConnections("%s.outColor" % shader) if connectedShadingEngine: meshList = [x for x in (cmds.listHistory(connectedShadingEngine[0]) or []) if cmds.nodeType(x) == "mesh"] cmds.delete(shader) if connectedSurfaceShader and cmds.objExists(connectedSurfaceShader[0]): cmds.delete(connectedSurfaceShader[0]) deleteNodes = cmds.ls("%s_*DIFF*" % swatch, "%s_*SPEC*" % swatch, "%s_*NRML*" % swatch) try: cmds.delete(deleteNodes) except: pass # Get the path to swatch file. fileName = os.path.join(directory, "%s.ma" % swatch) if os.path.isfile(fileName): # Import swatch file into tempGrp. cmds.file(fileName, i=True, groupReference=True, groupName=tempGrp) if cmds.objExists(tempGrp): # Remove any namespaces from imported nodes. tempGrp = ddRemoveNamespaces.doRemoveNamespaces(tempGrp) # Children consists of one swatch plane. children = cmds.listRelatives(tempGrp, children=True, path=True) if children: # Get the connected shading engine. shadingEngine = getConnectedShadingEngine(children[0]) meshList.extend(selection) if shadingEngine: # Assign shader to selected objects. for sel in meshList: selShape = sel if not (cmds.nodeType(sel) == "mesh"): selShape = cmds.listRelatives(sel, shapes=True, path=True) if selShape: selShape = selShape[0] if selShape: cmds.sets(selShape, forceElement=shadingEngine) else: sys.stdout.write("Swatch shader for %s not found. Skipping...\n" % swatch) # Change the button font to oblique for imported shader. try: cmds.iconTextCheckBox("%sBTN" % swatch, edit=True, font = "obliqueLabelFont") except: pass else: sys.stdout.write("Swatch mesh for %s not found. Skipping...\n" % swatch) cmds.delete(tempGrp) else: sys.stdout.write("Shader did not load properly. \n" % fileName) else: sys.stdout.write("Swatch file %s not found. Skipping... \n" % fileName) # Deselect the shader. deselectAll() # Reselect original selection. if selection: cmds.select(selection, replace=True)