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.
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. - 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, flat 3x0.5x3 cube on top of it
tower_model.add_voxel(0, 0.5, 0, anchor=cubeforge.CubeAnchor.BOTTOM_CENTER, dimensions=(3.0, 0.5, 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}")
Examples
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 a surface with random heights.
To run the examples:
python examples/create_shapes.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)): Initializes the model, optionally setting default voxel dimensions.add_voxel(self, x, y, z, anchor=CubeAnchor.CORNER_NEG, dimensions=None): Adds a single voxel, optionally with custom dimensions.add_voxels(self, coordinates, anchor=CubeAnchor.CORNER_NEG, dimensions=None): Adds multiple voxels, optionally with custom dimensions.remove_voxel(self, x, y, z, anchor=CubeAnchor.CORNER_NEG): Removes a voxel.clear(self): Removes all voxels.generate_mesh(self): Generates the triangle mesh data.save_mesh(self, filename, format='stl_binary', **kwargs): Generates and saves the mesh to a file.
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.
Project details
Release history Release notifications | RSS feed
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.0.tar.gz.
File metadata
- Download URL: cubeforge-0.2.0.tar.gz
- Upload date:
- Size: 11.9 kB
- Tags: Source
- Uploaded using Trusted Publishing? Yes
- Uploaded via: twine/6.1.0 CPython/3.12.9
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
d60b4fd6a911da307d75163058fcec6f88e0313f3c6fd4bf0e5c6e86819e5f1e
|
|
| MD5 |
362e02f6e5b3cbd636da08a9c01b2582
|
|
| BLAKE2b-256 |
4cf11b14a0c58da08596cbd53f5b6a7ab081aa163f4d70bc72b5a802a645b2d0
|
Provenance
The following attestation bundles were made for cubeforge-0.2.0.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.0.tar.gz -
Subject digest:
d60b4fd6a911da307d75163058fcec6f88e0313f3c6fd4bf0e5c6e86819e5f1e - Sigstore transparency entry: 272218987
- Sigstore integration time:
-
Permalink:
Teddy-van-Jerry/cubeforge@02ebe7b5245f0a41aad9614b3976ea9f1fa2212d -
Branch / Tag:
refs/tags/v0.2.0 - Owner: https://github.com/Teddy-van-Jerry
-
Access:
public
-
Token Issuer:
https://token.actions.githubusercontent.com -
Runner Environment:
github-hosted -
Publication workflow:
publish.yml@02ebe7b5245f0a41aad9614b3976ea9f1fa2212d -
Trigger Event:
push
-
Statement type:
File details
Details for the file cubeforge-0.2.0-py3-none-any.whl.
File metadata
- Download URL: cubeforge-0.2.0-py3-none-any.whl
- Upload date:
- Size: 11.2 kB
- Tags: Python 3
- Uploaded using Trusted Publishing? Yes
- Uploaded via: twine/6.1.0 CPython/3.12.9
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
0c6c8bfe600b9956ce1dbe231948d6b1845b0b0e8086217fa9c412c54f04f536
|
|
| MD5 |
1024c86d079ef66c0605361c41fd3da6
|
|
| BLAKE2b-256 |
7a9f4b3396545787edc8690fb013d6d79f5be7ddabfd8c9d218828980e8d0a83
|
Provenance
The following attestation bundles were made for cubeforge-0.2.0-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.0-py3-none-any.whl -
Subject digest:
0c6c8bfe600b9956ce1dbe231948d6b1845b0b0e8086217fa9c412c54f04f536 - Sigstore transparency entry: 272218992
- Sigstore integration time:
-
Permalink:
Teddy-van-Jerry/cubeforge@02ebe7b5245f0a41aad9614b3976ea9f1fa2212d -
Branch / Tag:
refs/tags/v0.2.0 - Owner: https://github.com/Teddy-van-Jerry
-
Access:
public
-
Token Issuer:
https://token.actions.githubusercontent.com -
Runner Environment:
github-hosted -
Publication workflow:
publish.yml@02ebe7b5245f0a41aad9614b3976ea9f1fa2212d -
Trigger Event:
push
-
Statement type: