예제 #1
0
def doRemoveNamespaces(arg=None):
    '''Button: Remove Namespaces.
    '''
    sys.stdout.write("Removing namespaces... \n")
    cmds.undoInfo(openChunk=True)
    nodes = getSelectionList()
    if nodes:
        ddRemoveNamespaces.do(nodes)
    cmds.undoInfo(closeChunk=True)
예제 #2
0
def doImportAsset(arg=None):
    '''
    Button: Import Asset. 
    Imports the asset using namespaces, then removes the namespaces.
    '''
    currentImageLibrary = getImageLibrary()
    currentAssetLibrary = getAssetLibrary()
    chesspieceTypes = ["CPF", "CPO", "CPD"]

    cmds.namespace(setNamespace=":")
    directory = getCategoryFolder()
    if not directory:
        return
    
    directory = directory.replace(currentImageLibrary, currentAssetLibrary)
    selected = cmds.textField("currentAssetTFD", query=True, text=True)
    if not selected:
        sys.stdout.write("Select an asset.\n")
        return
    
    assetDirectory = selected.partition("_")[0]
    filename = os.path.join(directory, assetDirectory, "%s.ma" % selected)
    if currentAssetLibrary == ddConstants.CHAR_ASSETLIBRARY:
        # due to character naming 'CHAR_character',
        #   assetDirectory needs to be more accurate
        char_patt = re.compile("^([A-Z]{3}_[a-zA-Z]+)\w*$")
        if char_patt.match(selected):
            assetDirectory = char_patt.search(selected).groups()[0]
        filename = os.path.join(directory, assetDirectory, "chesspiece", "published", "%s.ma" % selected)
    if not os.path.isfile(filename):
        sys.stdout.write("File not found: %s.\n" % filename)
        return
        
    importedNodes = cmds.file(filename, i=True, namespace=selected, returnNewNodes=True)
    
    topNode = [x for x in importedNodes if cmds.nodeType(x) == "transform" and not cmds.listRelatives(x, shapes=True)]
    if currentAssetLibrary == ddConstants.CHAR_ASSETLIBRARY:
        topNode = [x for x in importedNodes if cmds.nodeType(x) == "transform"]
    if topNode:
        topNode = ddRemoveNamespaces.do(topNode[0])
        ddRemoveDuplicateShaders.do(topNode)
    deselectAllAssets()
예제 #3
0
def do(node=None, override=False, publish=False):
    """Checks if textures files are saved in assetLibrary and file format is ".tif".
       Names texture files to match GEO.
    """
    if not node:
        selection = cmds.ls(selection=True, long=True)
        if selection:
            node = selection[0]
        else:
            return False, override

    if publish:
        invalidNode = ddCheckNames.do(node)
        if invalidNode:
            sys.stdout.write(
                "Cannot check textures. Names are not valid for node %s. Skipping... \n" % node.rpartition("|")[2]
            )
            return False, override

    if cmds.referenceQuery(node, isNodeReferenced=True):
        sys.stdout.write("--> Unable to check textures of referenced node %s. Skipping... \n" % node.rpartition("|")[2])
        return False, override

    currentNode = node
    if publish:
        newNode = ddRemoveNamespaces.do(node)
        if newNode:
            currentNode = newNode[0]

    # Texture file storage location
    textureDir = os.path.join(ddConstants.ASSETLIBRARY, "tex", "tif")
    textureDir = os.path.normpath(textureDir)
    if not os.path.isdir(textureDir):
        raise Exception("Directory %s does not exist" % textureDir)

    # Get list of child mesh nodes under GRP node
    meshList = cmds.listRelatives(currentNode, path=True, allDescendents=True, type="mesh") or []
    version = currentNode.partition("_GRP_")[2].partition("_")[0]
    overwriteOverride = False
    skipCopy = False
    skipOverwrite = False
    publishedOverride = False

    allShadingEngines = collect_shading_engines(meshList)

    for shadingEngine in allShadingEngines:
        cnxList = [x for x in (cmds.listConnections(shadingEngine) or []) if cmds.nodeType(x) == "transform"]
        cnxList = list(set(cnxList))
        cnxList.sort()
        rootName = cnxList[0].rpartition("|")[2].rpartition("_")[0]
        currentShadingEngine = shadingEngine
        connectedMeshes = [
            x for x in (cmds.listConnections(currentShadingEngine) or []) if cmds.listRelatives(x, shapes=True)
        ]
        connectedMeshes = list(set(connectedMeshes))
        if publish:
            if len(connectedMeshes) > 1:
                if rootName:
                    rootName = rootName.replace("_GEO", "")
                    while ord(rootName[-1]) in range(49, 58):
                        rootName = rootName[:-1]
                    while ord(rootName[-1]) in range(65, 91):
                        rootName = rootName[:-1]
                    rootName = "%s_GEO" % rootName

        # Find connected file nodes
        historyList = cmds.listHistory(currentShadingEngine) or []
        fileNodes = [x for x in historyList if cmds.nodeType(x) == "file"] or []
        for fileNode in fileNodes:
            fileTextureName = cmds.getAttr("%s.fileTextureName" % fileNode)
            newFileName = ""
            newFilePath = ""

            # Determine shader attribute file node is connected to
            surfaceShader = cmds.listConnections("%s.surfaceShader" % currentShadingEngine)
            if surfaceShader:
                surfaceShader = surfaceShader[0]
                cnxList = cmds.listConnections(fileNode, source=False, destination=True, plugs=True) or []
                fileNodeCnx = [x for x in cnxList if surfaceShader in x or "bumpValue" in x]
                if fileNodeCnx:
                    for nodeCnx in fileNodeCnx:
                        attr = nodeCnx.partition(".")[2]
                        if attr in ddConstants.textureTypes.keys():
                            if not validateTextureFile(fileNode, fileTextureName, publish):
                                return False, override

                            # make sure the color adjustments have not been made
                            #   to file node, these should be done to
                            #   the file itself
                            # if file_color_changed(fileNode):
                            #     return False, override

                            # Create texture name to match GEO
                            newFileName = "%s_%s.tif" % (
                                rootName.replace("GEO", ddConstants.textureTypes[attr]),
                                version,
                            )
                            newFilePath = os.path.join(textureDir, newFileName)

                            if publish:
                                fileNode = cmds.rename(
                                    fileNode, "%s_FIL" % rootName.replace("GEO", ddConstants.textureTypes[attr])
                                )
                                if "bump" in nodeCnx:
                                    cmds.rename(
                                        nodeCnx.partition(".")[0],
                                        "%s_BMP" % rootName.replace("GEO", ddConstants.textureTypes[attr]),
                                    )
                                placeTextureNode = cmds.listConnections(fileNode, source=True, destination=False)
                                if placeTextureNode:
                                    cmds.rename(
                                        placeTextureNode[0].partition(".")[0],
                                        "%s_PTN" % rootName.replace("GEO", ddConstants.textureTypes[attr]),
                                    )

                else:
                    sys.stdout.write(
                        "--- Skipping %s. Not connected to color, specularColor or normalCamera. \n" % fileTextureName
                    )

                if publish:
                    # Rename nodes
                    currentShadingEngine = cmds.rename(currentShadingEngine, rootName.replace("GEO", "SG"))
                    surfaceShader = cmds.rename(surfaceShader, rootName.replace("GEO", "SHD"))

                    # Remove stray namespaces
                    surfaceShaderCnxList = cmds.listConnections(surfaceShader) or []
                    for shaderCnx in surfaceShaderCnxList:
                        if ":" in shaderCnx:
                            shaderNamespace = shaderCnx.partition(":")[0]
                            try:
                                cmds.select(shaderCnx, replace=True)
                                cmds.namespace(mv=(shaderNamespace, ":"), force=True)
                            except:
                                pass

            # Check if file saved in assetLibrary
            if not newFilePath == "" and publish:
                confirm = "Copy to AssetLibrary"
                confirmOverwrite = "Overwrite Existing"

                # New section
                if fileTextureName.replace("/", os.sep).startswith(ddConstants.ASSETLIBRARY):
                    if not publishedOverride:
                        confirmOverwrite = cmds.confirmDialog(
                            title="Warning",
                            messageAlign="center",
                            message='Texture file "%s" already exists in the assetLibrary. ' % fileTextureName,
                            button=["Ok", "Ok to All", "Cancel"],
                            defaultButton="Skip",
                            cancelButton="Cancel",
                            dismissString="Cancel",
                        )
                        if confirmOverwrite == "Cancel":
                            return False, override
                        elif confirmOverwrite == "Ok to All":
                            publishedOverride = True
                            skipOverwrite = True
                    continue
                # End of new section

                if os.path.isfile(newFilePath):
                    if not overwriteOverride:
                        confirmOverwrite = cmds.confirmDialog(
                            title="Warning",
                            messageAlign="center",
                            message='Texture file "%s" already exists in the assetLibrary. ' % newFileName,
                            button=["Overwrite Existing", "Overwrite All", "Skip", "Skip All", "Cancel"],
                            defaultButton="Overwrite Existing",
                            cancelButton="Cancel",
                            dismissString="Cancel",
                        )
                        if confirmOverwrite == "Cancel":
                            return False, override
                        elif confirmOverwrite == "Overwrite All":
                            overwriteOverride = True
                        elif confirmOverwrite == "Skip All":
                            overwriteOverride = True
                            skipOverwrite = True
                elif not override:
                    confirm = cmds.confirmDialog(
                        title="Warning",
                        messageAlign="center",
                        message='Texture file "%s" is not in the assetLibrary. ' % newFileName,
                        button=["Copy to AssetLibrary", "Copy all to AssetLibrary", "Skip", "Skip All", "Cancel"],
                        defaultButton="Copy to assetLibrary",
                        cancelButton="Cancel",
                        dismissString="Cancel",
                    )

                if confirm == "Cancel":
                    return False, override  # Prevent asset export
                elif confirm == "Copy all to AssetLibrary":
                    override = True
                elif confirm == "Skip All":
                    override = True
                    skipCopy = True

                if not skipOverwrite and not skipCopy:
                    if confirm == "Copy to AssetLibrary" or confirm == "Copy all to AssetLibrary":
                        if os.path.isfile(fileTextureName):
                            if not fileTextureName.replace("/", os.sep) == newFilePath:
                                shutil.copy(fileTextureName, newFilePath)
                                cmds.setAttr("%s.fileTextureName" % fileNode, newFilePath, type="string")
                        else:
                            confirm = cmds.confirmDialog(
                                title="Warning",
                                messageAlign="center",
                                message='Original texture file "%s" does not exist. ' % fileTextureName,
                                button=["Continue", "Cancel"],
                                defaultButton="Continue",
                                cancelButton="Cancel",
                                dismissString="Cancel",
                            )
                            if confirm == "Cancel":
                                return False, override  # Prevent asset export

    # Allow asset export
    return True, override