Assign a Function to an Object Using Userdictionary

Hi All,

I already posted this discussion to grasshopper group but I’m posting it here too per Anders’s suggestion. Hopefully someone here can help me with this issue.

Has anyone successfully assigned a function to an object using userdictionary?

It works for assigning an integer or a geometry but when I assign a function it returns True instead of the function. Here is an example:


I wouldn’t expect this to work with the current implementation of UseDictionary. I’m surprised that GH is interpreting the value as True. I’ll have to see what is going on there.

What are you trying to do with this function? Save it on a curve?

Hi Steve,
I’m actually trying to pass an instance of a class with the object that carries different attributes/properties for that object coming from external analysis.

Hi again Steve,

Is there any work around for this? Almost for all the analysis components that I develop I need to find a way to carry a dictionary of data (or to be more accurate an instance of object with some associated data) between different components and so far I couldn’t find any work around.

Any hope that you add a new type to UseDictionary class so it can also accepts other data types like functions, and classes? That also would be awesome!


The one spot that I’m confused about is that the UserDictionary was built so it can save all of the items in the dictionary with the 3dm file. Instances of random classes and function pointers aren’t going to be something that I know “how” to save with the file.

Thank you for the quick reply. I see your point. That makes sense.

Can you think of any other functionality that I can use for this purpose? Maybe I should save all these information in sticky and call them from sticky using a unique ID which could be tricky in some cases. That’s the best way that I can think of for now unless you can show me a better way.


Hi Mostapha,
Did you ever come up with a solution for this? I may be able to implement something, but don’t want to spend a lot of time/effort on it if you already have a solution.

Hi Steve, Thank you for asking. For now I used the sticky dictionary and got it to work. It will be still useful if you can apply it later at some point. Thanks again. -Mostapha


My workaround in the past involved:

1 - Creating a dictionary where I store certain values for a given object using something like:

d= {}
d[guid] = {“parameter1”: “ABC”, “parameter2”:123, “function1”: “distance([0,0,0],[1,1,1])”}
d = str(d) # create a string representation of the dictionary, in this case if you print it our it would look like “{‘guid’: {‘function1’: ‘distance([0,0,0],[1,1,1])’, ‘parameter1’: ‘ABC’, ‘parameter2’: 123}}”

2 - Now I save my dictionary to the file using setDocumentData (or save it to the object using SetUserText):

rs.SetDocumentData(“MyData”, “d”, d) # save (i.e., serialize) the string representation of our dictionary to the file

3 - Whenever you want to read in the dictionary back again, you just do:

d = rs.GetDocumentData(“MyData”, “d”) # read in the string representation defining our dictionary. If you’ve used SetUserText, the you’ll have to call GetUserText

d = eval(d) # convert our string back to a dictionary

That’s it. Now if you have functions in your object, you just have use the eval again on each serialized function call. In this example you would do: eval(d[“function1”]).

Another workaround would be serialize your object using JSON (


1 Like