Importing a "Java Processing" library into py5

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

Thanks for your help

2 Likes

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. :+1:

@tabreturn can you shed any light on this?

@hx2A what do you think?

@Gof you can import other Processing libraries without needing py5_tools if your sketch folder is organized like so:

sketch001/
├─ sketch.py
├─ jars/
│  ├─ library01.jar
│  ├─ library02.jar

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').

2 Likes

Thanks @mcintyre, you’re right I don’t need to use py5_tools !
It works like this

import py5
import jpype.imports


from peasy import PeasyCam


def setup():
    global cam
    py5.size(400,400,py5.P3D)
    sketch = py5.get_current_sketch()
    cam = PeasyCam(sketch,500)
      
    
def draw():
    py5.background(200,0,50)
    py5.rotate_y(py5.frame_count*0.01)
    py5.box(100)
    

py5.run_sketch()
2 Likes

I’ve also tested in Win10 + Python 3.12.2 + Java 21.0.2 using just the terminal (no Thonny).
This import line isn’t needed: import jpype.imports:

import py5
from peasy import PeasyCam

BG = 0xff_C8_00_32 # medium-dark pink-red
FG = 0xff_FF_FF_00 # yellow

ROT_STEP = .05 # rotation speed

def setup():
    py5.size(400, 400, py5.P3D)

    py5.fill(FG)
    py5.stroke_weight(2)

    PeasyCam(py5.get_current_sketch(), 500)


def draw():
    py5.background(BG)
    py5.rotate_y(py5.frame_count * ROT_STEP)
    py5.box(50)


__name__ == '__main__' and py5.run_sketch()
2 Likes

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.

2 Likes

Could you explain the procedure that you use to run this demo using Terminal? I’d like to try it on a Mac.

1 Like

Turns out it’s not too difficult to run py5 files from terminal: just type ‘python3’ followed by the name of the file.

Example:

1 Like
  • We can also do it shorter w/ just "py". :wink:
  • And if we name our sketch "__main__.py", we can run it w/ just 1 dot: py . :mage:
  • Also, if we are 1 folder above our sketch project, we can run it passing the name of that folder if it has a "__main__.py" inside! :snake:
1 Like

My mac doesn’t much care for ‘py’; it responds with ‘command not found’.

A pity, since just “py” is much quicker to type in. :racing_car:

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. :grin:

As long as we don’t pass numbers larger than MAX_INT we can invoke all PApplet’s functions w/ their “originalCamelCaseNames”: :innocent:

import py5
from peasy import PeasyCam

ROT_STEP = .05 # rotation speed
FILENAME = 'cube_####.png'

this = py5._instance

def settings(): this.size(400, 400, this.P3D)

def setup():
    this.fill(255, 255, 0) # yellow
    this.strokeWeight(2)

    PeasyCam(this, 500)


def draw():
    this.background(200, 0, 50) # medium-dark pink-red
    this.rotateY(this.frameCount * ROT_STEP)
    this.box(50)


def key_pressed():
    this.key == ' ' and this.saveFrame(this.dataPath(FILENAME))


__name__ == '__main__' and py5.run_sketch()

P.S.: Method dataPath() doesn’t exist in py5’s API, not even as data_path()!

2 Likes

When trying to run the demo that you posted either in Thonny of from the command line I get this error:

ModuleNotFoundError: No module named 'peasy' How did you get your system to recognize ‘peasy’?

Well, I didn’t know how. I’ve just followed @mcintyre’s instruction from his 1st reply in this thread:

In my case, I only had those 2 “.jar” files from my Processing 3’s “libraries” subfolder:

  1. “peasycam.jar”
  2. “peasy-math.jar”

They’re located at “sketch’s folder”\libraries\peasycam\library\

So apparently, although py5 is using Processing 4, some Processing 3’s libraries are still compatible!

Anyways, here’s my actual “PeasyCam/” py5 project folder:

PeasyCam/
├── __main__.py
└── jars/
    ├── peasycam.jar
    └── peasy-math.jar
2 Likes

Found out how to convert Python’s int values greater than MAX_INT to Java’s 32-bit signed int using “jpype”'s JInt() function:

import py5

from jpype import JInt
from peasy import PeasyCam

BG = JInt(0xff_C8_00_32) # medium-dark pink-red
FG = JInt(0xff_FF_FF_00) # yellow

ROT_STEP = .05 # rotation speed
FILENAME = 'cube_####.png'

this = py5._instance

def settings(): this.size(400, 400, this.P3D)

def setup():
    this.fill(FG)
    this.strokeWeight(2)

    PeasyCam(this, 500)


def draw():
    this.background(BG)
    this.rotateY(this.frameCount * ROT_STEP)
    this.box(50)


def key_pressed():
    this.key == ' ' and this.saveFrame(this.dataPath(FILENAME))


__name__ == '__main__' and py5.run_sketch()

Now we can pass big values like 0xff_C8_00_32 or 0xff_FF_FF_00 to Java methods in Processing, such as fill() and background() via py5._instance. :star_struck:

2 Likes

I’m missing those two jars which I now know where to find them. Very helpful. Thanks, I’m going to try it.

1 Like

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.

1 Like

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.

1 Like

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.