From f9a981fce0fa4cacf8729bc4a4e4286e74a00561 Mon Sep 17 00:00:00 2001 From: "Moon D." Date: Thu, 18 Dec 2025 17:46:53 -0800 Subject: [PATCH 1/3] Calling setup() function --- crates/processing_pyo3/src/lib.rs | 31 ++++++++++--------------------- rectangle.py | 19 +++++++++++++++++++ 2 files changed, 29 insertions(+), 21 deletions(-) create mode 100644 rectangle.py diff --git a/crates/processing_pyo3/src/lib.rs b/crates/processing_pyo3/src/lib.rs index 9fd0611..98452da 100644 --- a/crates/processing_pyo3/src/lib.rs +++ b/crates/processing_pyo3/src/lib.rs @@ -12,7 +12,7 @@ mod glfw; mod graphics; use graphics::{Graphics, get_graphics, get_graphics_mut}; -use pyo3::{exceptions::PyRuntimeError, prelude::*, types::PyAny}; +use pyo3::{exceptions::PyRuntimeError, prelude::*, types::PyFunction}; #[pymodule] fn processing(m: &Bound<'_, PyModule>) -> PyResult<()> { @@ -38,27 +38,16 @@ fn size(module: &Bound<'_, PyModule>, width: u32, height: u32) -> PyResult<()> { } #[pyfunction] -#[pyo3(pass_module, signature = (draw_fn=None))] -fn run(module: &Bound<'_, PyModule>, draw_fn: Option>) -> PyResult<()> { - loop { - { - let mut graphics = get_graphics_mut(module)?; - if !graphics.surface.poll_events() { - break; - } - graphics.begin_draw()?; - } - - if let Some(ref draw) = draw_fn { - Python::attach(|py| { - draw.call0(py) - .map_err(|e| PyRuntimeError::new_err(format!("{e}"))) - })?; - } +#[pyo3(pass_module)] +fn run(module: &Bound<'_, PyModule>) -> PyResult<()> { + Python::attach(|py| { + let builtins = PyModule::import(py, "builtins")?; + let locals = builtins.getattr("locals")?.call0()?; + let setup_fn = locals.get_item("setup")?; + setup_fn.call0()?; - get_graphics(module)?.end_draw()?; - } - Ok(()) + Ok(()) + }) } #[pyfunction] diff --git a/rectangle.py b/rectangle.py new file mode 100644 index 0000000..5a33d03 --- /dev/null +++ b/rectangle.py @@ -0,0 +1,19 @@ +from processing import * + +def moon_func(): + print("Hello, Moon!") + +def setup(): + print("HELLO") + size(800, 600) + +def draw(): + background(220) + + fill(255, 0, 100) + stroke(0) + stroke_weight(2) + rect(100, 100, 200, 150) + +# TODO: this should happen implicitly on module load somehow +run() From 2a380c7f29b0797298722a9b688910f6c0c168a8 Mon Sep 17 00:00:00 2001 From: "Moon D." Date: Fri, 19 Dec 2025 13:12:42 -0800 Subject: [PATCH 2/3] Call draw every frame --- crates/processing_pyo3/examples/rectangle.py | 6 +++--- crates/processing_pyo3/src/lib.rs | 21 ++++++++++++++++++++ rectangle.py | 19 ------------------ 3 files changed, 24 insertions(+), 22 deletions(-) delete mode 100644 rectangle.py diff --git a/crates/processing_pyo3/examples/rectangle.py b/crates/processing_pyo3/examples/rectangle.py index cce3fe1..ac03477 100644 --- a/crates/processing_pyo3/examples/rectangle.py +++ b/crates/processing_pyo3/examples/rectangle.py @@ -1,7 +1,7 @@ from processing import * -# TODO: this should be in a setup function -size(800, 600) +def setup(): + size(800, 600) def draw(): background(220) @@ -12,4 +12,4 @@ def draw(): rect(100, 100, 200, 150) # TODO: this should happen implicitly on module load somehow -run(draw) +run() diff --git a/crates/processing_pyo3/src/lib.rs b/crates/processing_pyo3/src/lib.rs index 98452da..18bc4cf 100644 --- a/crates/processing_pyo3/src/lib.rs +++ b/crates/processing_pyo3/src/lib.rs @@ -43,9 +43,30 @@ fn run(module: &Bound<'_, PyModule>) -> PyResult<()> { Python::attach(|py| { let builtins = PyModule::import(py, "builtins")?; let locals = builtins.getattr("locals")?.call0()?; + let setup_fn = locals.get_item("setup")?; + let draw_fn = locals.get_item("draw")?; + + // call setup setup_fn.call0()?; + // start draw loop + loop { + { + let mut graphics = get_graphics_mut(module)?; + if !graphics.surface.poll_events() { + break; + } + graphics.begin_draw()?; + } + + draw_fn + .call0() + .map_err(|e| PyRuntimeError::new_err(format!("{e}")))?; + + get_graphics(module)?.end_draw()?; + } + Ok(()) }) } diff --git a/rectangle.py b/rectangle.py deleted file mode 100644 index 5a33d03..0000000 --- a/rectangle.py +++ /dev/null @@ -1,19 +0,0 @@ -from processing import * - -def moon_func(): - print("Hello, Moon!") - -def setup(): - print("HELLO") - size(800, 600) - -def draw(): - background(220) - - fill(255, 0, 100) - stroke(0) - stroke_weight(2) - rect(100, 100, 200, 150) - -# TODO: this should happen implicitly on module load somehow -run() From bfab4638306bcb9c86e898ceca9f6154414d1011 Mon Sep 17 00:00:00 2001 From: "Moon D." Date: Fri, 19 Dec 2025 13:29:07 -0800 Subject: [PATCH 3/3] clippy --- crates/processing_pyo3/src/lib.rs | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/crates/processing_pyo3/src/lib.rs b/crates/processing_pyo3/src/lib.rs index 18bc4cf..432c433 100644 --- a/crates/processing_pyo3/src/lib.rs +++ b/crates/processing_pyo3/src/lib.rs @@ -12,7 +12,7 @@ mod glfw; mod graphics; use graphics::{Graphics, get_graphics, get_graphics_mut}; -use pyo3::{exceptions::PyRuntimeError, prelude::*, types::PyFunction}; +use pyo3::{exceptions::PyRuntimeError, prelude::*}; #[pymodule] fn processing(m: &Bound<'_, PyModule>) -> PyResult<()> {