Esempio n. 1
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);
        }
Esempio n. 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));
            }
        }
Esempio n. 3
0
        public override void doIt(MArgList args)
        {
            MTypeId id      = new MTypeId(0x0008106c);
            MObject setNode = fDGMod.createNode(id);

            fDGMod.doIt();

            MSelectionList selList = new MSelectionList();

            MGlobal.getActiveSelectionList(selList);
            if (selList.length > 0)
            {
                try
                {
                    MFnSet setFn = new MFnSet(setNode);
                    setFn.addMembers(selList);
                }
                catch (System.Exception)
                {
                }
            }
            MFnDependencyNode depNodeFn = new MFnDependencyNode(setNode);

            setResult(depNodeFn.name);

            return;
        }
Esempio n. 4
0
        /// <summary>
        /// well, this action is truely dangerous
        /// </summary>
        /// <param name="list"></param>
        /// <returns></returns>
        public static bool CombineMaterials(MSelectionList list, bool deleteRepeated = true)
        {
            if (!BasicFunc.CheckSelectionList(list, 2))
            {
                Debug.Log("please choose at least 2 materials");
                return(false);
            }

            string                firstMatName  = "";
            List <MObject>        deleteList    = new List <MObject>();
            List <MSelectionList> waitForAssign = new List <MSelectionList>();

            MDGModifier dGModifier = new MDGModifier();

            for (uint i = 0; i < list.length; i++)
            {
                //Debug.Log(i + " mat test");
                MObject matObject = new MObject();
                list.getDependNode(i, matObject);
                MFnDependencyNode dnode = new MFnDependencyNode(matObject);
                if (i == 0)
                {
                    firstMatName = dnode.absoluteName;
                    continue;
                }
                else
                {
                    deleteList.Add(matObject);
                }
                //Debug.Log(i + " node:" + dnode.absoluteName);
                if (matObject.hasFn(MFn.Type.kLambert) || matObject.hasFn(MFn.Type.kBlinn) || matObject.hasFn(MFn.Type.kPhong))
                {
                    //Debug.Log("has mat fn");
                    //MMaterial mat = new MMaterial(matObject);
                    //MColor color = new MColor();
                    //mat.getDiffuse(color);
                    //Debug.Log("mat:" + dnode.absoluteName + " ,color:" + BasicFunc.MToString(color));
                    SelectObjectsWithMat(dnode);
                    //Debug.Log("finish select");


                    //waitForAssign.Add(BasicFunc.GetSelectedList());
                    AssignMat(firstMatName);
                    //Debug.Log("finish assign");
                    BasicFunc.DeleteByCMD(dnode.absoluteName);
                    //Debug.Log("finish delete");
                }
                else
                {
                    Debug.Log("no mat fn");
                }
            }

            dGModifier.doIt();

            //MGlobal.executeCommandOnIdle("hyperShade -objects " + matNode.absoluteName);
            return(true);
        }
Esempio n. 5
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);
        }
Esempio n. 6
0
        public static bool DeleteObject(MObject mo)
        {
            if (mo == null)
            {
                return(false);
            }
            MDGModifier dGModifier = new MDGModifier();

            dGModifier.deleteNode(mo);
            dGModifier.doIt();
            return(true);
        }
Esempio n. 7
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);
        }
Esempio n. 8
0
        public static bool CombineMaterials(List <MObject> list, bool deleteRepeated = true)
        {
            if (list.Count < 2)
            {
                Debug.Log("please choose at least 2 materials");
                return(false);
            }
            string                firstMatName  = "";
            List <MObject>        deleteList    = new List <MObject>();
            List <MSelectionList> waitForAssign = new List <MSelectionList>();

            MDGModifier dGModifier = new MDGModifier();

            for (int i = 0; i < list.Count; i++)
            {
                //Debug.Log(i + " mat test");
                MObject           matObject = list[i];
                MFnDependencyNode dnode     = new MFnDependencyNode(matObject);
                if (i == 0)
                {
                    firstMatName = dnode.absoluteName;
                    continue;
                }
                else
                {
                    deleteList.Add(matObject);
                }
                //Debug.Log(i + " node:" + dnode.absoluteName);
                if (matObject.hasFn(MFn.Type.kLambert) || matObject.hasFn(MFn.Type.kBlinn) || matObject.hasFn(MFn.Type.kPhong))
                {
                    SelectObjectsWithMat(dnode);
                    AssignMat(firstMatName);
                    BasicFunc.DeleteByCMD(dnode.absoluteName);
                }
                else
                {
                    Debug.Log("no mat fn");
                }
            }
            dGModifier.doIt();
            //MGlobal.executeCommandOnIdle("hyperShade -objects " + matNode.absoluteName);
            return(true);
        }
Esempio n. 9
0
        public static bool DeleteObjects(List <MFnDependencyNode> list)
        {
            if (list == null)
            {
                return(false);
            }
            MDGModifier dGModifier = new MDGModifier();

            for (int i = 0; i < list.Count; i++)
            {
                if (list[i] == null)
                {
                    continue;
                }
                dGModifier.deleteNode(list[i].objectProperty);
            }
            dGModifier.doIt();
            return(true);
        }
Esempio n. 10
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);
        }
Esempio n. 11
0
 override public void redoIt()
 {
     fDGModifier.doIt();
 }
Esempio n. 12
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);
        }
Esempio n. 13
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);
        }
Esempio n. 14
0
        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();
            }
        }
Esempio n. 15
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();
        }
Esempio n. 16
0
        public static void ToMaya(Curve CurveToSend, string name)
        {
            NurbsCurve ctsAsNurb = null;

            if (CurveToSend is Rectangle)
            {
                Rectangle rec = (Rectangle)CurveToSend;
                ctsAsNurb = NurbsCurve.ByControlPoints(rec.Points, 1, true);
            }
            else if (CurveToSend is Polygon)
            {
                Polygon rec = (Polygon)CurveToSend;
                ctsAsNurb = NurbsCurve.ByControlPoints(rec.Points, 1, true);
            }
            else
            {
                ctsAsNurb = CurveToSend.ToNurbsCurve();
            }

            var ncd = new MFnNurbsCurveData();


            MFnNurbsCurveForm mfnform;

            if (ctsAsNurb.IsClosed)
            {
                mfnform = MFnNurbsCurveForm.kClosed;
            }
            else
            {
                mfnform = MFnNurbsCurveForm.kOpen;
            }

            var mayaCurve = new MFnNurbsCurve();

            var vtxs = new MPointArray();

            var cvs = ctsAsNurb.ControlPoints();
            var yUp = MGlobal.isYAxisUp;

            if (yUp)
            {
                foreach (var cv in cvs)
                {
                    var pt = new MPoint(cv.X, cv.Z, -cv.Y);
                    vtxs.Add(pt);
                    //pt.Dispose();
                }
            }
            else
            {
                foreach (var cv in cvs)
                {
                    var pt = new MPoint(cv.X, cv.Y, cv.Z);
                    vtxs.Add(pt);
                    //pt.Dispose();
                }
            }

            var knots    = ctsAsNurb.Knots();
            var crvKnots = new MDoubleArray(knots);

            crvKnots.RemoveAt(0);
            crvKnots.RemoveAt(crvKnots.Count - 1);

            MDagPath node       = null;
            var      nodeExists = false;

            Task checkNode  = null;
            Task deleteNode = null;

            try
            {
                node       = DMInterop.getDagNode(name);
                nodeExists = true;
            }
            catch (Exception)
            {
                nodeExists = false;
            }

            MObject obj;

            if (nodeExists)
            {
                MDagPath nodeShape = node;
                nodeShape.extendToShape();
                var modifyCrv = new MDGModifier();
                mayaCurve = new MFnNurbsCurve(nodeShape);

                try
                {
                    MFnNurbsCurveData dataCreator  = new MFnNurbsCurveData();
                    MObject           outCurveData = dataCreator.create();
                    var    span   = (vtxs.Count - ctsAsNurb.Degree);
                    string rblCmd = $"rebuildCurve -rt 0 -s {span} -d {ctsAsNurb.Degree} {name}";

                    if (mayaCurve.numCVs != vtxs.Count || mayaCurve.degree != ctsAsNurb.Degree)
                    {
                        MGlobal.executeCommand(rblCmd);
                    }

                    mayaCurve.setCVs(vtxs);
                    mayaCurve.setKnots(crvKnots, 0, crvKnots.length - 1);
                    mayaCurve.updateCurve();
                    modifyCrv.doIt();

                    if (CurveToSend.GetType() == typeof(Circle))
                    {
                        span   = 8;
                        rblCmd = $"rebuildCurve -rt 0 -s {span} {name}";
                        MGlobal.executeCommand(rblCmd);
                    }
                }
                catch (Exception e)
                {
                    MGlobal.displayWarning(e.Message);
                }
            }

            else
            {
                obj = mayaCurve.create(vtxs, crvKnots, (uint)ctsAsNurb.Degree, (MFnNurbsCurve.Form)mfnform,
                                       false, ctsAsNurb.IsRational);
                MFnDependencyNode nodeFn = new MFnDagNode(obj);
                nodeFn.setName(name);
            }
        }
Esempio n. 17
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. 18
0
 public override void redoIt()
 {
     fMod.doIt();
     return;
 }
Esempio n. 19
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. 20
0
        public static void ToMaya(Surface SurfaceToSend, string name, string groupName)
        {
            NurbsSurface dynSurf;

            try
            {
                dynSurf = SurfaceToSend.ToNurbsSurface();
            }
            catch (Exception)
            {
                dynSurf = null;
                MGlobal.displayWarning("Surface has no nurbSurface form");
            }

            //MFnNurbsSurface updatedSurface;
            MPointArray cvs = new MPointArray();

            Point[][] ctrlPts = dynSurf.ControlPoints();

            for (int i = 0; i < ctrlPts.Length; i++)
            {
                for (int j = 0; j < ctrlPts[i].Length; j++)
                {
                    MPoint p = new MPoint();
                    if (MGlobal.isZAxisUp)
                    {
                        p.x = ctrlPts[i][j].X;
                        p.y = ctrlPts[i][j].Y;
                        p.z = ctrlPts[i][j].Z;
                    }
                    else
                    {
                        p.x = ctrlPts[i][j].X;
                        p.y = ctrlPts[i][j].Z;
                        p.z = -ctrlPts[i][j].Y;
                    }


                    cvs.Add(p);
                }
            }
            MDoubleArray uknot = new MDoubleArray(dynSurf.UKnots());

            uknot.RemoveAt(0);
            uknot.RemoveAt(uknot.Count - 1);
            MDoubleArray vknot = new MDoubleArray(dynSurf.VKnots());

            vknot.RemoveAt(0);
            vknot.RemoveAt(vknot.Count - 1);

            MFnNurbsSurface.Form formU = MFnNurbsSurface.Form.kInvalid;
            MFnNurbsSurface.Form formV = MFnNurbsSurface.Form.kInvalid;

            if (dynSurf.IsPeriodicInU)
            {
                formU = MFnNurbsSurface.Form.kPeriodic;
            }
            else if (dynSurf.ClosedInU)
            {
                formU = MFnNurbsSurface.Form.kClosed;
            }
            else
            {
                formU = MFnNurbsSurface.Form.kOpen;
            }

            if (dynSurf.IsPeriodicInV)
            {
                formV = MFnNurbsSurface.Form.kPeriodic;
            }
            else if (dynSurf.ClosedInV)
            {
                formV = MFnNurbsSurface.Form.kClosed;
            }
            else
            {
                formV = MFnNurbsSurface.Form.kOpen;
            }

            MDagPath existingDagPath = null;
            bool     nodeExists      = false;

            //trims
            //toDo: impement trims


            Task checkNode = null;



            try
            {
                checkNode = Task.Factory.StartNew(() => existingDagPath = DMInterop.getDagNode(name));
                checkNode.Wait(500);

                nodeExists = true;
            }
            catch (Exception)
            {
                nodeExists = false;
            }

            MObject obj;

            if (nodeExists)
            {
                if (checkNode.IsCompleted)
                {
                    MFnNurbsSurface existingSurface = new MFnNurbsSurface(existingDagPath);
                    MDGModifier     mdgModifier     = new MDGModifier();



                    // if (existingSurface.degreeU == dynSurf.DegreeU && existingSurface.degreeV== dynSurf.DegreeV && existingSurface.numCVsInU == ctrlPts.Length && existingSurface.numCVsInV == ctrlPts[0].Length )
                    //{

                    if (existingSurface.degreeU != dynSurf.DegreeU || existingSurface.degreeV != dynSurf.DegreeV || existingSurface.numCVsInU != ctrlPts.Length || existingSurface.numCVsInV != ctrlPts[0].Length)
                    {
                        //this is a hack to rebuild the surface. proper way is to make new surface and assign as input to aold surface
                        MGlobal.executeCommand(string.Format("rebuildSurface -du {0} -dv {1} -su {2} -sv {3} {4}", dynSurf.DegreeU, dynSurf.DegreeV, ctrlPts.Length - 3, ctrlPts[0].Length - 3, name));
                    }
                    //  updatedSurface = existingSurface;
                    existingSurface.setCVs(cvs);
                    existingSurface.setKnotsInU(uknot, 0, (uint)existingSurface.numKnotsInU - 1);
                    existingSurface.setKnotsInV(vknot, 0, (uint)existingSurface.numKnotsInV - 1);
                    existingSurface.updateSurface();


                    // }

                    /*
                     * else
                     *
                     * {
                     * //get all the existing node types
                     * MFnDagNode dagNodeExist = new MFnDagNode(existingDagPath);
                     * MObject existSurfObj = existingDagPath.node;
                     * MFnDependencyNode depNodeExist = new MFnDependencyNode(existSurfObj);
                     *
                     *
                     * updatedSurface = new MFnNurbsSurface();
                     * var newSurfObj = dagNodeExist.duplicate();
                     * updatedSurface.isIntermediateObject = true;
                     *
                     * updatedSurface.create(cvs, uknot, vknot, (uint)dynSurf.DegreeU, (uint)dynSurf.DegreeV, formU, formV, dynSurf.IsRational, newSurfObj);
                     * MFnDependencyNode depNodeNew = new MFnDependencyNode(newSurfObj);
                     *
                     * MPlug outSurf = depNodeExist.findPlug("outputSurface");
                     * MPlug inSurf = depNodeNew.findPlug("inputSurface");
                     *
                     *
                     * mdgModifier.connect(outSurf, inSurf);
                     *
                     *
                     * }
                     */
                    mdgModifier.doIt();

                    // MFnDependencyNode nodeFn = new MFnDagNode(mSurf);
                    //nodeFn.setName(name);
                    //MGlobal.executeCommand("sets -e -forceElement initialShadingGroup " + nodeFn.name);
                }
            }
            else
            {
                if (checkNode.IsCompleted)
                {
                    MFnNurbsSurface updatedSurface = new MFnNurbsSurface();
                    obj = updatedSurface.create(cvs, uknot, vknot, (uint)dynSurf.DegreeU, (uint)dynSurf.DegreeV, formU, formV, dynSurf.IsRational);
                    MFnDependencyNode nodeFn = new MFnDagNode(obj);

                    nodeFn.setName(name);
                    MGlobal.executeCommand("sets -e -forceElement initialShadingGroup " + nodeFn.name);
                }
            }

            if (groupName != "")
            {
                bool groupExists = false;
                try
                {
                    DMInterop.getDagNode(groupName);
                    groupExists = true;
                }
                catch { }

                if (groupExists)
                {
                    MGlobal.executeCommand(string.Format("parent {0} {1}", groupName, name));
                }
                else
                {
                    MGlobal.executeCommand(string.Format("group -n {0} {1}", groupName, name));
                }
            }
        }