PDA

View Full Version : API - Custom context


laluneverte
11-21-2012, 01:59 PM
Hey guys,

I have looked all through google, and I am not able to figure out what is going on.
I am working on a custom context maya, implemented the MPxContext and MPxContextCommand and all.
I have been using the marquee example in the devkit. Now, it compiles and all, but when I call the context
command I get a MEL error without any message (like: Error: line 1)

I am using Maya 2013 SP1. Is there anything special you have to do in MEL to activate
a custom context? afaik just running the Context command (eval "MyToolContext;") should work, no?

To me it seems I must be missing something, here's a header file:

class CPToolCmd : public MPxToolCommand
{
public:
CPToolCmd();
virtual ~CPToolCmd();

static void* creator();

MStatus doIt(const MArgList& args);
MStatus redoIt();
MStatus undoIt();
bool isUndoable() const;
};

class CPToolContext : public MPxContext
{
public:
CPToolContext();
virtual void getClassName(MString & name) const;
virtual void toolOnSetup(MEvent &event);
virtual void toolOffCleanup();
virtual MStatus doPress(MEvent &event);
virtual MStatus doDrag(MEvent &event);
virtual MStatus doRelease(MEvent &event);
virtual MStatus doEnterRegion(MEvent &event);

private:
short startPos_x, startPos_y;
short endPos_x, endPos_y;
M3dView view;
};

class CPToolContextCmd : public MPxContextCommand
{
public:
virtual MPxContext* makeObj();
static void* creator();
};

I am quite stuck - anything helps.

Thanks,

-H

laluneverte
11-21-2012, 03:08 PM
Here's the cpp file:

#include "CPToolContext.h"
#include <maya/MCursor.h>

CPToolCmd::CPToolCmd()
{
setCommandString("CPToolCmd");
}

CPToolCmd::~CPToolCmd()
{
}

void* CPToolCmd::creator()
{
return new CPToolCmd;
}

MStatus CPToolCmd::doIt(const MArgList &args)
{
return redoIt();
}

MStatus CPToolCmd::redoIt()
{
// we don't do anything during the tool really
return MStatus::kSuccess;
}

MStatus CPToolCmd::undoIt()
{
// TODO: we should fire the undo within CP
return MStatus::kSuccess;
}

bool CPToolCmd::isUndoable() const
{
return false;
}

const char helpString[] = "Click and drag to interact with Creation Platform.";

CPToolContext::CPToolContext()
{
setTitleString("Creation Platform Tool");
//setImage("c:\\users\\helge\\Desktop\\CPTool.xpm", MPxContext::kImage1 );
}

void CPToolContext::getClassName( MString & name ) const
{
name.set("CPToolContext");
}

void CPToolContext::toolOnSetup(MEvent &)
{
setCursor( MCursor::editCursor);
setHelpString(helpString);
}

void CPToolContext::toolOffCleanup()
{
// TODO: pop manipulator in CP
}

MStatus CPToolContext::doPress(MEvent &event)
{
MGlobal::displayInfo("press...");
event.getPosition(startPos_x, startPos_y);
return MS::kSuccess;
}

MStatus CPToolContext::doDrag(MEvent & event)
{
MGlobal::displayInfo("drag...");
event.getPosition(endPos_x, endPos_y);
return MS::kSuccess;
}

MStatus CPToolContext::doRelease( MEvent & event)
{
event.getPosition(endPos_x, endPos_y);
MGlobal::displayInfo("releasing...");

//CPTool * cmd = (CPTool*)newToolCommand();
//cmd->redoIt();
//cmd->finalize();
return MS::kSuccess;
}

MStatus CPToolContext::doEnterRegion( MEvent & )
{
MGlobal::displayInfo("enter region...");
return setHelpString( helpString );
}

MPxContext* CPToolContextCmd::makeObj()
{
return new CPToolContext;
}

void* CPToolContextCmd::creator()
{
return new CPToolContextCmd;
}

laluneverte
11-21-2012, 03:08 PM
And here's the initializePlugin code:

MStatus initializePlugin(MObject obj)
{
MFnPlugin plugin(obj, "CPMayaToolPlugin", "1.0", "Any");
MStatus status;

status = plugin.registerContextCommand("CPToolContext", CPToolContextCmd::creator, "CPToolCommand", CPToolCmd::creator );
return status;
}

Keilun
11-21-2012, 06:39 PM
I think the problem is your understanding of the classes. There are 3 classes that are involved with contexts:

MPxContext - the interactive context itself. This handles your mouse/key events and performs your tool's tasks.

MPxContextCommand - the command to create/edit/query instances of your custom context

MPxToolCommand - a MEL command that can be invoked from within the MPxContext as a means of finalizing the result of your context - ie. what is run when the user hits enter and exits the tool).

In your case, it appears that you're expecting MPxToolCommand to be creating and setting the context. That is not correct. You should instead be invoking your MPxContextCommand. This is responsible for creating an instance of your context as well as editing parameters on your context if you need to configure settings.

By default the context command should create a new context and return you its name. In order to then activate that context you need to invoke the setToolTo command on the returned context object. For example:

// Create an instance of my context.
string $myCtxObj = `myCtxCmd`;

// Now activate the context
setToolTo $myCtxObj;

In your case, this translates to:

string $cpToolCtx = `CPToolCommand`;
setToolTo $cpToolCtx;

You should see the active tool in the left pane change to show you the new active context. If you haven't set an icon for it, it will likely be a blank icon. And that's it.

Hope this helps.

FabricPaul
11-21-2012, 08:36 PM
We got some help elsewhere - this was related to the NT_PLUGIN define missing.

Thanks for your help, apologies for not posting the resolution earlier.

CGTalk Moderation
11-21-2012, 08:36 PM
This thread has been automatically closed as it remained inactive for 12 months. If you wish to continue the discussion, please create a new thread in the appropriate forum.