Skip to content

Commit f314598

Browse files
authored
Merge pull request #435 from hx2A/processingmode_params
pass params to constructor when using processing mode
2 parents eae25ab + 86e2c81 commit f314598

File tree

4 files changed

+8
-4
lines changed

4 files changed

+8
-4
lines changed

generator/reference.py

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -111,6 +111,7 @@
111111
"sketch_args: list[str] = None",
112112
"sketch_functions: dict[str, Callable] = None",
113113
"jclassname: str = None",
114+
"jclass_params: tuple[Any] = ()",
114115
],
115116
"None",
116117
)

py5_docs/Reference/api_en/Sketch_run_sketch.txt

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -5,10 +5,11 @@ category = structure
55
subcategory = None
66

77
@@ signatures
8-
run_sketch(block: bool = None, *, py5_options: list[str] = None, sketch_args: list[str] = None, sketch_functions: dict[str, Callable] = None, jclassname: str = None) -> None
8+
run_sketch(block: bool = None, *, py5_options: list[str] = None, sketch_args: list[str] = None, sketch_functions: dict[str, Callable] = None, jclassname: str = None, jclass_params: tuple[Any] = ()) -> None
99

1010
@@ variables
1111
block: bool = None - method returns immediately (False) or blocks until Sketch exits (True)
12+
jclass_params: tuple[Any] = () - parameters to pass to constructor when using py5 in processing mode
1213
jclassname: str = None - canonical name of class to instantiate when using py5 in processing mode
1314
py5_options: list[str] = None - command line arguments to pass to Processing as arguments
1415
sketch_args: list[str] = None - command line arguments that become Sketch arguments
@@ -27,7 +28,7 @@ When programming in [module mode](content-py5-modes-module-mode) and [imported m
2728

2829
When running a Sketch asynchronously through Jupyter Notebook, any `print` statements using Python's builtin function will always appear in the output of the currently active cell. This will rarely be desirable, as the active cell will keep changing as the user executes code elsewhere in the notebook. As an alternative, use py5's [](sketch_println) method, which will place all text in the output of the cell that made the `run_sketch()` call. This will continue to be true if the user moves on to execute code in other Notebook cells. Use [](sketch_set_println_stream) to customize this behavior. All py5 error messages and stack traces are routed through the [](sketch_println) method. Be aware that some error messages and warnings generated inside the Processing Jars cannot be controlled in the same way, and may appear in the output of the active cell or mixed in with the Jupyter Kernel logs.
2930

30-
The `jclassname` parameter should only be used when programming in Processing Mode. This value must be the canonical name of your Processing Sketch class (i.e. `"org.test.MySketch"`). The class must inherit from `py5.core.SketchBase`. Read py5's online documentation to learn more about Processing Mode.
31+
The `jclassname` parameter should only be used when programming in Processing Mode. This value must be the canonical name of your Processing Sketch class (i.e. `"org.test.MySketch"`). The class must inherit from `py5.core.SketchBase`. To pass parameters to your Processing Sketch class constructor, use the `jclass_params` parameter. Read py5's online documentation to learn more about [Processing Mode](/content/processing_mode).
3132

3233
@@ example
3334
py5.run_sketch(block=True)

py5_resources/py5_module/py5/__init__.py

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -140,6 +140,7 @@ def run_sketch(
140140
sketch_args: list[str] = None,
141141
sketch_functions: dict[str, Callable] = None,
142142
jclassname: str = None,
143+
jclass_params: tuple[Any] = (),
143144
_osx_alt_run_method: bool = True,
144145
) -> None:
145146
"""$module_Sketch_run_sketch"""
@@ -179,7 +180,7 @@ def run_sketch(
179180
)
180181
return
181182
if _py5sketch.is_dead or jclassname:
182-
_py5sketch = Sketch(jclassname=jclassname)
183+
_py5sketch = Sketch(jclassname=jclassname, jclass_params=jclass_params)
183184

184185
_prepare_dynamic_variables(caller_locals, caller_globals)
185186

py5_resources/py5_module/py5/sketch.py

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -205,6 +205,7 @@ def __new__(cls, *args, **kwargs):
205205
def __init__(self, *args, **kwargs):
206206
_instance = kwargs.get("_instance")
207207
jclassname = kwargs.get("jclassname")
208+
jclass_params = kwargs.get("jclass_params", ())
208209

209210
if _instance:
210211
if _instance == getattr(self, "_instance", None):
@@ -216,7 +217,7 @@ def __init__(self, *args, **kwargs):
216217
)
217218

218219
Sketch._cls = JClass(jclassname) if jclassname else _Sketch
219-
instance = Sketch._cls()
220+
instance = Sketch._cls(*jclass_params)
220221
if not isinstance(instance, _SketchBase):
221222
raise RuntimeError("Java instance must inherit from py5.core.SketchBase")
222223

0 commit comments

Comments
 (0)