0
$\begingroup$

I am using blenderGIS to create a mesh, out of a large area. I do this frequently at my job. The texture comes in at 650mb or so, and I need to chop it and the mesh up. I found a script which will separate the mesh into a grid for me, so that part works, but I cannot figure out how to bake the textures.

So far I have the script iterating through the objects, creating new textures and UVs and packing the new uvs (though I am not positive the packing is working right now, not sure how to tell (relatively new to blender as well)).

import bpy

sel_objs = [obj for obj in bpy.context.selected_objects if obj.type == 'MESH']

for obj in sel_objs: # <<<<<<<<<<
    obj.select_set(False) # <<<<<<<<<<

count = 0
meshName = "terrain_"

while len(sel_objs) >= 1:
    
    # Process Object
    count += 1
    objName = meshName + str(count)                  
    obj1 = sel_objs.pop()     
    obj1.select_set(True) 
    obj1.name = objName
    
    # Add UV Layer    
    layerExists = False
    
    for layer in obj1.data.uv_layers:
        if layer.name == 'bake':
            layerExists = True

    if layerExists == False:
        obj1.data.uv_layers.new(name="bake")
    
    # Edit Mode, Select All
    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')
    
    # New Image
    newImage = True
    for img in bpy.data.images:
        if img.name == objName:
            newImage = False
            image = img
            
    if newImage == True:
        print("NewImageTrue")
        image = bpy.ops.image.new(name=objName, width=4096, height=4096, color=(0.0, 0.0, 0.0, 1.0), alpha=True, generated_type='BLANK', float=False, use_stereo_3d=False, tiled=False)
    else:
        print("ImageAlreadyExists")
    
    for uv_face in obj1.data.uv_layers.active.data:
        x=0
        #uv_face.image = image

    image.file_format = "PNG"
    
    #image.save()
    bpy.ops.uv.pack_islands(margin=0.01)
    
    # Exit Edit Mode and Deselect
    bpy.ops.object.editmode_toggle()
    obj1.select_set(False) # <<<<<<<<<<
$\endgroup$

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.