Ejemplo n.º 1
0
        public static void RemoveUnusedTextures(MSelectionList list)
        {
            if (list == null)
            {
                Debug.Log("list null");
                return;
            }
            List <MObject> deleteList = new List <MObject>();

            for (int i = 0; i < list.length; i++)
            {
                MObject mo = new MObject();
                list.getDependNode((uint)i, mo);
                MFnDependencyNode imageNode     = new MFnDependencyNode(mo);
                MPlug             texOutputPlug = imageNode.findPlug(ConstantValue.plugName_fileTexOutputColor);
                MPlugArray        destPlugs     = new MPlugArray();
                texOutputPlug.destinations(destPlugs);
                if (destPlugs.Count == 0)
                {
                    deleteList.Add(mo);
                    Debug.Log("remove no use:" + imageNode.absoluteName);
                }
                else
                {
                    Debug.Log("still used:" + imageNode.absoluteName);
                    for (int j = 0; j < destPlugs.length; j++)
                    {
                        Debug.Log(" by:" + destPlugs[0].partialName(true));
                    }
                }
            }
            BasicFunc.DeleteObjects(deleteList);
        }
Ejemplo n.º 2
0
        public static MSelectionList GetMaterialsWithTex(MFnDependencyNode imageNode)
        {
            MPlug      plug      = imageNode.findPlug(ConstantValue.plugName_fileTexOutputColor);
            MPlugArray destPlugs = new MPlugArray();

            plug.destinations(destPlugs);
            //BasicFunc.PrintPlugs(destPlugs);

            MSelectionList newSelection = new MSelectionList();

            for (int i = 0; i < destPlugs.length; i++)
            {
                newSelection.add(destPlugs[i].node);
            }
            //BasicFunc.Select(newSelection);
            return(newSelection);
        }
Ejemplo n.º 3
0
        private MFnDependencyNode getBabylonMaterialNode(MFnDependencyNode materialDependencyNode)
        {
            // Retreive connection
            MPlug      connectionOutColor = materialDependencyNode.getConnection("outColor");
            MPlugArray destinations       = new MPlugArray();

            connectionOutColor.destinations(destinations);

            // Retreive all Babylon Attributes dependency nodes
            List <MFnDependencyNode> babylonAttributesNodes = new List <MFnDependencyNode>();

            foreach (MPlug destination in destinations)
            {
                MObject destinationObject = destination.node;

                if (destinationObject != null && destinationObject.hasFn(MFn.Type.kPluginHardwareShader))
                {
                    MFnDependencyNode babylonAttributesDependencyNode = new MFnDependencyNode(destinationObject);

                    if (babylonAttributesDependencyNode.typeId.id == babylonStandardMaterialNode.id ||
                        babylonAttributesDependencyNode.typeId.id == babylonStingrayPBSMaterialNode.id ||
                        babylonAttributesDependencyNode.typeId.id == babylonAiStandardSurfaceMaterialNode.id)
                    {
                        babylonAttributesNodes.Add(babylonAttributesDependencyNode);
                    }
                }
            }

            if (babylonAttributesNodes.Count == 0)
            {
                return(null);
            }
            else
            {
                // Occurs only if the user explicitly create a connection with several babylon attributes nodes.
                if (babylonAttributesNodes.Count > 1)
                {
                    RaiseWarning("Babylon attributes node attached to a material should be unique. The first one is going to be used.", 2);
                }

                // Return the first shading engine node
                return(babylonAttributesNodes[0]);
            }
        }
Ejemplo n.º 4
0
        public static void DeleteUnusedMats(MSelectionList list)
        {
            if (list == null)
            {
                Debug.Log("list null");
                return;
            }
            List <MFnDependencyNode> deleteList = new List <MFnDependencyNode>();

            for (int i = 0; i < list.length; i++)
            {
                MObject mo = new MObject();
                list.getDependNode((uint)i, mo);
                MFnDependencyNode matNode = new MFnDependencyNode(mo);
                MPlug             plug    = matNode.findPlug(ConstantValue.plugName_matColorOutput);
                MPlugArray        plugArr = new MPlugArray();
                plug.destinations(plugArr);
                if (plugArr.Count > 0)
                {
                    MFnDependencyNode sgNode = new MFnDependencyNode(plugArr[0].node);
                    Debug.Log(sgNode.name);



                    MPlug plug_dagSetMemebers = sgNode.findPlug(ConstantValue.plugName_dagSetMembers);
                    Debug.Log("numelements:" + plug_dagSetMemebers.numElements);
                    if (plug_dagSetMemebers.numElements == 0)
                    {
                        deleteList.Add(matNode);
                        deleteList.Add(sgNode);
                    }
                }
                else
                {
                    deleteList.Add(matNode);
                }
            }
            BasicFunc.DeleteObjects(deleteList);
        }
Ejemplo n.º 5
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();
        }
Ejemplo n.º 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);
        }