Skip to content

Instantly share code, notes, and snippets.

@zeffii
Last active April 19, 2017 14:45
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save zeffii/a0763e196b0dde7d369a4b2da36f85c7 to your computer and use it in GitHub Desktop.
Save zeffii/a0763e196b0dde7d369a4b2da36f85c7 to your computer and use it in GitHub Desktop.
2dgrid
"""
in in_colors v d=[[]] n=0
out dummy_out s
"""
import math
import bgl
import bpy
from sverchok.data_structure import node_id
from sverchok.ui import nodeview_bgl_viewer_draw_mk2 as nvBGL2
def get_location(i, desired_width, items_wide):
offset_x = math.floor(desired_width/items_wide)
offset_y = math.floor(desired_width/items_wide)
x = offset_x * (i % items_wide)
y = offset_y * math.floor(i / items_wide)
return [x, y]
self.n_id = node_id(self)
nvBGL2.callback_disable(self.n_id)
def simple_color_viewer_nvBGL(x, y, args):
colors = args[0]
size = 10.0
bgl.glEnable(bgl.GL_POINT_SMOOTH)
bgl.glPointSize(size)
bgl.glBlendFunc(bgl.GL_SRC_ALPHA, bgl.GL_ONE_MINUS_SRC_ALPHA)
for color_list in colors:
if len(color_list) >= 30:
desired_width = 300
elif len(color_list) >=25:
desired_width = 200
elif len(color_list) >=16:
desired_width = 100
elif len(color_list) > 0:
desired_width = 50
else:
continue
num_inputs = len(color_list)
best_square = math.floor(math.sqrt(num_inputs))
bgl.glBegin(bgl.GL_QUADS)
for idx, color in enumerate(color_list):
bgl.glColor4f(*color)
coord = get_location(idx, desired_width, items_wide=best_square)
tx = coord[0] + x + 170
ty = coord[1] + y
h = w = desired_width/best_square
coords = [(tx, ty), (tx+w, ty), (w+tx, ty+h), (tx, ty+h)]
for coord in coords:
bgl.glVertex2f(*coord)
bgl.glEnd()
bgl.glDisable(bgl.GL_POINT_SMOOTH)
bgl.glDisable(bgl.GL_POINTS)
socket = self.inputs['in_colors']
if socket.links:
draw_data = {
'tree_name': self.id_data.name[:],
'mode': 'custom_function',
'custom_function': simple_color_viewer_nvBGL,
'loc': (self.location[:]),
'args': (socket.sv_get(),)
}
nvBGL2.callback_enable(self.n_id, draw_data)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment