Find Correlating Definition Object of An InstanceReference

I’m having difficulty understanding where to find the definition of an instanceReference in Rhino3dm.js
I have a simple scene of a extrusion turned into a block and copied over twice. (see below)

Now trying to parse this file with Rhino3dm.js I get an object table of 3 objects (the original extrusion, and two instanceReferences).

const objecttable = model.objects();
for (var i = 0; i < objecttable.count; i++) {
        var modelObject = objecttable.get(i);

Now my question is how do I find out which instanceReference is pointing to which block definition object? It’s simple in this case since we just have one definition object, but what happens when I have more than one definition objects?

Perhaps by assigning a unique name to each instance?

You can loop over model.instanceDefinitions() to find all the InstanceDefinitions contained in a model.

Looping over model.objects() you will find the InstanceReferences, as you’ve already found. In your case you have two instances of InstanceReference. Both have the same InstanceDefinition. The InstanceDefinition is your blueprint, these are called block instances in Rhino UI. In your model you have two block instances.

InstanceDefinition has getObjectIds(), which will give you a list of object IDs that are part of the InstanceDefinition.

InstanceReference has parentIdefId(), which gives the UUID of the InstanceDefinition it was instanced from. It also has xform(), which gives you the point where the block instance is inserted.

Hope that helps.

1 Like

There’s a findId function on the instance definition table. Pass parentIdefId to this function to get the instance definition that the reference uses.

1 Like

Thanks @stevebaer and @nathanletwory,

I’m sharing the code here for anyone with the same issue in the future:

            let parentId = geometry.parentIdefId;
            let instanceDef = model.instanceDefinitions().findId(parentId);
            let instanceDefIds = instanceDef.getObjectIds();
            instanceDefIds.forEach(id => {
              let instanceDefObj = model.objects().findId(id);
              let geomCopy = instanceDefObj.geometry().duplicate();

I think you need to check object type in your loop in case one of the objects is another instance reference. This happens with nested instances

Good catch @stevebaer,
I made it a recursive function.

1 Like