1
$\begingroup$

This is my first post on the Blender StackExchange, so I hope that it's well received :)

Essentially, I'm making a 3D game in Godot, and for that I need to divide my terrain up into chunks. I've already found and got working a fantastic piece of code from this thread which successfully divides the terrain up into different objects within the same scene based on a grid.

However, to really get an efficient workflow going, I now need to find a way to export each one of those separated objects as its own .obj file - as manually doing it all by hand is excruciating. Is there anybody familiar with scripting who would feel charitable enough to modify this existing code from that thread:

from bpy import context
from  mathutils import Vector
# bounding box helper methods
def bbox(ob):
    return (Vector(b) for b in ob.bound_box)

def bbox_center(ob):
    return sum(bbox(ob), Vector()) / 8

def bbox_axes(ob):
    bb = list(bbox(ob))
    return tuple(bb[i] for i in (0, 4, 3, 1))

def slice(bm, start, end, segments):
    if segments == 1:
        return
    def geom(bm):
        return bm.verts[:] + bm.edges[:] + bm.faces[:]
    planes = [start.lerp(end, f / segments) for f in range(1, segments)]
    #p0 = start
    plane_no = (end - start).normalized() 
    while(planes): 
        p0 = planes.pop(0)                 
        ret = bmesh.ops.bisect_plane(bm, 
                geom=geom(bm),
                plane_co=p0, 
                plane_no=plane_no)
        bmesh.ops.split_edges(bm, 
                edges=[e for e in ret['geom_cut'] 
                if isinstance(e, bmesh.types.BMEdge)])


bm = bmesh.new()
ob = context.object
me = ob.data
bm.from_mesh(me)

o, x, y, z = bbox_axes(ob)        

x_segments = 2
y_segments = 2
z_segments = 1

slice(bm, o, x, x_segments)
slice(bm, o, y, y_segments)
slice(bm, o, z, z_segments)    
bm.to_mesh(me)

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

Basically having that modified so it can also export each of those chunks as its own .obj file into a given directory, after splitting the terrain up. Is that possible, and is there anybody who would be willing to help out? I have no experience with python (even though Godots scripting language is quite python like...) so I don't really know where to start doing it by myself!

Thanks for reading.

~ Skelly <3

$\endgroup$
2
  • $\begingroup$ Hi, regarding the actual splitting, I needed similar thing, check this question & answer: blender.stackexchange.com/questions/145176/… $\endgroup$ Commented Sep 14, 2022 at 19:58
  • $\begingroup$ Hi, thanks for your reply - but getting the terrain sliced up into chunks is actually working fine with the existing script I've got. What I really need now is a way to save those resulting chunks as objs without needing to do it all manually - Id even be okay kicking someone 20 bucks or so to write the code if needs be, its just that its waaayy out of my field, haha. But thank you for your reply :) $\endgroup$
    – SkellySoft
    Commented Sep 15, 2022 at 5:45

0

You must log in to answer this question.

Browse other questions tagged .