Skip to content

Commit b75067d

Browse files
committed
kwargs for mesh method and default topology
1 parent 4748187 commit b75067d

File tree

2 files changed

+34
-5
lines changed

2 files changed

+34
-5
lines changed

crates/processing_pyo3/src/graphics.rs

Lines changed: 32 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
use bevy::prelude::Entity;
22
use processing::prelude::*;
3-
use pyo3::{exceptions::PyRuntimeError, prelude::*};
3+
use pyo3::{exceptions::PyRuntimeError, prelude::*, types::PyDict};
44

55
use crate::glfw::GlfwContext;
66

@@ -40,12 +40,40 @@ pub struct Mesh {
4040
entity: Entity,
4141
}
4242

43+
#[pyclass]
44+
pub enum Topology {
45+
PointList = 0,
46+
LineList = 1,
47+
LineStrip = 2,
48+
TriangleList = 3,
49+
TriangleStrip = 4,
50+
}
51+
52+
impl Topology {
53+
pub fn as_u8(&self) -> u8 {
54+
match self {
55+
Self::PointList => 0,
56+
Self::LineList => 1,
57+
Self::LineStrip => 2,
58+
Self::TriangleList => 3,
59+
Self::TriangleStrip => 4,
60+
}
61+
}
62+
}
63+
4364
#[pymethods]
4465
impl Mesh {
4566
#[new]
46-
pub fn new() -> PyResult<Self> {
47-
let geometry = geometry_create(geometry::Topology::TriangleList)
48-
.map_err(|e| PyRuntimeError::new_err(format!("{e}")))?;
67+
#[pyo3(signature = (**kwargs))]
68+
pub fn new(kwargs: Option<&Bound<'_, PyDict>>) -> PyResult<Self> {
69+
let topology = kwargs
70+
.and_then(|k| k.get_item("topology").ok().flatten())
71+
.and_then(|t| t.cast_into::<Topology>().ok())
72+
.and_then(|t| geometry::Topology::from_u8(t.borrow().as_u8()))
73+
.unwrap_or(geometry::Topology::TriangleList);
74+
75+
let geometry =
76+
geometry_create(topology).map_err(|e| PyRuntimeError::new_err(format!("{e}")))?;
4977
Ok(Self { entity: geometry })
5078
}
5179

crates/processing_pyo3/src/lib.rs

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@
1111
mod glfw;
1212
mod graphics;
1313

14-
use graphics::{Graphics, Image, Mesh, get_graphics, get_graphics_mut};
14+
use graphics::{Graphics, Image, Mesh, Topology, get_graphics, get_graphics_mut};
1515
use pyo3::{exceptions::PyRuntimeError, prelude::*, types::PyTuple};
1616

1717
use std::env;
@@ -21,6 +21,7 @@ fn processing(m: &Bound<'_, PyModule>) -> PyResult<()> {
2121
m.add_class::<Graphics>()?;
2222
m.add_class::<Image>()?;
2323
m.add_class::<Mesh>()?;
24+
m.add_class::<Topology>()?;
2425
m.add_function(wrap_pyfunction!(size, m)?)?;
2526
m.add_function(wrap_pyfunction!(run, m)?)?;
2627
m.add_function(wrap_pyfunction!(mode_3d, m)?)?;

0 commit comments

Comments
 (0)