Noctua

CG Tools

Noctua header image 2

Extending maya with C# -part 1-

July 18th, 2008 · 2 Comments · Maya tools, Programming

This sample project provide a maya command  plugin which simply display a winform containing a single button and a list view . When  the button is pressed, the name of each mesh found in the current scene is displayed in the list box.

Download the visual studio solution

Impementation
A cli ref class ”MyControl” make the bridge between the Maya Api and the c# library. 
The maya command simply create a “MyControl” instance.

 MStatus test::doIt( const MArgList& args )
{
    // build the winform !!
    MyControl^ ctrl= gcnew MyControl();
    //That's all !!
    MGlobal::displayInfo("oK test done");
    return redoIt();
}

The c# Library is accessible through the interface IView exposing :

  • An event : UpdateList  triggered when the user push the button
  • a List : ObjectList

MyControl Constructor register UpdateList event with a delegate pointing to UpdateMeshList.

MyControl::MyControl()
{
    _view = gcnew View();
    //Register event (fill the mesh list on button click)
    _view->UpdateList +=  gcnew System::EventHandler(this,&MyControl::UpdateMeshList);
}

UpdateMeshList simply fill ObjectList instance with the mesh names.

void MyControl::UpdateMeshList(Object^ sender, EventArgs^ e)
{
    MStatus ms;
    MItDag dagIt(MItDag::kDepthFirst,MFn::kMesh,&ms);
    MStatusThrowFailure(ms);
    _view->ObjectList->Clear();
    for (dagIt.reset();!dagIt.isDone();dagIt.next())
    {
        String^ sMesh = gcnew String( dagIt.fullPathName(&ms).asChar() );
        if (sMesh !="")
        {
            _view->ObjectList->Add(sMesh);
        }
        MStatusThrowFailure(ms);
    }
}

Realy easy no ?  Soon, I will show how  to place the winform inside a Maya panel to get a better integration. 

Compilation Hints :

  • The “MayaPlug” project is a standard maya command-plugin except that it was compiled with /clr and linked with /noentry
  • The solution use the environement variable $(MAYA_PATH). If you didn’t set this environement variable don’t forget to replace it by the maya directory path in the “MayaPlug” project properties (Additional Library Directories & Additionnal Include Directories). 
  • The “#include Maya\MFnPlugin.h” is preceded by #pragma unmanaged to avoid the compilation of the dll entry point function to MSIL.

Tags: ····

2 responses so far ↓

  • 1 Thomas Goddard // Jan 14, 2009 at 10:48 am

    Wow! Great job!

  • 2 Erik // Feb 3, 2010 at 3:59 pm

    Thank you for this insight! i’m about to embark on a mission to start learning C# .NET and Maya and this will help me alot, Thanks!

Leave a Comment