ControlP5 and Python mode

Hello everyone! I'm trying to come up with a GUI class in Processing, Python mode, but the code does not work. The error says:

"NameError: global name 'cp5' is not defined"

But I've defined it in the main Sketch... Any suggestions? Here is the code

add_library('controlP5')

from gui import Gui
gui1=None



def setup():
    size(600, 600)
    cp5 = ControlP5(this)

    gui1=Gui()

############################ GUI CLASS ##########################

class Gui(object):

    def __init__(self):

    ################## CONTROLS #################

        cp5.addSlider("North St.") \
           .setPosition(width/2-10, 20) \
           .setSize(20,200) \
           .setRange(100,0) \
           .setValue(50) \
           .getCaptionLabel() \
           .align(ControlP5.CENTER, ControlP5.TOP_OUTSIDE) \
           .setPaddingX(0)

        cp5.addSlider("East St.") \
           .setPosition(width-210, height/2-10) \
           .setSize(200,20) \
           .setRange(100,0) \
           .setValue(50) \
           .getCaptionLabel() \
           .align(ControlP5.RIGHT, ControlP5.BOTTOM_OUTSIDE) \
           .setPaddingX(0) \  
        cp5.getController("East St.") \
           .getValueLabel() \
           .align(ControlP5.LEFT, ControlP5.BOTTOM_OUTSIDE) \
           .setPaddingX(0)

        cp5.addSlider("South St.") \
           .setPosition(width/2-10, height-220) \
           .setSize(20,200) \
           .setRange(0,100) \
           .setValue(50) \
           .getCaptionLabel() \
           .align(ControlP5.CENTER, ControlP5.BOTTOM_OUTSIDE) \
           .setPaddingX(0) 

        cp5.addSlider("West St.") \
           .setPosition(10, height/2-10) \
           .setSize(200,20) \
           .setRange(0,100) \
           .setValue(50) \
           .getCaptionLabel() \
           .align(ControlP5.LEFT, ControlP5.BOTTOM_OUTSIDE) \
           .setPaddingX(0) \  
        cp5.getController("West St.") \
           .getValueLabel() \
           .align(ControlP5.RIGHT, ControlP5.BOTTOM_OUTSIDE) \
           .setPaddingX(0)

        cp5.addKnob("Rate") \
           .setRange(0, 100) \
           .setValue(50) \
           .setPosition(width/7, height-150) \
           .setRadius(50) \
           .setNumberOfTickMarks(10) \
           .setTickMarkLength(5) \
           .setDragDirection(Knob.VERTICAL)

        cp5.addCheckBox("Cross") \
           .setPosition(width-150,height-150) \
           .setColorForeground(color(120)) \
           .setColorActive(color(255)) \
           .setColorLabel(color(255)) \
           .setSize(100, 100) \
           .addItem("CROSS", 0) \
           .getController("CROSS") \
           .getCaptionLabel() \
           .align(ControlP5.CENTER, ControlP5.BOTTOM_OUTSIDE) \
           .setPaddingX(0)

    ############################ BACKGROUND ################################

        noFill()
        stroke(255)
        beginShape()
        vertex(10, height/2-50)
        vertex(width/2-50, height/2-50)
        vertex(width/2-50, 10)
        endShape()

        beginShape()
        vertex(width/2+50, 10)
        vertex(width/2+50, height/2-50)
        vertex(width-10, height/2-50)
        endShape()

        beginShape()
        vertex(width-10, height/2+50)
        vertex(width/2+50, height/2+50)
        vertex(width/2+50, height-10)
        endShape()

        beginShape()
        vertex(width/2-50, height-10)
        vertex(width/2-50, height/2+50)
        vertex(10, height/2+50)
        endShape()

    ###################### ZEBRAS ######################
        fill(255)
        rectMode(CENTER)

        begin = width/2-40
        for i_begin in range(10):
            transition = i_begin * 9
            rect(begin + transition, height/2-65, 5, 20)

        begin = width/2-40
        for i_begin in range(10):
            transition = i_begin * 9
            rect(width/2 + 65 , begin + transition, 20, 5)

        begin = width/2-40
        for i_begin in range(10):
            transition = i_begin * 9
            rect(begin + transition, height/2+65, 5, 20)

        begin = width/2-40
        for i_begin in range(10):
            transition = i_begin * 9
            rect(width/2 - 65, begin + transition, 20, 5)

Thank you!

Answers

  • Dunno Python! But I believe a variable defined inside a def is local to that function only, isn't it? :-?

  • Hi! thanks for your answer, I've tried also like this

    add_library('controlP5')
    cp5 = ControlP5(this)
    from gui import Gui
    gui1=None
    
    
    def setup():
        size(600, 600)
    
        gui1=Gui()
    

    but I get a Null Pointer Exception….

  • No guarantees, since I dunno Python, but try out:

    cp5 = None
    
    def setup():
    
        size(600, 600, JAVA2D)
        smooth(4)
    
        cp5 = ControlP5(this)
    
  • Hi, did you see this python and controlP5 discussion and sample code on processing.py's github page?

  • Hello Andeas!

    I've read the discussion on github but I cannot figure out how to use that solution in my case. If I implement the ControlP5 directly in the main program (without using a class) it work just fine:

    add_library('controlP5')
    
    
    cp5 = None
    
    def setup():
    
        size(600, 600)
        smooth(4)
    
        cp5 = ControlP5(this)
        ######################## CONTROLS #########################
    
        cp5.addSlider("North St.") \
               .setPosition(width/2-10, 20) \
               .setSize(20,200) \
               .setRange(100,0) \
               .setValue(50) \
               .getCaptionLabel() \
               .align(ControlP5.CENTER, ControlP5.TOP_OUTSIDE) \
               .setPaddingX(0)
    
        cp5.addSlider("East St.") \
               .setPosition(width-210, height/2-10) \
               .setSize(200,20) \
               .setRange(100,0) \
               .setValue(50) \
               .getCaptionLabel() \
               .align(ControlP5.RIGHT, ControlP5.BOTTOM_OUTSIDE) \
               .setPaddingX(0) \  
        cp5.getController("East St.") \
               .getValueLabel() \
               .align(ControlP5.LEFT, ControlP5.BOTTOM_OUTSIDE) \
               .setPaddingX(0)
    
        cp5.addSlider("South St.") \
               .setPosition(width/2-10, height-220) \
               .setSize(20,200) \
               .setRange(0,100) \
               .setValue(50) \
               .getCaptionLabel() \
               .align(ControlP5.CENTER, ControlP5.BOTTOM_OUTSIDE) \
               .setPaddingX(0) 
    
        cp5.addSlider("West St.") \
               .setPosition(10, height/2-10) \
               .setSize(200,20) \
               .setRange(0,100) \
               .setValue(50) \
               .getCaptionLabel() \
               .align(ControlP5.LEFT, ControlP5.BOTTOM_OUTSIDE) \
               .setPaddingX(0) \  
        cp5.getController("West St.") \
               .getValueLabel() \
               .align(ControlP5.RIGHT, ControlP5.BOTTOM_OUTSIDE) \
               .setPaddingX(0)
    
        cp5.addKnob("Rate") \
               .setRange(0, 100) \
               .setValue(50) \
               .setPosition(width/7, height-150) \
               .setRadius(50) \
               .setNumberOfTickMarks(10) \
               .setTickMarkLength(5) \
               .setDragDirection(Knob.VERTICAL)
    
        cp5.addCheckBox("Cross") \
               .setPosition(width-150,height-150) \
               .setColorForeground(color(120)) \
               .setColorActive(color(255)) \
               .setColorLabel(color(255)) \
               .setSize(100, 100) \
               .addItem("CROSS", 0) \
               .getController("CROSS") \
               .getCaptionLabel() \
               .align(ControlP5.CENTER, ControlP5.BOTTOM_OUTSIDE) \
               .setPaddingX(0)
    
        ###################### BACKGROUND #######################
    
        noFill()
        stroke(255)
        beginShape()
        vertex(10, height/2-50)
        vertex(width/2-50, height/2-50)
        vertex(width/2-50, 10)
        endShape()
    
        beginShape()
        vertex(width/2+50, 10)
        vertex(width/2+50, height/2-50)
        vertex(width-10, height/2-50)
        endShape()
    
        beginShape()
        vertex(width-10, height/2+50)
        vertex(width/2+50, height/2+50)
        vertex(width/2+50, height-10)
        endShape()
    
        beginShape()
        vertex(width/2-50, height-10)
        vertex(width/2-50, height/2+50)
        vertex(10, height/2+50)
        endShape()
    
        ###################### ZEBRAS ######################
        fill(255)
        rectMode(CENTER)
    
        begin = width/2-40
        for i_begin in range(10):
            transition = i_begin * 9
            rect(begin + transition, height/2-65, 5, 20)
    
        begin = width/2-40
        for i_begin in range(10):
            transition = i_begin * 9
            rect(width/2 + 65 , begin + transition, 20, 5)
    
        begin = width/2-40
        for i_begin in range(10):
            transition = i_begin * 9
            rect(begin + transition, height/2+65, 5, 20)
    
        begin = width/2-40
        for i_begin in range(10):
            transition = i_begin * 9
            rect(width/2 - 65, begin + transition, 20, 5)
    

    The problem shows up just when I try to wrap it in a class to have the code in a different tab... Any idea?

  • edited August 2014

    In Java, JavaScript, CoffeeScript & Android modes, all ".pde" tabs are merged as 1 "top-class" file.
    Our own classes would then become nested classes from this unified "top-class".

    Variables defined at top level, like the cp5 for example, would be readily available to those nested classes too.
    Although that'd be frowned upon, since we coulda formally passed any top level variables to a nested class!

    Dunno whether Python Mode got nested classes too. Or whether it would work the same as the other modes do.
    Rather than wondering whether that's so or not, it's much safer & more formal to pass any needed top-level variables to a class anyways!

    Warning: Non-Python programmer ahead: :-&

    add_library('controlP5')
    
    from gui import Gui
    gui1 = None
    
    
    def setup():
    
        size(600, 600, JAVA2D)
        smooth(4)
    
        gui1 = Gui(ControlP5(this))
    
    
    class Gui(object):
    
        cp5 = None
    
    
        def __init__(self, cp5):
    
            # ...
    
  • Right, agree with you!

    I've also tried to write it as following, but does not work...

    add_library('controlP5')
    
    from gui import Gui
    gui1 = None
    cp5 = ControlP5
    
    def setup():
    
        size(600, 600, JAVA2D)
        smooth(4)
    
        gui1 = Gui(ControlP5(this))
    
    ########################## GUI CLASS ###########################
    
    class Gui(object):
        cp5 = None
    
        def __init__(self, cp5):
    
    ########## CONTROLS##############
            cp5 = new ControlP5(this)
    
            cp5.addSlider("North St.") \
               .setPosition(width/2-10, 20) \
               .setSize(20,200) \
               .setRange(100,0) \
               .setValue(50) \
               .getCaptionLabel() \
               .align(ControlP5.CENTER, ControlP5.TOP_OUTSIDE) \
               .setPaddingX(0)
    

    It's strange, I guess it's just a matter of writing it in a separate tab, because if written all in the "main program" it work fine...

  • edited July 2014

    @ line #22, you're instantiating another ControlP5 object, rather than using the passed reference!!! :-O

  • edited August 2014

    Made some research for Python's class and static & instance variables.
    Come up w/ this 1 now. Again, no idea whether it works or not: ~:>

    # forum.processing.org/two/discussion/6346/controlp5-and-python-mode
    
    add_library('controlP5')
    from gui import Gui
    
    myGui = None
    
    
    def setup():
    
        size(600, 600, JAVA2D)
        smooth(4)
    
        myGui = Gui(ControlP5(this))
    
    
    class Gui(object):
    
        def __init__(_, cp5):
    
            Gui.cp5 = cp5
            Gui.createControls()
    
    
        @ staticmethod
        def createControls():
    
            margin = 10
    
            w2 = width>>1
            w3 = width/3
            w4 = width>>2
    
            h2 = height>>1
            h3 = height/3
            h4 = height>>2
    
            cp5 = Gui.cp5
    
            cp5.addSlider("North St.") \
               .setPosition(w2 - margin, margin*2) \
               .setSize(20, 200) \
               .setRange(100, 0) \
               .setValue(50) \
               .getCaptionLabel() \
               .align(ControlP5.CENTER, ControlP5.TOP_OUTSIDE) \
               .setPaddingX(0)
    
             cp5.addSlider("East St.") \
                .setPosition(width - w3 - margin, h2 - margin) \
                .setSize(200, 20) \
                .setRange(100, 0) \
                .setValue(50) \
                .getCaptionLabel() \
                .align(ControlP5.RIGHT, ControlP5.BOTTOM_OUTSIDE) \
                .setPaddingX(0)
    
             cp5.getController("East St.") \
                .getValueLabel() \
                .align(ControlP5.LEFT, ControlP5.BOTTOM_OUTSIDE) \
                .setPaddingX(0)
    
             cp5.addSlider("South St.") \
                .setPosition(w2 - margin, height - h3 - margin*2) \
                .setSize(20, 200) \
                .setRange(0, 100) \
                .setValue(50) \
                .getCaptionLabel() \
                .align(ControlP5.CENTER, ControlP5.BOTTOM_OUTSIDE) \
                .setPaddingX(0)
    
             cp5.addSlider("West St.") \
                .setPosition(margin, h2 - margin) \
                .setSize(200, 20) \
                .setRange(0, 100) \
                .setValue(50) \
                .getCaptionLabel() \
                .align(ControlP5.LEFT, ControlP5.BOTTOM_OUTSIDE) \
                .setPaddingX(0)
    
             cp5.getController("West St.") \
                .getValueLabel() \
                .align(ControlP5.RIGHT, ControlP5.BOTTOM_OUTSIDE) \
                .setPaddingX(0)
    
             cp5.addKnob("Rate") \
                .setPosition(width/7, height - h4) \
                .setRange(0, 100) \
                .setValue(50) \
                .setRadius(50) \
                .setNumberOfTickMarks(10) \
                .setTickMarkLength(5) \
                .setDragDirection(Knob.VERTICAL)
    
             cp5.addCheckBox("Cross") \
                .setPosition(width - w4, height - h4) \
                .setSize(100, 100) \
                .setColorForeground(color(120)) \
                .setColorActive(-1) \
                .setColorLabel(-1) \
                .addItem("CROSS", 0) \
                .getController("CROSS") \
                .getCaptionLabel() \
                .align(ControlP5.CENTER, ControlP5.BOTTOM_OUTSIDE) \
                .setPaddingX(0)
    
  • Hey, thank you for your efforts!

    If I run the code with the line 4 commented I get this error here:

    Schermata 2014-07-23 alle 11.07.51

    but if I uncomment the line 4, the error now shows in the Gui Class at line 26 "def createControls():"

    Schermata 2014-07-23 alle 11.09.40

  • edited July 2014

    Hmm... Why @ staticmethod isn't indented to match def createControls(): as I posted? :O
    If it fails too, remove the space between @ and staticmethod.
    I've gotta insert a space due to forum's bug! 8-|

  • Still doesn't work my friend... thanks a lot for yours efforts!

  • I made it works. https://github.com/PymientoProject/josemaria/tree/master/SuperFormulaPy You can see it.

    Now... the function ... controlEvent(theEvent): doesn't work

Sign In or Register to comment.