I continued working on SWIG removal today. Even though I have no new class available, I improved the code quite a bit.
First, the Freestyle API classes are now (properly) available as module objects, instead of submodules. I had to incorporate object lifecycle-related methods. I first started adding a custom __new__
method. I understood later that the Python object type slot tp_new
should be set to the standard PyType_GenericNew
function, while it is __init__
that had to be customized (as is commonly the case for regular Python classes). I learned today that, by default, a PyTypeObject
‘s tp_new
slot is not available. It is crucial to define it for all base classes.
Second, I verified that my approach for replacing SWIG should work:
- I wrap each C++ base class with a
PyObject
of the same name (stored in the corresponding object type’s tp_name
slot): the PyObject
just contains a pointer to an instance of that C++ class.
- I redirect calls from Python to C++ by merely calling the corresponding methods on the C++ instance stored in the Python object.
- I bring the data back to Python by converting the C++ result to the appropriate
PyObject
(see source/blender/freestyle/python/Convert.{h,cpp}
).
As a proof-of-concept, I started implementing CurvePoint
(Interface0D
subclass). I added CurvePoint
‘s initialization method, which sets the C++ Interface0D
pointer if0D
to the address of an instance of the C++ CurvePoint
class. When I test an instance of each class with the getExactTypeName
method, I obtain the proper result (their name !).
So far, BinaryPredicate0D
, BinaryPredicate1D
, Id
, Interface0D
and Interface1D
have been implemented (they are available in Blender’s Python environment under Blender.Freestyle). I’ll update the blog again tomorrow to give you the list of available classes.