0
$\begingroup$

The active command of the object does not work in classes, but it works if I write code without using classes

How can I change the active object in the loop? (blender 2.93)

import bpy

class ApplyAllOV_OT_ModB(bpy.types.Operator):
    bl_idname = "test.fancy_visible"
    bl_label = "Test"
    
    def execute(self,context):

        def ShowMessageBox(message = "", title = "Message Box", icon = 'INFO'):

            def draw(self, context):
                self.layout.label(text=message)

            bpy.context.window_manager.popup_menu(draw, title = title, icon = icon)


        ListON = []
        pi = 0
        for obj in bpy.data.objects:
            if obj.type == "MESH":
                ListON.append(obj.name)

        for ObjN in ListON :
            print(ListON)
            bpy.ops.object.select_all(action='DESELECT')
            bpy.data.objects[ObjN].select_set(True)
            bpy.context.view_layer.objects.active = bpy.data.objects[ObjN]
            print(ObjN)
            print("for: "+bpy.context.object.name)
            try:
                
                if len(bpy.context.object.modifiers.items())>0:
                    itemEN = ""
                    for item in bpy.context.object.modifiers.items():
                        print("type: ",type(item[0]),"name: ",item[0])
                        itemEN = item[0]
                        bpy.ops.object.modifier_apply(modifier=item[0])
                    ShowMessageBox("applied.")
                else:
                    ShowMessageBox("No modifier available. "+ObjN)
            except RuntimeError as ex:
                ShowMessageBox(itemEN +" modifier settings are not complete.", "Error", 'ERROR')
        
$\endgroup$
1
  • $\begingroup$ Bit uncliear what you are asking here. Will note that bpy.data.objects is all objects in the blend file. If an object is not in the context scene scene.objects then setting it as active may not work as expected. $\endgroup$
    – batFINGER
    Sep 19 at 18:08

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Browse other questions tagged or ask your own question.