Exemplo n.º 1
0
def exportMayaFiles(assetPath, force=False):
    """Exports selection to the ".ma" and ".fbx" files
    """
    asciiPath = "%s.ma" % assetPath
    sys.stdout.write('Exporting "%s"...\n' % asciiPath)
    try:
        exportedFile = cmds.file(asciiPath, type="mayaAscii", exportSelected=True, force=force)
    except:
        sys.stdout.write("Export cancelled.\n")
        return False

    # Remove unused requires from ".ma" file
    ddRemoveRequires.do(path=asciiPath)

    # Export fbx
    sys.stdout.write('Exporting "%s.fbx"...\n' % assetPath)
    exported = False

    try:
        sys.stdout.write("Exporting fbx file (1-FBXExport).\n")
        mel.eval('FBXExport -f "%s.fbx" -s' % assetPath.replace(os.sep, "/"))
        exported = True
    except:
        pass

    if not exported:
        try:
            sys.stdout.write("Exporting fbx file (2-FBX export).\n")
            cmds.file("%s.fbx" % assetPath, type="FBX export", exportSelected=True, force=force)
            exported = True
        except:
            pass

    if not exported:
        try:
            sys.stdout.write("Exporting fbx file (3-Fbx).\n")
            cmds.file("%s.fbx" % assetPath, type="Fbx", exportSelected=True, force=force)
            exported = True
        except:
            pass

    if not exported:
        confirm = cmds.confirmDialog(
            title="Warning",
            messageAlign="center",
            message='Fbx "%s" was not exported.' % assetPath,
            button=["Ok"],
            defaultButton="Ok",
            cancelButton="Ok",
            dismissString="Ok",
        )

    # Remove stray ".mayaSwatches" and "Keyboard" directories
    dirPath = asciiPath.rpartition(os.sep)[0]
    swatchPath = os.path.join(dirPath, ".mayaSwatches")
    keyboardPath = os.path.join(dirPath, "Keyboard")

    if os.path.isdir(swatchPath):
        try:
            swatchList = os.listdir(swatchPath)
            for swatch in swatchList:
                os.remove(os.path.join(swatchPath, swatch))
            os.rmdir(swatchPath)
            os.rmdir(keyboardPath)
        except:
            pass

    return True
Exemplo n.º 2
0
def do(nodes=None):
    """
    Publish shader attached to nodes.
    @param nodes: One or more GEO or shader nodes.
    """
    currentShaderLibrary = getAssetShaderLibrary()

    # Swatches file storage location.
    if not os.path.isdir(currentShaderLibrary):
        raise Exception("Directory %s does not exist" % currentShaderLibrary)

    if not nodes:
        nodes = cmds.ls(selection=True, long=True)
    # If still no nodes...
    if not nodes:
        sys.stdout.write("Select a mesh with an attached shader to be published.\n")

    if not isinstance(nodes, list):
        nodes = [nodes]

    for node in nodes:
        currentNode = node

        # Check if referenced.
        if cmds.referenceQuery(node, isNodeReferenced=True):
            confirm = cmds.confirmDialog(
                title="Warning",
                messageAlign="center",
                message="Cannot publish a referenced shader. Try importing from reference.",
                button=["Ok"],
                defaultButton="Ok",
                cancelButton="Ok",
                dismissString="Ok",
            )
            if confirm == "Ok":
                return

        # If node is not a mesh, look for a shading engine.
        if not cmds.listRelatives(currentNode, shapes=True):
            # Check if node is a GRP with one mesh below.
            children = cmds.listRelatives(currentNode, children=True)
            if children:
                if len(children) == 1 and cmds.listRelatives(children[0], shapes=True):
                    currentNode = children[0]
                else:
                    confirm = cmds.confirmDialog(
                        title="Warning",
                        messageAlign="center",
                        message="Unable to determine which shader to publish.",
                        button=["Ok"],
                        defaultButton="Ok",
                        cancelButton="Ok",
                        dismissString="Ok",
                    )
                    if confirm == "Ok":
                        return
            else:
                # Look for a connected shading engine.
                shadingEngines = [
                    x for x in (cmds.listHistory(currentNode, future=True) or []) if cmds.nodeType(x) == "shadingEngine"
                ]
                if shadingEngines:
                    currentNode = shadingEngines[0]
                else:
                    confirm = cmds.confirmDialog(
                        title="Warning",
                        messageAlign="center",
                        message="Unable to find a shader to publish.",
                        button=["Ok"],
                        defaultButton="Ok",
                        cancelButton="Ok",
                        dismissString="Ok",
                    )
                    if confirm == "Ok":
                        return

        # Get the shader category.
        directory = getCategoryFolder()
        shadingEngine = doNameShaders(node=currentNode, directory=directory)
        if not shadingEngine:
            continue

        rootName = shadingEngine.replace("_SG", "")

        # Create swatch.
        swatch = cmds.polyPlane(name="%s_Swatch" % rootName, width=1, height=1, sx=1, sy=1, ax=[0, 0, 1], ch=0)
        swatchShape = cmds.listRelatives(swatch, shapes=True, path=True)
        cmds.sets(swatchShape, forceElement=shadingEngine)

        imagePath = os.path.join(directory, "%s.png" % rootName)

        # Create snapshot.
        screenGrabCam = cmds.camera(centerOfInterest=378.926)
        cmds.setAttr("%s.tz" % screenGrabCam[0], 378.926)
        cmds.setAttr("%s.orthographic" % screenGrabCam[1], 1)

        cmds.select(swatch, replace=True)

        currentPanel = "modelPanel4"
        if not cmds.modelEditor(currentPanel, exists=True):
            currentPanel = cmds.getPanel(withFocus=True)

        mel.eval("enableIsolateSelect %s true;" % currentPanel)
        cmds.isolateSelect(currentPanel, state=1)
        mel.eval("lookThroughModelPanel %s %s" % (screenGrabCam[0], currentPanel))

        cmds.viewFit()
        cmds.setAttr("%s.orthographicWidth" % screenGrabCam[1], 1)
        cmds.select(clear=True)
        gridValue = cmds.modelEditor(currentPanel, query=True, grid=True)
        cmds.modelEditor(
            currentPanel,
            edit=True,
            displayAppearance="smoothShaded",
            displayTextures=True,
            displayLights="default",
            grid=False,
        )
        cmds.playblast(
            frame=1,
            format="image",
            completeFilename=imagePath,
            clearCache=True,
            viewer=False,
            showOrnaments=False,
            compression="png",
            quality=40,
            percent=100,
            widthHeight=[144, 144],
        )
        cmds.isolateSelect(currentPanel, state=0)
        cmds.modelEditor(currentPanel, edit=True, grid=gridValue)
        mel.eval("lookThroughModelPanel persp %s" % currentPanel)
        cmds.delete(screenGrabCam[0])
        sys.stdout.write("Screen grab of swatch saved to: %s\n" % imagePath)

        # Delete unknown nodes (mental ray).
        ddDeleteUnknownNodes.do()

        # Export swatch file.
        swatchPath = os.path.join(directory, "%s.ma" % rootName)
        cmds.select(swatch, replace=True)
        exportedFile = cmds.file(swatchPath, type="mayaAscii", exportSelected=True, force=True)
        ddRemoveRequires.do(path=swatchPath)

        if exportedFile:
            cmds.delete(swatch)