ControlP5 in py5

The following source code demonstrates using cp5 controls in py5. This demo may be run in a Thonny editor using ‘Imported mode for py5’. In addition, the controlP5.jar must be in a folder entitled ‘jars’ in the py5 sketch folder. The controlP5.jar file may be copy/pasted from your Processing libraries folder; on MacOS it is located at the following path: Documents/Processing/libraries/controlP5/library/controlP5.jar.

# Uses Imported mode for py5
from controlP5 import ControlP5

this = get_current_sketch()
print(this)

def settings():
  size(450,400)

def setup():
  global slider
  global cw
  
  window_title("DotView Demo") 
  cp5 = ControlP5(this)
  slider = cp5.addSlider("slider", 0, 255, 50, 30, 10, 200, 20)
  cw = cp5.addColorWheel("c", 300,10,100)
  cw.setRGB(color(128,0,255))
 
def draw():
  global slider
  global cw

  background(209)
  fill(cw.getRGB())
  circle(200,220,slider.getValue())

Output:

Requisite jar file for ‘jars’ folder:


3 Likes

This is so cool, @svan! I love this, thank you for sharing it.

With the py5_tools.processing.download_library() feature it becomes even easier to use it, you don’t have to manually copy the jar file!

Check out my attempt at simplifying a bit the sketch:

peek_1

# Uses Imported mode for py5, learn more at <py5coding.org>

# To install the ControlPy5 .jar, you only need the following two lines once
from py5_tools.processing import download_library
print(download_library('ControlP5'))  # printed to see details of how it went

from controlP5 import ControlP5  # on module mode, this must come after import py5

def setup():
    global slider, cw
    size(450, 450)

    window_title("DotView Demo") 
    this = get_current_sketch()
    cp5 = ControlP5(this)
    slider = cp5.addSlider("slider", 0, 255, 50, 30, 10, 200, 20)
    cw = cp5.addColorWheel("c", 300,10,100)
    cw.setRGB(color(128,0,255))
 
def draw():
    background(209)
    fill(cw.getRGB())
    circle(200, 220, slider.getValue())
1 Like

Cool beans!

I had to make these changes for the @villares version to work with Windows 10:

# Uses Imported mode for py5

# Extras
print(py5_tools.processing.library_storage_dir())
print(py5_tools.processing.installed_libraries())

from py5_tools.processing import download_library
println(download_library('ControlP5'))

# This did not work in Windows 10
# from controlP5 import ControlP5

# This worked in Windows 10
from py5 import JClass
ControlP5 = JClass("controlP5.ControlP5")

That last part was suggested by Chat GPT.
I did not find a reference to that in the py5 site.

I have not visited this in a while so it may be related to my setup.

This @svan version in the original post worked as is with this addition:

:)

1 Like

Cheers @glv !

It is great that you could make it work… but I don’t think you should need to import JClass, py5 should take care of it for you. This might be an issue that we might want to show to @hx2A

Can you give us more information about your setup?
Windows 10 has been giving us trouble with the Thonny plug-in sometimes.

Hello @villares,

I believe I simply followed the instructions on the site a while back.

Working with W10:

This did not work:

I then updated everything (not from scratch) and no change.

Reports version numbers:

import sys
import py5
from importlib.metadata import version, PackageNotFoundError

python_version = sys.version.split()[0]
py5_version = getattr(py5, "__version__", "unknown")

try:
    thonny_py5mode_version = version("thonny-py5mode")
except PackageNotFoundError:
    thonny_py5mode_version = "not installed"

print("Python:", python_version)
print("py5:", py5_version)
print("thonny-py5mode:", thonny_py5mode_version)

:)

2 Likes

On a Windows 11 system I am also unable to run your revision. My findings are similar to @glv. It can be made to work by one of two methods:

  1. import JClass

  2. Alternatively copy/paste the controlP5.jar as described in the original post.

2 Likes

I’ll see if I can try this tomorrow at my Community Center job. Then I can help filing an issue. Can you please, @glv and @svan, try the same thing on module mode?

That would be using import py5 then the py5. prefix everywhere and py5.run_sketch() in the end (turning off the “imported mode for py5” option on Thonny).

If that works it would be a hit that something on imported mode is going wrong.

It is very interesting to me that the first bit of code does not work on Windows 10 and the second piece does. The first part is leveraging a jpype feature that adds import hooks to let you import java classes. Perhaps jpype can’t do that on Windows 10 for some reason?

Recall we also have a problem with the Thonny plugin not working correctly on Windows 10. Parts of py5 are written with the expectation that the import hooks work. Perhaps these two things are related?

2 Likes

Results in comments

# Works:
from controlP5 import ControlP5
print("ControlP5: import")

# Does NOT work:
# ControlP5 = JClass("controlP5.ControlP5")
# print("ControlP5: JClass")

slider = None
cw = None

def settings():
    py5.size(450, 450)

def setup():
    global slider, cw

    py5.window_title("DotView Demo")
    this = py5.get_current_sketch()

    cp5 = ControlP5(this)
    slider = cp5.addSlider("slider", 0, 255, 50, 30, 10, 200, 20)
    cw = cp5.addColorWheel("c", 300, 10, 100)
    cw.setRGB(py5.color(128, 0, 255))

def draw():
    py5.background(209)
    py5.fill(cw.getRGB())
    py5.circle(200, 220, slider.getValue())

py5.run_sketch()

:)

1 Like

Thank you for helping with this! So does the sketch work on module mode?
(I see no error messages)

1 Like

I unchecked “Imported mode for py5” and ran the code:

Is that module mode?

I am not that familiar with all the modes.

The answer is here I am sure:

Still trying to find time to get back into learning Python.

:)

1 Like

Yes, that should be module mode! Does the sketch window open?
With the sliders, etc. ?

I usually use imported mode and sometimes static mode in class, and module mode by myself and with advanced students… The Five py5 Modes — py5 documentation

1 Like

It opens confidently with unbridled enthusiasm!

:)

1 Like

I can confirm that it does work on a Windows 11 system in module mode, but not in imported mode.

1 Like