1+ import clr
2+ clr .AddReference ("Grasshopper" )
3+ import Grasshopper as GH
4+
5+ def __makefunc__ (obj ):
6+ def component_function (* args , ** kwargs ):
7+ """"returns dictionary of output values"""
8+ comp = obj .CreateInstance ()
9+ comp .ClearData ()
10+
11+ if args :
12+ for i , arg in enumerate (args ):
13+ param = comp .Params .Input [i ]
14+ param .PersistentData .Clear ()
15+ if hasattr (arg , '__iter__' ): #TODO deal with polyline
16+ for a in arg :
17+ param .AddPersistentData (a )
18+ else :
19+ param .AddPersistentData (arg )
20+ if kwargs :
21+ for param in comp .Params .Input :
22+ name = param .NickName
23+ if name in kwargs :
24+ param .PersistentData .Clear ()
25+ arg = kwargs [name ]
26+ if hasattr (arg , '__iter__' ): #TODO deal with polyline
27+ for a in arg :
28+ param .AddPersistentData (a )
29+ else :
30+ param .AddPersistentData (arg )
31+
32+ doc = GH .Kernel .GH_Document ()
33+ doc .AddObject (comp , False , 0 )
34+ comp .CollectData ()
35+ comp .ComputeData ()
36+ rc = {}
37+ for output in comp .Params .Output :
38+ data = output .VolatileData .AllData (True )
39+ name = output .NickName
40+ rc [name ] = [x .Value for x in data ]
41+ return rc
42+ return component_function
43+
44+ class namespace_object (object ):
45+ def __init__ (self ):
46+ pass
47+
48+ class __builder (object ):
49+ def __init__ (self ):
50+ import sys , types , string
51+ core_module = sys .modules ['ghcomponents' ]
52+ translation = string .maketrans ("'()*|" ,"_____" )
53+ for obj in GH .Instances .ComponentServer .ObjectProxies :
54+ if obj .Exposure == GH .Kernel .GH_Exposure .hidden or obj .Obsolete :
55+ continue
56+ t = clr .GetClrType (GH .Kernel .IGH_Component )
57+ if not (t .IsAssignableFrom (obj .Type )):
58+ continue
59+ m = core_module
60+ library_id = obj .LibraryGuid
61+ assembly = GH .Instances .ComponentServer .FindAssembly (library_id )
62+ if not assembly .IsCoreLibrary :
63+ module_name = assembly .Assembly .GetName ().Name
64+ if core_module .__dict__ .has_key (module_name ):
65+ m = core_module .__dict__ [module_name ]
66+ else :
67+ m = namespace_object ()
68+ setattr (core_module , module_name , m )
69+ name = obj .Desc .Name .Replace (" " ,"" )
70+ name = name .translate (translation )
71+ if not name [0 ].isalpha (): name = '_' + name
72+
73+ if m == core_module :
74+ setattr (m , name , __makefunc__ (obj ))
75+ comp = obj .CreateInstance ()
76+ a = m .__dict__ [name ]
77+ a .__name__ = name
78+ params = self .param_descriptions (comp .Params )
79+ help = "\n " + obj .Desc .Description
80+ for param in params :
81+ help = help + "\n " + param
82+ a .__doc__ = help
83+ else :
84+ setattr (m , name , types .MethodType (__makefunc__ (obj ), m , type (m )))
85+
86+ def param_descriptions (self , params ):
87+ rc = []
88+ for param in params :
89+ if param .Kind == GH .Kernel .GH_ParamKind .input :
90+ s = "{0} (in) [{1}] - {2}"
91+ if param .Optional :
92+ s = "{0} (in|optional) [{1}] - {2}"
93+ rc .append (s .format (param .NickName , param .TypeName , param .Description ))
94+ elif param .Kind == GH .Kernel .GH_ParamKind .output :
95+ s = "{0} (out) [{1}] - {2}"
96+ rc .append (s .format (param .NickName , param .TypeName , param .Description ))
97+ return rc
98+
99+
100+ __b = __builder ()
0 commit comments