Grasshopper Component Instance GUID from Duplication

Hi,

I would like to keep track of the InstanceGuid of all instances for a component that I created. I override AddedToDcoument to maintain a list of InstanceGuid . However, when the component is duplicated from an existing component, the InstanceGuid (while within AddedToDocument) is the same as the component it is copied from. I assume a new Guid will be assigned to this component later, but how do I keep track of this new Guid?

Thanks,
Alan

Yep, copying will retain the ID, however when components are pasted (or otherwise imported) into an existing document, the ids will be mutated to avoid collisions. This happens by calling GH_Document.MutateAllIds().

Certain types of object need to be aware of when ids change (primarily groups, because they reference their contents using ID), so there’s a mechanism for getting that information. Make your component implement the IGH_InstanceGuidDependent interface which necessitates you write the following method:

public void InstanceGuidsChanged(SortedDictionary<Guid, Guid> map)
{

}

The dictionary you’re given maps old ids (before the mutation) to new ids (after the mutation). So basically you see whether your old id appears as a key in the map, and then the value tells you what new id you’ve been given. If you need to track it down the other way around then you’ll have to enumerate over all the keyvalue pairs and check values. Slower, but equally exact.

David,

Thanks. This works.

Alan