Skip to content

Instantly share code, notes, and snippets.

@zeffii
Created January 7, 2016 10:26
Show Gist options
  • Save zeffii/e556ebcb33737a3fd58b to your computer and use it in GitHub Desktop.
Save zeffii/e556ebcb33737a3fd58b to your computer and use it in GitHub Desktop.
import bmesh
def sv_main(grid_x=3.0, grid_y=4.0, grid_z=5.0, div_x=4, div_y=6, div_z=2, grid_scale=0.1):
verts_out = []
edges_out = []
in_sockets = [
['s', 'grid_x', grid_x],
['s', 'grid_y', grid_y],
['s', 'grid_z', grid_z],
['s', 'div_x', div_x],
['s', 'div_y', div_y],
['s', 'div_z', div_z],
['s', 'grid_scale', grid_scale]
]
grid_x = abs(grid_x)
grid_y = abs(grid_y)
grid_z = abs(grid_z)
div_x = max(2, div_x)
div_y = max(2, div_y)
div_z = max(2, div_z)
start_x = -grid_x / 2
start_y = -grid_y / 2
start_z = -grid_z / 2
seg_x = grid_x / (div_x - 1)
seg_y = grid_y / (div_y - 1)
seg_z = grid_z / (div_z - 1)
for i in range(div_z):
for j in range(div_x):
for k in range(div_y):
x = start_x + (seg_x * j)
y = start_y + (seg_y * k)
z = start_z + (seg_z * i)
verts_out.append([x, y, z])
if verts_out:
xy = (div_x) * (div_y)
limit = (xy * (div_z)) - xy
x_limit = xy * div_z
''' make z axis '''
if True:
for e in range(limit):
edges_out.append([e, e+xy])
''' make x axis '''
if True:
m = [((div_x * div_y) - div_y) + i for i in range(0, div_x*div_y*div_z, div_x*div_y)]
x_excluder = []
for i in m:
x_excluder.extend(list(range(i, i+div_y, 1)))
for e in range(x_limit):
if not e in x_excluder:
edges_out.append([e, e+div_y])
''' make y axis '''
if True:
last_y = (xy * div_z) - 1
y_excluder = set(range(div_y-1, last_y, div_y))
for j in range(last_y):
if not j in y_excluder:
edges_out.append([j, j+1])
# print(len(verts_out))
# print(edges_out)
objects = bpy.data.objects
meshes = bpy.data.meshes
if 'base_grid' in objects:
obj = objects['base_grid']
base_mesh = obj.data
else:
base_mesh = meshes.new('base_grid_mesh')
obj = objects.new('base_grid', base_mesh)
bpy.context.scene.objects.link(obj)
if obj and base_mesh:
obj.modifiers.clear()
bm = bmesh.new() # create an empty BMesh
bm.from_mesh(base_mesh) # fill it in from a Mesh
bm.clear()
bm.to_mesh(base_mesh)
base_mesh.update()
base_mesh.from_pydata(verts_out, edges_out, [])
base_mesh.update()
obj.modifiers.new('skin', 'SKIN')
num_verts = len(verts_out)
flat_list = list((grid_scale,) * (2 * num_verts))
grid_scale = max(0.01, grid_scale)
grid_scale = min(0.5, grid_scale)
base_mesh.skin_vertices[0].data.foreach_set("radius", flat_list)
# for v in base_mesh.skin_vertices[0].data:
# v.radius = (grid_scale, grid_scale)
base_mesh.update()
out_sockets = [
['v', 'verts', [verts_out]],
['s', 'edges', [edges_out]]
]
return in_sockets, out_sockets
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment