Hi,
You can add new functions with the C++ API using the Python library defined in frameworks/cinema.frameworksource/c4d_libs/lib_py.h
This header is not documented but it allows to extend and embed Python.
I already posted some code snippets and information in this topic.
The posted code mostly compiles in R20 except the enumerations.
Here's the code and corresponding files to add c4d.extendpyapi.HelloPython() Python function adapted for R20:
extendpyapi.h
#ifndef EXTENDPYAPI_H__
#define EXTENDPYAPI_H__
namespace extendpyapi
{
void InitExtendPython();
void FreeExtendPython();
}
#endif
extendpyapi.cpp
#include "extendpyapi.h"
#include "lib_py.h"
namespace extendpyapi
{
// Global table of c4d.extendpyapi functions for PythonLibrary::InitModule()
static maxon::BaseArray<PythonMethodData>* g_extendpyapi_functions = nullptr;
// Prints "Hello from Python!" to the console
static _PyObject *ExtendPyAPI_HelloPython(_PyObject *self)
{
PythonLibrary pylib;
ApplicationOutput("Hello from Python!");
return pylib.ReturnPyNone();
}
// Initializes c4d.extendpyapi module
void InitExtendPyAPI()
{
PythonLibrary pylib;
// Allocates c4d.extendpyapi module functions
g_extendpyapi_functions = NewObjClear(maxon::BaseArray<PythonMethodData>);
if (g_extendpyapi_functions == nullptr)
return;
// Initializes c4d.extendpyapi module functions
g_extendpyapi_functions->Resize(2);
PythonMethodData* moduleFunctions = g_extendpyapi_functions->GetFirst();
moduleFunctions[0].Init("HelloPython", (PyFn)ExtendPyAPI_HelloPython, PYFN_FLAGS::NOARGS, "HelloPython() - Extend Python API");
moduleFunctions[1].Init(String(), nullptr, PYFN_FLAGS::NONE, String()); // Last dummy element!
// Initializes c4d.extendpyapi module
if (pylib.InitModule("c4d.extendpyapi", moduleFunctions, "Extend Python API"))
ApplicationOutput("\'c4d.extendpyapi\' module successfully initialized");
}
// Frees the module global function table
void FreeExtendPyAPI()
{
DeleteObj(g_extendpyapi_functions);
}
}
main.cpp
#include "lib_py.h"
#include "extendpyapi.h"
::Bool PluginStart()
{
return true;
}
void PluginEnd()
{
}
::Bool PluginMessage(::Int32 id, void* data)
{
switch (id)
{
case C4DPL_PYINITTYPES:
{
extendpyapi::InitExtendPyAPI();
return true;
}
case C4DPL_PYFINALIZE:
{
extendpyapi::FreeExtendPyAPI();
return true;
}
default: break;
}
return false;
}
Note ApplicationOutput()
writes to the Default logger in the console and not the Python one.
The use of the Python library defined in lib_py.h is not straightforward so do not hesitate to ask any question you may have.