Skip to content

Commit dc8d7ab

Browse files
committed
Pass sketch file based on python file being executed
Clean up debug statements Remove unused code
1 parent dc23804 commit dc8d7ab

File tree

5 files changed

+30
-35
lines changed

5 files changed

+30
-35
lines changed

crates/processing_pyo3/examples/rectangle.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -7,8 +7,8 @@ def draw():
77
background(220)
88

99
fill(255, 0, 100)
10-
stroke(0)
11-
stroke_weight(2)
10+
stroke(1)
11+
stroke_weight(25)
1212
rect(100, 100, 200, 150)
1313

1414
# TODO: this should happen implicitly on module load somehow

crates/processing_pyo3/src/graphics.rs

Lines changed: 7 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -120,17 +120,18 @@ impl Drop for Graphics {
120120
#[pymethods]
121121
impl Graphics {
122122
#[new]
123-
pub fn new(width: u32, height: u32, asset_path: &str) -> PyResult<Self> {
123+
pub fn new(
124+
width: u32,
125+
height: u32,
126+
asset_path: &str,
127+
sketch_root_path: &str,
128+
) -> PyResult<Self> {
124129
let glfw_ctx =
125130
GlfwContext::new(width, height).map_err(|e| PyRuntimeError::new_err(format!("{e}")))?;
126131

127132
let mut config = Config::new();
128133
config.set(ConfigKey::AssetRootPath, asset_path.to_string());
129-
config.set(
130-
// TODO: this needs to be handed to us by python
131-
ConfigKey::SketchRootPath,
132-
"/home/moon/Code/libprocessing/crates/processing_pyo3/examples".to_string(),
133-
);
134+
config.set(ConfigKey::SketchRootPath, sketch_root_path.to_string());
134135
init(config).map_err(|e| PyRuntimeError::new_err(format!("{e}")))?;
135136

136137
let surface = glfw_ctx
@@ -160,10 +161,6 @@ impl Graphics {
160161
source: "".to_string(),
161162
}),
162163
}
163-
164-
// Ok(Sketch {
165-
// source: sketch.unwrap().source,
166-
// })
167164
}
168165

169166
pub fn background(&self, args: Vec<f32>) -> PyResult<()> {

crates/processing_pyo3/src/lib.rs

Lines changed: 20 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -71,11 +71,30 @@ fn get_asset_root() -> PyResult<String> {
7171
})
7272
}
7373

74+
fn get_sketch_root() -> PyResult<String> {
75+
Python::attach(|py| {
76+
let sys = PyModule::import(py, "sys")?;
77+
let argv: Vec<String> = sys.getattr("argv")?.extract()?;
78+
let filename: &str = argv[0].as_str();
79+
let os = PyModule::import(py, "os")?;
80+
let path = os.getattr("path")?;
81+
let dirname = path.getattr("dirname")?.call1((filename,))?;
82+
let abspath = path.getattr("abspath")?.call1((dirname,))?;
83+
Ok(abspath.to_string())
84+
})
85+
}
86+
7487
#[pyfunction]
7588
#[pyo3(pass_module)]
7689
fn size(module: &Bound<'_, PyModule>, width: u32, height: u32) -> PyResult<()> {
7790
let asset_path: String = get_asset_root()?;
78-
let graphics = Graphics::new(width, height, asset_path.as_str())?;
91+
let sketch_root_path: String = get_sketch_root()?;
92+
let graphics = Graphics::new(
93+
width,
94+
height,
95+
asset_path.as_str(),
96+
sketch_root_path.as_str(),
97+
)?;
7998
module.setattr("_graphics", graphics)?;
8099
Ok(())
81100
}

crates/processing_render/src/lib.rs

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -224,7 +224,6 @@ fn create_app(config: Config) -> App {
224224
}
225225

226226
if let Some(sketch_path) = config.get(ConfigKey::SketchRootPath) {
227-
println!("DEBUG SKETCH PATH = {sketch_path}");
228227
app.register_asset_source(
229228
"sketch_directory",
230229
AssetSourceBuilder::platform_default(sketch_path, None),

crates/processing_render/src/sketch.rs

Lines changed: 1 addition & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -9,20 +9,14 @@ use bevy::{
99
};
1010
use std::path::Path;
1111

12-
#[derive(Resource)]
13-
struct SketchNeedsReload(bool);
14-
1512
/// Plugin that registers the Sketch asset type and its loader.
1613
pub struct LivecodePlugin;
1714

1815
impl Plugin for LivecodePlugin {
1916
fn build(&self, app: &mut App) {
2017
app.init_asset::<Sketch>()
2118
.init_asset_loader::<SketchLoader>()
22-
// TODO: this could be switched to Message
23-
.insert_resource(SketchNeedsReload(false))
2419
.add_systems(PreStartup, load_current_sketch);
25-
// .add_systems(Update, sketch_update_handler);
2620
}
2721
}
2822

@@ -33,35 +27,21 @@ pub fn sketch_update_handler(
3327
) -> Option<Sketch> {
3428
for event in events.read() {
3529
match event {
36-
AssetEvent::Added { id } => {
37-
info!("Added: {id}")
38-
}
3930
AssetEvent::Modified { id } => {
4031
info!("Modified: {id}");
41-
// we want to emit some event to bevy??
42-
// needs_reload.0 = true;
4332
if let Some(sketch) = sketches.get(*id) {
4433
let sketch = sketch.clone();
4534
return Some(sketch);
4635
}
4736
}
48-
AssetEvent::Removed { id } => {
49-
info!("Removed: {id}")
50-
}
51-
AssetEvent::Unused { id } => {
52-
info!("Unused: {id}")
53-
}
54-
AssetEvent::LoadedWithDependencies { id } => {
55-
info!("LoadedWithDependencies: {id}")
56-
}
37+
_ => (),
5738
}
5839
}
5940

6041
None
6142
}
6243

6344
fn load_current_sketch(mut commands: Commands, asset_server: Res<AssetServer>) {
64-
info!("DEBUG: calling load_current_sketch");
6545
let path = Path::new("rectangle.py");
6646
let source = AssetSourceId::from("sketch_directory");
6747
let asset_path = AssetPath::from_path(path).with_source(source);

0 commit comments

Comments
 (0)