Skip to content

Instantly share code, notes, and snippets.

@zeffii
Forked from anonymous/json_reader.py
Last active August 29, 2015 14:15
Show Gist options
  • Save zeffii/e32f3f6bd06860d1bc9a to your computer and use it in GitHub Desktop.
Save zeffii/e32f3f6bd06860d1bc9a to your computer and use it in GitHub Desktop.
import bpy
import json
def sv_main(scale=75.0, verticals=0.005):
verts_out = []
widths_out = []
in_sockets = [
['s', 'scale', scale],
['s', 'verticals', verticals]
]
out_sockets = [
['v', 'verts', [verts_out]],
['s', 'widths', [widths_out]]
]
text_obj = bpy.data.texts['multifile.json']
text_str = text_obj.as_string()
print(text_str)
nodes_json = json.loads(text_str)
graphs = nodes_json['graphs']
x_jump = 0
prime_start = graphs['0']['items']['0']['start']
for graph_key in sorted(graphs.keys()):
for idx in sorted(graphs[graph_key]['items']):
f = graphs[graph_key]['items'][idx]
print(f)
v1 = [(f['start'] - prime_start), x_jump, 0]
v2 = [(v1[0] + f['duration']), x_jump, 0]
v1[0] *= scale
v2[0] *= scale
rect_width = v2[0] - v1[0]
x_jump -= verticals
verts_out.append(v1)
widths_out.append(rect_width)
x_jump -= (verticals * 3)
return in_sockets, out_sockets
{
"graphs": {
"0": {
"items": {
"0": {
"bl_idname": "SvGenFloatRange",
"duration": 0.00010203810819531256,
"name": "Float Series",
"start": 2.624644403129448
},
"1": {
"bl_idname": "ScalarMathNode",
"duration": 0.00022125717095322628,
"name": "function",
"start": 2.6247534253655145
},
"2": {
"bl_idname": "ScalarMathNode",
"duration": 0.00018102859441659902,
"name": "function.001",
"start": 2.624980758727715
},
"3": {
"bl_idname": "GenVectorsNode",
"duration": 0.00013528255686123813,
"name": "Vectors in",
"start": 2.625167514306986
},
"4": {
"bl_idname": "ViewerNode2",
"duration": 0.00014540954227415526,
"name": "Viewer Draw2",
"start": 2.625308942896374
}
},
"name": "NodeTree"
},
"1": {
"items": {
"0": {
"bl_idname": "SvGenFloatRange",
"duration": 6.53714368725744e-05,
"name": "Float Series",
"start": 2.6261603779251272
},
"1": {
"bl_idname": "GenVectorsNode",
"duration": 8.5695248977391e-05,
"name": "Vectors in",
"start": 2.6262328033311495
},
"2": {
"bl_idname": "MatrixGenNode",
"duration": 0.00015867938522928782,
"name": "Matrix in",
"start": 2.6263248541364894
},
"3": {
"bl_idname": "GenListRangeIntNode",
"duration": 5.838730900142863e-05,
"name": "List Range Int",
"start": 2.626488981141458
},
"4": {
"bl_idname": "SvCircleNode",
"duration": 0.00030716194376667616,
"name": "Circle",
"start": 2.6265529557527563
},
"5": {
"bl_idname": "ViewerNode2",
"duration": 0.00025282542893023674,
"name": "Viewer Draw2",
"start": 2.626866543094164
}
},
"name": "NodeTree.001"
}
}
}
import math
from math import sin, cos, pi
def sv_main(widths=[], h=0.5, radius=0.02):
verts_out = []
faces_out = []
in_sockets = [
['s', 'widths', widths],
['s', 'height', h],
['s', 'radius', radius]
]
out_sockets = [
['v', 'verts', verts_out],
['s', 'faces', faces_out]
]
def offset(v2, vlist):
return [[v2[0]+v[0], v2[1]+v[1], v2[2]+v[2]] for v in vlist]
def make_rect(w, h, r):
r = 0 if r <= 0 else r
# define division here.
divs = 15
# this goes clockwise.
num_v = (4*divs)
if r > 0:
radial_verts = []
for v in range(num_v):
theta = v/num_v * 2 * pi
radial_verts.append((sin(theta)*r, cos(theta)*r, 0))
top_left = radial_verts[3*divs:] + [radial_verts[0]]
top_right = radial_verts[:divs+1]
bot_right = radial_verts[divs:(2*divs)+1]
bot_left = radial_verts[2*divs:(3*divs)+1]
local = []
local.extend(offset((r, -r, 0), top_left))
local.extend(offset((w-r, -r, 0), top_right))
local.extend(offset((w-r, -h+r, 0), bot_right))
local.extend(offset((r, -h+r, 0), bot_left))
fac = [[i for i in range(num_v+3)]]
return local, fac
if widths:
print('yes here')
for w in widths[0][0]:
print(w, h, radius)
v_out, f_out = make_rect(w, h, radius)
verts_out.append(v_out)
faces_out.append(f_out)
return in_sockets, out_sockets
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment