Get the list of InstanceObjects (including nested InstanceObjects)

Initially, I was trying to get the list of InstanceObjects in a document. It seems that there is no such method. So I used a workaround. I started with:

scriptcontext.doc.InstanceDefinitions.GetList(True)

But when I tried to use:

InstanceDefintion.GetReference(1)

it only returned the top level reference. According to the help, passing “1” to the method would get top level and nested references in active document, but it didn’t. Is my understanding of the method correct? Is there any way I can get the immediate InstanceObject for an InstanceDefinition?

I would be extremely grateful for your help. This has driven me crazy…

Here is a RhnoCommon sample that you should be able to port to Python. I uses a recursive function to get nested instance definitions.

https://github.com/dalefugier/SampleCsCommands/blob/master/SampleCsDumpBlockTree.cs

Thanks Dale! I write a python one based on your sample and it works greatly. Initially, I was afraid that exhausting all the objects in a file would be very time-consuming, but it actually takes less than a second!