diff options
author | Campbell Barton <ideasman42@gmail.com> | 2010-09-09 09:37:22 +0400 |
---|---|---|
committer | Campbell Barton <ideasman42@gmail.com> | 2010-09-09 09:37:22 +0400 |
commit | f4be9a6393e76f1dc40488ab25c43763af2edcc2 (patch) | |
tree | 2043d45af4bd8a2af2f9e97e9c10ab8fa9a4cf47 /release/scripts | |
parent | 4eaa10aa02994c4609e1c12e38d0eda6355f9077 (diff) |
support for defining rna class properties as class attributes
eg:
bpy.types.Scene.myprop = BoolProperty()
note, this uses an ugly python metaclass, this should be replaced with a C implimentation which is included but commented out, causing crashes in pythons GC which gives no hint as to where the bug comes from.
Diffstat (limited to 'release/scripts')
-rw-r--r-- | release/scripts/modules/bpy_types.py | 74 |
1 files changed, 69 insertions, 5 deletions
diff --git a/release/scripts/modules/bpy_types.py b/release/scripts/modules/bpy_types.py index 923e9f91c9e..9219f7edcb0 100644 --- a/release/scripts/modules/bpy_types.py +++ b/release/scripts/modules/bpy_types.py @@ -18,6 +18,65 @@ # <pep8 compliant> + +class RNA_IDProp_Meta(type): + # metaclass for all structures which can have rna prop's defined. + # important this class is defined first. + + # setattr, so we can do this... + # bpy.types.Scene.myprop = bpy.props.BoolProperty() + def __setattr__(cls, attr, value): + if type(value) == tuple and len(value) == 2: + print(cls, attr, value) + if attr in cls.bl_rna.properties: + _bpy.props.RemoveProperty(cls, attr=attr) + func, kw = value + kw["attr"] = attr + func(cls, **kw) + else: + # XXX, pure evil, need to find a better way + _setattr = RNA_IDProp_Meta.__setattr__ + del RNA_IDProp_Meta.__setattr__ + try: + setattr(cls, attr, value) + except Exception as exc: + RNA_IDProp_Meta.__setattr__ = _setattr + raise exc + + RNA_IDProp_Meta.__setattr__ = _setattr + + def __getattr__(cls, attr): + if attr in cls.bl_rna.properties: + return cls.bl_rna.properties[attr] + elif attr: + # XXX, pure evil, need to find a better way + _getattr = RNA_IDProp_Meta.__getattr__ + del RNA_IDProp_Meta.__getattr__ + try: + ret = getattr(cls, attr) + except Exception as exc: + RNA_IDProp_Meta.__getattr__ = _getattr + raise exc + + RNA_IDProp_Meta.__getattr__ = _getattr + return ret + + def __delattr__(cls, attr): + if attr in cls.bl_rna.properties: + _bpy.props.RemoveProperty(cls, attr=attr) + elif attr: + # XXX, pure evil, need to find a better way + _delattr = RNA_IDProp_Meta.__delattr__ + del RNA_IDProp_Meta.__delattr__ + try: + delattr(cls, attr, value) + except Exception as exc: + RNA_IDProp_Meta.__delattr__ = _delattr + raise exc + + RNA_IDProp_Meta.__delattr__ = _delattr + + from _bpy import types as bpy_types import _bpy from mathutils import Vector @@ -42,6 +101,10 @@ class Context(StructRNA): return new_context +class ID(StructRNA, metaclass=RNA_IDProp_Meta): + __slots__ = () + + class Library(bpy_types.ID): __slots__ = () @@ -60,7 +123,7 @@ class Library(bpy_types.ID): return tuple(id_block for attr in attr_links for id_block in getattr(bpy.data, attr) if id_block.library == self) -class Texture(bpy_types.ID): +class Texture(bpy_types.ID, metaclass=RNA_IDProp_Meta): __slots__ = () @property @@ -257,15 +320,15 @@ class _GenericBone: return bones -class PoseBone(StructRNA, _GenericBone): +class PoseBone(StructRNA, _GenericBone, metaclass=RNA_IDProp_Meta): __slots__ = () -class Bone(StructRNA, _GenericBone): +class Bone(StructRNA, _GenericBone, metaclass=RNA_IDProp_Meta): __slots__ = () -class EditBone(StructRNA, _GenericBone): +class EditBone(StructRNA, _GenericBone, metaclass=RNA_IDProp_Meta): __slots__ = () def align_orientation(self, other): @@ -617,12 +680,13 @@ class RNAMeta(type): return result -class RNAMetaRegister(RNAMeta): +class RNAMetaRegister(RNAMeta, RNA_IDProp_Meta): @classmethod def _register_immediate(cls): return True + class OrderedMeta(RNAMeta): def __init__(cls, name, bases, attributes): |