Using Python from the c++ sdk



  • THE POST BELOW IS MORE THAN 5 YEARS OLD. RELATED SUPPORT INFORMATION MIGHT BE OUTDATED OR DEPRECATED

    On 09/09/2011 at 08:11, xxxxxxxx wrote:

    User Information:
    Cinema 4D Version:   13 
    Platform:   Windows  ;   
    Language(s) :     C++  ;   PYTHON  ;

    ---------
    Hallo,

    i wrote some c++ plugins for C4D V11.5, which embed python modules. Now i want to upgrade to V13 and none of my python-using plugins work anymore. My guess is, that it has to do with the C4D API embedding python itself and so interfering with my code.

    Usually my PluginStart() method crashes instantly when PyImport_ImportModule() is called.

    Bool PluginStart(void)  
    {  
      Py_Finalize();  
      
      if (Py_IsInitialized() != 0)  
      {  
          // import  python module  
          PyObject *pyObj = PyImport_ImportModule("some.module.name");  
      
          // use the module  
          // ...  
      
          Py_Finalize();  
       }  
      
      return TRUE;  
    }
    

    I started looking around in the API and found the file "lib_py.h" which defines classes for using python. I messed around with some of its functionality and i would love to use it, but without a proper documentation it's not possible

    So here comes my question: Is there any documentation for those classes? I didn't find anything in the current SDK docs.
    If not, could someone give my a hint what i can do to fix my code and make it work again?

    Edit:
    Looks like i have to aquire a GIL to make it work, but this will take some time for to understand the mechanisms. Maybe some can give me a brief introduction?



  • THE POST BELOW IS MORE THAN 5 YEARS OLD. RELATED SUPPORT INFORMATION MIGHT BE OUTDATED OR DEPRECATED

    On 12/09/2011 at 05:59, xxxxxxxx wrote:

    The lib_py.h library is private. It's currently only used by the character builder. Maybe something for future releases.

    I have to forward your problem to our developers because I'm not familiar with Python.

    cheers,
    Matthias



  • THE POST BELOW IS MORE THAN 5 YEARS OLD. RELATED SUPPORT INFORMATION MIGHT BE OUTDATED OR DEPRECATED

    On 12/09/2011 at 06:32, xxxxxxxx wrote:

    Hi, in your code you call Py_Finalize() which does a shutdown of the interpreter and shouldnt be called in the C4D environment. Because Python code in c4d is called from several threads, you have to ensure and release the GIL around Python API calls. In this case just create an instance of GePythonGIL before you call the Python API functions.

      
    GePythonGIL gil_state;  
    PyAPICalls1();  
    PyAPICalls2();  
    [ ... ]  
    

    To get more background information on this check out this page: http://docs.python.org/c-api/init.html.

    Cheers, Sebastian


Log in to reply