Skip to content

Instantly share code, notes, and snippets.

@zeffii
Forked from anonymous/node_Script.py
Created April 17, 2014 23:41
Show Gist options
  • Save zeffii/11017454 to your computer and use it in GitHub Desktop.
Save zeffii/11017454 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')
def draw_buttons(self, context, layout):
layout.prop(self, "script", "Select Script")
# layout.prop(self,"file","File") external file, TODO
layout.prop(self, 'scriptmode', 'scriptmode', expand=True)
row = layout.row(align=True)
# row.operator('self.load_py', text='make parametric')
#op = layout.operator('node.sverchok_text_input', text='Load')
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'}
if self.data_found:
for k in self.data_found.keys():
if k in name_dict:
new_output_socket(self, name_dict[k], k)
self.use_custom_color = True
self.color = READY_COLOR
if 'types' in self.data_found:
# skip = IntProperty(name='start', default=0, update=updateNode)
print(self.data_found['types'])
for sock_in_type in self.data_found['types']:
print(sock_in_type, '<---')
#new_input_socket(self, node, name, stype)
if sock_in_type == int:
self.inputs.new('StringSocket', 'int', 'int')
if sock_in_type == float:
self.inputs.new('StringSocket', 'float', 'float')
print('----')
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