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()
try, exceptonly if there is no way around it. In this case, you do not need any custom operator, just addrow.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 thecontextvariable is passed to each method, replacebpy.context.Xbycontext.X. Also, the name of the operator class should be something likeCATEGORY_OT_selectObject, see: https://wiki.blender.org/wiki/Reference/Release_Notes/2.80/Python_API/Addons – brockmann May 24 '21 at 08:45IndexErrorwhich never being raised because you're not using an index operator likemy_list[10]to return a certain entry from a list in yourtry-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:20if not context.selected_objects: continuerather than catching an index error oncontext.selected_objects[0]As commented prior would seriously rethink using try-catch. – batFINGER May 24 '21 at 10:17