Beispiel #1
0
        public static string RenameTexFile(MFnDependencyNode imageNode, string newPartialName, string newFolder = null, bool relinkImgNode = false, bool deleteOrigin = false, bool overwrite = false)
        {
            MPlug  plug_fileTexPath = imageNode.findPlug(ConstantValue.plugName_fileTexPath);
            string originFullPath   = plug_fileTexPath.asString();
            string newFullPath      = BasicFunc.RenameFile(originFullPath, newPartialName, newFolder, deleteOrigin, overwrite);

            if (relinkImgNode)
            {
                plug_fileTexPath.setString(newFullPath);
            }
            return(newFullPath);
        }
Beispiel #2
0
 public static void ChangeTexturesPrefix(MSelectionList list, string newFolderPath)
 {
     if (list == null)
     {
         Debug.Log("list null");
         return;
     }
     for (int i = 0; i < list.length; i++)
     {
         MObject mo = new MObject();
         list.getDependNode((uint)i, mo);
         MFnDependencyNode imageNode = new MFnDependencyNode(mo);
         MPlug             plug      = imageNode.findPlug(ConstantValue.plugName_fileTexPath);
         string            filePath  = plug.asString();
         Debug.Log("filePath:" + filePath);
         string fileName = Path.GetFileName(filePath);
         plug.setString(Path.Combine(newFolderPath, fileName));
     }
 }
Beispiel #3
0
 public static void RenameTextures(MSelectionList list)
 {
     if (list == null)
     {
         Debug.Log("list null");
         return;
     }
     for (int i = 0; i < list.length; i++)
     {
         MObject mo = new MObject();
         list.getDependNode((uint)i, mo);
         MFnDependencyNode imageNode = new MFnDependencyNode(mo);
         MPlug             plug      = imageNode.findPlug(ConstantValue.plugName_fileTexPath);
         string            filePath  = plug.asString();
         Debug.Log("filePath:" + filePath);
         string fileName = BasicFunc.GetFileName(filePath);
         Debug.Log("fileName:" + fileName);
         imageNode.setName(fileName);
     }
 }
        // -------------------------
        // --------- Utils ---------
        // -------------------------

        private string getSourcePathFromFileTexture(MFnDependencyNode textureDependencyNode)
        {
            MObject sourceObject = textureDependencyNode.objectProperty;

            // Retreive texture file path
            if (!sourceObject.hasFn(MFn.Type.kFileTexture))
            {
                RaiseError("Only file texture is supported.", logRankTexture + 1);
                return(null);
            }
            MPlug fileTextureNamePlug = textureDependencyNode.findPlug("fileTextureName");

            if (fileTextureNamePlug == null || fileTextureNamePlug.isNull)
            {
                RaiseError("Texture path is missing.", logRankTexture + 1);
                return(null);
            }
            string sourcePath = fileTextureNamePlug.asString();

            return(sourcePath);
        }
Beispiel #5
0
        public static MFnDependencyNode CombineToUDIM(List <MFnDependencyNode> imageNodes, string prename, string newFolder = "UDIM", int maxUCount = 5)
        {
            if (imageNodes.Count < 2)
            {
                return(null);
            }
            MFnDependencyNode udimImgNode = CreateShadingNode(ShadingNodeType.Texture, ConstantValue.nodeName_fileTex);

            //MFnDependencyNode udimImgNode = new MFnDependencyNode();
            //udimImgNode.create("file");

            MPlug texOutColorPlug        = udimImgNode.findPlug(ConstantValue.plugName_fileTexOutputColor);
            MPlug texOutTransparencyPlug = udimImgNode.findPlug(ConstantValue.plugName_fileTexOutputTransparency);

            udimImgNode.findPlug(ConstantValue.plugName_fileTexUVTilingMode).setInt((int)ConstantValue.UVTilingMode.UDIM);


            MDGModifier dGModifier = new MDGModifier();

            for (int i = 0; i < imageNodes.Count; i++)
            {
                MPlug  plug_fileTexPath = imageNodes[i].findPlug(ConstantValue.plugName_fileTexPath);
                string originFullPath   = plug_fileTexPath.asString();

                int    uIndex       = i % maxUCount;
                int    vIndex       = i / maxUCount;
                string mariIndexStr = string.Format("{0}.10{1}{2}", prename, vIndex, uIndex + 1);
                string newFullPath  = RenameTexFile(imageNodes[i], mariIndexStr, newFolder);
                if (i == 0)
                {
                    udimImgNode.findPlug(ConstantValue.plugName_fileTexPath).setString(newFullPath);
                }

                //move uv and reconnect link
                MPlugArray     plugArr_transparencyDest, plugArr_colorDest;
                MSelectionList matList = GetMaterialsWithTex(imageNodes[i], out plugArr_colorDest, out plugArr_transparencyDest);
                //move uv for every material
                for (int j = 0; j < matList.length; j++)
                {
                    MObject matObj = new MObject();
                    matList.getDependNode((uint)j, matObj);
                    MFnDependencyNode matNode = new MFnDependencyNode(matObj);
                    string            matName = matNode.absoluteName;
                    //Debug.Log("move uv for mat:" + matName);
                    MoveUV(uIndex, vIndex, matName);
                    //MPlug plug_matColorInput = matNode.findPlug(ConstantValue.plugName_matColorInput);
                    //if (plug_matColorInput != null)
                    //{
                    //    dGModifier.disconnect(plug_matColorInput.source, plug_matColorInput);
                    //    dGModifier.connect(texOutColorPlug, plug_matColorInput);
                    //    dGModifier.doIt();
                    //}
                }
                //reconnect alpha
                for (int j = 0; j < plugArr_transparencyDest.length; j++)
                {
                    dGModifier.disconnect(plugArr_transparencyDest[j].source, plugArr_transparencyDest[j]);
                    dGModifier.connect(texOutTransparencyPlug, plugArr_transparencyDest[j]);
                }
                //reconnect color
                for (int j = 0; j < plugArr_colorDest.length; j++)
                {
                    dGModifier.disconnect(plugArr_colorDest[j].source, plugArr_colorDest[j]);
                    dGModifier.connect(texOutColorPlug, plugArr_colorDest[j]);
                }
            }
            dGModifier.doIt();

            return(udimImgNode);
        }
Beispiel #6
0
        public static bool CombineSameTextures(MSelectionList list, bool deleteRepeated = true)
        {
            if (!BasicFunc.CheckSelectionList(list, 2))
            {
                Debug.Log("please choose at least 2 materials");
                return(false);
            }

            //string texFilePath = "";
            //List<string> texFilePaths = new List<string>();
            Dictionary <string, int> combineDic     = new Dictionary <string, int>();
            List <MPlug>             texOutputPlugs = new List <MPlug>();

            MDGModifier    dGModifier = new MDGModifier();
            List <MObject> deleteList = new List <MObject>();

            for (int i = 0; i < list.length; i++)
            {
                MObject texObject = new MObject();
                list.getDependNode((uint)i, texObject);
                //MImage img = new MImage();
                //img.readFromTextureNode(texObject, MImage.MPixelType.kUnknown);
                MFnDependencyNode texDN         = new MFnDependencyNode(texObject);
                MPlug             texPlug       = texDN.findPlug(ConstantValue.plugName_fileTexPath);
                MPlug             texOutputPlug = texDN.findPlug(ConstantValue.plugName_fileTexOutputColor);
                //Debug.Log("texplug name:" + texPlug.name);
                texOutputPlugs.Add(texOutputPlug);
                string filePath = texPlug.asString();
                //Debug.Log("path:" + filePath);
                if (combineDic.ContainsKey(filePath))
                {
                    //combine
                    int targetIndex = combineDic[filePath];
                    //Debug.Log("combine " + i + " to " + targetIndex);

                    MPlugArray destPlugs = new MPlugArray();
                    texOutputPlug.destinations(destPlugs);
                    for (int j = 0; j < destPlugs.Count; j++)
                    {
                        //Debug.Log("texPlugs[targetIndex]:" + texOutputPlugs[targetIndex].name + " , destPlugs[j]" + destPlugs[j].name);
                        dGModifier.disconnect(texOutputPlug, destPlugs[j]);
                        dGModifier.connect(texOutputPlugs[targetIndex], destPlugs[j]);
                    }
                    deleteList.Add(texObject);
                }
                else
                {
                    combineDic.Add(filePath, i);
                }
            }
            dGModifier.doIt();
            if (deleteRepeated)
            {
                for (int i = 0; i < deleteList.Count; i++)
                {
                    dGModifier.deleteNode(deleteList[i]);
                }
            }
            dGModifier.doIt();
            return(true);
        }