@@ -1144,10 +1144,37 @@ def set_collections(obj, collections):
11441144 # Modifiers
11451145 @staticmethod
11461146 def apply_all_modifiers (context , obj ):
1147- """apply all modifiers to an object"""
1147+ """Replace obj's mesh data with the fully evaluated result of its
1148+ modifier stack - including any unrealized instances a modifier like
1149+ Geometry Nodes "Instance on Points" produces without a "Realize
1150+ Instances" node - then clear the modifiers.
1151+
1152+ bpy.ops.object.modifier_apply() applied per-modifier fails outright
1153+ ("Evaluated geometry from modifier does not contain a mesh") once a
1154+ modifier's output is instances-only, since Blender's built-in Apply
1155+ can't bake unrealized instances into real geometry. Evaluating via
1156+ the depsgraph and merging instances manually (merge_object_instances)
1157+ sidesteps that limitation.
1158+ """
11481159 context .view_layer .objects .active = obj
1149- for mod in obj .modifiers :
1150- bpy .ops .object .modifier_apply (modifier = mod .name )
1160+ if not obj .modifiers :
1161+ return
1162+
1163+ depsgraph = context .evaluated_depsgraph_get ()
1164+ me = bpy .data .meshes .new_from_object (obj .evaluated_get (depsgraph ), depsgraph = depsgraph )
1165+
1166+ bm = bmesh .new ()
1167+ bm .from_mesh (me )
1168+ OBJECT_OT_add_bounding_object .merge_object_instances (bm , obj , depsgraph )
1169+ bm .to_mesh (me )
1170+ bm .free ()
1171+
1172+ old_data = obj .data
1173+ obj .data = me
1174+ obj .modifiers .clear ()
1175+
1176+ if old_data .users == 0 :
1177+ bpy .data .meshes .remove (old_data )
11511178
11521179 @staticmethod
11531180 def remove_all_modifiers (context , obj ):
@@ -1486,6 +1513,19 @@ def cancel_cleanup(self, context, delete_colliders=True):
14861513
14871514 self .reset_display (context )
14881515
1516+ # execute() normally restores the starting selection/active object/mode
1517+ # via reset_to_initial_state() at its own end. If generation is
1518+ # cancelled or raises before that point is reached, none of that ever
1519+ # runs, so the user can be left stranded in Object Mode after starting
1520+ # in Edit Mode. Restore it explicitly here as a guarantee.
1521+ for obj in bpy .data .objects :
1522+ obj .select_set (False )
1523+ for obj in self .selected_objects :
1524+ obj .select_set (True )
1525+ context .view_layer .objects .active = self .active_obj
1526+ if context .object and context .object .mode != self .obj_mode :
1527+ bpy .ops .object .mode_set (mode = self .obj_mode )
1528+
14891529 try :
14901530 bpy .types .SpaceView3D .draw_handler_remove (self ._handle , 'WINDOW' )
14911531 except ValueError :
0 commit comments