4
$\begingroup$

The documentation on bpy.utils.register_class states:

If the class has a register class method it will be called before registration.

How should this method be declared? What arguments will it be given?

$\endgroup$
5
$\begingroup$

A class method takes at least one argument - the class itself. Here, register (and its counterpart, unregsiter) do not take anything else, so you must declare them this way:

    @classmethod
    def register(cls):
        pass

    @classmethod
    def unregister(cls):
        pass

You have a good example of their usage in Cycles' propertie.py file:

class CyclesRenderSettings(bpy.types.PropertyGroup):
    @classmethod
    def register(cls):
        bpy.types.Scene.cycles = PointerProperty(
                name="Cycles Render Settings",
                description="Cycles render settings",
                type=cls,
                )
        cls.device = EnumProperty(
                name="Device",
                description="Device to use for rendering",
                items=enum_devices,
                default='CPU',
                )
        # ... and so on adding properties to cls

    @classmethod
    def unregister(cls):
        del bpy.types.Scene.cycles

That way, you can embed the definition of your propertygroup and handle its container’s creation and deletion inside the class, instead of having to create/delete bpy.types.Scene.cycles in the global (un)register funcs.

$\endgroup$

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.