Exploring Blender Registration

This is mainly experimental stuff I’m doing right now. I wrote this script to take the names of blender operators and put them in a Python dictionary using the ops submodules as keys. I guess I was just kind of curious.

So I learned a few interesting things.

  1. I wasn’t aware of the idea of fake modules and submodules until now.

  2. There are these _bpy python modules which do the background work of registering scripts for you and other lower level tasks.

So here’s my tiny little script I just figured I’d share. If I can think of more to expand on this, I will. Feel free to fire comments and suggestions towards me.


import bpy
import _bpy
from collections import defaultdict


def make_operator_dict():
    submod_op_pairs = [entry.split("_OT_") for entry in _bpy.ops.dir()]
    submod_op_pairs = [(k.lower(), v) for k,v in submod_op_pairs]
    
    op_dict = defaultdict(list)
    for key, val in submod_op_pairs:
        op_dict[key].append(val)
    
    return op_dict




if __name__ =='__main__':
    op_dict = make_operator_dict()
    
    for key in op_dict:
        print("

%s...... %d operators" % (key, len(op_dict[key])))
        print(op_dict[key])

As a followup, I just want to say Oops! The ultimate end goal for this project ended up being just stuffing addon information into a json file. It turned out to be a lot simpler than I initially made it out to be. So here’s that.


import bpy


if __name__ =='__main__':
    import addon_utils
    import json
    addon_dict = {mod.__name__:mod.bl_info for mod in addon_utils.modules(refresh=False)} 
    encoder = json.JSONEncoder()
    json_data = encoder.encode(addon_dict)
    file_path = ""
    target_file = open(file_path + "addon_info.json", "wt")
    target_file.write(json_data)
    target_file.flush()
    target_file.close()