GUID generation in python

I’m trying to create á GUID for my geometry before i bake it so i can retrieve it later for further operations

I found this very clear example in Visual Basics

However i’m unsure how to get this working in python due to my lack of knowledge of VB

Thanks in advance


If you just need a unique identifier then you can use the uuid module in Python:

import uuid

my_uuid = uuid.uuid4()

I’m not sure how you would then attach it to your geometry…


Here is one way of doing this:

I’m not sure if Grasshopper’s bake action gives you the opportunity to specify Guid. However you can “bake” straight from GhPython.
You need to get to the ObjectTable of the current RhinoDoc, call the appropriate add method such as this one, then provide an ObjectAttribute whose ObjectId property is set to your desired Guid.
Of course remember to switch scriptcontext.doc from ghdoc to ActiveDoc and back. (7.3 KB)

The VB example is to create Guids for other purposes. Rhino takes care of Guid creation for you, and you cannot choose the Guid of the added object. However, once the object is added to the document, Rhino informs you of the Guid value that it assigned to the geometry. Example:

@Will_Wang is showing a good example for this.

Just to be sure: You can choose the Guid of the added object, like i’ve shown in the linked example from an older thread above, by setting the Atrributes.ObjectId property and adding to the RhinoDoc with Attributes.

If the Guid assigned doesn’t really matter and just holding on to it is important, I agree @Will_Wang’s solution is the way to go.

Yes, just make sure that the Guid is not already present. Specs. If you want to code this way, you need to check if the Guid is present.

what will happen if an object with the same Guid is already in ObjectTable?
old one gets bumped? new one never added?

The universe as we know it will suddenly cease to exist as the second GUID tries to materialize in the same space/time as the first, creating a gigantic GUID-AntiGUID explosion…


and no one has done this?
hold my beer…

If the value is not null but it is already used by another object in the model, a new Guid is created.
1 Like