Rust¶
The Rust examples run without Python and need Rust 1.88 or later. This checkout
pins Rust 1.88.0 in rust-toolchain.toml.
Run the example¶
From the repository root:
cargo run --example build
The complete example loads a catalog, builds a floor, validates it, and writes a schematic. For a larger generated structure, see the Eiffel Tower example.
Catalogs download on first use. The cache must be writable; see catalogs and offline setup.
Dependencies¶
Add these dependencies to your application's Cargo.toml, replacing the path
with the location of this checkout:
[dependencies]
mcschemora = { path = "/path/to/mcschemora" }
pollster = "0.4"
Copy examples/build.rs to your application's src/main.rs
and run cargo run. The example writes examples/output/floor.schem relative to
the working directory.
Rendering¶
In the example's async block, after the output directory is created, add:
let assets = data.geometry_assets(&schematic.version)?;
let prepared = assets.prepare(&schematic, &mcschemora::render::SceneOptions::default())?;
let png = mcschemora::render::png::encode(&prepared, &mcschemora::render::png::Options::default())?;
fs::write(output.join("floor.png"), png)?;
println!("Visual diagnostics: {:?}", prepared.diagnostics);
Geometry preparation downloads visual assets on first use. Inspect its diagnostics
for approximations and check the rendering coverage.
Build the Rust API reference locally with
cargo doc -p mcschemora --no-deps --open.
Complete example¶
use mcschemora::{
catalog::MinecraftData,
formats,
model::{Block, Bounds, Schematic, Selection},
};
use std::{error::Error, fs, path::Path, sync::Arc};
fn main() -> Result<(), Box<dyn Error>> {
pollster::block_on(async {
let data = Arc::new(MinecraftData::new(None, false)?);
data.load("1.21.1").await?;
let mut schematic = Schematic::new("java", "1.21.1", data.clone())?;
let floor = Selection::new(
schematic.region("main")?,
Bounds::new([0, 0, 0], [7, 1, 7])?,
);
schematic.fill("main", &floor, &Block::parse("stone_bricks")?)?;
println!("Validation: {:?}", schematic.validate());
let output = Path::new("examples/output");
fs::create_dir_all(output)?;
fs::write(
output.join("floor.schem"),
formats::encode(&schematic, "schem", None, false, false).await?,
)?;
println!("Saved floor.schem");
Ok(())
})
}