Hello,
I’m a beginner and a new py5 user and I’m very enthusiastic about this new version of Processing.
I read in the tutorial that you could use “Processing java” libraries with the camera3D example. Is it possible to import other libraries? I’m trying to import “Peasycam” but without success.
I’m trying
import jpype.imports
import py5_tools
py5_tools.add_jars(‘/jars’)
from peasycam import Peasycam
As far as I know, users can’t run py5 code in the Processing 4 editor. Usually you would need to use an editor called ‘Thonny’ with a py5-plugin. An alternative would be to use an older version of Processing (3.5.4); it does support Python, but not py5, which is new and hopefully will be added to the Processing editor in the future.
Yes, I use py5 with ‘Thonny’. In the py5 references they explain a way to import ‘java’ libraries, and it works with the ‘camera3D’ library. But I try with other libraries but without success…
Yes, I hope that in the future py5 will be added to the Processing editor.
That said, the code snippet you shared seems to have one minor bug: the path to your jars folder is absolute /jars instead of relative ./jars. Try py5_tools.add_jars('jars') or py5_tools.add_jars('./jars').
Thank you everyone for your responses here! I’m really excited to see the community coming together to answer this.
One thing I’ll add is that you don’t need import jpype.imports is because import py5 takes care of that for you. Early versions of py5 did not, but this has been in py5 for a while now.
Putting the jars in a “jars” subdirectory is the easiest way to add Jars to a Sketch. Jars must be added to the Java Virtual Machine (JVM) before the JVM is started, which happens when import py5 is called. For folks using py5 in imported mode via Thonny, you can’t use py5_tools to add the Jars because the JVM will always be already running by that time. Instead, put the Jars in a “jars” subdirectory relative to the current working directory (os.getcwd()). py5 will always check for this before starting the JVM.
A pity, since just “py” is much quicker to type in.
BtW, I’ve found this undocumented _instance property in py5 which allows us to access Processing’s full API beyond those provided w/ “snake_case” names.
As long as we don’t pass numbers larger than MAX_INT we can invoke all PApplet’s functions w/ their “originalCamelCaseNames”:
Works like a charm. I copied those two jar files and pasted them in a ‘jars’ folder in another folder that I created to hold all of my Python files. There is no sketch folder created by py5. It worked just the same. I ran it in both Thonny and from the cmd-line.
Is that a folder junction or hardlink? Perhaps a Thonny special folder?
Well, py5 is just a Python package we can install via pip.
It’s not supposed to create any folders.
BtW, I have a folder called py5 where I create subfolders for py5 related projects/sketches.
I use the same technique except I call it ‘Python’ and the ‘jars’ folder is in there. There is no subfolder system, and there’s all types of .py files in the ‘Python’ folder including py5. I’ve thought of splitting those out like you did, but haven’t done it yet.