## Espaço de Tecnologias e Artes - Sesc Avenida Paulista ### `hackmd.io/@sesc-av-paulista/estudos-em-python-25-junho` # Grupo de estudos em Python ## 25/6 Desenho e fabricação digital - Falamos sobre fabricação digital, arquivos vetoriais (em contraponto aos arquivos bitmap/raster), GCODE e impressão 3D - Queremos explorar `shapely`, `trimesh` (vamos usar o Thonny) - no shell do sistema aberto pelo Thonny é possível pedir `pip install trimesh[easy]` - Falhamos miseravelmente em fazer o Jupyter Lab funcionar (o Kernel desconecta), e o Alexandre se recusou a conectar no Google Colab mais uma vez. - No Thonny, agora com `py5`: ![image](https://hackmd.io/_uploads/Hys_jKO8A.png) ```python import trimesh import shapely pontos = [(0, 0), (0, 100), (100, 100)] triangulo = shapely.Polygon(pontos) import py5 def setup(): py5.size(400, 400) py5.translate(200, 200) b = triangulo.buffer(15) resultado = b.difference(triangulo) shp = py5.convert_shape(resultado) py5.shape(shp, 0, 0) py5.run_sketch() ``` ![image](https://hackmd.io/_uploads/r17tyqO8R.png) ```python import trimesh import shapely pontos = [(0, 0), (0, 100), (100, 100)] triangulo = shapely.Polygon(pontos) import py5 def setup(): py5.size(400, 400, py5.P3D) py5.lights() py5.no_stroke() py5.translate(200, 200) py5.rotate_y(py5.radians(45)) b = triangulo.buffer(15) resultado = b.difference(triangulo) resultado3D = trimesh.creation.extrude_polygon(resultado, 50) shp = py5.convert_shape(resultado3D) py5.shape(shp, 0, 0) py5.run_sketch() ``` - Código "interativo" ```python! import trimesh import shapely pontos = [(0, 0), (0, 100), (100, 100)] triangulo = shapely.Polygon(pontos) import py5 def setup(): global shp, resultado3D py5.size(400, 400, py5.P3D) b = triangulo.buffer(15) resultado = b.difference(triangulo) resultado3D = trimesh.creation.extrude_polygon(resultado, 50) shp = py5.convert_shape(resultado3D) shp.disable_style() def draw(): py5.background(200) if py5.is_key_pressed: py5.stroke(0) else: py5.no_stroke() py5.lights() py5.translate(200, 200) py5.rotate_y(py5.radians(py5.mouse_x)) py5.shape(shp, 0, 0) def key_pressed(): if py5.key == 's': print('exportar STL') resultado3D.export('resultado.stl') py5.run_sketch() ``` - Régua de desenho de estrela! ![image](https://hackmd.io/_uploads/HJA4FqOU0.png) ```python! import trimesh import shapely import py5 def setup(): global shp, resultado3D py5.size(400, 400, py5.P3D) estrela = shapely_estrela(0, 0, 20, 10, 8) circulo = shapely_circle(0, 0, 50) resultado = circulo.difference(estrela) resultado3D = trimesh.creation.extrude_polygon(resultado, 0.75) shp = py5.convert_shape(resultado3D) shp.disable_style() def shapely_circle(x, y, diametro): p = shapely.Point((x, y)) c = p.buffer(diametro / 2) return c def shapely_estrela(x, y, raio_a, raio_b, num_pontas): from py5 import TWO_PI, sin, cos passo = TWO_PI / num_pontas pontos = [] ang=0 while ang < TWO_PI: # enquanto o ângulo for menor que 2 * PI: sx = cos(ang) * raio_a sy = sin(ang) * raio_a pontos.append((x + sx, y + sy)) sx = cos(ang + passo / 2.) * raio_b sy = sin(ang + passo / 2.) * raio_b pontos.append((x + sx, y + sy)) ang += passo # aumente o ângulo um passo return shapely.Polygon(pontos) def draw(): py5.background(200) if py5.is_key_pressed: py5.stroke(0) else: py5.no_stroke() py5.lights() py5.translate(200, 200) py5.scale(4) py5.rotate_y(py5.radians(py5.mouse_x)) py5.shape(shp, 0, 0) def key_pressed(): if py5.key == 's': print('exportar STL') resultado3D.export('resultado.stl') py5.run_sketch() ```