algorithmic 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:
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
Hi Benjamin, great this works perfectly. a big thank you!
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
forgot about that, thanks.
You can also pass functions (or callable objects) in GhPython.
- Giulio
________________
giulio@mcneel.com
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?
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
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).
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.
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?
Welcome to
Grasshopper
Added by Parametric House 0 Comments 0 Likes
Added by Parametric House 0 Comments 0 Likes
Added by Parametric House 0 Comments 0 Likes
Added by Parametric House 0 Comments 0 Likes
© 2024 Created by Scott Davidson. Powered by