Replies: 4 comments 29 replies
-
You probably already know this, but the following works on my mac. To get it to run I had to add a def settings(): to setup the window and set blocking to True. For some reason icosa is not recognized by key_pressed(): as being a global which I don't understand since draw(): seems to recognize it. By defining icosa a second time inside of key_pressed() it seems to fix it: import py5
import numpy as np
def settings():
py5.size(600, 600, py5.P3D)
def setup():
global icosa
icosa = icosahedron(250)
icosa.set_fills(random_color() for _ in range(60))
def draw():
py5.background(0)
py5.translate(py5.width / 2, py5.height / 2)
py5.shape(icosa, 0, 0)
def random_color():
return py5.color(py5.random_int(255), py5.random_int(255), py5.random_int(255))
def key_pressed():
global icosa;
print(py5.key)
if py5.key == 'a':
icosa = icosahedron(250)
icosa.set_fills(random_color() for _ in range(60))
elif py5.key == 'b':
icosa.set_fill(random_color())
def icosahedron(radius):
global vs, sequence
f = (radius / 2) # / 0.95 # aprox. ?
phi = (py5.sqrt(5) + 1) / 2
a = phi * f
b = 1 * f
vs = np.array([
(a, b, 0),
(a, -b, 0),
(-a, -b, 0),
(-a, b, 0),
(b, 0, a),
(-b, 0, a),
(-b, 0, -a),
(b, 0, -a),
(0, a, b),
(0, a, -b),
(0, -a, -b),
(0, -a, b),
])
sequence = np.array([
2, 11, 5, 2, 5, 3, 5, 8, 3, 4, 8, 5, 11, 4, 5,
10, 2, 6, 2, 3, 6, 3, 9, 6, 3, 8, 9, 2, 10, 11,
1, 4, 11, 1, 0, 4, 7, 0, 1, 10, 1, 11, 0, 8, 4,
0, 9, 8, 7, 9, 0, 1, 10, 7, 10, 6, 7, 6, 9, 7,
])
shp = py5.create_shape()
shp.begin_shape(py5.TRIANGLES)
shp.vertices(vs[sequence])
shp.end_shape()
return shp
py5.run_sketch(block=True) |
Beta Was this translation helpful? Give feedback.
-
I just tried running this. I believe it might be a Processing bug where once the shape is drawn to the screen, Processing's PShapeOpenGL method |
Beta Was this translation helpful? Give feedback.
-
Beta Was this translation helpful? Give feedback.
-
Alternate way of using setFills() more than once: # Uses Imported mode for py5
def drawMyCube():
global cube
cube = create_shape()
s = 75
cube.begin_shape(QUADS)
# back (-z)
cube.vertex(-s, -s, -s)
cube.vertex(+s, -s, -s)
cube.vertex(+s, +s, -s)
cube.vertex(-s, +s, -s)
# front (+z)
cube.vertex(-s, -s, +s)
cube.vertex(+s, -s, +s)
cube.vertex(+s, +s, +s)
cube.vertex(-s, +s, +s)
# top (-y)
cube.vertex(-s, -s, +s)
cube.vertex(-s, -s, -s)
cube.vertex(+s, -s, -s)
cube.vertex(+s, -s, +s)
# bottom (+y)
cube.vertex(+s, +s, +s)
cube.vertex(+s, +s, -s)
cube.vertex(-s, +s, -s)
cube.vertex(-s, +s, +s)
# left (-x)
cube.vertex(-s, -s, +s)
cube.vertex(-s, -s, -s)
cube.vertex(-s, +s, -s)
cube.vertex(-s, +s, +s)
# right (+x)
cube.vertex(+s, -s, +s)
cube.vertex(+s, -s, -s)
cube.vertex(+s, +s, -s)
cube.vertex(+s, +s, +s)
cube.end_shape()
cube.set_fills(color(random(255),random(255),random(255)) for _ in range(24))
def setup():
size(600, 600, P3D)
window_title("Rotate with mouse click to change colors.")
stroke_weight(3)
drawMyCube()
def draw():
background(209)
translate(width/2, height/2, 0)
rotate_x(remap(mouse_y, 0, height, PI, -PI))
rotate_y(remap(mouse_x, 0, width, -PI, PI))
shape(cube)
def mouse_pressed():
drawMyCube() |
Beta Was this translation helpful? Give feedback.
-
Season's greetings!
Am I doing something wrong? It looks like something happens after a call to
.set_fills()
. Maybe Py5Shape objects get tired after a long year? 😄Maybe something fun to investigate in 2025!
Beta Was this translation helpful? Give feedback.
All reactions