Maya API in Visual Studio 2010

Maya API doesn’t compile with dot net framework 4.0, as it defines MStatus as MS, which is a namespace in .NET 4.0

Hence create a new project, select Visual C++, at the top select .Net Framework3.5 and make an Empty Project.

Then per super quick test you can add the following files:

stdafx.h
#include
#include <maya/MString.h>
#include <maya/MArgList.h>
#include <maya/MFnPlugin.h>
#include <maya/MPxCommand.h>
#include <maya/MIOStream.h>

main.h

#include "stdafx.h"


class hello : public MPxCommand
{
public:
	hello(){ }
	virtual ~hello(){ }
	static void* creator(){ return new hello; }
	bool isUndoable() const{ return false; }

	MStatus doIt( const MArgList& args );
};

main.cpp

#include "stdafx.h"
#include "main.h"


MStatus hello::doIt( const MArgList& args )
{
	displayInfo("Hello "+args.asString(0)+"\n");
	return MS::kSuccess;
}


MStatus initializePlugin( MObject obj ) {
	MFnPlugin plugin( obj, "Autodesk", "1.0", "Any" );
	plugin.registerCommand( "hello", hello::creator );
	cout << "initializePlugin()\n";
	return MS::kSuccess;
}


MStatus uninitializePlugin( MObject obj ) {
	MFnPlugin plugin( obj );
	plugin.deregisterCommand( "hello" );
	cout << "uninitializePlugin()\n";
	return MS::kSuccess;
}

I use precompiled headers for all external stuff and all finished parts (so I can have multiple nodes and commands in one plugin and work on them one by one), you don't have to and in that case can dump the contents of stdafx.h into main.h and leave it out.

Now let's go to the project settings: Select the project you just created in the solution explorer and hit ALT + F7
Note that you select the Project and not the Solution, or the wrong options will appear (being you miss about every category that I'm going to mention next).

In the options set Configuration (at the top) to All Configurations. If you are developing for Maya 64 bit click the Configuration Manager... button, then in Active solution platform select . Most likely it will already be set to x64 and to copy from Win32. Do this, otherwise type x64 in the top box and set Copy settings from to Win32.

Now we're building for x64, so we can continue. Platform could be set to either Active or, preferably, all (in case you wish to change to x86 you already have the majority of the settings in place).

Then on the left go to Configuration Properties, expand it and hit General. There in General set Target Extension to .mll and set Configuration Type to Dynamic Library (.dll). This is the maya plugin format. The rest can remain as is.

So we have at least one .cpp (main.cpp) file, otherwise we do not get the C/C++ options. So do create these test files please. Then expand C/C++ and go to General under that category.

Additional Include Directories should be something along the lines of:
C:\Program Files\Autodesk\Maya2013\include;%(AdditionalIncludeDirectories)
You can click the text field, then the arrow appearing on the right, hit , in the window click the new folder icon, click the appeared '...' button and browse to your Maya installation (and version), select include and hit ok and again ok. Or you can just copy paste the text I just gave you.

Now under C/C++ go to Preprocessor and in Preprocessor Definitions paste:
WIN32;NDEBUG;_WINDOWS;NT_PLUGIN;REQUIRE_IOSTREAM;%\(PreprocessorDefinitions)
For Debug mode you may wish to explicitly define DEBUG instead of NDEBUG. Do not get confused by the WIN32, this also goes for 64-bit systems.

Then, also in C/C++, head to Precompiled Headers. If you want those that is, set it to Create (/Yc), if you use my test files the Header File should be stdafx.h and the Output File can remain default.

Now expand the Linker category, here in General somewhere halfway down you must set the Additional Library Directories. Same as the include, except it is named lib rather than include:
C:\Program Files\Autodesk\Maya2013\lib;%(AdditionalLibraryDirectories)

In Linker -> Input go to Additional Dependencies, you can edit this manually and separte each entry by enters or semicolons, or just paste this again:
Foundation.lib;
OpenMaya.lib;
OpenMayaUI.lib;
OpenMayaAnim.lib;
OpenMayaFX.lib;
OpenMayaRender.lib;
Image.lib;
opengl32.lib;
%(AdditionalDependencies)

This opens up the full maya functionality, most of the time the UI, FX and Render libs are not necessary.

When developing a plugin that is not using the simple plugin macro you need to define your own initialize and uninitialize to register all the commands and nodes. The docs will tell you that the simplecommand can only make plugins with one single command, which is so limited that I wonder why you would do something that simple in C++ rather than Python. So in Linker -> Command Line under additional options enter:
/export:initializePlugin /export:uninitializePlugin
This allows the functions to be exposed from the DLL and to be called by maya when loading the plugin. Which is necessary for the test files I gave above.

Now build and get angry if it doesn't work. Also post your errors so I can add the solutions in the relevant places above.

Leave a Reply

Your email address will not be published. Required fields are marked *