Segment an object in X, Y and Z
Using a script written for blender 2.80
Split this thing 4 in x 3 in z
I've been playing around with a bmesh bisect script that is a lot quicker, but unfortunately doesn't fill in the slices like a boolean. Here is a boolean modifier version.
The method is similar in using the bounding box to align the planes.
Add a bisect plane, make it solid by adding a solidify modifier and
thickness. Use the object dimensions to make sure it is both way bigger and thicker.
Slice along the bounding box, shave a piece off as a dupe, flip the
direction of thickness and shave that piece off original. By shave
I am using boolean difference modifier
Example of reversing the boolean by negating the thickness of the solidify modifier
Add a boolean difference modifer to object, make the plane the
modifier object.
Move the Plane to each cut location and repeat the process on result meshes for each axis.
Clean up, remove the plane
.
The script. Edit segments_x, segments_y, segments_z
to suit. (Splits in local coordinates)
import bpy
from mathutils import Matrix, Vector
context = bpy.context
ob = context.object
size = 4 * max(ob.dimensions)
mw = ob.matrix_world
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))
o, x, y, z = bbox_axes(ob)
bpy.ops.mesh.primitive_plane_add(
location=mw @ bbox_center(ob),
size=size)
chopper = context.object
m = chopper.modifiers.new("Sol", type='SOLIDIFY')
m.thickness = size
chopper.select_set(False)
def chop(ob, start, end, segments):
slices = []
planes = [(f, start.lerp(end, f / segments))
for f in range(1, segments)]
for i, p in planes:
m.thickness = -size
bm = ob.modifiers.new("BOOL",type="BOOLEAN")
bm.object = chopper
bm.operation = 'DIFFERENCE'
M = (mw @ end - mw @ start).to_track_quat('Z', 'X').to_matrix().to_4x4()
M.translation = mw @ p
chopper.matrix_world = M
cp = ob.copy()
cp.data = cp.data.copy()
context.scene.collection.objects.link(cp)
bpy.ops.object.modifier_apply({"object" : cp}, modifier="BOOL")
slices.append(cp)
m.thickness = size
bpy.ops.object.modifier_apply(
{"object" : ob}, modifier = 'BOOL')
slices.append(ob)
return slices
segments_x = 4
segments_y = 1
segments_z = 3
for ox in chop(ob, o, x, segments_x):
for oy in chop(ox, o, y, segments_y):
chop(oy, o, z, segments_z)
bpy.data.objects.remove(chopper)
Related
Create Cross Sections and 2D planes in Blender
Render 2D cross sections of 3D Objects
Python: Bisect mesh into n parts, without separate 'LOOSE'