33import Grasshopper as GH
44
55
6- def __make_function__ (obj ):
6+ def __make_function__ (helper ):
77 def component_function (* args , ** kwargs ):
8+ obj = helper .proxy
89 comp = obj .CreateInstance ()
910 comp .ClearData ()
10-
1111 if args :
1212 for i , arg in enumerate (args ):
13- if arg is None :
14- continue
13+ if arg is None : continue
1514 param = comp .Params .Input [i ]
1615 param .PersistentData .Clear ()
1716 if hasattr (arg , '__iter__' ): #TODO deal with polyline
18- for a in arg :
19- param .AddPersistentData (a )
17+ [param .AddPersistentData (a ) for a in arg ]
2018 else :
2119 param .AddPersistentData (arg )
2220 if kwargs :
2321 for param in comp .Params .Input :
24- name = param .NickName
22+ name = param .Name . lower ()
2523 if name in kwargs :
2624 param .PersistentData .Clear ()
2725 arg = kwargs [name ]
2826 if hasattr (arg , '__iter__' ): #TODO deal with polyline
29- for a in arg :
30- param .AddPersistentData (a )
27+ [param .AddPersistentData (a ) for a in arg ]
3128 else :
3229 param .AddPersistentData (arg )
33-
3430 doc = GH .Kernel .GH_Document ()
3531 doc .AddObject (comp , False , 0 )
3632 comp .CollectData ()
3733 comp .ComputeData ()
38- rc = {}
39- for output in comp .Params .Output :
40- data = output .VolatileData .AllData (True )
41- name = output .NickName
42- rc [name ] = [x .Value for x in data ]
43- return rc
34+ return helper .create_output (comp .Params )
4435 return component_function
4536
4637
4738class namespace_object (object ):
4839 def __init__ (self ):
4940 pass
5041
42+
43+ class function_helper (object ):
44+ def __init__ (self , proxy ):
45+ self .proxy = proxy
46+ self .return_type = None
47+
48+ def create_output (self , params ):
49+ import collections
50+ output_values = []
51+ for output in params .Output :
52+ data = output .VolatileData .AllData (True )
53+ v = [x .Value for x in data ]
54+ if len (v )< 1 :
55+ output_values .append (None )
56+ elif len (v )== 1 :
57+ output_values .append (v [0 ])
58+ else :
59+ output_values .append (v )
60+ if len (output_values )== 1 : return output_values [0 ]
61+ if self .return_type is None :
62+ names = [output .Name .lower () for output in params .Output ]
63+ try :
64+ t = collections .namedtuple ('Output' , names , rename = True )
65+ self .return_type = t
66+ except :
67+ self .return_type = False
68+ if not self .return_type : return output_values
69+ return self .return_type (* output_values )
70+
71+
5172def __build_module ():
52- def param_descriptions (params ):
53- rc = []
54- for param in params :
55- if param .Kind == GH .Kernel .GH_ParamKind .input :
56- s = "{0} (in) [{1}] - {2}"
57- if param .Optional :
58- s = "{0} (in, optional) [{1}] - {2}"
59- rc .append (s .format (param .NickName , param .TypeName , param .Description ))
60- elif param .Kind == GH .Kernel .GH_ParamKind .output :
61- s = "{0} (out) [{1}] - {2}"
62- rc .append (s .format (param .NickName , param .TypeName , param .Description ))
63- return rc
64-
73+ def function_description (description , params ):
74+ rc = ['' ,description , "Input:" ]
75+ for param in params .Input :
76+ s = "\t {0} [{1}] - {2}"
77+ if param .Optional :
78+ s = "\t {0} (in, optional) [{1}] - {2}"
79+ rc .append (s .format (param .Name .lower (), param .TypeName , param .Description ))
80+ if params .Output .Count == 1 :
81+ param = params .Output [0 ]
82+ rc .append ("Returns: [{0}] - {1}" .format (param .TypeName , param .Description ))
83+ elif params .Output .Count > 1 :
84+ rc .append ("Returns:" )
85+ for out in params .Output :
86+ s = "\t {0} [{1}] - {2}"
87+ rc .append (s .format (out .Name .lower (), out .TypeName , out .Description ))
88+ return '\n ' .join (rc )
89+
6590 import sys , types , string
6691 core_module = sys .modules ['ghpython.components' ]
67- translation = string .maketrans ("'()*|" , "_____ " )
92+ translation = string .maketrans ("'()*|+& " , "_______ " )
6893 for obj in GH .Instances .ComponentServer .ObjectProxies :
6994 if obj .Exposure == GH .Kernel .GH_Exposure .hidden or obj .Obsolete :
7095 continue
@@ -76,28 +101,23 @@ def param_descriptions(params):
76101 assembly = GH .Instances .ComponentServer .FindAssembly (library_id )
77102 if not assembly .IsCoreLibrary :
78103 module_name = assembly .Assembly .GetName ().Name
79- if core_module .__dict__ . has_key ( module_name ) :
104+ if module_name in core_module .__dict__ :
80105 m = core_module .__dict__ [module_name ]
81106 else :
82107 m = namespace_object ()
83108 setattr (core_module , module_name , m )
84109 name = obj .Desc .Name .Replace (" " , "" )
85- if "LEGACY" in name :
110+ if "LEGACY" in name or "#" in name :
86111 continue
87112 name = name .translate (translation )
88113 if not name [0 ].isalpha (): name = '_' + name
89-
90- function = __make_function__ (obj )
114+ function = __make_function__ (function_helper (obj ))
91115 if m == core_module :
92116 setattr (m , name , function )
93117 comp = obj .CreateInstance ()
94118 a = m .__dict__ [name ]
95119 a .__name__ = name
96- params = param_descriptions (comp .Params )
97- documentation = "\n " + obj .Desc .Description
98- for param in params :
99- documentation = documentation + "\n " + param
100- a .__doc__ = documentation
120+ a .__doc__ = function_description (obj .Desc .Description , comp .Params )
101121 else :
102122 setattr (m , name , types .MethodType (function , m , type (m )))
103123
0 commit comments