Maxon Developers Maxon Developers
    • Documentation
      • Cinema 4D Python API
      • Cinema 4D C++ API
      • Cineware API
      • ZBrush Python API
      • ZBrush GoZ API
      • Code Examples on Github
    • Forum
    • Downloads
    • Support
      • Support Procedures
      • Registered Developer Program
      • Plugin IDs
      • Contact Us
    • Categories
      • Overview
      • News & Information
      • Cinema 4D SDK Support
      • Cineware SDK Support
      • ZBrush 4D SDK Support
      • Bugs
      • General Talk
    • Unread
    • Recent
    • Tags
    • Users
    • Login

    Can C++ execute python? [SOLVED]

    SDK Help
    0
    6
    627
    Loading More Posts
    • Oldest to Newest
    • Newest to Oldest
    • Most Votes
    Reply
    • Reply as topic
    Log in to reply
    This topic has been deleted. Only users with topic management privileges can see it.
    • H
      Helper
      last edited by

      On 25/09/2014 at 16:28, xxxxxxxx wrote:

      User Information:
      Cinema 4D Version:   14 
      Platform:      Mac OSX  ; 
      Language(s) :     C++  ;

      ---------
      is it possible to run some Python code inside a C++ plugin?
      I mean, can I embed some Python code inside a C++ plugin so that it executes a bit of Python while being executed?

      1 Reply Last reply Reply Quote 0
      • H
        Helper
        last edited by

        On 25/09/2014 at 18:34, xxxxxxxx wrote:

        I suppose that from a simple perspective, you should be able to execute a Python script from C++ using the C++ SDK.  But whether this is possible or not from the C++ SDK, I don't know.  It may be as simple as CallCommand() with the Python plugin's ID.  Have you checked the C++ SDK docs?

        1 Reply Last reply Reply Quote 0
        • H
          Helper
          last edited by

          On 26/09/2014 at 01:51, xxxxxxxx wrote:

          Hi Rui,

          Python scripts can be loaded and executed with the ID_PYTHONSCRIPT node.
          The example in its documentation isn't currently complete and useful:
          'fn' variable for the ReadHyperFile() call is the filename of the script to load.
          Send MSG_SCRIPT_EXECUTE instead of MSG_SCRIPT_RETRIEVEBITMAP to execute the script.

          1 Reply Last reply Reply Quote 0
          • H
            Helper
            last edited by

            On 26/09/2014 at 02:41, xxxxxxxx wrote:

            But the script had to be already typed and saved, right?
            What I was thinking was something like this:

            I have a python script, like, for example, this (this is a very simple script, just for the sake of example) :

            if __name__ == "__main__":
            return "ABC"

            I would save this as, for example, test.pyp
            Then I would protect it with Cinema 4D's "Source Protector" to create a .pype and/or .pypv file.
            Then, if possible, I would like to load and run the script from C++, getting a return value.
            It would be something like:

            String result;
            result=Some_way_to_call_the_Python_Script();

            and get the result from it.
            Better yet, if was possible to pass parameters along to the script.
            Is this in any way possible?

            1 Reply Last reply Reply Quote 0
            • H
              Helper
              last edited by

              On 26/09/2014 at 07:53, xxxxxxxx wrote:

              These examples are pulled from my notes.
              They seem to indicate that the file extension in each case is important.
              So you might not be able to use an encrypted python file.

              //This code loads and executes a saved coffee script file  
              //NOTE: The script file has to be saved from C4D in the .CSC file format  
                
              BaseDocument *doc = GetActiveDocument();  
                
              Filename fn = "C:\\Users\\user\\Desktop\\test.CSC";  
                
              BaseList2D *op=(BaseList2D* )AllocListNode(ID_COFFEESCRIPT);  
                
              if(op && ReadHyperFile(doc, op, fn,'scrp', nullptr) == FILEERROR_NONE)  
              {   
                  op->Message(MSG_SCRIPT_EXECUTE, nullptr);  
                  
                  //Print's the text of the script  
                  String scriptText = op->GetDataInstance()->GetString(COFFEESCRIPT_TEXT);  
                  GePrint(scriptText);  
              }  
                
                //Delete the Python script node  
                blDelete(op);  
                
                
                
                
                
                
                
                
              //This is how to execute a saved Python script in R13 & R14  
              //NOTE: The script file has to be saved from C4D in the .py file format  
                
                Filename fn = "C:\\Users\\user\\Desktop\\test.py";  
                AutoAlloc<BaseFile> file;  
                
                //Allocate a Python script node  
                BaseList2D* op = (BaseList2D* )AllocListNode(ID_PYTHONSCRIPT);  
                if(op!=nullptr && file->Open(fn))  
                {  
                  BaseContainer *data = op->GetDataInstance();  
                  if(data==nullptr) return FALSE;  
                
                  //Allocate a memory buffer to hold the Python script file  
                  LONG length = file->GetLength();  
                
                  char *text = new char[length+1];          
                  if(text == nullptr) return FALSE;  
                
                  //Read the Python script one character at a time  
                  char ch;  
                  while(file->ReadChar(&ch))  
                  {  
                     text[file->GetPosition()-1] = ch;  
                  }  
                
                  //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  
                  text[length] = '\0';  
                  delete[] text;  
                
                  //Execute the script  
                  op->Message(MSG_SCRIPT_EXECUTE, nullptr);  
                
                  //Print's the text of the script  
                  String scriptText = op->GetDataInstance()->GetString(PYTHONSCRIPT_TEXT);  
                  GePrint(scriptText);  
                }  
                
                //Delete the Python script node  
                blDelete(op);  
                
                
                
                
                
                
                
                
              //This is how to execute a python script in R15  
                
                Filename fname = "C:\\Users\\user\\Desktop\\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();  
                  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);
              

              -ScottA

              1 Reply Last reply Reply Quote 0
              • H
                Helper
                last edited by

                On 26/09/2014 at 10:27, xxxxxxxx wrote:

                Well, I guess this will help me a lot 🙂
                Even if the python script needs to be in .py (meaning that is ASCII and easy to read, I can encrypt it internally or even store it internally (not in a file).
                The only problem I see here is that I can't return values from the python script to use in C++.
                I will give this a try. Thank you very much, Scott 🙂

                1 Reply Last reply Reply Quote 0
                • First post
                  Last post