Skip to content

Instantly share code, notes, and snippets.

Created May 18, 2014 15:49
Show Gist options
  • Save anonymous/a1eb2b23dee031701d46 to your computer and use it in GitHub Desktop.
Save anonymous/a1eb2b23dee031701d46 to your computer and use it in GitHub Desktop.
test
import bpy
from node_s import *
from util import *
import bpy
from mathutils import Vector
import random
from random import random
import re
import ast
import os
def openjson_asdict(fname):
sv_path = os.path.dirname(os.path.realpath(__file__))
path_to_json = os.path.join(sv_path, fname)
with open(path_to_json) as d:
return ast.literal_eval(''.join(d.readlines()))
fontdict = openjson_asdict('font1.dict')
fdict = {
str(ord('a')): 'NurbsPath.001',
str(ord('b')): 'NurbsPath.002',
str(ord('c')): 'NurbsPath.003',
str(ord('d')): 'NurbsPath.004',
str(ord('e')): 'NurbsPath.005',
str(ord('f')): 'NurbsPath.006',
str(ord('g')): 'NurbsPath.007',
str(ord('h')): 'NurbsPath.008',
str(ord('i')): 'NurbsPath.009',
str(ord('j')): 'NurbsPath.010',
str(ord('k')): 'NurbsPath.011',
str(ord('l')): 'NurbsPath.012',
str(ord('m')): 'NurbsPath.013',
str(ord('n')): 'NurbsPath.014',
str(ord('o')): 'NurbsPath.015',
str(ord('p')): 'NurbsPath.016',
str(ord('q')): 'NurbsPath.017',
str(ord('r')): 'NurbsPath.018',
str(ord('s')): 'NurbsPath.019',
str(ord('t')): 'NurbsPath.020',
str(ord('u')): 'NurbsPath.021',
str(ord('v')): 'NurbsPath.022',
str(ord('w')): 'NurbsPath.023',
str(ord('x')): 'NurbsPath.024',
str(ord('y')): 'NurbsPath.025',
str(ord('z')): 'NurbsPath.026',
str(ord('0')): 'NurbsPath.027',
str(ord('1')): 'NurbsPath.028',
str(ord('2')): 'NurbsPath.029',
str(ord('3')): 'NurbsPath.030',
str(ord('4')): 'NurbsPath.031',
str(ord('5')): 'NurbsPath.032',
str(ord('6')): 'NurbsPath.033',
str(ord('7')): 'NurbsPath.034',
str(ord('8')): 'NurbsPath.035',
str(ord('9')): 'NurbsPath.036',
str(ord('!')): 'NurbsPath.039',
str(ord('?')): 'NurbsPath.040',
str(ord('#')): 'NurbsPath.041',
str(ord('%')): 'NurbsPath.042',
str(ord('&')): 'NurbsPath.043',
str(ord('$')): 'NurbsPath.044',
str(ord('@')): 'NurbsPath.045',
str(ord('*')): 'NurbsPath.047',
str(ord('.')): 'NurbsPath.055',
str(ord(',')): 'NurbsPath.056',
str(ord('{')): 'NurbsPath.048',
str(ord('(')): 'NurbsPath.049',
str(ord('/')): 'NurbsPath.050',
str(ord('|')): 'NurbsPath.051',
str(ord('\\')): 'NurbsPath.052',
str(ord(')')): 'NurbsPath.053',
str(ord('}')): 'NurbsPath.051',
str(ord('-')): 'NurbsPath.037',
str(ord('+')): 'NurbsPath.038',
str(ord('=')): 'NurbsPath.046',
str(ord('^')): 'NurbsPath.057',
'RAD': 'NurbsPath.059',
'DIAM': 'NurbsPath.058',
'DEG': 'NurbsPath.060',
'SIGMA': 'NurbsPath.061',
}
remaps = {
'\\r': 'RAD',
'\D': 'DIAM',
'\d': 'DEG',
'\z': 'SIGMA'
}
def font_map(ch, fontdict):
if ch in remaps:
remapable = remaps[ch]
else:
if len(ch) > 1:
print(ch)
return
remapable = fdict.get(str(ord(ch)), None)
if remapable:
print('>>>', remapable)
print((remapable in fontdict), 'in fontdict')
return fontdict.get(remapable, "")
else:
print(repr(ch), 'not found in charmap')
return
def generate_greasepencil(node, text, col, pxwide, pos, fontdict):
line_height = 38
char_width = pxwide
spaces = 0
yof = 0
xof = 0
bcx, bcy = pos
nt = node.id_data
gp = nt.grease_pencil = bpy.data.grease_pencil.new('temp')
layer = gp.layers.new('damzel')
layer.line_width = 1
layer.frames.new(1)
# this gives lines, split by newline.
tokens = []
ptext = re.split("(\n)", text)
for j in ptext:
ftoken = []
if any(k in j for k in remaps.keys()):
ftoken = re.split("(,| )", j)
ftoken = [i for i in ftoken if len(i) > 0]
tokens.extend(ftoken)
else:
tokens.extend([i for i in j])
for ch in tokens:
if ch == "\n":
yof -= line_height
xof = 0
continue
if ch == " ":
xof += char_width
continue
if ch in remaps:
remapped = remaps[ch]
v = fontdict[fdict[remapped]]
else:
v = font_map(ch, fontdict)
if not v:
xof += char_width
continue
r = lambda v: v + v * ((random() + 0.769) * 0.037)
humanize = lambda x, y: (r(x), r(y))
for chain in v:
s = layer.frames[0].strokes.new()
s.draw_mode = '2DSPACE'
s.points.add(len(chain))
for idx, p in enumerate(chain):
ap = Vector(p) * 25
x, y = ap[:2]
x, y = humanize(x, y)
xyz = ((x + bcx + xof), (y + bcy + yof), 0)
s.points[idx].co = xyz
xof += char_width
class SverchokGText(bpy.types.Operator):
bl_idname = "node.sverchok_gtext_button"
bl_label = "Sverchok gtext"
bl_options = {'REGISTER', 'UNDO'}
text = bpy.props.StringProperty(name='text', default='')
def execute(self, context):
name = context.screen.name
areas = bpy.data.screens[name].areas
return {'FINISHED'}
class SverchokUnGText(bpy.types.Operator):
"""Sverchok UnGText"""
bl_idname = "node.sverchok_gtext_unbutton"
bl_label = "Sverchok Un gtext"
bl_options = {'REGISTER', 'UNDO'}
text_element = bpy.props.StringProperty(name='text', default='')
def execute(self, context):
name = context.screen.name
areas = bpy.data.screens[name].areas
return {'FINISHED'}
class GTextNode(Node, SverchCustomTreeNode):
''' Note '''
bl_idname = 'GTextNode'
bl_label = 'GText'
bl_icon = 'OUTLINER_OB_EMPTY'
text = bpy.props.StringProperty(name='text', default='your text here')
intx = bpy.props.IntProperty(default=0)
inty = bpy.props.IntProperty(default=0)
def draw_buttons(self, context, layout):
pass
def init(self, context):
self.intx, self.inty = self.location
pass
def update(self):
if not (self.intx, self.inty) == self.location:
self.intx, self.inty = self.location
self.draw_gtext()
pass
def draw_gtext(self):
text = """
gtext iteration 1.
not a full character set yet:
!?()/\|,.*$%#
123456789
12 = 13 - 1
+ -
4^2 = 16
\d, \d, \D, \z, \\r
"""
#text = self.text_element
print("should be drawing!")
col = []
pxwide = 28
pos = self.location
generate_greasepencil(self, text, col, pxwide, pos, fontdict)
def register():
bpy.utils.register_class(GTextNode)
bpy.utils.register_class(SverchokGText)
bpy.utils.register_class(SverchokUnGText)
def unregister():
bpy.utils.unregister_class(SverchokUnGText)
bpy.utils.unregister_class(SverchokGText)
bpy.utils.unregister_class(GTextNode)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment