Question: how can I make blocks embedded and linked again

Hi,

Context

I have a file with many linked and nested linked blocks. Occasionally the linked blocks refer to the same nested linked blocks as in the diagram below. For keeping versions of my models, I'd like to:
  1. embed all linked blocks and their nested references in the file
  2. save the file
  3. restore the status and path of the previously linked blocks

using python script

Problem

Embedding and restoring the linked blocks changes the InstanceDefinition Table:
  • block names are prefixed with ‘{parent}>{parent.filename} :’ (see screenshots)
  • InstanceDefinition Table counts of referenced blocks are increasing
  • Initially rhino is aware that both linked blocks point to the same ‘shared nested reference’. Afterwards rhino seems to use to different references

Can anyone help me restore the InstanceDefinition Table to the original state after embedding the linked blocks and references?

I use the script and the files below
Main.3dm (66.5 KB)
Linked_A.3dm (50.3 KB)
Linked_B.3dm (51.4 KB)
shared_nested_C.3dm (44.9 KB)


#! python3
import rhinoscriptsyntax as rs
import scriptcontext as sc
import Rhino

def print_bt_status(key=''):

    blocknames = rs.BlockNames()
    refs = list(filter(rs.IsBlockReference, blocknames))
    embedded = list(filter(rs.IsBlockEmbedded, blocknames))
    print(key, len(blocknames), len(refs), len(embedded))


def main():

    try:
        rs.EnableRedraw(False)
        print_bt_status('start')

        # make all linked blocks embedded
        # store all linked blocks that are not references
        linked_but_not_ref = []
        for block_name in rs.BlockNames():

            path = rs.BlockPath(block_name)
            idef = sc.doc.InstanceDefinitions.Find(block_name)

            if idef.UpdateType == Rhino.DocObjects.InstanceDefinitionUpdateType.Linked:
                if not idef.IsReference:
                    linked_but_not_ref.append({'idef':idef, 'path':path, 'name':block_name})

                sc.doc.InstanceDefinitions.ModifySourceArchive(
                    idef.Index,
                    '',
                    Rhino.DocObjects.InstanceDefinitionUpdateType.Embedded,
                    False
                )

        # save file with embedded blocks
        rs.AllObjects(select=True)
        filename = r"C:\Users\info\Downloads\embedded_exports.3dm"
        rs.Command('-Export "%s" _EnterEnd' % filename  )

        # # failed attempt to remove the nested references before reloading
        # for entry in linked_but_not_ref:
        #     path = entry['path']
        #     idef = entry['idef']
        #     sc.doc.InstanceDefinitions.ModifyGeometry(idef.Index, [], [])
        # rs.Command('-Purge EnterEnd')
        
        # reload block contents from file
        for entry in linked_but_not_ref:
            path = entry['path']
            idef = entry['idef']

            sc.doc.InstanceDefinitions.ModifySourceArchive(
                idef.Index,
                path,
                Rhino.DocObjects.InstanceDefinitionUpdateType.Linked,
                False
            )
        print_bt_status('after update')

    finally:
        rs.EnableRedraw(True)

if __name__ == '__main__':               
    main()