1
$\begingroup$

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) < 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) >= 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) < 1:

    obj.data.uv_layers.new(name="UVMap")        

    
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
$\endgroup$
2
  • $\begingroup$ Hello, thanks for taking the time to post your answer. However on this site you shouldn't post it inside your question but rather as a new answer with the information you provided, so it is clearer for other people who are experiencing the same problem. Cheers $\endgroup$ – Gorgious Nov 14 '20 at 1:05
  • $\begingroup$ Thanks for the guidance! $\endgroup$ – John Cheathem Nov 14 '20 at 20:30
2
$\begingroup$

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: # <<<<<<<<<<
    obj.select_set(False) # <<<<<<<<<<

while len(sel_objs) >= 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) # <<<<<<<<<<
$\endgroup$
1
  • $\begingroup$ Thanks lemon! I'll mark your answer after I test the code $\endgroup$ – John Cheathem Nov 14 '20 at 20:31

Your Answer

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

Not the answer you're looking for? Browse other questions tagged or ask your own question.