generative modeling for Rhino
Hi, i have multiple python components that re-use serveral functions over and over again. I would like to avoid having to cut and paste the same functions into all components as it makes editing/updating them very inconvenient. With the C#/VB components I could reference dlls of my code libraries, how do I do the same thing with the Python component? Thanks
Tags:
Permalink Reply by Benjamin Golder on March 7, 2012 at 12:02am Let's assume you have a module called 'pyscripts.py'
if you look at the output of this code:
import sys
for p in sys.path:
print p
It will show you a list of folders that you can import modules from. You can put pyscripts.py in one of those folders and then import as usual:
import pyscripts
Or, you can add new folders to the list like this:
import sys
my_path = '/path/to/my/folder/of/code'
sys.path.append(my_path)
import myscripts
Permalink Reply by Benjamin Golder on March 7, 2012 at 12:05am
Permalink Reply by Jason Lim on March 7, 2012 at 2:00am Hi Benjamin, great this works perfectly. a big thank you!
Permalink Reply by Giulio Piacentino on March 7, 2012 at 4:20am Please use the sample from https://github.com/mcneel/ghpython/issues/16 and not the one here above. There needs to be an if, or the code will add more and more items to the list, making it slower at each iteration.
Thanks,
- Giulio
_______________
giulio@mcneel.com
Permalink Reply by Benjamin Golder on March 8, 2012 at 6:42pm forgot about that, thanks.
Permalink Reply by Giulio Piacentino on March 7, 2012 at 2:18am You can also pass functions (or callable objects) in GhPython.
- Giulio
________________
giulio@mcneel.com
Permalink Reply by Jason Lim on March 7, 2012 at 3:10am Hi Giulio, I took a look at that definition but how do you pass multiple functions? Am I restricted to only 1 function per component?
Permalink Reply by Giulio Piacentino on March 7, 2012 at 4:15am You could also pass a list or tuple of functions.
This is just a possibility, though: you can choose the method that works best for you
I'd say all these methods are valid and you can choose the one you like better.
- Giulio
______________
giulio@mcneel.com
Permalink Reply by Jason Lim on March 8, 2012 at 6:27pm Hi Giulio,
Thanks for listing out the various options. Out of curiousity is it possible to pass a dictionary of functions between components? In the attached defintion, instead of an output that is a dictionary, i get a list of strings (of the keys).
Permalink Reply by Benjamin Golder on March 8, 2012 at 6:40pm If you wrap it in a custom class you can:
class FuncDict():
def __init__(self, d):
self.d = d
f = FuncDict(my_dict)
my_new_dict = f.d
The problem you're encountering is that dictionaries have an __iter__() method that returns their keys, and Grasshopper automatically looks for an Iterable interface to convert objects that exit components. So dictionaries get turned into a list of keys.
It would be nice if instead they could be recognized as KeyValuePairs, or as Python objects.
Permalink Reply by Jason Lim on March 8, 2012 at 7:11pm Hi Benjamin, thanks for the quick response and clearing up the confusion about dictionaries. It works now but I think using either your method or Giulio's suggestions for passing functions /add references would be a more elegant way to go about it. Yes it would be very useful if we could pass dictionaries around, it seems like there is no way to do this in either C# or python?
© 2013 Created by Scott Davidson.
Powered by