1

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)

  • Related https://blender.stackexchange.com/a/183817/15543 – batFINGER Aug 17 '20 at 18:41
  • Hello, i've read that post and other similar and I can't fix the problem, the way of that post is avaiable to do it in the text editor? Or only works if I install the Addon? Can you help me, please? Thanks u – Amancio Delgado Aug 18 '20 at 18:04
  • Looks like you are calling register method from the main thread, ie when __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" can from foo_bar import Operator – batFINGER Aug 18 '20 at 18:13
  • Thanks u so much, I was trying to do the import in the Text Editor of Blender. Installing the scripts as an Addon I dont have more problems with the import. Thanks you sou much again, I had a headhache with this. – Amancio Delgado Aug 18 '20 at 19:02

0 Answers0