diff --git a/generator/reference.py b/generator/reference.py index 25897050..e9c9473f 100644 --- a/generator/reference.py +++ b/generator/reference.py @@ -111,6 +111,7 @@ "sketch_args: list[str] = None", "sketch_functions: dict[str, Callable] = None", "jclassname: str = None", + "jclass_params: tuple[Any] = ()", ], "None", ) diff --git a/py5_docs/Reference/api_en/Sketch_run_sketch.txt b/py5_docs/Reference/api_en/Sketch_run_sketch.txt index 8bb04aea..3e3ae5fc 100644 --- a/py5_docs/Reference/api_en/Sketch_run_sketch.txt +++ b/py5_docs/Reference/api_en/Sketch_run_sketch.txt @@ -5,10 +5,11 @@ category = structure subcategory = None @@ signatures -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 +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 @@ variables block: bool = None - method returns immediately (False) or blocks until Sketch exits (True) +jclass_params: tuple[Any] = () - parameters to pass to constructor when using py5 in processing mode jclassname: str = None - canonical name of class to instantiate when using py5 in processing mode py5_options: list[str] = None - command line arguments to pass to Processing as arguments 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 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. -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. +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). @@ example py5.run_sketch(block=True) diff --git a/py5_resources/py5_module/py5/__init__.py b/py5_resources/py5_module/py5/__init__.py index b10271f5..72940b4f 100644 --- a/py5_resources/py5_module/py5/__init__.py +++ b/py5_resources/py5_module/py5/__init__.py @@ -140,6 +140,7 @@ def run_sketch( sketch_args: list[str] = None, sketch_functions: dict[str, Callable] = None, jclassname: str = None, + jclass_params: tuple[Any] = (), _osx_alt_run_method: bool = True, ) -> None: """$module_Sketch_run_sketch""" @@ -179,7 +180,7 @@ def run_sketch( ) return if _py5sketch.is_dead or jclassname: - _py5sketch = Sketch(jclassname=jclassname) + _py5sketch = Sketch(jclassname=jclassname, jclass_params=jclass_params) _prepare_dynamic_variables(caller_locals, caller_globals) diff --git a/py5_resources/py5_module/py5/sketch.py b/py5_resources/py5_module/py5/sketch.py index 127153f2..69de1715 100644 --- a/py5_resources/py5_module/py5/sketch.py +++ b/py5_resources/py5_module/py5/sketch.py @@ -205,6 +205,7 @@ def __new__(cls, *args, **kwargs): def __init__(self, *args, **kwargs): _instance = kwargs.get("_instance") jclassname = kwargs.get("jclassname") + jclass_params = kwargs.get("jclass_params", ()) if _instance: if _instance == getattr(self, "_instance", None): @@ -216,7 +217,7 @@ def __init__(self, *args, **kwargs): ) Sketch._cls = JClass(jclassname) if jclassname else _Sketch - instance = Sketch._cls() + instance = Sketch._cls(*jclass_params) if not isinstance(instance, _SketchBase): raise RuntimeError("Java instance must inherit from py5.core.SketchBase")