In order to run a python script from c++, the script must be read into memory before it can be executed.
Except char and string handling in the c++ sdk is somewhat puzzling me.
I have following code, but nothing happens (it seems cinema 4d is in a loop)?
Filename fname = "C:\\Program Files\\Maxon\\CINEMA 4D R20.026\\plugins\\commandline\\test.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();
// pChar* text = NewMem(Char, length + 1); // Old R15 code
maxon::String text;
// 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
// pDeleteMem(text);
// Execute the script
op->Message(MSG_SCRIPT_EXECUTE, nullptr);
Or should I use HF file and if so,how should I do that?