Is example ExecutePythonScript() also avialable in R19



  • Maxime pointed me this great example ExecutePythonScript() of running a python script from c++.
    See https://plugincafe.maxon.net/topic/12439/reading-script-file-into-memory/4

    I succeeded to compile this example in R20.
    Now, because we still have user running R19, I want to do the same in R19.

    Is this possible?
    Is there a similar example for R19.
    Especially, setting scope variables is really good and what I need.

    I can use the example from https://plugincafe.maxon.net/topic/8164/10638_can-c-execute-python-solved/5, but that one does not support scope variables (I guess).



  • The interfaces used in the ExecutePythonScript() are part of the python.framework, which was introduced with R20. So I guess that there is nothing equivalent in prior versions.



  • Yes, I think so too.
    Thanks.



  • Hi Pim sorry for the late reply, this is possible by using the lib_py.h (typically this is somehow what we used before R20 to expose feature in python, but it's way elder, not everything is possible to do, you have less control than with the new python.framework, and the lib_py.h is not maintained (so don't expect any enhancement in it) Only the python.framework will be enhanced.

    // Init Python
    GePython pythonScope;
    pythonScope.Init();
    
    // Acquiere the GIL
    GePythonGIL gil_state;
    
    pythonScope.SetNode("doc", doc);
    // If doc->GetFirstObject() is nullptr, op variable will not be declared in the python scope, and you cant add a None.
    pythonScope.SetNode("op", doc->GetFirstObject());
    
    maxon::String pythonCode = "\
    import c4d\n\
    \n\
    def main():\n\
        print(doc, op)\n\
    \n\
    if __name__ == '__main__' :\n\
        main()\n\
    "_s;
    
    StopAllThreads();
    pythonScope.Run(pythonCode);
    

    Cheers,
    Maxime.