Generate 3D (STL) files from cube definitions.
Project description
CubeForge
CubeForge is a Python library designed to easily generate 3D mesh files (currently STL format) by defining models voxel by voxel. It allows for flexible voxel dimensions and positioning using various anchor points.
[Documentation] | [GitHub] | [PyPI]
Features
- Voxel-based Modeling: Define 3D shapes by adding individual voxels (cubes).
- Non-Uniform Voxel Dimensions: Specify default non-uniform dimensions for a model, and override dimensions on a per-voxel basis.
- Flexible Anchoring: Position voxels using different anchor points (
cubeforge.CubeAnchor) like corners or centers. - Configurable Coordinate Systems: Choose between Y-up (default) or Z-up coordinate systems for compatibility with different tools.
- Mesh Optimization: Optional greedy meshing algorithm reduces file sizes by 10-100× for regular structures.
- STL Export: Save the generated mesh to both ASCII and Binary STL file formats.
- Simple API: Easy-to-use interface with the core
cubeforge.VoxelModelclass.
Installation
Install from PyPI:
You can install CubeForge directly from PyPI using pip:
pip install cubeforge
Install from source:
You can also clone the repository and install the package using pip:
pip install .
Usage
Here's a basic example of how to create a simple shape and save it as an STL file:
import cubeforge
import os
# Create a model with default 1x1x1 voxel dimensions
model = cubeforge.VoxelModel()
# Add some voxels using the default CORNER_NEG anchor
model.add_voxel(0, 0, 0)
model.add_voxel(1, 0, 0)
model.add_voxel(1, 1, 0)
# --- Or add multiple voxels at once ---
# model.add_voxels([(0, 0, 0), (1, 0, 0), (1, 1, 0)])
# --- Example with custom dimensions per voxel ---
tower_model = cubeforge.VoxelModel(voxel_dimensions=(1.0, 1.0, 1.0))
# Add a 1x1x1 base cube centered at (0,0,0)
tower_model.add_voxel(0, 0, 0, anchor=cubeforge.CubeAnchor.CENTER)
# Stack a wide, low 3x1x3 cube on top of it
tower_model.add_voxel(0, 0.5, 0, anchor=cubeforge.CubeAnchor.BOTTOM_CENTER, dimensions=(3.0, 1.0, 3.0))
# Define output path
output_dir = "output"
os.makedirs(output_dir, exist_ok=True)
output_filename = os.path.join(output_dir, "my_shape.stl")
# Save the mesh as a binary STL file
model.save_mesh(output_filename, format='stl_binary', solid_name="MyCustomShape")
print(f"Saved mesh to {output_filename}")
[!NOTE] Custom voxel dimensions are snapped to the model's grid spacing (multiples of
voxel_dimensions). Non-aligned sizes are rounded to the nearest grid multiple.
Coordinate Systems
CubeForge supports two coordinate system modes:
Important: Dimensions are always specified as (x_size, y_size, z_size) in axis order, regardless of coordinate system. The coordinate system only determines which axis is vertical.
Y-up Mode (Default)
In Y-up mode, the Y axis represents the vertical/height direction:
- Coordinates:
(x, y, z)where y is up - Dimensions:
(x_size, y_size, z_size)where y_size is the vertical dimension BOTTOM_CENTER/TOP_CENTERanchors refer to Y faces (top/bottom)
Note: Models created in Y-up mode will appear rotated 90° in most STL viewers and 3D printing slicers, which expect Z-up orientation.
Z-up Mode (Recommended for 3D Printing)
In Z-up mode, the Z axis represents the vertical/height direction:
- Coordinates:
(x, y, z)where z is up - Dimensions:
(x_size, y_size, z_size)where z_size is the vertical dimension BOTTOM_CENTER/TOP_CENTERanchors refer to Z faces (top/bottom)
This mode ensures exported STL files appear correctly oriented in most 3D printing slicers and CAD programs.
Example: Creating a Vertical Tower for 3D Printing
import cubeforge
# Create model in Z-up mode for correct STL orientation
model = cubeforge.VoxelModel(voxel_dimensions=(1.0, 1.0, 1.0), coordinate_system='z_up')
# Stack voxels vertically along Z axis
model.add_voxel(0, 0, 0) # Bottom
model.add_voxel(0, 0, 1) # Middle
model.add_voxel(0, 0, 2) # Top
# Save - will appear correctly oriented in slicers!
model.save_mesh("tower.stl", format='stl_binary')
Mesh Optimization
CubeForge uses greedy meshing optimization by default to dramatically reduce file sizes for voxel-based models.
How It Works
Instead of creating separate triangles for each voxel face, the optimizer merges adjacent coplanar faces into larger rectangles:
Without optimization (9 voxels): With optimization:
[■][■][■] 9 quads = 18 triangles [■■■■■■■] 1 quad = 2 triangles
[■][■][■] [■■■■■■■]
[■][■][■] [■■■■■■■]
Usage
Optimization is enabled by default - just use save_mesh():
import cubeforge
model = cubeforge.VoxelModel(coordinate_system='z_up')
# Create a large flat surface
for x in range(20):
for y in range(20):
model.add_voxel(x, y, 0)
# Save - optimization enabled by default, 99% smaller!
model.save_mesh("surface.stl")
# To disable optimization (not recommended):
# model.save_mesh("surface.stl", optimize=False)
When to disable: Only if you specifically need individual voxel faces preserved (rare).
Examples
Basic Shapes Example
The examples/create_shapes.py script demonstrates various features, including:
- Creating simple and complex shapes.
- Using different default voxel dimensions.
- Overriding dimensions for individual voxels.
- Utilizing various
CubeAnchoroptions. - Saving in both ASCII and Binary STL formats.
- Generating random height surfaces in both Y-up and Z-up modes for comparison.
Coordinate Systems Example
The examples/coordinate_systems.py script demonstrates:
- Comparing Y-up vs Z-up coordinate systems
- Creating vertical towers in both modes
- Using
BOTTOM_CENTERandTOP_CENTERanchors correctly in Z-up mode - Custom dimensions in Z-up mode for 3D printing
Mesh Optimization Example
The examples/mesh_optimization.py script demonstrates:
- Comparing file sizes with and without optimization
- Greedy meshing on various model types (surfaces, cubes, towers)
- Real-world performance measurements
- When optimization provides the most benefit
To run the examples:
python examples/create_shapes.py
python examples/coordinate_systems.py
python examples/mesh_optimization.py
The output STL files will be saved in the examples directory.
API Overview
cubeforge.VoxelModel: The main class for creating and managing the voxel model.__init__(self, voxel_dimensions=(1.0, 1.0, 1.0), coordinate_system='y_up'): Initializes the model with default voxel dimensions and coordinate system. Usecoordinate_system='z_up'for 3D printing.
add_voxel(self, x, y, z, anchor=CubeAnchor.CORNER_NEG, dimensions=None): Adds a single voxel, optionally with custom dimensions snapped to the voxel grid spacing (multiples ofvoxel_dimensions).add_voxels(self, coordinates, anchor=CubeAnchor.CORNER_NEG, dimensions=None): Adds multiple voxels, optionally with custom dimensions snapped to the voxel grid spacing.remove_voxel(self, x, y, z, anchor=CubeAnchor.CORNER_NEG): Removes a voxel.clear(self): Removes all voxels.generate_mesh(self, optimize=True): Generates the triangle mesh data. Optimization enabled by default. Setoptimize=Falseto disable.save_mesh(self, filename, format='stl_binary', optimize=True, **kwargs): Generates and saves the mesh to a file. Optimization enabled by default for smaller files.
cubeforge.CubeAnchor: Anenumdefining the reference points for voxel placement (CORNER_NEG,CENTER,CORNER_POS,BOTTOM_CENTER,TOP_CENTER).cubeforge.get_writer(format_id): Factory function to get mesh writer instances (used internally bysave_mesh). Supports'stl','stl_binary','stl_ascii'.
Contributing
Contributions are welcome! Please feel free to submit issues or pull requests on the GitHub repository.
License
This project is licensed under the MIT License.
This project is developed and maintained by Teddy van Jerry (Wuqiong Zhao). The development is assisted by Gemini 2.5 Pro and Claude Code.
Project details
Download files
Download the file for your platform. If you're not sure which to choose, learn more about installing packages.
Source Distribution
Built Distribution
Filter files by name, interpreter, ABI, and platform.
If you're not sure about the file name format, learn more about wheel file names.
Copy a direct link to the current filters
File details
Details for the file cubeforge-0.2.3.tar.gz.
File metadata
- Download URL: cubeforge-0.2.3.tar.gz
- Upload date:
- Size: 19.1 kB
- Tags: Source
- Uploaded using Trusted Publishing? Yes
- Uploaded via: twine/6.1.0 CPython/3.13.7
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
3cb2df083643186d825ea1fb80027bedcf76f07df12a0fcb90820c9dfa2595f1
|
|
| MD5 |
35996098c662aa4f8672923ae3d6ca4b
|
|
| BLAKE2b-256 |
7566b596b4bb21ec13ba0faab9cdc092b4343c0f2aa227930de3936831b2c874
|
Provenance
The following attestation bundles were made for cubeforge-0.2.3.tar.gz:
Publisher:
publish.yml on Teddy-van-Jerry/cubeforge
-
Statement:
-
Statement type:
https://in-toto.io/Statement/v1 -
Predicate type:
https://docs.pypi.org/attestations/publish/v1 -
Subject name:
cubeforge-0.2.3.tar.gz -
Subject digest:
3cb2df083643186d825ea1fb80027bedcf76f07df12a0fcb90820c9dfa2595f1 - Sigstore transparency entry: 814123852
- Sigstore integration time:
-
Permalink:
Teddy-van-Jerry/cubeforge@bef87768cbaf777edc94d6ded1c3bbe00f7d5f38 -
Branch / Tag:
refs/tags/v0.2.3 - Owner: https://github.com/Teddy-van-Jerry
-
Access:
public
-
Token Issuer:
https://token.actions.githubusercontent.com -
Runner Environment:
github-hosted -
Publication workflow:
publish.yml@bef87768cbaf777edc94d6ded1c3bbe00f7d5f38 -
Trigger Event:
push
-
Statement type:
File details
Details for the file cubeforge-0.2.3-py3-none-any.whl.
File metadata
- Download URL: cubeforge-0.2.3-py3-none-any.whl
- Upload date:
- Size: 16.8 kB
- Tags: Python 3
- Uploaded using Trusted Publishing? Yes
- Uploaded via: twine/6.1.0 CPython/3.13.7
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
9c36698b661dda0c800bcc90a18bdcdc6b840aa516a64a0cfe4cdc727adfdd16
|
|
| MD5 |
1308b3a2f7a7998ad4186c82bd3426f0
|
|
| BLAKE2b-256 |
52481548ed2e9e91c2578b4e090be027c7ea4e3fbbc50718923e9683b3b7c3af
|
Provenance
The following attestation bundles were made for cubeforge-0.2.3-py3-none-any.whl:
Publisher:
publish.yml on Teddy-van-Jerry/cubeforge
-
Statement:
-
Statement type:
https://in-toto.io/Statement/v1 -
Predicate type:
https://docs.pypi.org/attestations/publish/v1 -
Subject name:
cubeforge-0.2.3-py3-none-any.whl -
Subject digest:
9c36698b661dda0c800bcc90a18bdcdc6b840aa516a64a0cfe4cdc727adfdd16 - Sigstore transparency entry: 814123855
- Sigstore integration time:
-
Permalink:
Teddy-van-Jerry/cubeforge@bef87768cbaf777edc94d6ded1c3bbe00f7d5f38 -
Branch / Tag:
refs/tags/v0.2.3 - Owner: https://github.com/Teddy-van-Jerry
-
Access:
public
-
Token Issuer:
https://token.actions.githubusercontent.com -
Runner Environment:
github-hosted -
Publication workflow:
publish.yml@bef87768cbaf777edc94d6ded1c3bbe00f7d5f38 -
Trigger Event:
push
-
Statement type: