Beispiel #1
0
        public static MDagPath CreateCTL_Square(string ctlName = "square", float up = 0.5f, float down = -0.5f, float left = -0.5f, float right = 0.5f)
        {
            MPointArray points = new MPointArray();

            points.Add(new MPoint(left, up, 0));
            points.Add(new MPoint(left, down, 0));
            points.Add(new MPoint(right, down, 0));
            points.Add(new MPoint(right, up, 0));
            points.Add(new MPoint(left, up, 0));
            return(CreateCurve(points, ctlName, 1, MFnNurbsCurve.Form.kClosed));
        }
Beispiel #2
0
        public static MDagPath CreateCTL_Square(string ctlName = "square", float height = 1, float width = 1)
        {
            MPointArray points = new MPointArray();
            float       up     = height / 2;
            float       left   = width / 2;

            points.Add(new MPoint(left, up, 0));
            points.Add(new MPoint(left, -up, 0));
            points.Add(new MPoint(-left, -up, 0));
            points.Add(new MPoint(-left, up, 0));
            points.Add(new MPoint(left, up, 0));
            return(CreateCurve(points, ctlName, 1, MFnNurbsCurve.Form.kClosed));
        }
Beispiel #3
0
        public static MDagPath CreateCurve(MVector[] positions, string curveName, int degree = 1, MFnNurbsCurve.Form form = MFnNurbsCurve.Form.kOpen)
        {
            MPointArray points = new MPointArray();

            for (int i = 0; i < positions.Length; i++)
            {
                points.Add(new MPoint(positions[i]));
            }
            return(CreateCurve(points, curveName, degree, form));
            //string cmdStr = "cmds.curve(n='" + curveName + "',d=1,p=[";
            //for (int i = 0; i < ptCount; i++)
            //{
            //    if (i != 0)
            //    {
            //        cmdStr += ",";
            //    }
            //    cmdStr += ToCMDSParamStr(points[i]);
            //}
            //int[] indices = new int[ptCount];
            //for (int i = 0; i < ptCount; i++)
            //{
            //    indices[i] = i;
            //}
            //cmdStr += "],k=[" + ToCMDSParamStr(indices, ptCount) + "])";
            //Debug.Log(cmdStr);
            //string resultName = MGlobal.executePythonCommandStringResult(cmdStr);
            //return GetDagPathByName(resultName);
        }
Beispiel #4
0
        public void sendCurveToMaya(string node_name, Point3DCollection controlVertices, List <double> knots, int degree,
                                    MFnNurbsCurveForm form)
        {
            var dn             = new MFnDagNode(getDagNode(node_name));
            var plCreate       = dn.findPlug("create");
            var plDynamoCreate = new MPlug();

            try
            {
                plDynamoCreate = dn.findPlug("dynamoCreate");
            }
            catch
            {
                var tAttr           = new MFnTypedAttribute();
                var ldaDynamoCreate = tAttr.create("dynamoCreate", "dc", MFnData.Type.kNurbsCurve, MObject.kNullObj);
                try
                {
                    dn.addAttribute(ldaDynamoCreate, MFnDependencyNode.MAttrClass.kLocalDynamicAttr);
                    plDynamoCreate = dn.findPlug(ldaDynamoCreate);
                    var dagm = new MDagModifier();
                    dagm.connect(plDynamoCreate, plCreate);
                    dagm.doIt();
                }
                catch
                {
                    return;
                }
            }

            var ncd    = new MFnNurbsCurveData();
            var oOwner = ncd.create();
            var nc     = new MFnNurbsCurve();

            var p_aControlVertices = new MPointArray();

            foreach (var p in controlVertices)
            {
                p_aControlVertices.Add(new MPoint(p.X, p.Y, p.Z));
            }

            var d_aKnots = new MDoubleArray();

            for (var i = 1; i < knots.Count - 1; ++i)
            {
                d_aKnots.Add(knots[i]);
            }

            nc.create(p_aControlVertices, d_aKnots, (uint)degree, (MFnNurbsCurve.Form)form, false, true, oOwner);

            plDynamoCreate.setMObject(oOwner);

            MGlobal.executeCommandOnIdle(string.Format("dgdirty {0}.create;", node_name));
        }
Beispiel #5
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));
                }
            }
        }
        public void sendCurveToMaya(string node_name, Point3DCollection controlVertices, List<double> knots, int degree, MFnNurbsCurveForm form)
        {
            MFnDagNode dn = new MFnDagNode(getDagNode(node_name));
            MPlug plCreate = dn.findPlug("create");
            MPlug plDynamoCreate = new MPlug();

            try
            {
                plDynamoCreate = dn.findPlug("dynamoCreate");
            }
            catch
            {
                MFnTypedAttribute tAttr = new MFnTypedAttribute();
                MObject ldaDynamoCreate = tAttr.create("dynamoCreate", "dc", MFnData.Type.kNurbsCurve, MObject.kNullObj);
                try
                {
                    dn.addAttribute(ldaDynamoCreate, MFnDependencyNode.MAttrClass.kLocalDynamicAttr);
                    plDynamoCreate = dn.findPlug(ldaDynamoCreate);
                    MDagModifier dagm = new MDagModifier();
                    dagm.connect(plDynamoCreate, plCreate);
                    dagm.doIt();
                }
                catch
                {
                    return;
                }
            }

            MFnNurbsCurveData ncd = new MFnNurbsCurveData();
            MObject oOwner = ncd.create();
            MFnNurbsCurve nc = new MFnNurbsCurve();

            MPointArray p_aControlVertices = new MPointArray();
            foreach (Point3D p in controlVertices)
            {
                p_aControlVertices.Add(new MPoint(p.X, p.Y, p.Z));
            }

            MDoubleArray d_aKnots = new MDoubleArray();
            for (int i = 1; i < knots.Count - 1; ++i )
            {
                d_aKnots.Add(knots[i]);
            }

            nc.create(p_aControlVertices, d_aKnots, (uint)degree, (MFnNurbsCurve.Form)form, false, true, oOwner);

            plDynamoCreate.setMObject(oOwner);

            MGlobal.executeCommandOnIdle(String.Format("dgdirty {0}.create;", node_name));
        }
Beispiel #7
0
        public static MDagPath CreateCTL_Crystal(string ctlName = "crystal")
        {
            MPointArray points = new MPointArray();

            points.Add(new MPoint(0, 1, 0));
            points.Add(new MPoint(0, 0, 1));
            points.Add(new MPoint(1, 0, 0));
            points.Add(new MPoint(0, -1, 0));

            points.Add(new MPoint(0, 0, -1));
            points.Add(new MPoint(1, 0, 0));
            points.Add(new MPoint(0, 1, 0));
            points.Add(new MPoint(0, 0, -1));

            points.Add(new MPoint(-1, 0, 0));
            points.Add(new MPoint(0, -1, 0));
            points.Add(new MPoint(0, 0, 1));
            points.Add(new MPoint(-1, 0, 0));
            points.Add(new MPoint(0, 1, 0));
            return(CreateCurve(points, ctlName, 1, MFnNurbsCurve.Form.kClosed));
        }
Beispiel #8
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);
            }
        }