Loading a Python script from a file in C++ plugin

100 views
Skip to first unread message

Peter Makal

unread,
Nov 22, 2015, 6:15:29 AM11/22/15
to Python Programming for Autodesk Maya
I'm trying to write my first Maya plugin and at the very beginning I'm starting to have problems...
In my initializePlugin() function I wanted to add some Python script to execute when my plugin is loaded and because this script is quite large I've put it in separate text file. I added to my C++ code simple function to load content of that file and store it in string object:


std
::string loadTextFile(const std::string& filePath)
{
    std
::ifstream file;
    file
.open(filePath.c_str());

    std
::string output;
    std
::string line;

   
if (file.is_open())
   
{
       
while (file.good())
       
{
            getline
(file, line);
            output
.append(line + "\n");
       
}
   
}
   
else
   
{
        std
::cerr << "Unable to load file: " << filePath << std::endl;
   
}

   
return output;
}

And then in initializePlugin() I invoke loadTextFile() and pass my string object as an argument using .c_str() method in MGlobal::executePythonCommand(). But when I build my .mll file and try to load that in Maya I get error message from my loadTextFile() function: Unable to load file...

My first guess is that the .txt file is in wrong place. For now, just for testing purposes, I have simple test.txt file with just one line of code: print "Hello!" and as an argument I pass "text.txt" to the
loadTextFile() but I've tried to put that file in different folders, starting with the most obvious: in the same folder where my source files and header files are, in the folder where my .mll file is... but none of those works and I get the same message.

Does anybody know what may be an issue here? Or maybe there is some another way to load Python script into plugin code and then execute it?

Peter Makal

unread,
Nov 22, 2015, 8:42:18 AM11/22/15
to Python Programming for Autodesk Maya
Ok, I finally manage to read Python code from my file. I checked current working directory in my plugin code and it happens to be Maya installation folder which is not ideal but for now it will suffice.
I guess I should use MEL getenv MAYA_PLUG_IN_PATH command and extract some absolute path where my plugin will be installed.

Justin Israel

unread,
Nov 22, 2015, 3:12:00 PM11/22/15
to Python Programming for Autodesk Maya
You may be able to simplify this whole thing by either using MGlobal::executePythonCOmmand("execfile <myScript.py>") or using the module system to bundle your extra files and then be able to query the module path (getModulePath).

As a side note, I think you could shorten your process for reading a file to just reading it into a buffer, instead of reading each line, and building up strings:
std::ifstream ifs("myfile.txt");
std::stringstream stream;
stream << ifs.rdbuf();
return stream.str();

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/c2a85735-a2e3-48a7-9cf9-54368ddb8e30%40googlegroups.com.
For more options, visit https://groups.google.com/d/optout.

Peter Makal

unread,
Nov 22, 2015, 5:41:12 PM11/22/15
to Python Programming for Autodesk Maya
I will definitely try this.
Thanks Justin!
To unsubscribe from this group and stop receiving emails from it, send an email to python_inside_maya+unsub...@googlegroups.com.

Justin Israel

unread,
Nov 22, 2015, 7:57:45 PM11/22/15
to Python Programming for Autodesk Maya
Cool. Let us know if you end up solving it!

To unsubscribe from this group and stop receiving emails from it, send an email to python_inside_m...@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_m...@googlegroups.com.
To view this discussion on the web visit https://groups.google.com/d/msgid/python_inside_maya/fe23dadb-b36a-4773-bbbd-352a4bb8cbb7%40googlegroups.com.
Reply all
Reply to author
Forward
0 new messages