#parser #blender #blend #gamedev

blend-rs

A Rust library to read Blender's .blend files

3 releases (breaking)

0.3.0 Dec 23, 2022
0.2.0 Aug 7, 2022
0.1.0 Aug 7, 2022

#1641 in Game dev

Apache-2.0

1.5MB
677 lines

blend-rs

A Rust library to read Blender's .blend files without the hassle of byte parsing.

Note: For more details see the documentation.

Description

This library belongs to a set of three libraries which are all related to the topic of reading Blender's .blend files:

  • blend-inspect-rs: A Rust library to parse and analyse Blender's .blend files.

  • blend-bindgen-rs: A Rust library to generated Blender's data structures.

  • blend-rs: A Rust library to read objects from Blender's .blend files.

Contributions

All contributions are welcome: ideas, patches, documentation, bug reports, complaints. Any contribution intentionally submitted for inclusion in this crate by you, as defined in the Apache-2.0 license, shall be licensed without any additional terms or conditions.

License

Licensed under Apache License, Version 2.0.


lib.rs:

githubcrates-iodocs-rs

blender-rs

A Rust library to read Blender's .blend files without the hassle of byte parsing.

Note: Due to the functioning of this library not all blender version are supported. The table about Crate Features lists the supported blender version.

Example

The example below illustrates how to extract the coordinates of each vertex of an object's mesh.

use blend_rs::blend::{read, StructIter, PointerLike, NameLike};
use blend_rs::blend::traverse::Named;

use blend_rs::blender3_2::{Object, Mesh, MPoly, MVert, MLoop};

pub struct Vertex {
    pub position: [f32; 3],
}

fn main() {

   let blend_data = std::fs::read("examples/example-3.2.blend")
       .expect("Blend file not found!");

   let reader = read(&blend_data)
       .expect("Failed to read blend data!");

   let plane: &Object = reader.iter::<Object>().unwrap()
       .find(|object| object.id.get_name() == "Plane")
       .unwrap();

   let mesh = reader.deref_single(&plane.data.as_instance_of::<Mesh>())
       .expect("Could not get mesh from object!");

   let mesh_polygons: StructIter<MPoly> = reader.deref(&mesh.mpoly)
       .expect("Could not get polygons from mesh!");

   let mesh_loops: Vec<&MLoop> = reader.deref(&mesh.mloop)
       .expect("Could not get loops from mesh!")
       .collect();

   let mesh_vertices: Vec<&MVert> = reader.deref(&mesh.mvert)
       .expect("Could not get vertices from mesh!")
       .collect();

   let vertices_per_polygon: Vec<Vec<Vertex>> = mesh_polygons
       .map(|polygon| {
           (polygon.loopstart..polygon.loopstart + polygon.totloop).into_iter()
               .map(|loop_index| {
                   Vertex {
                       position: mesh_vertices[mesh_loops[loop_index as usize].v as usize].co
                   }
               })
               .collect()
       })
       .collect();
}

Crate Features

Enable or disable features according to your needs and in order to optimize compile time.

Feature Default Description
blender2_79 Generate and include code for blender 2.79 (64 Bit, little endian)
blender2_80 Generate and include code for blender 2.80 (64 Bit, little endian)
blender2_80x86 Generate and include code for blender 2.80 (32 Bit, little endian)
blender2_93 Generate and include code for blender 2.93 (64 Bit, little endian)
blender3_2 Generate and include code for blender 3.2 (64 Bit, little endian)
blender3_3 Generate and include code for blender 3.3 (64 Bit, little endian)
all Generate and include code for all above blender versions.

✔ enabled, ✗ disabled

Details

blend-rs depends heavily on code generated from the Blender DNA. The Blender DNA is a part of each *.blend file and contains a description for all structs, types and names within the file. Blender uses the DNA for forward- and backward compatibility. blend-rs uses the DNA to generate rust code.

This library belongs to a set of three libraries which are all related to the topic of reading Blender's .blend files:

  • blend-inspect-rs: A Rust library to parse and analyse Blender's .blend files.
  • blend-bindgen-rs: A Rust library to generated Blender's data structures.
  • blend-rs: A Rust library to read Blender's .blend files.

Dependencies