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:
- 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')
- with all tiles selected, I set the origin of each object to geometry. I don’t know how to do that.
- 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
- For each object, I create a new image, unwrap “project from view(bounds)”. No idea how to do that.
- 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.
- 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.