Skip to content Skip to sidebar Skip to footer

How To Override __setattr__ In A Wrapped Class (from C++)?

Using boost::python, I have been able to wrap a class (Node) which has some virtual functions, and it's been a hoot, but now I'm trying to override setattr/getattr for the class. I

Solution 1:

I believe the Python C-API function you want is PyObject_GenericSetAttr; that is, you should replace the "problematic line" with:

bpy::strattr_str(attr);
if (PyObject_GenericSetAttr(obj.ptr(), attr_str.ptr(), val.ptr()) != 0)
    bpy::throw_error_already_set();

I think this is equivalent to calling object.__setattr__(self, attr, val) in Python, which is not the same as calling __setattr__ on a base class, but the difference only matters if a base class also overrides __setattr__. If that's the case, you'd need to do something like this:

bpy::object cls(bpy::handle<>(PyObject_Type(obj.ptr())));
bpy::object base_cls = cls.attr("__bases__")[0];
base_cls.attr("__setattr__")(obj, attr, val);

Post a Comment for "How To Override __setattr__ In A Wrapped Class (from C++)?"