Find out if a class is registered?

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
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

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!

my god I’m bad

1 Like

@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

Ah, “is_registered” doesn’t show up in my VSCode intellisense for “MyPG”.
But yes, this works as well, thanks :slight_smile:

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 ?

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