Python - Iterate over each object and unwrap - Problem posting a gif for reference. When I run this script on one object I get exactly what I want, the object is unwrapped and fits fully in its UV space. But when I run the script on both objects selected, Blender treats both objects as one and packs their UVs together creating empty space in each objects UV space. I would expect blender to select one object, unwrap it, select the next object, unwrap it etc but it's instead treating it like it selected both objects and unwrapped them at once. Any thoughts? https://gyazo.com/d15393eec2d782492d02f24a45d966af
import bpy    

if bpy.context.selected_objects != []:
if len(bpy.context.object.data.uv_layers) &lt; 1:
    
    bpy.ops.mesh.uv_texture_add()

sel_objs = [obj for obj in bpy.context.selected_objects if obj.type == 'MESH']
    
while len(sel_objs) &gt;= 1:  
                  
    obj1 = sel_objs.pop() 
    
    obj1.select_set(True) 
    
    bpy.context.view_layer.objects.active = obj1
     
    bpy.ops.object.editmode_toggle()              
    
    bpy.ops.mesh.select_all(action='SELECT')
    bpy.ops.uv.select_all(action='SELECT')
    bpy.ops.uv.cube_project(cube_size=0.333957)            
    bpy.ops.uv.average_islands_scale()
    bpy.ops.uv.pack_islands(margin=0.01)
    
    bpy.ops.object.editmode_toggle()

Any help is greatly appreciated
EDIT: Found a solution but not sure what made it work. I assume setting the UV layer as active is important
import bpy

selection_names = [] #declaring array

for obj in bpy.context.selected_objects: #loop through all the selected 
objects
    if obj.type == 'MESH':
        selection_names.append(obj) #add to array

if len(obj.data.uv_layers) &lt; 1:

    obj.data.uv_layers.new(name=&quot;UVMap&quot;)        

    
obj.select_set(False)

if selection_names != []:

    for obj in selection_names: #loop through array    
        
    
            bpy.context.view_layer.objects.active = obj
            lm = obj.data.uv_layers[0]
            lm.active = True
            bpy.ops.object.editmode_toggle() #entering edit mode
            bpy.ops.mesh.select_all(action='SELECT') #select all objects elements
            bpy.ops.uv.select_all(action='SELECT')
            #py.ops.uv.cube_project(cube_size=0.333957)   
            bpy.ops.uv.smart_project(angle_limit = 66 , island_margin = 0.1 , 
            stretch_to_bounds=False)       
            bpy.ops.uv.average_islands_scale()
            bpy.ops.uv.pack_islands(margin=0.01)
        
            bpy.ops.object.editmode_toggle() #exiting edit mode
From the first script, just manage the selections in order to use the bpy.ops on each one separately:
Deselect all once you get the selected ones.
Select before using bpy.ops and deselect after that.
sel_objs = [obj for obj in bpy.context.selected_objects if obj.type == 'MESH']

for obj in sel_objs: # &lt;&lt;&lt;&lt;&lt;&lt;&lt;&lt;&lt;&lt;
    obj.select_set(False) # &lt;&lt;&lt;&lt;&lt;&lt;&lt;&lt;&lt;&lt;

while len(sel_objs) &gt;= 1:  
                  
    obj1 = sel_objs.pop() 
    
    obj1.select_set(True) 
    
    bpy.context.view_layer.objects.active = obj1
     
    bpy.ops.object.editmode_toggle()              
    
    bpy.ops.mesh.select_all(action='SELECT')
    bpy.ops.uv.select_all(action='SELECT')
    bpy.ops.uv.cube_project(cube_size=0.333957)            
    bpy.ops.uv.average_islands_scale()
    bpy.ops.uv.pack_islands(margin=0.01)
    
    bpy.ops.object.editmode_toggle()

    obj1.select_set(False) # &lt;&lt;&lt;&lt;&lt;&lt;&lt;&lt;&lt;&lt;