|
| 1 | +# Processing Mode |
| 2 | + |
| 3 | +Processing Mode refers to py5’s ability to serve as a bridge from Java to Python, allowing Processing Sketches to call Python code using a new `callPython()` method. This is a solid feature that in time will add a significant amount of value to the Processing community. |
| 4 | + |
| 5 | +[Processing Mode](http://py5coding.org/content/processing_mode.html) is documented on py5's website. The first two examples are code samples from the online documentation. |
| 6 | + |
| 7 | +Here is a quick example of a Processing Sketch that uses py5 in Processing Mode: |
| 8 | + |
| 9 | +```java |
| 10 | +package test; |
| 11 | + |
| 12 | +import processing.core.PImage; |
| 13 | +import py5.core.SketchBase; |
| 14 | + |
| 15 | +public class Example1Sketch extends SketchBase { |
| 16 | + |
| 17 | + public void settings() { |
| 18 | + size(400, 400, P2D); |
| 19 | + } |
| 20 | + |
| 21 | + public void setup() { |
| 22 | + String msg = "Hello from Java!"; |
| 23 | + PImage img = createImage(200, 200, RGB); |
| 24 | + |
| 25 | + // call Python function `alter_image(msg, img)` and get back a PImage |
| 26 | + PImage imgResponse = (PImage) callPython("test_transfer", msg, img); |
| 27 | + image(imgResponse, 100, 100); |
| 28 | + |
| 29 | + // call numpy `random.randint()` function |
| 30 | + long randomNumber = (long) callPython("np.random.randint", 0, 100); |
| 31 | + py5Println("JAVA: Random number from numpy: " + randomNumber); |
| 32 | + } |
| 33 | + |
| 34 | + public void draw() { |
| 35 | + rect(mouseX, mouseY, 20, 20); |
| 36 | + } |
| 37 | + |
| 38 | +} |
| 39 | +``` |
| 40 | + |
| 41 | +The `callPython()` method in this Sketch is added to Processing with py5's `py5.core.SketchBase` class. It can pass parameters and return objects back to Java. |
| 42 | + |
| 43 | +The Python code used to execute the above example Sketch is the following: |
| 44 | + |
| 45 | +```python |
| 46 | +import numpy as np |
| 47 | + |
| 48 | +import py5_tools |
| 49 | +import py5 |
| 50 | + |
| 51 | + |
| 52 | +def alter_image(msg: str, img: py5.Py5Image): |
| 53 | + py5.println("PYTHON:", msg) |
| 54 | + py5.println("PYTHON:", img) |
| 55 | + |
| 56 | + img.load_np_pixels() |
| 57 | + img.np_pixels[::2, ::2] = [255, 255, 0, 0] |
| 58 | + img.update_np_pixels() |
| 59 | + |
| 60 | + return img |
| 61 | + |
| 62 | + |
| 63 | +# register processing mode keys so the Java `callPython()` method can find them |
| 64 | +py5_tools.register_processing_mode_key('test_transfer', alter_image) |
| 65 | +py5_tools.register_processing_mode_key('np', np) |
| 66 | + |
| 67 | +# run the sketch in processing mode, specifying the Java class to instantiate |
| 68 | +py5.run_sketch(jclassname='test.Example1Sketch') |
| 69 | +``` |
| 70 | + |
| 71 | +The main idea is for py5 to provide Processing users with efficient access to the Python Ecosystem. Calls to `callPython()` in Java are linked to Python through registered "keys" that map to Python functions and modules. Refer to the online [Processing Mode](http://py5coding.org/content/processing_mode.html) documentation to learn more. |
0 commit comments