Imported class from another tab doesn't work

Hey! So I’ve been trying to make a voronoi animation in processing.py and For some reason, I can’t call my voronoi class from the other tab.
I’m getting the error : NameError: global name ‘v’ is not defined
I can’t figure it out. please help!!!

Voronoi.pyde

from voronoi_class import *
w, h = 640, 480
global v
def setup():
    size(w, h)
    background(255)
    noStroke()
    colorMode(HSB, 360,100,100)
    v = voronoi(100, w, h)
    println(v.r)
    
def draw():
    if v.r >= 0:
        v.update()
    v.show()
    ```

**voronoi_class.py**

class voronoi:
def init(self, number, w, h):
self.points = []
self.colors = []
self.r = 1000
self.n = number
for i in range(self.n):
self.points.append(PVector(random(1)*w, random(1)*h))
self.colors.append((random(0,360), random(60,80), random(85,90)))
def show(self):
for c, p in enumerate(self.points):
fill(self.colors[c][0], self.colors[c][1], self.colors[c][2])
ellipse(p.x,p.y, self.r,self.r)
def update(self):
self.r-=1

1 Like

I believe you’ve got instead NameError: global name ‘voronoi’ is not defined, right?

Should be instead: from voronoi_class import voronoi

Keyword global should go inside a function instead:
Py.Processing.org/reference/globals.html

def setup():

    # ...

    global v
    v = voronoi(100, w, h)
    println(v.r)

It’s customary for class names to be “CamelCase” instead: class Voronoi:

4 Likes