0
$\begingroup$

Using these examples

Slicing an object in 4 parts How can I evenly split a cube?

I was able to cut an object into pieces along the x, y and z axes with a python script. But I can't do it along different axes (not x,y,z of the object). For example I would like to cut a cube according to axes not parallel to the edges and save the pieces obtained.

Please could someone give me an example or give me some suggestions to do this?

$\endgroup$
0
$\begingroup$

Alignment object.

enter image description here

Have made a small modification to my answer to linked question, to instead of using the objects bounding box, use the global bounding box of the object named "Cube".

Above shows a test run on default cone. The active object gets sliced. It uses the object named "Cube" as axes.

This could all be done via script by realigning and calculating the bbox given the chosen slicing axis.

import bpy
from mathutils import Matrix, Vector
context = bpy.context
ob = context.object
scene = context.scene
axis_ob = scene.objects.get("Cube")

size = 4 * max(axis_ob.dimensions)
mw = axis_ob.matrix_world

def bbox(ob, matrix=Matrix()):
    return (matrix @ Vector(b) for b in ob.bound_box)

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

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

        

bpy.ops.mesh.primitive_plane_add(
        location=bbox_center(axis_ob, axis_ob.matrix_world),
        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 = 1
segments_y = 1
segments_z = 8

# change the cutting box
block = context.scene.objects.get("Block")

o, x, y, z = bbox_axes(axis_ob, mw.inverted() @ axis_ob.matrix_world)
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)
$\endgroup$
1
  • $\begingroup$ Thank you so much I try it now with some objects $\endgroup$ – greenhouse Jan 14 at 16:16

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.