78956412
(78956412)
February 23, 2014, 1:05pm
1
Is it possible to find out if a class is registered - in particular, a panel class (bpy.types.Panel)?
If you try to unregister a class that isn’t already registered, that raises “RuntimeError: unregister_class(…): missing bl_rna attribute from ‘RNAMeta’ instance (may not be registered)”. I’m wondering if there’s a nice way to avoid that.
1 Like
CoDEmanX
(CoDEmanX)
February 23, 2014, 5:05pm
2
if hasattr(bpy.types, "YOUR_PT_panel_class"): ...
or simply catch the exception
try:
bpy.utils.unregister_class(YOUR_PT_panel_class)
except RuntimeError:
pass
6 Likes
78956412
(78956412)
February 23, 2014, 10:01pm
3
Ah, thank you so much! I feel silly :rolleyes:
Although this reply is over 6 years old - it caused an “Aha! Moment” into understanding what is going on with bpy.utils.register_class". Thank you!
TomErnst
(Tom Ernst)
July 26, 2023, 10:43am
6
@CoDEmanX ’s solution is good, but doesn’t work for PropertyGroups.
PropertyGroups are registered different, they don’t end up in bpy.types.
For PropertyGroups the following works:
def is_pg_registered(pg_class) -> bool:
return getattr(pg_class, "bl_rna").name == pg_class.__name__
What happens behind the curtains:
class MyPG(bpy.types.PropertyGroup):
txt: bpy.props.StringProperty()
class MySubPG(MyPG):
sub_txt: bpy.props.StringProperty()
def get_rna_name(cls):
rna = getattr(cls, "bl_rna", None)
if rna is None: return ""
return rna.name
def print_rna_names(text):
print("\n" + text + ":",
"\n MyPG rna_name=" + repr(get_rna_name(MyPG)),
"\n MySubPG rna_name=" + repr(get_rna_name(MySubPG)))
def register():
print_rna_names("before MyPG before MySubPG reg")
bpy.utils.register_class(MyPG)
print_rna_names("after MyPG before MySubPG reg")
bpy.utils.register_class(MySubPG)
print_rna_names("after MyPG after MySubPG reg")
Output:
before MyPG before MySubPG reg:
MyPG rna_name='ID Property Group'
MySubPG rna_name='ID Property Group'
after MyPG before MySubPG reg:
MyPG rna_name='MyPG'
MySubPG rna_name='MyPG'
after MyPG after MySubPG reg:
MyPG rna_name='MyPG'
MySubPG rna_name='MySubPG'
Additional teaching by this post:
Be careful with the naming of your PropertyGroups, to not get in conflicts with existing RNAs.
Have you tried bpy.types.Property.is_registered ?
print(MyPG.is_registered)
print(MySubPG.is_registered)
1 Like
TomErnst
(Tom Ernst)
July 26, 2023, 5:40pm
8
Ah, “is_registered” doesn’t show up in my VSCode intellisense for “MyPG”.
But yes, this works as well, thanks
Hmm it might have been a recent addition, or maybe it’s because it’s defined in the parent class ? Are you using the latest version for intellisense ?
TomErnst
(Tom Ernst)
July 27, 2023, 1:19pm
10
I am not sure, how and where I did install the intellisense fake-module (already 1 1/2 yrs ago).
Just downloaded it from “https://github.com/Korchy/blender_autocomplete ” and tried to add it to my user settings.json, according to my Blender version 3.4, which I do currently use:
"python.autoComplete.extraPaths": [
"C:/Blender/_addons/autocomplete/3.4",
"C:/Blender/_addons"
],
but didn’t work hitherto.
Do you have an additional tip?
Yup looks like it’s because it belongs to the parent class bpy.types.Property. Search for is_registered
in there for instance https://raw.githubusercontent.com/Korchy/blender_autocomplete/master/3.4/bpy/types.py
1 Like