Calling my C++ plugin with args via Python maya.cmds

506 views
Skip to first unread message

Michael Boon

unread,
Mar 6, 2017, 1:52:27 AM3/6/17
to Python Programming for Autodesk Maya
Hi all, I have yet another question about arguments to Maya C++ plugins.

I have a plugin that accepts flags and objects using an MSyntax and MArgDatabase, and everything works as expected when I call it from MEL, like this:
myExportCmd -f "c:/temp" -verbose "pCube3";

Calling it from Python like this works:
maya.cmds.myExportCmd('-f', 'c:/temp', '-verbose', 'pCube3')

But calling it the expected way doesn't:
maya.cmds.myExportCmd('pCube3', f='c:/temp', verbose=True)

If I do it like the last example above, the plugin gets the "pCube3" object string, but doesn't get any flag arguments at all.

Is that expected? I've seen tutorials on writing plugins in Python that work correctly (like the last example above), but I can't find mention online of C++ plugins that do. I thought there would be mention of it not working, though.

Thanks again!

Justin Israel

unread,
Mar 6, 2017, 2:29:30 AM3/6/17
to Python Programming for Autodesk Maya

Can we see an example of how you are defining your MSyntax? Are you use addArg()? If so you probably want to switch to using the object list instead

http://help.autodesk.com/cloudhelp/2016/ENU/Maya-SDK/cpp_ref/class_m_syntax.html#a299f2c91c864387f1b4208e99a72631d

Justin


--
You received this message because you are subscribed to the Google Groups "Python Programming for Autodesk Maya" group.
To unsubscribe from this group and stop receiving emails from it, send an email to python_inside_m...@googlegroups.com.
To view this discussion on the web visit https://groups.google.com/d/msgid/python_inside_maya/8efe0c53-8bfd-4c53-9cf1-7ba90939f72d%40googlegroups.com.
For more options, visit https://groups.google.com/d/optout.

Michael Boon

unread,
Mar 6, 2017, 5:58:06 PM3/6/17
to Python Programming for Autodesk Maya
Sure. I take it from that that it should work in Python? I'll keep investigating then (and your help is appreciated!)
static MSyntax getSyntax()
{
       
MSyntax syntax;
        syntax
.addFlag(kOutFileFlagShort, kOutFileFlag, MSyntax::kString);
        syntax
.addFlag(kVerboseFlagShort, kVerboseFlag);
        // ... More addFlag calls here
        syntax
.enableQuery(false);
       syntax.enableEdit(false);
        syntax
.setObjectType(MSyntax::kStringObjects, 1);
       
return syntax;
}

I am registering my command with the getSyntax function:
plugin.registerCommand( MY_EXPORT_CMD, myExportCmd::creator, myExportCmd::getSyntax);


Calls like this return true if I call the plugin from MEL, false if I call it from Python:
flagSet = inputArgs->isFlagSet(kVerboseFlag, &status);

When I print out my args, I don't see the flags at all when I call using regular Python syntax. I only see the objects (just 'pCube3' in this example).
MArgList args2 = MArgList(args); // This line is interesting. If I call from Python, I get nothing at all from the << operator unless I do this. Calling from MEL, it works with or without this line.
MString argsStr;
std::ostringstream stream;
stream
<< args2;
std::string str = stream.str();
argsStr = MString(str.c_str());
MGlobal::displayInfo(argsStr);



On Monday, 6 March 2017 18:29:30 UTC+11, Justin Israel wrote:

Can we see an example of how you are defining your MSyntax? Are you use addArg()? If so you probably want to switch to using the object list instead

http://help.autodesk.com/cloudhelp/2016/ENU/Maya-SDK/cpp_ref/class_m_syntax.html#a299f2c91c864387f1b4208e99a72631d

Justin


On Mon, Mar 6, 2017, 7:52 PM Michael Boon <boon...@gmail.com> wrote:
Hi all, I have yet another question about arguments to Maya C++ plugins.

I have a plugin that accepts flags and objects using an MSyntax and MArgDatabase, and everything works as expected when I call it from MEL, like this:
myExportCmd -f "c:/temp" -verbose "pCube3";

Calling it from Python like this works:
maya.cmds.myExportCmd('-f', 'c:/temp', '-verbose', 'pCube3')

But calling it the expected way doesn't:
maya.cmds.myExportCmd('pCube3', f='c:/temp', verbose=True)

If I do it like the last example above, the plugin gets the "pCube3" object string, but doesn't get any flag arguments at all.

Is that expected? I've seen tutorials on writing plugins in Python that work correctly (like the last example above), but I can't find mention online of C++ plugins that do. I thought there would be mention of it not working, though.

Thanks again!

--
You received this message because you are subscribed to the Google Groups "Python Programming for Autodesk Maya" group.
To unsubscribe from this group and stop receiving emails from it, send an email to python_inside_maya+unsub...@googlegroups.com.

Michael Boon

unread,
Mar 8, 2017, 12:00:08 AM3/8/17
to Python Programming for Autodesk Maya
So, I was doing two things wrong, it seems:
First, I was passing my MArgList object by reference into a function, where I created an MArgDatabase object from it. Now I create my MArgDatabase in my doIt function and it works. (So far I have still been able to pass the MArgDatabase into the other function, which is good.)
Second, the << operator doesn't show flags from Python, only from MEL. I was using that for debugging, and it was misleading me.

I've given up trying to keep MArgList and MArgDatabase around at all. As Justin pointed out in answer to my previous question, "it is possible that this class isn't safe to copy and reuse." I've now crashed Maya a few times by trying to call isFlagSet later during my command execution. So now I have a page or so of extra code to maintain, but it doesn't crash :)

Cameron Fulton

unread,
Mar 8, 2017, 12:16:47 AM3/8/17
to python_in...@googlegroups.com
Question, just because this was my problem before, is the short versions of your flags under 4 characters? I was having a similar issue and it was because my short flags were too long. 

To unsubscribe from this group and stop receiving emails from it, send an email to python_inside_maya+unsubscribe@googlegroups.com.

--
You received this message because you are subscribed to the Google Groups "Python Programming for Autodesk Maya" group.
To unsubscribe from this group and stop receiving emails from it, send an email to python_inside_maya+unsub...@googlegroups.com.

Michael Boon

unread,
Mar 8, 2017, 12:20:35 AM3/8/17
to Python Programming for Autodesk Maya
Yes my short names are all under 4 characters. My long names were all 4 or more at one point, but now I've shortened a few of them (back to what they were before I started working on this plugin) and TBH I'm not sure if they were all 4+ characters when I had this problem. The names I was using were all the appropriate lengths, but some of the others may not have been. So yeah, that might have been the issue, thanks.
To unsubscribe from this group and stop receiving emails from it, send an email to python_inside_maya+unsub...@googlegroups.com.
To view this discussion on the web visit https://groups.google.com/d/msgid/python_inside_maya/8efe0c53-8bfd-4c53-9cf1-7ba90939f72d%40googlegroups.com.
For more options, visit https://groups.google.com/d/optout.
Reply all
Reply to author
Forward
0 new messages