algorithmic modeling for Rhino
Currently, if I output an IronPython 'dict' object from a script component, it is automatically converted into a list of strings (each item being one of the keys).
Is there a way I can pass a dict object to another python scripting component?
I know that if I write my own class, I can pass that object to another scripting component, and any Grasshopper Panel component would probably list it by calling its __str__() method.
What built-in methods are being accessed to interpret/cast the output of the Python component? __iter__()?
Tags:
I did find a simple work around:
class GhPythonDictionary(object):
def __init__(self, pythonDict=None):
if pythonDict:
self.d = pythonDict
else:
self.d = {}
def ToString(self):
return 'GhPythonDictionary object'
This also illustrates something interesting to me:
Where and when are class declarations necessary?
If I have a set of classes that I want to use in a variety of GhPython components,
can I simply make one GhPython component that stores all the class definitions, and then reference the same classes in other scripts that run in the same grasshopper definition?
Are all multiple GhPython components in the same Grasshopper definition running on the same python engine instance? What does that imply for namespaces within python scripts?
I was able to answer most of these questions myself.
It seems that a new GhPython component has no trouble reading instance objects of custom classes that are input, and even provides Intellisense for their attributes, but I cannot access variables declared in another GhPython component.
Is there perhaps a way to access a global namespace for all the python components within a single Grasshopper definition? So that I could purposely declare a class and then instantiate it in other GhPython components? Just curious ...
Hi Benjamin,
you can pass class definitions and use them, sure. Or add them to the sticky variable, for example:
import scriptcontext
scriptcontext.sticky["someName"] = someThing
Attached a slightly larger example.
- Giulio
_______________
giulio@mcneel.com
so 'sticky' is a dictionary that I can toss variables into! nice! Thank you
Hi Benjamin,
any object that is .Net enumerable will be rendered as a "Grasshopper list" in Grasshopper.
Items exposed in Grasshopper will be the iteration elements. The fact that dict shows this behavior means that if you iterate it, it will give all its keys.
- Giulio
_______________
giulio@mcneel.com
Thanks Giulio
How might I override the IEnumerable interface to define this relationship for new classes? Can I just use __iter__(self)?
You normally would inherit from it. I haven't tried but please let me know if you need a sample. The easiest thing, though, is probably just to pass a list or a tuple, as they already implement that interface so they are already treated as "Grasshopper lists".
- Giulio
_______________
giulio@mcneel.com
Dear Giulio,
I'm trying to call GH components from python, to see if it is possible running in parallel cores, the operations for "mesh¦ray" intersection operations. I don't know how to get their names and add them to the code. Is it any way I could get them??
I'm starting into this thing of programming in python, and I think you call them "Class" or "Kernel" just correct me if I'm wrong.
Thanks
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