PDA

View Full Version : imbedding a python plugin c++ mll?


Chrisgibbs
01-29-2013, 08:03 PM
I have a plugin that requires a command to make the necessary connections within Maya. Python is so much easier than C++ to make dg connections. Is it possible for me to write the command in python and imbed it in the c++/mll code somehow?

ChipsAhoy
01-29-2013, 08:12 PM
Are you just looking to execute Python commands from a C++ plugin? If so you can pass the Python code through the MGlobal::executePythonCommand method inside your plugin.

zoharl
01-30-2013, 03:54 AM
http://download.autodesk.com/us/maya/2011help/API/class_m_global.html#be22d652c5a62c33095afa28562db99f

Chrisgibbs
01-30-2013, 11:12 PM
http://download.autodesk.com/us/maya/2011help/API/class_m_global.html#be22d652c5a62c33095afa28562db99f

Thanks zoharl, would the python command be a separate file or is there a way to compile the python code along with the c++ code within one VS solution? The reason I want this is so that when the user loads the plugin, all the commands necessary to use the plugin are loaded as well. In a straight forward c++ plugin, I would compile the commands along with the dependency nodes. I'm not sure how to combine Python code.

zoharl
01-31-2013, 01:03 AM
It executes a python command as if you executed it in the script editor. Which means that if you call external scripts, they should be in the path like they are required when calling them within maya. If you don't want to use external scripts, the simplest solution would be to embed a python script as a long c++ string. I think it should be quite straight forward to write a script that takes a python script and convert it to a C++ const char *. It should involve putting each line of the python script in quotation marks, escaping special characters, and connecting all these strings with + and '\n'.
Another option might be to embed the python script as a string resource (resource: dialog, icon, etc...).

zoharl
01-31-2013, 01:20 AM
Other ideas:

http://stackoverflow.com/questions/72616/embed-data-in-a-c-program
http://stackoverflow.com/questions/2933295/embed-text-file-in-a-resource-in-a-native-windows-application

Chrisgibbs
01-31-2013, 01:38 AM
It executes a python command as if you executed it in the script editor. Which means that if you call external scripts, they should be in the path like they are required when calling them within maya. If you don't want to use external scripts, the simplest solution would be to embed a python script as a long c++ string. I think it should be quite straight forward to write a script that takes a python script and convert it to a C++ const char *. It should involve putting each line of the python script in quotation marks, escaping special characters, and connecting all these strings with + and '\n'.
Another option might be to embed the python script as a string resource (resource: dialog, icon, etc...).

yup, I was afraid of that. Thanks zoharl

zeroeffect
01-31-2013, 11:46 AM
Another option for including external resources:

If you build as a Qt project (Maya 2011+) , you can include the python file in the .qrc (resource) file. These files are compiled into the executable and can be opened and read similar to a file that exists on disk (by using the resource path).

e.g.
QFile file(":/python/test_file.py");
if (!file.open(QIODevice::ReadOnly | QIODevice::Text)) {
Logger::displayError("Failed to open python file");
}
QByteArray data = file.readAll();
MString command = MQtUtil::toMString(QString(data));
MGlobal::executePythonCommand(command);
There are several benefits to this approach, most notably, it is cross-platform. It also allows you to write and maintain the python code in a more suitable python editor. For larger chunks of code, it can become a real pain trying to hard code python directly in C++ (managing the newlines, tab spacing, etc...).

The python file can read and executed when the plugin is loaded, making all of the functions available to the plugin (through executePythonCommand), and the user.

CGTalk Moderation
01-31-2013, 11:46 AM
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.