-
-
Save zeffii/0f9a0dc321acfa9bb49a to your computer and use it in GitHub Desktop.
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
# simple subdiv algo | |
# zeffii | 2015 | |
from mathutils import Vector as V | |
from math import floor | |
import re | |
def produce(verts, polys, iterations, scheme): | |
summa = sum(scheme) | |
rates = [r/summa for r in scheme] | |
first, last = rates[0], 1-(rates[-1]) | |
v_back, e_back = [], [] | |
ext_v = v_back.extend | |
add_e = e_back.append | |
def make_verts(poly): | |
local_verts = [V(verts[i]) for i in poly] + [V(verts[poly[0]])] | |
final_verts = local_verts | |
for n in range(iterations): | |
temp_verts = [] | |
v_ext = temp_verts.extend | |
for i in range(len(final_verts)-1): | |
v1 = final_verts[i] | |
v2 = final_verts[i+1] | |
va = v1.lerp(v2, first) | |
vb = v1.lerp(v2, last) | |
v_ext([va, vb]) | |
final_verts = temp_verts + [temp_verts[0]] | |
return final_verts, [] | |
for poly in polys: | |
if len(poly) > 2: | |
new_verts, new_edges = make_verts(poly) | |
ext_v(new_verts) | |
add_e(new_edges) | |
return v_back, e_back | |
def sv_main(verts=[[]], polygons=[[]], iterations=1, scheme=[[["1,3,1"]]]): | |
# scheme can be defined as 1,3,1 or 1-3-1 using Note Node | |
verts_out = [] | |
faces_out = [] | |
in_sockets = [ | |
['v', 'verts', verts], | |
['s', 'polygons', polygons], | |
['s', 'iterations', iterations], | |
['s', 'scheme', scheme] | |
] | |
out_sockets = [ | |
['v', 'verts', [verts_out]], | |
['s', 'faces', [faces_out]], | |
] | |
if verts and verts[0]: | |
verts = verts[0] | |
if polygons and polygons[0]: | |
polygons = polygons[0] | |
if scheme and scheme[0] and scheme[0][0]: | |
if iterations < 1: | |
iterations = 1 | |
elif iterations > 5: | |
iterations = 5 | |
scheme = re.split('[,-]', scheme[0][0][0]) | |
scheme = [int(c) for c in scheme] | |
Verts, Faces = produce(verts, polygons, iterations, scheme) | |
verts_out.extend([v[:] for v in Verts]) | |
faces_out.extend(Faces) | |
return in_sockets, out_sockets |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment