https://sinestesia.co/blog/tutorials/getting-property-groups-as-dictionaries
我最近重新设计了Mirage的实时模式,使之更有效率。我想改进检测地形设置的变化。最好的方法是获得一个设置的字典,并找到其中的变化。
所以写个函数来获取一个PropertyGroup里面的所有属性作为一个字典。
from typing import Dict
from bpy.types import PropertyGroup
def group_as_dict(group: PropertyGroup) -> Dict:
""" 从属性组中获取值 作为字典 """
EXCLUDE = {'a_prop_I_dont_want', 'another'}
prop_dict = {}
for key in group.__annotations__.keys():
# 跳过不要的属性
if key in EXCLUDE:
continue
# 在__annotations__中的每个项目是一个元组,包含类型和一个字典
prop_type = group.__annotations__[key][0]
# Pointers 到其他属性组
if prop_type == PointerProperty:
prop_dict[key] = group_as_dict(getattr(settings, key))
# 将集合属性存储为列表
elif prop_type == CollectionProperty:
prop_dict[key] = [group_as_dict(i)
for i in getattr(settings, key)]
# BUG? IntVectorProperties在 getattr()上不返回Vector值。必须自己去获得。
elif prop_type == IntVectorProperty:
prop_dict[key] = [i for i in getattr(settings, key)]
# 获取其他值
else:
prop_dict[key] = getattr(settings, key)
return prop_dict