Esempio n. 1
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);
        }
Esempio n. 2
0
        public static void ConvertToRSMaterial(MFnDependencyNode matNode, bool deleteOrigin)
        {
            //replace output to shadingEngine
            MPlug      plug_matColorOutput     = matNode.findPlug(ConstantValue.plugName_matColorOutput);
            MPlugArray plugArr_matColorOutDest = new MPlugArray();

            plug_matColorOutput.destinations(plugArr_matColorOutDest);
            //get textures
            MPlug plug_matColorInput   = matNode.findPlug(ConstantValue.plugName_matColorInput);
            MPlug plug_matTransparency = matNode.findPlug(ConstantValue.plugName_matTransparency);

            MFnDependencyNode rsArchiNode = CreateShadingNode(ShadingNodeType.Shader, ConstantValue.nodeName_RS_Architectural);

            if (matNode.name.StartsWith("mat_"))
            {
                rsArchiNode.setName("rs" + matNode.name);
            }
            else
            {
                rsArchiNode.setName("rsmat_" + matNode.name);
            }
            MPlug       plug_rsArchiDiffuse     = rsArchiNode.findPlug(ConstantValue.plugName_RS_diffuse);
            MPlug       plug_rsArchiTransColor  = rsArchiNode.findPlug(ConstantValue.plugName_RS_transColor);
            MPlug       plug_rsArchiTransWeight = rsArchiNode.findPlug(ConstantValue.plugName_RS_transWeight);
            MPlug       plug_rsArchiOutColor    = rsArchiNode.findPlug(ConstantValue.plugName_RS_outColor);
            MDGModifier dGModifier = new MDGModifier();

            for (int i = 0; i < plugArr_matColorOutDest.length; i++)
            {
                dGModifier.disconnect(plug_matColorOutput, plugArr_matColorOutDest[i]);
                dGModifier.connect(plug_rsArchiOutColor, plugArr_matColorOutDest[i]);
            }

            CopyShaderParam(plug_matColorInput, plug_rsArchiDiffuse);
            //if (plug_matColorInput.source == null)
            //{
            //    plug_rsArchiDiffuse.child(0).setFloat(plug_matColorInput.child(0).asFloat());
            //    plug_rsArchiDiffuse.child(1).setFloat(plug_matColorInput.child(1).asFloat());
            //    plug_rsArchiDiffuse.child(2).setFloat(plug_matColorInput.child(2).asFloat());
            //}
            //else
            //{
            //    dGModifier.connect(plug_matColorInput.source, plug_rsArchiDiffuse);
            //}

            CopyShaderParam(plug_matTransparency, plug_rsArchiTransColor);
            if (plug_matTransparency.child(0).asFloat() == 0 && plug_matTransparency.child(1).asFloat() == 0 && plug_matTransparency.child(2).asFloat() == 0)
            {
                plug_rsArchiTransWeight.setFloat(1);
            }
            else
            {
                plug_rsArchiTransWeight.setFloat(0);
            }
            //if (plug_matTransparency.source == null)
            //{
            //    //plug_rsArchiTransColor.setValue(plug_matColorInput.asMObject());
            //    float matTransparency = plug_matTransparency.asFloat();
            //    if (matTransparency == 0)
            //    {
            //        plug_rsArchiTransWeight.setFloat(0);
            //        plug_rsArchiTransColor.child(0).setFloat(0);
            //        plug_rsArchiTransColor.child(1).setFloat(0);
            //        plug_rsArchiTransColor.child(2).setFloat(0);
            //    }
            //    else
            //    {
            //        plug_rsArchiTransWeight.setFloat(1);
            //        plug_rsArchiTransColor.child(0).setFloat(plug_matTransparency.child(0).asFloat());
            //        plug_rsArchiTransColor.child(1).setFloat(plug_matTransparency.child(1).asFloat());
            //        plug_rsArchiTransColor.child(2).setFloat(plug_matTransparency.child(2).asFloat());
            //    }
            //}
            //else
            //{
            //    dGModifier.connect(plug_matTransparency.source, plug_rsArchiTransColor);
            //    plug_rsArchiTransWeight.setFloat(1);
            //}
            if (deleteOrigin)
            {
                dGModifier.deleteNode(matNode.objectProperty);
            }
            dGModifier.doIt();
        }
Esempio n. 3
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);
        }