Skip to content

Instantly share code, notes, and snippets.

@zeffii
Forked from anonymous/node_Script.py
Last active August 29, 2015 14:00
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/11019465 to your computer and use it in GitHub Desktop.
Save zeffii/11019465 to your computer and use it in GitHub Desktop.
# ##### BEGIN GPL LICENSE BLOCK #####
#
# This program is free software; you can redistribute it and/or
# modify it under the terms of the GNU General Public License
# as published by the Free Software Foundation; either version 2
# of the License, or (at your option) any later version.
#
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
# GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with this program; if not, write to the Free Software Foundation,
# Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
#
# ##### END GPL LICENSE BLOCK #####
import bpy
from bpy.props import StringProperty, EnumProperty, BoolProperty
from bpy.props import IntProperty, FloatProperty
from node_s import *
from util import *
import ast
FAIL_COLOR = (0.8, 0.1, 0.1)
READY_COLOR = (0, 0.8, 0.95)
# utility function
def new_output_socket(node, name, stype):
socket_type = {
'v': 'VerticesSocket',
's': 'StringsSocket',
'm': 'MatrixSocket'}.get(stype, None)
if socket_type:
node.outputs.new(socket_type, name, name)
# def new_input_socket(node, name, stype):
# socket_type = {
# 'int': 'IntSocket',
# 'float': 'FloatSocket'}.get(stype, None)
# if socket_type:
# node.inputs.new(socket_type, name, name)
class SvScriptOp(bpy.types.Operator):
""" Load Script as Generator """
bl_idname = "node.sverchok_script_input"
bl_label = "Sverchok script input"
bl_options = {'REGISTER', 'UNDO'}
# from object in
name_obj = StringProperty(name='object name')
name_tree = StringProperty(name='tree name')
def execute(self, context):
print('here')
node = bpy.data.node_groups[self.name_tree].nodes[self.name_obj]
node.load()
return {'FINISHED'}
class SvScriptNode(Node, SverchCustomTreeNode):
''' Text Input '''
bl_idname = 'SvScriptNode'
bl_label = 'Script Generator'
bl_icon = 'OUTLINER_OB_EMPTY'
def avail_scripts(self, context):
scripts = bpy.data.texts
items = [(t.name, t.name, "") for t in scripts]
return items
script = EnumProperty(
items=avail_scripts,
name="Texts",
description="Choose text to load",
update=updateNode)
script_modes = [
("Py", "Python", "Python File", "", 1),
("coffee", "CoffeeScript", "cf File", "", 2)]
scriptmode = EnumProperty(
items=script_modes,
default='Py',
update=updateNode)
# name of loaded text, to support reloading
current_scripts = StringProperty(default="")
# external file
file = StringProperty(subtype='FILE_PATH')
parametric_in = {}
data_out = {}
def draw_buttons(self, context, layout):
layout.prop(self, "script", "Select Script")
layout.prop(self, 'scriptmode', 'scriptmode', expand=True)
row = layout.row(align=True)
op = row.operator('node.sverchok_script_input', text='Load')
op.name_tree = self.id_data.name
op.name_obj = self.name
# dispatch functions
def load(self, reload = False):
if self.scriptmode == 'Py':
self.load_py()
def update(self):
# dispatch based on mode
if self.scriptmode == 'Py':
self.load_py()
elif self.scriptmode == 'coffee':
self.load_cf()
def update_socket(self, context):
self.update()
def local_temp(self, variable_inputs):
try:
exec(self.script_str.format(*variable_inputs))
except:
return None
finally:
return vars()
def load_py(self, reload=False):
self.script_str = bpy.data.texts[self.script].as_string()
self.data_found = self.local_temp((30, 5))
name_dict = {
'm': 'Matrix',
's': 'Data',
'v': 'Vertices',
'p': 'Polygons',
'e': 'Edges'
}
if self.data_found:
for k in self.data_found.keys():
if k in name_dict and not (k in self.data_out):
self.data_out[k] = self.data_found[k]
ktype = k
if k in {'p', 'e'}:
ktype = 's'
new_output_socket(self, name_dict[k], ktype)
output = self.data_found[k]
SvSetSocketAnyType(self, name_dict[k], output)
self.use_custom_color = True
self.color = READY_COLOR
else:
print('socket type', k, ' not recognized')
if 'types' in self.data_found:
vnames = [vn[1] for vn in self.data_found['types']]
if set(self.parametric_in.keys()) == set(vnames):
return
for sock_in_type, varname in self.data_found['types']:
if sock_in_type == int:
prop = IntProperty(
name=varname, default=2, update=updateNode)
elif sock_in_type == float:
prop = FloatProperty(
name=varname, default=1.0, update=updateNode)
else:
return
self.parametric_in[varname] = prop
self.inputs.new('StringsSocket', varname, varname)
else:
self.use_custom_color = True
self.color = FAIL_COLOR
def register():
bpy.utils.register_class(SvScriptOp)
bpy.utils.register_class(SvScriptNode)
def unregister():
bpy.utils.unregister_class(SvScriptOp)
bpy.utils.unregister_class(SvScriptNode)
if __name__ == "__main__":
register()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment