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())
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:
# 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())
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:
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.
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?
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