I'm trying to make my own bpy.ops python function, like bpy.ops.hi() for example, or even bpy.someotherfunction().

I realize that the submodules are only "fakes" as it says in the ops.py file, and they all point to C++ functions, here's the class to create all bpy.something():

class BPyOps:
    Fake module like class.

    __slots__ = ()

    def __getattr__(self, module):
        gets a bpy.ops submodule
        if module.startswith('__'):
            raise AttributeError(module)
        return BPyOpsSubMod(module)

    def __dir__(self):

        submodules = set()

        # add this classes functions
        for id_name in dir(self.__class__):
            if not id_name.startswith('__'):

        for id_name in op_dir():
            id_split = id_name.split('_OT_', 1)

            if len(id_split) == 2:

        return list(submodules)

    def __repr__(self):
        return "<module like class 'bpy.ops'>"

so for now without even getting into submodules I'd like to first know how to make a custom bpy.something(),

it seems like all the module names are coming from op_dir(), which is defined in the beginning:

from _bpy import ops as ops_module

# op_add = ops_module.add
op_dir = ops_module.dir

but I can't seem to find "_bpy" anywhere, I tried doing a windows search in both the blender release folder and the source code, and I couldn't find it (yet).

SO I think the step to make a custom module is to edit _bpy.dir, but where is that ???? Is it in the blender source somewhere, or a compiled python file?

  • $\begingroup$ Could it be there is an import bpy as _bpy import you missed? $\endgroup$ – batFINGER Jan 9 at 3:21
  • $\begingroup$ no... look at the entire file here:\2.80\scripts\modules\bpy\ops.py. I actually found in the source: blender\python\intern\bpy.c, which apparently "fakes" the _bpy python class: * This file defines the '_bpy' module which is used by python's 'bpy' package * to access C defined builtin functions. * A script writer should never directly access this module. */, although I'm still not exactly sure where to find the ".dir" property of that to modify it $\endgroup$ – bluejayke Jan 9 at 3:25

Your Answer

By clicking "Post Your Answer", you agree to our terms of service, privacy policy and cookie policy

Browse other questions tagged or ask your own question.