how to get the return value with MGlobal::executePythonCommandStringResult

20 views
Skip to first unread message

Rudi Hammad

unread,
Apr 30, 2023, 4:22:35 PM4/30/23
to Python Programming for Autodesk Maya
Hi,
I am writing a Plugin in C++ and I am having trouble figuring out how to get a return from python execute code.
For instance, this works perfectly:

 MString pyCommand2;
pyCommand2 = "cmds.joint()";
auto test2 = MGlobal::executePythonCommandStringResult(pyCommand2);
MGlobal::displayInfo(test2);

Here I get displayed "joint1", so it is catching the return. But this doesn't work

MString pyCommand;
pyCommand =
"def foo():\n"
"    xx = cmds.joint()\n"
"    return xx\n"
" foo()";

auto test1 = MGlobal::executePythonCommandStringResult(pyCommand);
MGlobal::displayInfo(test1);

Here the return value that MGlobal::displayInfo(test1) should provide is empty.
Any ideas?

thanks


Justin Israel

unread,
Apr 30, 2023, 5:14:38 PM4/30/23
to python_in...@googlegroups.com
"Executes a Python command that returns a string result from the command engine"

Given the documentation, executePythonCommandStringResult wants to parse the python statement through its own command engine. It kind of assumes that the statement sent to this particular function is a single command where it can automatically arrange to capture the string result. So if you feed it larger snippets, the parser gets all wonky. What you can do instead is first feed your supporting code to executePythonCommand(), and then just call executePythonCommandStringResult() with your command. The following works in python:

import maya.OpenMaya as om

bootstrap = """
def foo():
    xx = cmds.joint()
    return xx
"""
om.MGlobal.executePythonCommand(bootstrap)

c = "foo()"
ret = om.MGlobal.executePythonCommandStringResult(c)
print(ret)

--
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/f8e57985-df64-40fe-b8fb-12906ab943b2n%40googlegroups.com.

Rudi Hammad

unread,
Apr 30, 2023, 6:15:55 PM4/30/23
to Python Programming for Autodesk Maya
I spent 2 hours looking into this and you solved it in 2 minutes XD. I looked into that documentation definition before, but I didn't understand as you did what was going on.
All good now.(In cpp bootstrap is MString type).
Thanks Justin.

Justin Israel

unread,
Apr 30, 2023, 6:22:51 PM4/30/23
to python_in...@googlegroups.com
On Mon, May 1, 2023 at 10:15 AM Rudi Hammad <rudih...@gmail.com> wrote:
I spent 2 hours looking into this and you solved it in 2 minutes XD. I looked into that documentation definition before, but I didn't understand as you did what was going on.
All good now.(In cpp bootstrap is MString type).
Thanks Justin.

No worries. I actually didn't remember how this all works since it has been years. So I needed to have a quick play, and saw the issues with trying to send it all through the result command. It triggered some vague memories of having done this a long time ago and needing to set up the python env first, with the context code.
 
Reply all
Reply to author
Forward
0 new messages