|
| 1 | +use bevy::prelude::Entity; |
| 2 | +use processing::prelude::*; |
| 3 | +use pyo3::{exceptions::PyRuntimeError, prelude::*}; |
| 4 | + |
| 5 | +use crate::graphics::{Geometry, Light, get_graphics}; |
| 6 | +use crate::material::Material; |
| 7 | + |
| 8 | +#[pyclass(unsendable)] |
| 9 | +pub struct Gltf { |
| 10 | + entity: Entity, |
| 11 | +} |
| 12 | + |
| 13 | +#[pymethods] |
| 14 | +impl Gltf { |
| 15 | + pub fn geometry(&self, name: &str) -> PyResult<Geometry> { |
| 16 | + let entity = gltf_geometry(self.entity, name) |
| 17 | + .map_err(|e| PyRuntimeError::new_err(format!("{e}")))?; |
| 18 | + Ok(Geometry { entity }) |
| 19 | + } |
| 20 | + |
| 21 | + pub fn material(&self, name: &str) -> PyResult<Material> { |
| 22 | + let entity = gltf_material(self.entity, name) |
| 23 | + .map_err(|e| PyRuntimeError::new_err(format!("{e}")))?; |
| 24 | + Ok(Material { entity }) |
| 25 | + } |
| 26 | + |
| 27 | + pub fn mesh_names(&self) -> PyResult<Vec<String>> { |
| 28 | + gltf_mesh_names(self.entity).map_err(|e| PyRuntimeError::new_err(format!("{e}"))) |
| 29 | + } |
| 30 | + |
| 31 | + pub fn material_names(&self) -> PyResult<Vec<String>> { |
| 32 | + gltf_material_names(self.entity).map_err(|e| PyRuntimeError::new_err(format!("{e}"))) |
| 33 | + } |
| 34 | + |
| 35 | + pub fn camera(&self, index: usize) -> PyResult<()> { |
| 36 | + gltf_camera(self.entity, index).map_err(|e| PyRuntimeError::new_err(format!("{e}"))) |
| 37 | + } |
| 38 | + |
| 39 | + pub fn light(&self, index: usize) -> PyResult<Light> { |
| 40 | + let entity = |
| 41 | + gltf_light(self.entity, index).map_err(|e| PyRuntimeError::new_err(format!("{e}")))?; |
| 42 | + Ok(Light { entity }) |
| 43 | + } |
| 44 | +} |
| 45 | + |
| 46 | +#[pyfunction] |
| 47 | +#[pyo3(pass_module)] |
| 48 | +pub fn load_gltf(module: &Bound<'_, PyModule>, path: &str) -> PyResult<Gltf> { |
| 49 | + let graphics = get_graphics(module)?; |
| 50 | + let entity = |
| 51 | + gltf_load(graphics.entity, path).map_err(|e| PyRuntimeError::new_err(format!("{e}")))?; |
| 52 | + Ok(Gltf { entity }) |
| 53 | +} |
0 commit comments