How to get Document User Data from Block Instances

Hey There,

I have some Document User Text embedded in some blocks. I’m using these blocks in a project, and I’m wondering if it is possible to extract this data when they are used in a project, attaching this data to the block instance’s Attribute User Text fields?

Cheers, Otto.

Is this in Rhino 8? Do you use Grasshopper?

Yep this is in Rhino 8, I can use grasshopper, but would prefer to use Python if possible! I’ve tried so many methods and I can only seem to get the document text for the document the block is inserted into, not in the actual block itself.

I think some others may have to pipe in here to see what is possible. Once the data is attached to the block, it is not document text anymore, but user text on the definition in the block table. Does this thread help a bit with Python: Can you add User Text to Block Definition?

As for Grasshopper, this is the method to import and read the block definition data (Originally Document data on the external files):

The Block definition userdata sits on the block definition in the block table.

This is a bit of a stretch, but here are is a bunch of code to walk around in the block table in Python: rhinopython/scripts/rhinoscript/block.py at master · stgeorges/rhinopython · GitHub

And this is a great thread on walking through the block table: From Python into Rhino Common

And what is on a Block definition object: https://developer.rhino3d.com/api/rhinocommon/rhino.docobjects.instancedefinition

I am also trying to put a sample together with Python, but still working on it.

Sorry, took me a bit to get back to this. Here is some code using Python to get to the Key/Values from the block definition. This data was originally created by importing the Blocks into this file. And Document data is now on the definition:

import Rhino

def DumpInstanceDefinitinUserText():
    filter = Rhino.DocObjects.ObjectType.InstanceReference
    rc, objref = Rhino.Input.RhinoGet.GetOneObject("Select block instance", False, filter)
    if not objref or rc != Rhino.Commands.Result.Success:
        return
    
    iref = objref.Object()
    if not isinstance(iref, Rhino.DocObjects.InstanceObject):
        return
    
    idef = iref.InstanceDefinition
    if not idef:
        return
        
    # https://learn.microsoft.com/en-us/dotnet/api/system.collections.specialized.namevaluecollection
    dict = idef.GetUserStrings()
    for Ustrings in range(dict.Count):
        print(dict.GetKey(Ustrings) + " contains " + dict.Get(Ustrings))
    
DumpInstanceDefinitinUserText()

udatatest.3dm (365.2 KB)

1 Like