Grasshopper

algorithmic modeling for Rhino

Is there a method to script user text attached to a Grasshopper document which is retrievable by a key value? I'm basically looking for something like that which can currently be done within a Rhino document:

def SetDocumentUserText(key, value=None):
    """Sets or removes user text stored in the document
    Parameters:
        key = key name to set
        value[opt] = The string value to set. If omitted the key/value
            pair specified by key will be deleted
    Returns:
        True or False indicating success
    """
    if value: scriptcontext.doc.Strings.SetString(key,value)
    else: scriptcontext.doc.Strings.Delete(key)
    return True
The intention is to be able to write custom metadata elements (in addition to existing field like "Description" or "Copyright") to a Grasshopper file. I'm able to reference this volatile data from a document (e.g. rhinoDependency = rh.RhinoDoc.ActiveDoc.Path) but I'm not certain of a way to internalize this data to the document. In this example the key would be "rhinoDependency" and the value would be the string returned by rh.RhinoDoc.ActiveDoc.Path.
Thanks!

Views: 2997

Replies to This Discussion

Hi Brian,

What do you mean by writing the data to Grasshopper file?

In case you want to change settings, they are get<>set which means you can simply set the value. Give Grasshopper.CentralSettings a try. Something like this:

import Grasshopper.CentralSettings as settings

settings.AuthorAddress = "Brooklyn"
settings.AuthorName = "Brian"

Depending on the data type (i.e. strings) you could use the GH_Document.Properties property for persistent metadata. Not sure if that is what it is meant for, but it seems to work. See attached definition. Also, credits to Mostapha for figuring out how to get the Grasshopper document using Python a while back :)

Attachments:

Added a version where the "metadata" is converted to a dictionary (Pythons key/value data type).

Attachments:

Thanks Mostapha and Anders!

I'm pretty familiar with reading metadata but it seems like I could append multiple strings together representing difference document property fields, write them to an existing property field such as description, and then call them individually using Anders' dictionary method.

So here's the catch: I don't want to overwrite any existing property fields so writing to any available fields in Grasshopper.CentralSettings is hypothetically off limits. I want to define my own custom fields (which of course do not need to display in the Grasshopper Settings GUI but I should be able to call nonetheless). I didn't think it would be possible to create something like Grasshopper.CentralSettings.BriansEsotericDataNeeds which is why I was wondering if Grasshopper had any place to store generic metadata (all strings in my case) like Rhino's "SetDocumentUserText" command.

RSS

About

Translate

Search

Videos

  • Add Videos
  • View All

© 2024   Created by Scott Davidson.   Powered by

Badges  |  Report an Issue  |  Terms of Service