simple composition use of a a blender module/menu class

I am taking a stab at python development in blender, but the execution of a simple concept is defeating me. I want to simply use composition and packages in my .py development. So instead of a single py file with a bunch of classes within it (messy) I want to place my classes in a separate py file (module) within a sub package; However I can’t seem to get this simple concept to work.

my package structure I have my Initial file init.py I also have a directory named menu, and two files within that: my_menu.py and init.py

e.g.

src/
     __init__.py
     menu/
          __init__.py
          __my_menu__.py

In my top level init.py I have the following code:



if "bpy" in locals():
    import imp
    imp.reload(menu.__my_menu__)
else:
    import menu.__my_menu__

import bpy

def menu(self, context):
    self.layout.menu(menu.__my_menu__.bl_idname, icon="PLUGIN")
        
def register():
    bpy.utils.register_module(__name__)

    bpy.types.INFO_MT_mesh_add.append(menu)
        
def unregister():
    bpy.utils.unregister_module(__name__)

    bpy.types.INFO_MT_mesh_add.remove(menu)

Within the menu directory, my_menu.py file I have the following:


import bpy

class MyMenu(bpy.types.Menu):

    bl_idname = "OBJECT_MT_my_menu"
    
    bl_label = "My Menu"
    
    def draw(self, context):
        layout = self.layout
        layout.operator_context = 'INVOKE_REGION_WIN'

I get the following error

Import Error: No module named menu.__my_menu__

Thanks

shouldn’t it be more like this?

menu/
     __init__.py
     mymenu.py

menu will be the module

i don’t think there is a need for a top-level init.py

you may have a look at the structure of my export addon:

http://code.google.com/p/blender-cod/source/browse/blender_26

inside the menu subfolder your init.py will have to have a import __my_menu

files are not automagically imported

thus why you get that error.

please bare in mind that using double underscore in front of any name is used by python to note “privacy” that the user should not access that feature, but other than that double score is both ugly and meaningless and not recommended as general python practice. As matter of fact that “privacy” is really fake.

Thank you kilon, I am quite new to python and assumed double underscore was a naming convention for python modules, glad to hear it is not, and you are right, it is quite ugly I will fix directly. I will give placing an import my_menu_ within my init.py a try.

Also I figured out I have to place a a import from add_on_directory_name import fileName for blender to to see my addon. Is this correct?

Thank again.

in case of init.py it is, that is the module it actually import when you do an import menu the same way init() of your class is executed every time you create a new instance of your class.

if you talk about your top level init.py it depends

do you really need to import init.py , of course not, after all that module is what you already execute
the only thing that would make sense is

import menu

remember init.py takes always the name of the folder it belongs too

now if you don’t like doing something like

mymenu=menu.MyMenu()

and instead you prefer doing

mymenu = MyMenu()

then your import should be

from menu import *

or if you like to import only MyMenu

from menu import MyMenu

if you don’t like the name of MyMenu then you could

from menu import MyMenu as menu

so you can do

menu = menu()
:wink:

python offers you loads and loads of flexibility and I just barely scratched the surface.

all the above implies that your menu init.py contains a from my_menu import * or something similar to what i described above