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. 

On Sunday, November 22, 2015 at 12:15:29 PM UTC+1, Peter Makal wrote:
>
> 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?
>
>

-- 
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 [email protected].
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.

Reply via email to