0

I'm making a blender addon located on the N panel with some custom error messages. On the first button, I get the error message working properly, but in the second button is not working although it seems the structure is the same.

On the second button (the non-working one) if there is nothing selected then my error message should popup but is not working. What could be causing it?

import bpy

structures_in_window = [ { 'name': 'WINDOWS', 'items': [ "frame-bnd-[!A-Z]", "glass[!A-Z]", ], }, ]

class SelectByNameOperator_window(bpy.types.Operator): """Select objects by name""" bl_idname = "object.selected_by_name_operator_window" bl_label = "Select by Name" bl_options = {'REGISTER', 'UNDO'}

def execute(self, context):
    try:
        bpy.ops.object.select_all(action='DESELECT')
        for structure in structures_in_window:

            selection_pattern = bpy.ops.object.select_pattern

            for item in structure['items']:
                selection_pattern(pattern = item)

            all_in_list = bpy.context.selected_objects.copy()

        bpy.context.view_layer.objects.active = bpy.context.selected_objects[0]


        objects = bpy.context.scene.objects.keys()
        if not 'WINDOW' in objects:
            print('There is no parent yet!!')

        elif 'WINDOW' in objects:

            ob = bpy.context.scene.objects["WINDOW"]
            bpy.context.view_layer.objects.active = ob   
            ob.select_set(True)                          

    except IndexError:
            self.report({'ERROR'}, "Windows not found")

    return {'FINISHED'} 

class MYADDON_OT_my_op_move_to_existing_collection(bpy.types.Operator): """Move Selected to Existing Collection""" bl_label = "Move Selected to Existing Collection" bl_idname = "addonname.myop_operator_move_to_existing_collection" bl_options = {'REGISTER', 'UNDO'}

def execute(self, context):
    try:
        bpy.ops.object.move_to_collection('INVOKE_DEFAULT')

    except IndexError:
        if selection is None:
            self.report({'ERROR'}, "Why is not working")

    return {'FINISHED'}


class MYADDON_PT_main_panel(bpy.types.Panel): bl_label = "Main Panel" bl_idname = "MYADDON_PT_main_panel" bl_space_type = 'VIEW_3D' bl_region_type = 'UI' bl_category = "New Tab"

def draw(self, context):
    layout = self.layout
    scene = context.scene

    row=layout.row()
    scale_row = row.row()

    scale_row.scale_x = 0.7
    scale_row.label(text="Window", icon="OBJECT_DATA")

    scale_row.scale_x = 2.1
    scale_row.operator('object.selected_by_name_operator_window', text='', icon="RESTRICT_SELECT_OFF")

    row = layout.row()
    row.operator("addonname.myop_operator_move_to_existing_collection", icon="NLA_PUSHDOWN")


classes = [ SelectByNameOperator_window, MYADDON_PT_main_panel, MYADDON_OT_my_op_move_to_existing_collection, ]

def register(): for cls in classes: bpy.utils.register_class(cls)

def unregister(): for cls in classes: bpy.utils.unregister_class(cls) del bpy.types.Scene.my_tool

if name == "main": register()

Juan Carlos
  • 179
  • 2
  • 9
  • 3
    Be explicit if you can and use try, except only if there is no way around it. In this case, you do not need any custom operator, just add row.operator("object.move_to_collection", icon="NLA_PUSHDOWN") to your draw method of the panel, see: https://blender.stackexchange.com/a/73548/31447 Notice that the context variable is passed to each method, replace bpy.context.X by context.X. Also, the name of the operator class should be something like CATEGORY_OT_selectObject, see: https://wiki.blender.org/wiki/Reference/Release_Notes/2.80/Python_API/Addons – brockmann May 24 '21 at 08:45
  • 2
    To answer your question: You are trying to catch an IndexError which never being raised because you're not using an index operator like my_list[10] to return a certain entry from a list in your try-block. Recommend take a python course like: https://developers.google.com/edu/python will speed things up, you'll see. – brockmann May 24 '21 at 09:20
  • 1
    As with your previously closed question this one treads the line of being more about generic python syntax than python specific to blender's API. IMO would be simpler to test for an empty list if not context.selected_objects: continue rather than catching an index error on context.selected_objects[0] As commented prior would seriously rethink using try-catch. – batFINGER May 24 '21 at 10:17

0 Answers0