Load and run Python scripts
-
On 26/01/2014 at 07:33, xxxxxxxx wrote:
User Information:
Cinema 4D Version: R15
Platform: Windows ;
Language(s) : C++ ;---------
I trying to load and run a python script using the example given.
It keeps coming up with FILEERROR = -100 =FILEERROR_WRONG_VALUE
[URL-REMOVED]; ???What am I doing wrong?
case 1000: //do command { Filename fn = "test.py"; BaseList2D *op=(BaseList2D* )AllocListNode(ID_PYTHONSCRIPT); //BaseDocument *doc = GetActiveDocument(); //Int32 error = ReadHyperFile(doc,op,fn,'scrp',nullptr); Int32 error = ReadHyperFile(nullptr,op,fn,'scrp',nullptr); GePrint ("error: " + String::IntToString(error)); // always FILEERROR_WRONG_VALUE? if (op && ReadHyperFile(nullptr,op,fn,'scrp',nullptr)==FILEERROR_NONE) { BaseBitmap *ptr=nullptr; op->Message(MSG_SCRIPT_EXECUTE,&ptr); //annotations = op->GetDataInstance()->GetString(PYTHONSCRIPT_TEXT); } break; }
Note: I am doing the ReadHyperFile twice to get the FILEERROR more clear.
The script is found, if else a -1 would be given.Also, I do not know how to define "annotations", String?
[URL-REMOVED] @maxon: This section contained a non-resolving link which has been removed.
-
On 28/01/2014 at 07:44, xxxxxxxx wrote:
Hi,
I can confirm this issue with ReadHyperFile().
I've got the same FILEERROR_WRONG_VALUE returned with a valid script filename.I've asked the developers on this issue.
-
On 29/01/2014 at 05:07, xxxxxxxx wrote:
Hi,
The documentation is wrong.
The example code for ID_PYTHONSCRIPT only works with ID_COFFEESCRIPT because a Python script isn't a hyper file (a COFFEE script is).
This is why ReadHyperFile() is always returning FILEERROR_WRONG_VALUE.The Python script content has to be put manually into PYTHONSCRIPT_TEXT.
-
On 29/01/2014 at 06:30, xxxxxxxx wrote:
Thanks for the quick reply.
Could you please provide an example? -
On 29/01/2014 at 08:26, xxxxxxxx wrote:
Here's how to execute a Python script with the C++ API:
Filename fname = "path/to/script.py"; AutoAlloc<BaseFile> file; // Allocate a Python script node BaseList2D* op = (BaseList2D* )AllocListNode(ID_PYTHONSCRIPT); if (op!=nullptr && file->Open(fname)) { BaseContainer* data = op->GetDataInstance(); if (data==nullptr) return false; // Allocate a memory buffer to hold the Python script file Int64 length = file->GetLength(); Char* text = NewMem(Char, length+1); if (text==nullptr) return false; // Read the Python script Char ch; while (file->ReadChar(&ch)) text[file->GetPosition()-1]= ch; text[length] = '\0'; // Set the Python script text in the Python script node container data->SetString(PYTHONSCRIPT_TEXT, text); // Do not forget to delete the memory buffer for the script text DeleteMem(text); // Execute the script op->Message(MSG_SCRIPT_EXECUTE, nullptr); } // Delete the Python script node blDelete(op);
-
On 30/01/2014 at 05:15, xxxxxxxx wrote:
Great, it works, thanks.
One addition. Apparently there is some mismatch wit the end -of-line handling, because I have to add a remark (#) at the end of every line. But it works!import c4d # from c4d import gui # #Welcome to the world of Python # # def main() : # gui.MessageDialog('Hello World!') # # if __name__=='__main__': # main() #