Example #1
0
        public static bool ProjectPlug(MPlug from, MPlug to, float fromMin, float fromMax, float toMin, float toMax)
        {
            MFnDependencyNode remapValueNode = BasicFunc.CreateRemapValueNode(fromMin, fromMax, toMin, toMax);
            MDGModifier       dGModifier     = new MDGModifier();

            dGModifier.connect(from, remapValueNode.findPlug(ConstantValue.plugName_remapValueInput));
            dGModifier.connect(remapValueNode.findPlug(ConstantValue.plugName_remapValueOutput), to);
            dGModifier.doIt();
            return(true);
        }
Example #2
0
        public static MFnDependencyNode GetPlace2dTextureForTex(MFnDependencyNode imageNode, bool createIfNotExist = true)
        {
            if (imageNode == null)
            {
                Debug.Log("image Node null");
                return(null);
            }
            MPlug      uvPlug      = imageNode.findPlug(ConstantValue.plugName_texFileUVCoord);
            MPlugArray sourcePlugs = new MPlugArray();

            uvPlug.connectedTo(sourcePlugs, true, false);
            if (sourcePlugs.length == 0)
            {
                //no input
                if (createIfNotExist)
                {
                    MFnDependencyNode place2dTexNode = CreateShadingNode(ShadingNodeType.Utility, "place2dTexture");
                    MPlug             p2tUVOut       = place2dTexNode.findPlug(ConstantValue.plugName_place2dOutUV);
                    string            nodeName       = place2dTexNode.absoluteName;
                    MDGModifier       dgModifier     = new MDGModifier();
                    dgModifier.connect(p2tUVOut, uvPlug);
                    dgModifier.doIt();
                    return(place2dTexNode);
                }
                else
                {
                    return(null);
                }
            }
            else
            {
                return(new MFnDependencyNode(sourcePlugs[0].node));
            }
        }
Example #3
0
        public static bool BindBlendShapeCtl(MFnBlendShapeDeformer bs, MDagPath ctlDagPath = null)
        {
            //MFnDependencyNode ctlNode = new MFnDependencyNode(ctlDagPath.node);
            if (bs == null)
            {
                Debug.Log("null blendShape");
                return(false);
            }
            //Debug.Log("here i am");

            if (ctlDagPath == null)
            {
                ctlDagPath = BasicFunc.CreateCTL_Crystal("ctl_bs_" + bs.name);
            }

            MFnDependencyNode ctlNode = new MFnDependencyNode(ctlDagPath.node);

            MPlug weightPlug = bs.findPlug(ConstantValue.plugName_blendShapeWeight);
            int   count      = (int)weightPlug.numElements;
            //Debug.Log("target count:" + count);

            MDGModifier dGModifier = new MDGModifier();

            for (int i = 0; i < count; i++)
            {
                //Debug.Log("process:" + i);
                MPlug  singleWeightPlug = weightPlug.elementByLogicalIndex((uint)i);
                string weightName       = singleWeightPlug.name.Split('.').Last();
                MPlug  ctlAttrPlug      = AddFloatAttr(ctlDagPath, weightName);
                dGModifier.connect(ctlAttrPlug, singleWeightPlug);
            }
            dGModifier.doIt();

            return(true);
        }
Example #4
0
        public static MFnDependencyNode CreateLayeredTextureNode(List <MFnDependencyNode> imageNodes)
        {
            //MFnDependencyNode layeredTextureNode = new MFnDependencyNode();
            //layeredTextureNode.create("layeredTexture");
            MFnDependencyNode layeredTextureNode   = CreateShadingNode(ShadingNodeType.Utility, "layeredTexture");
            MPlug             layeredTexInputsPlug = layeredTextureNode.findPlug(ConstantValue.plugName_layeredTextureInputs);
            //check place2DTextures
            MDGModifier dGModifier = new MDGModifier();

            for (int i = 0; i < imageNodes.Count; i++)
            {
                //place2dTexture setting
                MFnDependencyNode p2dNode = GetPlace2dTextureForTex(imageNodes[i]);
                p2dNode.findPlug("wrapU").setBool(false);
                p2dNode.findPlug("translateFrameU").setFloat(i);

                //set tex default color to 0
                imageNodes[i].findPlug(ConstantValue.plugName_fileTexDefaultColorR).setFloat(0);
                imageNodes[i].findPlug(ConstantValue.plugName_fileTexDefaultColorG).setFloat(0);
                imageNodes[i].findPlug(ConstantValue.plugName_fileTexDefaultColorB).setFloat(0);

                //move uv
                MSelectionList matList = GetMaterialsWithTex(imageNodes[i]);
                for (int j = 0; j < matList.length; j++)
                {
                    MObject matObj = new MObject();
                    matList.getDependNode((uint)j, matObj);
                    string matName = new MFnDependencyNode(matObj).absoluteName;
                    Debug.Log("move uv for mat:" + matName);
                    MoveUV(i, 0, matName);
                }
                MPlug layeredTexInputPlug  = layeredTexInputsPlug.elementByLogicalIndex((uint)i);
                MPlug texOutColorPlug      = imageNodes[i].findPlug(ConstantValue.plugName_fileTexOutputColor);
                MPlug layeredTexInputColor = layeredTexInputPlug.child((int)ConstantValue.LayeredTextureInputDataIndex.Color);
                dGModifier.connect(texOutColorPlug, layeredTexInputColor);
                //set blendMode to add
                MPlug blendMode = layeredTexInputPlug.child((int)ConstantValue.LayeredTextureInputDataIndex.BlendMode);
                if (i < imageNodes.Count - 1)
                {
                    blendMode.setInt((int)ConstantValue.LayeredTextureBlendMode.Add);
                }
                else
                {
                    blendMode.setInt((int)ConstantValue.LayeredTextureBlendMode.None);
                }
            }
            dGModifier.doIt();
            return(layeredTextureNode);
        }
Example #5
0
        static bool CopyShaderParam(MPlug from, MPlug to, ShaderParamType spt = ShaderParamType.Color)
        {
            if (from == null || to == null)
            {
                return(false);
            }
            MPlugArray plugArr = new MPlugArray();

            from.connectedTo(plugArr, true, false);
            if (plugArr.length == 0)
            {
                switch (spt)
                {
                case ShaderParamType.Color:
                {
                    to.child(0).setFloat(from.child(0).asFloat());
                    to.child(1).setFloat(from.child(1).asFloat());
                    to.child(2).setFloat(from.child(2).asFloat());
                    break;
                }

                case ShaderParamType.Float:
                {
                    to.setFloat(from.asFloat());
                    break;
                }
                }
            }
            else
            {
                MDGModifier dGModifier = new MDGModifier();
                Debug.Log(from.source.partialName(true));
                dGModifier.connect(from.source, to);
                dGModifier.doIt();
            }
            return(true);
        }
Example #6
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();
        }
Example #7
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);
        }
Example #8
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);
        }
Example #9
0
        public void Load(string name)
        {
            List <StaticObjectVertex> vertices = GetVertices();
            List <uint> indices = GetIndices();

            MIntArray        polygonIndexCounts = new MIntArray((uint)indices.Count / 3);
            MIntArray        polygonIndices     = new MIntArray((uint)indices.Count);
            MFloatPointArray meshVertices       = new MFloatPointArray((uint)vertices.Count);
            MFloatArray      arrayU             = new MFloatArray((uint)vertices.Count);
            MFloatArray      arrayV             = new MFloatArray((uint)vertices.Count);
            MFnMesh          mesh        = new MFnMesh();
            MDagPath         meshDagPath = new MDagPath();
            MDGModifier      modifier    = new MDGModifier();
            MFnSet           set         = new MFnSet();

            for (int i = 0; i < indices.Count / 3; i++)
            {
                polygonIndexCounts[i] = 3;
            }

            for (int i = 0; i < indices.Count; i++)
            {
                polygonIndices[i] = (int)indices[i];
            }

            for (int i = 0; i < vertices.Count; i++)
            {
                StaticObjectVertex vertex = vertices[i];

                meshVertices[i] = new MFloatPoint(vertex.Position.X, vertex.Position.Y, vertex.Position.Z);
                arrayU[i]       = vertex.UV.X;
                arrayV[i]       = 1 - vertex.UV.Y;
            }

            //Assign mesh data
            mesh.create(vertices.Count, indices.Count / 3, meshVertices, polygonIndexCounts, polygonIndices, arrayU, arrayV, MObject.kNullObj);
            mesh.getPath(meshDagPath);
            mesh.assignUVs(polygonIndexCounts, polygonIndices);

            //Set names
            mesh.setName(name);
            MFnTransform transformNode = new MFnTransform(mesh.parent(0));

            transformNode.setName("transform_" + name);

            //Get render partition
            MFnPartition renderPartition = MayaHelper.FindRenderPartition();

            //Create Materials
            uint startIndex = 0;

            for (int i = 0; i < this.Submeshes.Count; i++)
            {
                MFnDependencyNode   dependencyNode = new MFnDependencyNode();
                MFnLambertShader    lambertShader  = new MFnLambertShader();
                StaticObjectSubmesh submesh        = this.Submeshes[i];

                lambertShader.create(true);
                lambertShader.setName(submesh.Name);
                lambertShader.color = MaterialProvider.GetMayaColor(i);

                MObject shadingEngine = dependencyNode.create("shadingEngine", submesh.Name + "_SG");
                MObject materialInfo  = dependencyNode.create("materialInfo", submesh.Name + "_MaterialInfo");
                MPlug   partitionPlug = new MFnDependencyNode(shadingEngine).findPlug("partition");
                MPlug   setsPlug      = MayaHelper.FindFirstNotConnectedElement(renderPartition.findPlug("sets"));
                modifier.connect(partitionPlug, setsPlug);

                MPlug outColorPlug      = lambertShader.findPlug("outColor");
                MPlug surfaceShaderPlug = new MFnDependencyNode(shadingEngine).findPlug("surfaceShader");
                modifier.connect(outColorPlug, surfaceShaderPlug);

                MPlug messagePlug      = new MFnDependencyNode(shadingEngine).findPlug("message");
                MPlug shadingGroupPlug = new MFnDependencyNode(materialInfo).findPlug("shadingGroup");
                modifier.connect(messagePlug, shadingGroupPlug);

                modifier.doIt();

                MFnSingleIndexedComponent component = new MFnSingleIndexedComponent();
                MObject   faceComponent             = component.create(MFn.Type.kMeshPolygonComponent);
                MIntArray groupPolygonIndices       = new MIntArray();
                uint      endIndex = (startIndex + (uint)submesh.Indices.Count) / 3;
                for (uint j = startIndex / 3; j < endIndex; j++)
                {
                    groupPolygonIndices.append((int)j);
                }
                component.addElements(groupPolygonIndices);

                set.setObject(shadingEngine);
                set.addMember(meshDagPath, faceComponent);

                startIndex += (uint)submesh.Indices.Count;
            }

            mesh.updateSurface();
        }
        public void Load(string name, SKLFile skl = null)
        {
            MIntArray        polygonIndexCounts = new MIntArray((uint)this.Indices.Count / 3);
            MIntArray        polygonIndices     = new MIntArray((uint)this.Indices.Count);
            MFloatPointArray vertices           = new MFloatPointArray((uint)this.Vertices.Count);
            MFloatArray      arrayU             = new MFloatArray((uint)this.Vertices.Count);
            MFloatArray      arrayV             = new MFloatArray((uint)this.Vertices.Count);
            MVectorArray     normals            = new MVectorArray((uint)this.Vertices.Count);
            MIntArray        normalIndices      = new MIntArray((uint)this.Vertices.Count);
            MFnMesh          mesh        = new MFnMesh();
            MDagPath         meshDagPath = new MDagPath();
            MDGModifier      modifier    = new MDGModifier();
            MFnSet           set         = new MFnSet();

            for (int i = 0; i < this.Indices.Count / 3; i++)
            {
                polygonIndexCounts[i] = 3;
            }

            for (int i = 0; i < this.Indices.Count; i++)
            {
                polygonIndices[i] = this.Indices[i];
            }

            for (int i = 0; i < this.Vertices.Count; i++)
            {
                SKNVertex vertex = this.Vertices[i];

                vertices[i]      = new MFloatPoint(vertex.Position.X, vertex.Position.Y, vertex.Position.Z);
                arrayU[i]        = vertex.UV.X;
                arrayV[i]        = 1 - vertex.UV.Y;
                normals[i]       = new MVector(vertex.Normal.X, vertex.Normal.Y, vertex.Normal.Z);
                normalIndices[i] = i;
            }

            //Assign mesh data
            mesh.create(this.Vertices.Count, this.Indices.Count / 3, vertices, polygonIndexCounts, polygonIndices, arrayU, arrayV, MObject.kNullObj);
            mesh.setVertexNormals(normals, normalIndices);
            mesh.getPath(meshDagPath);
            mesh.assignUVs(polygonIndexCounts, polygonIndices);

            //Set names
            mesh.setName(name);
            MFnTransform transformNode = new MFnTransform(mesh.parent(0));

            transformNode.setName("transform_" + name);

            //Get render partition
            MGlobal.displayInfo("SKNFile:Load - Searching for Render Partition");
            MItDependencyNodes itDependencyNodes = new MItDependencyNodes(MFn.Type.kPartition);
            MFnPartition       renderPartition   = new MFnPartition();
            bool foundRenderPartition            = false;

            for (; !itDependencyNodes.isDone; itDependencyNodes.next())
            {
                renderPartition.setObject(itDependencyNodes.thisNode);
                MGlobal.displayInfo("SKNFile:Load - Iterating through partition: " + renderPartition.name + " IsRenderPartition: " + renderPartition.isRenderPartition);
                if (renderPartition.name == "renderPartition" && renderPartition.isRenderPartition)
                {
                    MGlobal.displayInfo("SKNFile:Load - Found render partition");
                    foundRenderPartition = true;
                    break;
                }
            }


            //Create Materials
            for (int i = 0; i < this.Submeshes.Count; i++)
            {
                MFnDependencyNode dependencyNode = new MFnDependencyNode();
                MFnLambertShader  lambertShader  = new MFnLambertShader();
                SKNSubmesh        submesh        = this.Submeshes[i];
                MObject           shader         = lambertShader.create(true);

                lambertShader.setName(submesh.Name);
                lambertShader.color = MaterialProvider.GetMayaColor(i);

                MObject shadingEngine = dependencyNode.create("shadingEngine", submesh.Name + "_SG");
                MObject materialInfo  = dependencyNode.create("materialInfo", submesh.Name + "_MaterialInfo");
                if (foundRenderPartition)
                {
                    MPlug partitionPlug = new MFnDependencyNode(shadingEngine).findPlug("partition");
                    MPlug setsPlug      = MayaHelper.FindFirstNotConnectedElement(renderPartition.findPlug("sets"));
                    modifier.connect(partitionPlug, setsPlug);
                }
                else
                {
                    MGlobal.displayInfo("SKNFile:Load - Couldn't find Render Partition for mesh: " + name + "." + submesh.Name);
                }

                MPlug outColorPlug      = lambertShader.findPlug("outColor");
                MPlug surfaceShaderPlug = new MFnDependencyNode(shadingEngine).findPlug("surfaceShader");
                modifier.connect(outColorPlug, surfaceShaderPlug);

                MPlug messagePlug      = new MFnDependencyNode(shadingEngine).findPlug("message");
                MPlug shadingGroupPlug = new MFnDependencyNode(materialInfo).findPlug("shadingGroup");
                modifier.connect(messagePlug, shadingGroupPlug);

                modifier.doIt();

                MFnSingleIndexedComponent component = new MFnSingleIndexedComponent();
                MObject   faceComponent             = component.create(MFn.Type.kMeshPolygonComponent);
                MIntArray groupPolygonIndices       = new MIntArray();
                uint      endIndex = (submesh.StartIndex + submesh.IndexCount) / 3;
                for (uint j = submesh.StartIndex / 3; j < endIndex; j++)
                {
                    groupPolygonIndices.append((int)j);
                }
                component.addElements(groupPolygonIndices);

                set.setObject(shadingEngine);
                set.addMember(meshDagPath, faceComponent);
            }

            if (skl == null)
            {
                mesh.updateSurface();
            }
            else
            {
                MFnSkinCluster skinCluster             = new MFnSkinCluster();
                MSelectionList jointPathsSelectionList = new MSelectionList();

                jointPathsSelectionList.add(meshDagPath);
                for (int i = 0; i < skl.Influences.Count; i++)
                {
                    short    jointIndex = skl.Influences[i];
                    SKLJoint joint      = skl.Joints[jointIndex];
                    jointPathsSelectionList.add(skl.JointDagPaths[jointIndex]);

                    MGlobal.displayInfo(string.Format("SKNFile:Load:Bind - Added joint [{0}] {1} to binding selection", joint.ID, joint.Name));
                }

                MGlobal.selectCommand(jointPathsSelectionList);
                MGlobal.executeCommand("skinCluster -mi 4 -tsb -n skinCluster_" + name);

                MPlug      inMeshPlug        = mesh.findPlug("inMesh");
                MPlugArray inMeshConnections = new MPlugArray();
                inMeshPlug.connectedTo(inMeshConnections, true, false);

                if (inMeshConnections.length == 0)
                {
                    MGlobal.displayError("SKNFile:Load:Bind - Failed to find the created Skin Cluster");
                    throw new Exception("SKNFile:Load:Bind - Failed to find the created Skin Cluster");
                }

                MPlug         outputGeometryPlug = inMeshConnections[0];
                MDagPathArray influencesDagPaths = new MDagPathArray();

                skinCluster.setObject(outputGeometryPlug.node);
                skinCluster.influenceObjects(influencesDagPaths);

                MIntArray influenceIndices = new MIntArray((uint)skl.Influences.Count);
                for (int i = 0; i < skl.Influences.Count; i++)
                {
                    MDagPath influencePath = skl.JointDagPaths[skl.Influences[i]];

                    for (int j = 0; j < skl.Influences.Count; j++)
                    {
                        if (influencesDagPaths[j].partialPathName == influencePath.partialPathName)
                        {
                            influenceIndices[i] = j;
                            MGlobal.displayInfo("SKNReader:Load:Bind - Added Influence Joint: " + i + " -> " + j);
                            break;
                        }
                    }
                }

                MFnSingleIndexedComponent singleIndexedComponent = new MFnSingleIndexedComponent();
                MObject   vertexComponent    = singleIndexedComponent.create(MFn.Type.kMeshVertComponent);
                MIntArray groupVertexIndices = new MIntArray((uint)this.Vertices.Count);

                for (int i = 0; i < this.Vertices.Count; i++)
                {
                    groupVertexIndices[i] = i;
                }
                singleIndexedComponent.addElements(groupVertexIndices);

                MGlobal.executeCommand(string.Format("setAttr {0}.normalizeWeights 0", skinCluster.name));

                MDoubleArray weights = new MDoubleArray((uint)(this.Vertices.Count * skl.Influences.Count));
                for (int i = 0; i < this.Vertices.Count; i++)
                {
                    SKNVertex vertex = this.Vertices[i];

                    for (int j = 0; j < 4; j++)
                    {
                        double weight    = vertex.Weights[j];
                        int    influence = vertex.BoneIndices[j];

                        if (weight != 0)
                        {
                            weights[(i * skl.Influences.Count) + influence] = weight;
                        }
                    }
                }

                skinCluster.setWeights(meshDagPath, vertexComponent, influenceIndices, weights, false);
                MGlobal.executeCommand(string.Format("setAttr {0}.normalizeWeights 1", skinCluster.name));
                MGlobal.executeCommand(string.Format("skinPercent -normalize true {0} {1}", skinCluster.name, mesh.name));
                mesh.updateSurface();
            }
        }
Example #11
0
        public static bool BindFinger(MDagPath rootJointDagPath, MDagPath middleJointDagPath, MDagPath finalJointDagPath, string fingerTag, bool useIK = false)
        {
            JointProcess.SetJointLimit(rootJointDagPath, JointProcess.JointType.FingerRoot);
            JointProcess.SetJointLimit(middleJointDagPath, JointProcess.JointType.FingerMiddle);
            JointProcess.SetJointLimit(finalJointDagPath, JointProcess.JointType.FingerMiddle);

            if (useIK)
            {
            }
            else
            {
                MDagPath          ctlDagPath         = BasicFunc.AddParentCircle(rootJointDagPath, true);
                MFnDependencyNode remapNode_root     = BasicFunc.CreateRemapValueNode(-2, 3, 60, -90);
                MFnDependencyNode remapNode_rootSide = BasicFunc.CreateRemapValueNode(-1, 1, 30, -30);
                MFnDependencyNode remapNode_middle   = BasicFunc.CreateRemapValueNode(-1, 3, 30, -90);
                MFnDependencyNode remapNode_final    = BasicFunc.CreateRemapValueNode(-1, 3, 30, -90);
                //MFnDependencyNode** ptr_remapNode_root = &remapNode_root,


                //string remapValueNodeName_root = BasicFunc.CreateRemapValueNode(-2, 3, 60, -90, ptr_remapNode_root);
                //string remapValueNodeName_rootSide = BasicFunc.CreateRemapValueNode(-1, 1, 30, -30, ptr_remapNode_rootSide);
                //string remapValueNodeName_middle = BasicFunc.CreateRemapValueNode(-1, 3, 30, -90, ptr_remapNode_middle);
                //string remapValueNodeName_final = BasicFunc.CreateRemapValueNode(-1, 3, 30, -90, ptr_remapNode_final);
                string            ctlName   = ctlDagPath.fullPathName;
                MFnDependencyNode dn_ctl    = new MFnDependencyNode(ctlDagPath.node);
                MFnDependencyNode dn_root   = new MFnDependencyNode(rootJointDagPath.node);
                MFnDependencyNode dn_middle = new MFnDependencyNode(middleJointDagPath.node);
                MFnDependencyNode dn_final  = new MFnDependencyNode(finalJointDagPath.node);

                /*MPlug plug_ctlTy = dn_ctl.findPlug("translateY");
                 * Debug.Log("plug name:" + plug_ctlTy.partialName() + " fullname:" + plug_ctlTy.name());*/
                //MStatus status;
                //MPlug plug_remapNode_root_input = remapNode_root.findPlug("inputValue", &status);
                //if (status == MStatus::kSuccess)
                //{
                //	Debug.Log("success 634634");
                //	//Debug.Log("plug name:" + plug_remapNode_root_input.partialName() + " fullname:" + plug_remapNode_root_input.name());
                //}
                //else
                //{
                //	Debug.Log("failed a23234234");
                //}
                MDGModifier dgModifier = new MDGModifier();
                dgModifier.doIt();
                dgModifier.connect(dn_ctl.findPlug("translateY"), remapNode_root.findPlug("inputValue"));
                dgModifier.connect(remapNode_root.findPlug("outValue"), dn_root.findPlug("rotateZ"));

                dgModifier.connect(dn_ctl.findPlug("translateZ"), remapNode_rootSide.findPlug("inputValue"));
                dgModifier.connect(remapNode_rootSide.findPlug("outValue"), dn_root.findPlug("rotateY"));

                dgModifier.connect(dn_ctl.findPlug("translateX"), remapNode_middle.findPlug("inputValue"));
                dgModifier.connect(remapNode_middle.findPlug("outValue"), dn_middle.findPlug("rotateZ"));

                dgModifier.connect(dn_ctl.findPlug("translateX"), remapNode_final.findPlug("inputValue"));
                dgModifier.connect(remapNode_final.findPlug("outValue"), dn_final.findPlug("rotateZ"));
                dgModifier.doIt();

                BasicFunc.SetTranslateLimit(new MFnTransform(ctlDagPath), -1, -2, -1, 3, 3, 1);
            }
            return(true);
        }