Im trying to import the class named "MyOperator", which is in the script "Operator.py", into the script "init.py" but the console shows me: ImportError: cannot import name 'Operator' from 'main' (_init_.py) Note: Operator.py and init.py are in the same location. Im doubting too about register the class this way: bpy.utils.register_class(Operator.MyOperator) or this way Operator.MyOperator.register()I have read a lot in different posts and i can't fix this problem.. The main script is:
import bpy
from . import Operator
class LayoutDemoPanel(bpy.types.Panel):
"""Creates a Panel in the scene context of the properties editor"""
bl_label = "Layout Demo"
bl_idname = "SCENE_PT_layout"
bl_space_type = 'PROPERTIES'
bl_region_type = 'WINDOW'
bl_context = "scene"
def draw(self, context):
layout = self.layout
scene = context.scene
# Create a simple row.
layout.label(text=" Simple Row:")
row = layout.row()
row.prop(scene, "frame_start")
row.prop(scene, "frame_end")
row = layout.row()
row.operator('my.operator',text="Operator", icon='MOD_MESHDEFORM')
def register():
bpy.utils.register_class(LayoutDemoPanel)
bpy.utils.register_class(Operator.MyOperator)
def unregister():
bpy.utils.unregister_class(LayoutDemoPanel)
bpy.utils.unregister_class(Operator.MyOperator)
if name == "main":
register()
Operator.py
import bpy
class MyOperator(bpy.types.Operator):
bl_idname= "my.operator"
bl_label = "Mi Operador"
def execute(self,context):
bpy.ops.object.select_all(action='SELECT')
return {'FINISHED'}
def register():
bpy.utils.register_class(MyOperator)
def unregister():
bpy.utils.unregister_class(MyOperator)
__name__ == "__main__"Install as an addon and enable it, in which case dot imports have a module path. In the link posted it mentions this. If your add is in a folder named "foo_bar" canfrom foo_bar import Operator– batFINGER Aug 18 '20 at 18:13