i try to find the best and simple way to make addon with multi script files, but i cant find some good help about it…
i try to make the init file and from there to import the other files but i get an error that he cannot import it, what i do wrong and how to do that in the right way? and why i cant find some good tut about it?
thx!
In this directory blender\2.83\scripts\addons\operator_simple
you will put two files needed.
__init__.py
show_objects.py
The __init__.py
file contains this:
bl_info = {
"name": "Simple Operator",
"author": "Your Name Here",
"version": (1, 0),
"blender": (2, 80, 0),
"location": "View3D > Simple Operator",
"description": "Only a simple operator",
"warning": "",
"doc_url": "",
"category": "3D View"
}
import bpy
from . import show_objects
class SimpleOperator(bpy.types.Operator):
"""Tooltip"""
bl_idname = "object.simple_operator"
bl_label = "Simple Object Operator"
@classmethod
def poll(cls, context):
return context.active_object is not None
def execute(self, context):
show_objects.main(context)
return {'FINISHED'}
def register():
bpy.utils.register_class(SimpleOperator)
def unregister():
bpy.utils.unregister_class(SimpleOperator)
The show_objects.py
file contains this:
import bpy
def main(context):
for ob in context.scene.objects:
print(ob)
The only important thing to note here is that __init__.py
is a special filename that Python can understand. It means that now all of your filesystem directory becomes is a Python module. Otherwise Python can’t understand simple things like importing a file from a full path (as it works in C for example). Python wants modules only.
Another important thing, in these two scripts is this the most essential line from . import show_objects
and it means that from this directory import the show_objects
file and use the main
function. There is also another way to import things, and avoid typing show_objects.this
and show_objects.that
all of the time. It is done like this from .show_objects import *
and then you simply do not use the name of the module.
thx, sorry of the noob question, but its still not work, i copy waht you write and its still give me the same error, i also try to add the registery part (but i dont sure that i put the right thing there) and i try to write the execute in a class but it also not work, should i do something else in the code to make it work? or it should work as is?
@gilad.r.f
I have simplified the explanation further so is easy to understand. If something goes wrong just open the console and post a screenshot.