Script for baking tiles

Hello,

I try to script a repetitive task in blender, and I hope you could help me with the python api.
What I try to do is to divide a very large terrain mesh in tiles and export the tiles to a game engine (Godot engine).
I know how to explode a mesh in equally divided small meshes, which are the tiles. But the texture is the big problem. In blender it’s a procedural texture made of multiple nodes. It means the quality is not a problem and I just have to bake it.
I could do it manually but it would take days. And it’s where python comes in action.

My approach for the script is the following:

  1. Divide the mesh into tiles. I do it like this:

import bpy, bmesh
from bpy import context as C


bpy.ops.object.mode_set(mode='EDIT')


bm = bmesh.from_edit_mesh(C.object.data)


edges = []


for i in range(-10, 10, 1):
        ret = bmesh.ops.bisect_plane(bm, geom=bm.verts[:]+bm.edges[:]+bm.faces[:], plane_co=(i,0,0), plane_no=(-1,0,0))
        bmesh.ops.split_edges(bm, edges=[e for e in ret['geom_cut'] if isinstance(e, bmesh.types.BMEdge)])


for i in range(-10, 10, 1):
        ret = bmesh.ops.bisect_plane(bm, geom=bm.verts[:]+bm.edges[:]+bm.faces[:], plane_co=(0,i,0), plane_no=(0,1,0))
        bmesh.ops.split_edges(bm, edges=[e for e in ret['geom_cut'] if isinstance(e, bmesh.types.BMEdge)])
                


bmesh.update_edit_mesh(C.object.data)


bpy.ops.mesh.separate(type='LOOSE')
bpy.ops.object.mode_set(mode='OBJECT')  

  1. with all tiles selected, I set the origin of each object to geometry. I don’t know how to do that.
  2. I rename each object based on its location. Easily done with:

import bpy


count=1
for obj in bpy.context.selected_objects:
    obj.name = "terrain_"+str(count)+"-"+str(int(obj.location[0]))+"_"+str(int(obj.location[1]))+"-col"
    count+=1

  1. For each object, I create a new image, unwrap “project from view(bounds)”. No idea how to do that.
  2. Again, for each object bake the texture and save each image to a png filename with the same name as the node. No idea how to do that either.
  3. Set as texture for each node its newly created image file. And update the mapping coordinat to UV. No idea how to do that.

After that, I can export the scene and take care of the rest in the engine.

Any help for a part of the script would be very appreciated. Thanks in advance.

By digging here and there, I could little by little find stuffs. I don’t know if I’m doing right, though.

simply like that.


bpy.ops.object.origin_set(type='ORIGIN_GEOMETRY')

I found a very dirty way to do it. But it works.


import bpy


render = bpy.context.scene.render 


for obj in bpy.context.selected_objects:
    bpy.context.scene.objects.active = bpy.data.objects[obj.name]
    bpy.ops.object.editmode_toggle()
    image = bpy.data.images.new("image_"+obj.name, alpha=False, width=2048, height=2048)
    
    me=obj.data
    
    bpy.data.screens['UV Editing'].areas[1].spaces[0].image=image


    for area in bpy.context.screen.areas:
        if area.type == 'VIEW_3D':
            for region in area.regions:
                if region.type == 'WINDOW':
                    override = {'area': area, 'region': region, 'edit_object': bpy.context.edit_object}
                    bpy.ops.uv.project_from_view(override,camera_bounds=False, correct_aspect=True, scale_to_bounds=True)    
        
    bpy.ops.object.editmode_toggle()

I could not find anything better, that doesn’t rely on the opened screens.

Surprisingly simple:


render.bake_type = 'TEXTURE'
bpy.ops.object.bake_image()

I only need to save it to a file and its done.

Already done.

I won’t hide I hoped a little more answers from this forum that was supposed to help.