使用场景:bpy.types.Scene
与bpy.context.scene
部分功能重叠。
def Get(obj, attr: str | Sequence[str], root=False):"""injected recursive getattr, could pollute objects on chain in whole session"""IS_STR = isinstance(attr, str)if IS_STR and attr.startswith("__") and attr != "__getattribute__":return object.__getattribute__(obj, attr)E: Exception | None = Noneobjs = obj if root else (obj,)for i, o in enumerate(objs):at = attr if IS_STR else attr[i]try:obj_inject = getattr(o, at)setattr(obj_inject, "__getattribute__", Get)return obj_injectexcept AttributeError as e:if " object attribute '__getattribute__' is read-only" in str(e):return obj_injectE = eif E:raise Eelse:raiseclass A:att = True
class B:at = False
if __name__ == "__main__":at = Get([A, B], "at", root=True)print("😄", at)
等价于:
A.at # no attr named 'at'
B.at