Skip to content

Commit 6e3d866

Browse files
committed
Pass sketch filename to Sketch
1 parent dc8d7ab commit 6e3d866

File tree

4 files changed

+28
-3
lines changed

4 files changed

+28
-3
lines changed

crates/processing_pyo3/src/graphics.rs

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -125,13 +125,15 @@ impl Graphics {
125125
height: u32,
126126
asset_path: &str,
127127
sketch_root_path: &str,
128+
sketch_file_name: &str,
128129
) -> PyResult<Self> {
129130
let glfw_ctx =
130131
GlfwContext::new(width, height).map_err(|e| PyRuntimeError::new_err(format!("{e}")))?;
131132

132133
let mut config = Config::new();
133134
config.set(ConfigKey::AssetRootPath, asset_path.to_string());
134135
config.set(ConfigKey::SketchRootPath, sketch_root_path.to_string());
136+
config.set(ConfigKey::SketchFileName, sketch_file_name.to_string());
135137
init(config).map_err(|e| PyRuntimeError::new_err(format!("{e}")))?;
136138

137139
let surface = glfw_ctx

crates/processing_pyo3/src/lib.rs

Lines changed: 14 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,6 @@ mod graphics;
1414
use graphics::{Geometry, Graphics, Image, Topology, get_graphics, get_graphics_mut};
1515
use pyo3::{
1616
exceptions::PyRuntimeError,
17-
ffi::c_str,
1817
prelude::*,
1918
types::{PyDict, PyTuple},
2019
};
@@ -84,16 +83,30 @@ fn get_sketch_root() -> PyResult<String> {
8483
})
8584
}
8685

86+
fn get_sketch_filename() -> PyResult<String> {
87+
Python::attach(|py| {
88+
let sys = PyModule::import(py, "sys")?;
89+
let argv: Vec<String> = sys.getattr("argv")?.extract()?;
90+
let filename: &str = argv[0].as_str();
91+
let os = PyModule::import(py, "os")?;
92+
let path = os.getattr("path")?;
93+
let basename = path.getattr("basename")?.call1((filename,))?;
94+
Ok(basename.to_string())
95+
})
96+
}
97+
8798
#[pyfunction]
8899
#[pyo3(pass_module)]
89100
fn size(module: &Bound<'_, PyModule>, width: u32, height: u32) -> PyResult<()> {
90101
let asset_path: String = get_asset_root()?;
91102
let sketch_root_path: String = get_sketch_root()?;
103+
let sketch_filename: String = get_sketch_filename()?;
92104
let graphics = Graphics::new(
93105
width,
94106
height,
95107
asset_path.as_str(),
96108
sketch_root_path.as_str(),
109+
sketch_filename.as_str(),
97110
)?;
98111
module.setattr("_graphics", graphics)?;
99112
Ok(())

crates/processing_render/src/config.rs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@ use std::collections::HashMap;
99
pub enum ConfigKey {
1010
AssetRootPath,
1111
SketchRootPath,
12+
SketchFileName,
1213
}
1314

1415
// TODO: Consider Box<dyn Any> instead of String

crates/processing_render/src/sketch.rs

Lines changed: 11 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,8 @@ use bevy::{
99
};
1010
use std::path::Path;
1111

12+
use crate::config::{Config, ConfigKey};
13+
1214
/// Plugin that registers the Sketch asset type and its loader.
1315
pub struct LivecodePlugin;
1416

@@ -41,8 +43,15 @@ pub fn sketch_update_handler(
4143
None
4244
}
4345

44-
fn load_current_sketch(mut commands: Commands, asset_server: Res<AssetServer>) {
45-
let path = Path::new("rectangle.py");
46+
fn load_current_sketch(
47+
mut commands: Commands,
48+
asset_server: Res<AssetServer>,
49+
config: Res<Config>,
50+
) {
51+
let filename = config
52+
.get(ConfigKey::SketchFileName)
53+
.expect("SketchFileName not set");
54+
let path = Path::new(filename);
4655
let source = AssetSourceId::from("sketch_directory");
4756
let asset_path = AssetPath::from_path(path).with_source(source);
4857
let sketch_handle: Handle<Sketch> = asset_server.load(asset_path);

0 commit comments

Comments
 (0)