Cube In Cube Trimesh to Py5Shape

The following source demonstrates conversion of a trimesh cube in a cube object to a Py5Shape. It was created and run in a Thonny editor.

import trimesh
import math
import py5

one = trimesh.creation.box((150,150,150));
other = trimesh.creation.box((150,150,150))

angle = math.pi / 3
direction = [1, 1, 1]
center = [0, 0, 0]

rot_matrix = trimesh.transformations.rotation_matrix(angle, direction, center)
other.apply_transform(rot_matrix)
mesh = one.union(other)

def setup():
    global cube
    py5.size(600, 600, py5.P3D)
    py5.window_title("Cube In a Cube Trimesh to Py5Shape")
    cube = py5.convert_shape(mesh)
    cube.set_stroke_weight(2)
    cube.set_fills(py5.color(py5.random(255),py5.random(255),py5.random(255)) for _ in range(108))

def draw():
    py5.background(255)
    py5.translate(py5.width/2, py5.height/2)
    py5.scale(1)
    py5.rotate_x(py5.remap(py5.mouse_y, 0, py5.height, py5.PI, -py5.PI))
    py5.rotate_y(py5.remap(py5.mouse_x, 0, py5.width, -py5.PI, py5.PI))
    py5.shape(cube)

py5.run_sketch()

Output:

1 Like