Wrappers
Wrappers exist for simple objects such as vectors and matrices. They are generally fully implemented C++ classes with public constructors and destructors. When a method returns a wrapper, you are responsible for it. You always own the wrapper that you reference.
You can allocate and deallocate them as necessary. Leaving scope will usually be adequate for deleting the wrapper.
Move wrapper declarations (MIntArray
, MFloatArray
etc.) out of heavy loops if possible. The constructor of a wrapper will in most cases call new to allocate the internal Maya object. As a result, if a wrapper is declared in a heavy loop memory will be allocated and de-allocated repeatedly.This does not apply to static wrapper classes such as MGlobal
.
The following is a plug-in that builds a helical curve. See Appendix A: NURBS Geometry for a brief explanation of NURBS geometry.
In this example, MPointArray
and MDoubleArray
are wrappers.
#include <math.h>
#include <maya/MIOStream.h>
#include <maya/MSimple.h>
#include <maya/MPoint.h>
#include <maya/MPointArray.h>
#include <maya/MDoubleArray.h>
#include <maya/MFnNurbsCurve.h>
DeclareSimpleCommand( doHelix, "Autodesk - Example", "2017");
MStatus doHelix::doIt( const MArgList& )
{
MStatus stat;
const unsigned deg = 3; // Curve Degree
const unsigned ncvs = 20; // Number of CVs
const unsigned spans = ncvs - deg; // Number of spans
const unsigned nknots = spans+2*deg-1; // Number of knots
double radius = 4.0; // Helix radius
double pitch = 0.5; // Helix pitch
unsigned i;
MPointArray controlVertices;
MDoubleArray knotSequences;
// Set up cvs and knots for the helix
//
for (i = 0; i < ncvs; i++)
controlVertices.append( MPoint( radius * cos( (double)i ),
pitch * (double)i, radius * sin( (double)i ) ) );
for (i = 0; i < nknots; i++)
knotSequences.append( (double)i );
// Now create the curve
//
MFnNurbsCurve curveFn;
MObject curve = curveFn.create( controlVertices,
knotSequences, deg, MFnNurbsCurve::kOpen, false, false, MObject::kNullObj, &stat );
if ( MS::kSuccess != stat )
cout << "Error creating curve.\n";
return stat;
}
Compile this plug-in, load it, then enter "doHelix" at the prompt. A helical curve displays in the Maya view.