kopia lustrzana https://github.com/villares/sketch-a-day
239 first
rodzic
dcd755df01
commit
6be534c81b
|
@ -0,0 +1,49 @@
|
|||
"""
|
||||
Alexandre B A Villares http://abav.lugaralgum.com - GPL v3
|
||||
|
||||
Version 180811
|
||||
A helper for the Processing gifAnimation library (https://github.com/jordanorelli)
|
||||
ported to Processing 3 by 01010101 (https://github.com/01010101)
|
||||
Download the library from https://github.com/01010101/GifAnimation/archive/master.zip
|
||||
This helper was inspired by an example by Art Simon https://github.com/APCSPrinciples/AnimatedGIF/
|
||||
|
||||
Put at the start of your sketch:
|
||||
add_library('gifAnimation')
|
||||
from gif_exporter import gif_export
|
||||
and at the end of draw():
|
||||
gif_export(GifMaker)
|
||||
"""
|
||||
|
||||
def gif_export(GifMaker, # gets a reference to the library
|
||||
filename="exported", # .gif will be added
|
||||
repeat=0, # 0 makes it an "endless" animation
|
||||
quality=128, # quality range 0 - 255
|
||||
delay=400, # this is quick
|
||||
frames=0,
|
||||
finish=False): # 0 will stop on keyPressed or frameCount >= 100000
|
||||
global gifExporter
|
||||
try:
|
||||
gifExporter
|
||||
except NameError:
|
||||
gifExporter = GifMaker(this, filename + ".gif")
|
||||
gifExporter.setRepeat(repeat)
|
||||
gifExporter.setQuality(quality)
|
||||
gifExporter.setDelay(delay)
|
||||
gif_export._frame = frameCount
|
||||
print("gif start")
|
||||
|
||||
gifExporter.addFrame()
|
||||
if finish:
|
||||
gifExporter.finish()
|
||||
print("gif saved")
|
||||
noLoop()
|
||||
return False
|
||||
if frames != 0 and frameCount - gif_export._frame >= frames:
|
||||
gifExporter.finish()
|
||||
#background(255)
|
||||
print("gif saved")
|
||||
del(gifExporter)
|
||||
noLoop()
|
||||
return False
|
||||
else:
|
||||
return True
|
|
@ -0,0 +1,48 @@
|
|||
def my_box(side):
|
||||
front = ((-1, +1, -1),
|
||||
(-1, +1, +1),
|
||||
(+1, +1, +1),
|
||||
(+1, +1, -1),
|
||||
)
|
||||
back = ((-1, -1, -1),
|
||||
(-1, -1, +1),
|
||||
(+1, -1, +1),
|
||||
(+1, -1, -1),
|
||||
)
|
||||
left = ((-1, -1, -1),
|
||||
(-1, -1, +1),
|
||||
(-1, +1, +1),
|
||||
(-1, +1, -1),
|
||||
)
|
||||
right = ((+1, -1, -1),
|
||||
(+1, -1, +1),
|
||||
(+1, +1, +1),
|
||||
(+1, +1, -1),
|
||||
)
|
||||
down = ((-1, -1, -1),
|
||||
(+1, -1, -1),
|
||||
(+1, +1, -1),
|
||||
(-1, +1, -1),
|
||||
)
|
||||
top = ((-1, -1, +1),
|
||||
(+1, -1, +1),
|
||||
(+1, +1, +1),
|
||||
(-1, +1, +1),
|
||||
)
|
||||
|
||||
faces = ((top, color(255)),
|
||||
(down, color(0)),
|
||||
(right, color(150)),
|
||||
(left, color(150)),
|
||||
(back, color(75)),
|
||||
(front, color(75)),
|
||||
)
|
||||
|
||||
for pts, shade in faces:
|
||||
hs = side / 2 # half side
|
||||
#fill(shade)
|
||||
beginShape()
|
||||
for pt in pts:
|
||||
x, y, z = pt
|
||||
vertex(x * hs, y * hs, z * hs)
|
||||
endShape(CLOSE)
|
|
@ -0,0 +1,234 @@
|
|||
# Alexandre B A Villares - https://abav.lugaralgum.com/sketch-a-day
|
||||
SKETCH_NAME = "s239" # 20180825
|
||||
|
||||
# made my own randint so no need of this: # from random import randint
|
||||
from gif_export_wrapper import gif_export
|
||||
from my_box import my_box
|
||||
add_library('gifAnimation')
|
||||
add_library('peasycam')
|
||||
add_library('dxf')
|
||||
add_library('OBJExport')
|
||||
|
||||
GRID_SIZE = 32
|
||||
OUTPUT = ".gif"
|
||||
# Cutting-plane position (press + & - to change)
|
||||
cut_plane = GRID_SIZE
|
||||
export = False
|
||||
|
||||
def setup():
|
||||
print_text_for_readme(SKETCH_NAME, OUTPUT)
|
||||
size(700, 700, P3D)
|
||||
colorMode(HSB)
|
||||
# optional PeasyCam setup to allow orbiting with a mouse drag
|
||||
cam = PeasyCam(this, 100)
|
||||
cam.setMinimumDistance(1000)
|
||||
cam.setMaximumDistance(1000)
|
||||
# sets border and grid spacing
|
||||
Node.border = 50
|
||||
Node.spacing = (width - Node.border * 2) / GRID_SIZE
|
||||
# Node.nodes is a list of nodes in a 3D grid
|
||||
for x in range(GRID_SIZE):
|
||||
for y in range(GRID_SIZE):
|
||||
for z in range(GRID_SIZE):
|
||||
new_node = Node(x, y, z)
|
||||
Node.nodes.append(new_node)
|
||||
Node.grid[x, y, z] = new_node
|
||||
# crear objects
|
||||
create_frames()
|
||||
# create_tubes()
|
||||
|
||||
def draw():
|
||||
global export
|
||||
rotateY(QUARTER_PI)
|
||||
lights()
|
||||
background(100)
|
||||
if export:
|
||||
beginRaw("nervoussystem.obj.OBJExport", "filename.obj")
|
||||
|
||||
for node in Node.nodes:
|
||||
if node.iz < cut_plane:
|
||||
node.plot()
|
||||
|
||||
if export:
|
||||
endRaw()
|
||||
export = False
|
||||
|
||||
|
||||
def keyPressed():
|
||||
""" press P to save an image """
|
||||
if key in ['p', 'P']:
|
||||
saveFrame("####" + SKETCH_NAME + OUTPUT)
|
||||
if key == " ":
|
||||
for node in Node.nodes:
|
||||
node.cor = None
|
||||
create_frames()
|
||||
# create_tubes()
|
||||
if key == "g":
|
||||
gif_export(GifMaker, delay=2000, filename=SKETCH_NAME)
|
||||
if key == "f":
|
||||
gif_export(GifMaker, finish=True)
|
||||
# cut plane adjust
|
||||
global cut_plane, export
|
||||
if key == "-":
|
||||
cut_plane -= 1
|
||||
print(cut_plane)
|
||||
if key in ['+', '=']:
|
||||
cut_plane += 1
|
||||
print(cut_plane)
|
||||
if key == 'e':
|
||||
export = True
|
||||
|
||||
|
||||
def big_box(px, py, pz, w, h=None, d=None, c=255):
|
||||
h = h if h else w
|
||||
d = d if d else w
|
||||
for z in range(pz, pz + d):
|
||||
for y in range(py, py + h):
|
||||
for x in range(px, px + w):
|
||||
if (0 <= x < GRID_SIZE and
|
||||
0 <= y < GRID_SIZE and
|
||||
0 <= z < GRID_SIZE):
|
||||
Node.grid[(x, y, z)].cor = c
|
||||
|
||||
def cube_frame(px, py, pz, w, h=None, d=None, c=255):
|
||||
h = h if h else w
|
||||
d = d if d else w
|
||||
for z in range(pz, pz + d + 1):
|
||||
for y in range(py, py + h + 1):
|
||||
for x in range(px, px + w + 1):
|
||||
if (0 <= x < GRID_SIZE and
|
||||
0 <= y < GRID_SIZE and
|
||||
0 <= z < GRID_SIZE):
|
||||
if ((x == px and y == py) or
|
||||
(x == px + w and y == py + h) or
|
||||
(x == px + w and y == py) or
|
||||
(x == px and y == py) or
|
||||
(x == px and y == py + h) or
|
||||
(y == py + h and z == pz) or
|
||||
(y == py + h and z == pz + d) or
|
||||
(y == py and z == pz) or
|
||||
(y == py and z == pz + d) or
|
||||
(z == pz and x == px) or
|
||||
(z == pz + d and x == px) or
|
||||
(z == pz and x == px + w) or
|
||||
(z == pz + d and x == px + w)
|
||||
):
|
||||
Node.grid[(x, y, z)].cor = c
|
||||
else:
|
||||
pass
|
||||
# Node.grid[(x, y, z)].cor = None
|
||||
|
||||
|
||||
def create_tubes():
|
||||
# sets the objects, list of tuples -> hollowed boxes
|
||||
seed = int(random(1000)) # seed = 205
|
||||
println("seed: {}".format(seed))
|
||||
randomSeed(seed)
|
||||
m = GRID_SIZE - 1
|
||||
tube_list = []
|
||||
num_tubes = 10
|
||||
border = 1
|
||||
for i in range(num_tubes):
|
||||
# random size in range 3 to GRID_SIZE - borders
|
||||
w = randint(3, m - border * 2)
|
||||
h = randint(3, m - border * 2)
|
||||
d = randint(3, m - border * 2)
|
||||
# random position
|
||||
x = randint(border, m - w - border)
|
||||
y = randint(border, m - h - border)
|
||||
z = randint(border, m - d - border)
|
||||
box_tuple = (x, y, z, w, h, d)
|
||||
print(box_tuple)
|
||||
tube_list.append(box_tuple)
|
||||
# solid boxes
|
||||
for i in range(num_tubes):
|
||||
x, y, z, w, h, d = tube_list[i]
|
||||
big_box(x, y, z, w, h, d,
|
||||
color(64 + (i % 3) * 32, 200, 200))
|
||||
# erase inside boxes, making tubes
|
||||
for i in range(num_tubes):
|
||||
x, y, z, w, h, d = tube_list[i]
|
||||
side = (i % 3) + 1
|
||||
if side == 1:
|
||||
h -= 2
|
||||
w -= 2
|
||||
z -= 1
|
||||
elif side == 2:
|
||||
d -= 2
|
||||
h -= 2
|
||||
x -= 1
|
||||
else:
|
||||
w -= 2
|
||||
d -= 2
|
||||
y -= 1
|
||||
big_box(x + 1, y + 1, z + 1, w, h, d,
|
||||
# use color(0) below, instead of None to debug
|
||||
# color(0))
|
||||
None)
|
||||
|
||||
def create_frames():
|
||||
# sets the objects, list of tuples -> hollowed boxes
|
||||
seed = int(random(1000)) # seed = 205
|
||||
println("seed: {}".format(seed))
|
||||
randomSeed(seed)
|
||||
m = GRID_SIZE - 1
|
||||
box_list = []
|
||||
num_boxes = 10
|
||||
border = 1
|
||||
for i in range(num_boxes):
|
||||
# random size in range 3 to GRID_SIZE - borders
|
||||
w = randint(3, m - border * 2)
|
||||
h = randint(3, m - border * 2)
|
||||
d = randint(3, m - border * 2)
|
||||
# random position
|
||||
x = randint(border, m - w - border)
|
||||
y = randint(border, m - h - border)
|
||||
z = randint(border, m - d - border)
|
||||
box_tuple = (x, y, z, w, h, d)
|
||||
# print(box_tuple)
|
||||
box_list.append(box_tuple)
|
||||
# solid boxes
|
||||
for i in range(num_boxes):
|
||||
x, y, z, w, h, d = box_list[i]
|
||||
cube_frame(x, y, z, w, h, d, color(64 + (i % 3) * 32, 200, 200))
|
||||
|
||||
class Node():
|
||||
nodes = []
|
||||
grid = dict()
|
||||
|
||||
def __init__(self, x, y, z):
|
||||
self.ix = x
|
||||
self.iy = y
|
||||
self.iz = z
|
||||
self.x = Node.border + Node.spacing / 2 + x * Node.spacing - width / 2
|
||||
self.y = Node.border + Node.spacing / 2 + y * Node.spacing - width / 2
|
||||
self.z = Node.border + Node.spacing / 2 + z * Node.spacing - width / 2
|
||||
self.size_ = 1
|
||||
self.cor = None
|
||||
|
||||
def plot(self):
|
||||
""" draws box """
|
||||
if self.cor:
|
||||
# stroke(0, 200)
|
||||
noStroke() #stroke(0)
|
||||
fill(self.cor)
|
||||
with pushMatrix():
|
||||
translate(self.x, self.y, self.z)
|
||||
my_box(Node.spacing * self.size_)
|
||||
|
||||
def randint(a, b=None):
|
||||
if not b:
|
||||
b = a
|
||||
a = 0
|
||||
return int(random(a, b + 1))
|
||||
|
||||
|
||||
def print_text_for_readme(name, output):
|
||||
""" prints text in the console to add to project README.md """
|
||||
println("""
|
||||

|
||||
|
||||
{1}: [code](https://github.com/villares/sketch-a-day/tree/master/{0}) [[Py.Processing](https://villares.github.io/como-instalar-o-processing-modo-python/index-EN)]
|
||||
|
||||
""".format(name, name[1:], output)
|
||||
)
|
Ładowanie…
Reference in New Issue