MagicaX (MagicaX v0.1.0)
View SourceMagicaX - Elixir toolkit for parsing and generating MagicaVoxel (.vox) files.
MagicaX provides two main components:
MagicaX.VoxParser- Complete VOX file parser with 99.85% data coverageMagicaX.VoxGenerator- Flexible VOX file generator with multiple input methods
Quick Start
Parsing a VOX file
{:ok, data} = MagicaX.parse_file("model.vox")
IO.inspect(data.size) # {32, 32, 32}
IO.inspect(data.voxels) # [{0, 0, 0, 1}, {1, 0, 0, 2}, ...]Generating a VOX file
# From JSON
{:ok, message} = MagicaX.generate_from_json_file("model.json")
# Basic shapes
{:ok, message} = MagicaX.generate_cube("cube.vox", 10)
{:ok, message} = MagicaX.generate_sphere("sphere.vox", 8)
# Programmatically
dimensions = {10, 10, 10}
voxels = [{0, 0, 0, 1}, {1, 1, 1, 2}]
{:ok, message} = MagicaX.generate_vox_file("model.vox", dimensions, voxels)Data Structures
Voxels
Represented as tuples of {x, y, z, color_index} where coordinates are 0-255.
Palette
Color definitions as tuples of {r, g, b, a} with 256 color entries.
Dimensions
Model size as tuples of {x_size, y_size, z_size}.
Supported Features
Parser Features
- All major chunk types (SIZE, XYZI, RGBA, MATL, LAYR, rOBJ, rCAM, NOTE, nTRN, nSHP, nGRP, META)
- 3D matrix representation for spatial operations
- Comprehensive chunk analysis and statistics
- Graceful error handling with detailed reporting
Generator Features
- JSON-to-VOX conversion with validation
- Programmatic voxel creation
- Built-in shape generators (cube, sphere, teapot)
- Custom palette support
- No external dependencies
Summary
Functions
Generates a cube VOX file.
Generates a VOX file from JSON data.
Generates a VOX file from a JSON file.
Generates a sphere VOX file.
Generates a teapot VOX file.
Generates a VOX file from dimensions, voxels, and optional palette.
Parses a VOX file and returns structured data.
Returns the version of the MagicaX library.
Functions
Generates a cube VOX file.
This is a convenience function that delegates to MagicaX.VoxGenerator.generate_cube/2.
Parameters
filename- Output VOX file pathsize- Cube size (default: 10)
Examples
{:ok, message} = MagicaX.generate_cube("cube.vox", 15)
Generates a VOX file from JSON data.
This is a convenience function that delegates to MagicaX.VoxGenerator.generate_from_json/2.
Parameters
filename- Output VOX file pathjson_string- JSON string containing model data
JSON Structure
{
"dimensions": [x, y, z], // REQUIRED
"voxels": [ // REQUIRED
{"x": 0, "y": 0, "z": 0, "color_index": 1}
],
"palette": [ // OPTIONAL
{"r": 255, "g": 0, "b": 0, "a": 255}
]
}Examples
json = ~s({"dimensions": [5,5,5], "voxels": [{"x":0,"y":0,"z":0,"color_index":1}]})
{:ok, message} = MagicaX.generate_from_json("model.vox", json)
Generates a VOX file from a JSON file.
This is a convenience function that delegates to MagicaX.VoxGenerator.generate_from_json_file/2.
Parameters
json_filename- Path to JSON file containing model dataoutput_filename- Optional output VOX file path (defaults to JSON filename with .vox extension)
Examples
# Creates model.vox from model.json
{:ok, message} = MagicaX.generate_from_json_file("model.json")
# Creates custom.vox from model.json
{:ok, message} = MagicaX.generate_from_json_file("model.json", "custom.vox")
Generates a sphere VOX file.
This is a convenience function that delegates to MagicaX.VoxGenerator.generate_sphere/2.
Parameters
filename- Output VOX file pathradius- Sphere radius (default: 10)
Examples
{:ok, message} = MagicaX.generate_sphere("sphere.vox", 12)
Generates a teapot VOX file.
This is a convenience function that delegates to MagicaX.VoxGenerator.generate_teapot/2.
Parameters
filename- Output VOX file pathscale- Scale factor (default: 1.0)
Examples
{:ok, message} = MagicaX.generate_teapot("teapot.vox", 0.5)
Generates a VOX file from dimensions, voxels, and optional palette.
This is a convenience function that delegates to MagicaX.VoxGenerator.generate_vox_file/4.
Parameters
filename- Output VOX file pathdimensions- Tuple of{x, y, z}dimensionsvoxels- List of{x, y, z, color_index}tuplespalette- Optional custom palette (defaults to built-in palette)
Examples
dimensions = {10, 10, 10}
voxels = [{0, 0, 0, 1}, {1, 1, 1, 2}, {2, 2, 2, 3}]
{:ok, message} = MagicaX.generate_vox_file("model.vox", dimensions, voxels)
Parses a VOX file and returns structured data.
This is a convenience function that delegates to MagicaX.VoxParser.parse_vox_file/1.
Parameters
filename- Path to the VOX file to parse
Returns
{:ok, data}- Successfully parsed data structure{:error, reason}- Parse error with description
Examples
{:ok, data} = MagicaX.parse_file("model.vox")
IO.inspect(data.size) # {32, 32, 32}
IO.inspect(length(data.voxels)) # 1024
Returns the version of the MagicaX library.
Examples
MagicaX.version() # "0.1.0"