I had a complex code, which failed. To test things out I simplified it to this:
from bpy.types import Scene, PropertyGroup
from bpy.utils import register_class, unregister_class
from bpy.props import StringProperty, PointerProperty
class PropGroup_1(PropertyGroup):
my_str_1: StringProperty()
class PropGroup_2(PropertyGroup):
my_str_2: StringProperty()
my_prop_groups = [
('my_props_1', PropGroup_1),
('my_props_2', PropGroup_2)
]
def set_prop_groups(enable):
if enable:
for group_name,group in my_prop_groups:
register_class(group)
setattr(Scene, group_name, PointerProperty(type=group))
else:
for group_name,group in reversed(my_prop_groups):
delattr(Scene, group_name)
unregister_class(group)
set_prop_groups(True) #True/False switched
Describing the problem :
When I run the code with set_prop_groups(True) — it works fine.
Few seconds later when I run it with set_prop_groups(False) — an error emerges,
unregister_class(group) causes the exception:
RuntimeError: unregister_class(...):, missing bl_rna attribute from 'RNAMetaPropGroup' instance (may not be registered)
- Out of curiosity, I tried to test the automated switching, hence I replaced the last line with this:
from time import sleep
set_prop_groups(True)
sleep(2)
set_prop_groups(False)
(!) Oddly enough, with automated delay the code works fine.
* Disregarding if the delay is set to few seconds or one millisecond – all delays do the job.
The question:
Why unregister_class(group) fails in case when I switch True/False manually?