Instantly share code, notes, and snippets.

# zeffii/template_cyl2.py

Last active August 29, 2015 14:00
Show Gist options
• Save zeffii/11157348 to your computer and use it in GitHub Desktop.
drop the .py extension for loading as a node script. variable vertical segment count.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters. Learn more about bidirectional Unicode characters
 def sv_main(height=4.0, radius=2.0, num_verts=10, h_seg=1): # in boilerplate - make your own sockets in_sockets = [ ['s', 'Height', height], ['s', 'Radius', radius], ['s', 'Num Verts', num_verts], ['s', 'Num segments', h_seg], ] # import libraries if you need. from math import sin, cos, pi # your code here pi2 = 2*pi # ---- Vertices theta = pi2/num_verts top, bottom = [], [] for i in range(num_verts): delta = theta*i vec = [sin(delta)*radius, cos(delta)*radius, 0] vec2 = [vec[0], vec[1], height] top.append(vec2) bottom.append(vec) prime = [[0,0,0]] prime.extend(bottom) inner_verts = [] h_seg = 1 if h_seg < 1 else h_seg if h_seg > 1: height_jumps = height/h_seg for j in range(1, h_seg): inner_segments = [] for i in range(num_verts): delta = theta*i offset = height_jumps*j vec = [sin(delta)*radius, cos(delta)*radius, offset] inner_segments.append(vec) prime.extend(inner_segments) prime.extend(top) prime.append([0,0,height]) verts = prime polys = [] # ---- Polygons polys = [] for h in range(h_seg): b = (num_verts*h) for i in range(1, num_verts): f = [b+i, b+i+1, b+i+1+num_verts, b+i+num_verts] polys.append(f) # fill gap (vertical) polys.append([b+1, b+num_verts+1, b+2*num_verts, b+num_verts]) # cap bottom, then top for i in range(num_verts-1): polys.append([0, i+1, i+2]) polys.append([1,0,num_verts]) lvert = len(verts)-1 for i in range(1, num_verts): offset = (num_verts*(h_seg))+i polys.append([lvert, offset, offset+1]) polys.append([lvert, lvert-1, lvert-num_verts]) # out boilerplate - set your own sockets packet out_sockets = [ ['v', 'Verts', verts], ['s', 'Polygons', polys] ] return in_sockets, out_sockets