Is there any Alias/script to import keys and values

  • in my workflow, I want to assign properties quickly to the newly created object either by any create or copy command or whatever way, for that rhino provides keys and values under attribute user text with options like add, import & match. we can also use layers but we cant create layers for every object.

  • I want to prepare some csv file so i dont have to type keys and values each time for every project. Although, I can manually switch panel from properties to Attribute user text and then press the import button but doing this every time for each object is not efficient, i want to bind it to some alias or Keyboard shortcut so i can use it quickly.


    Now, i want to know if there is any command/script that can do it.

  • Also, if user imported csv file which have exactly same key-value like below
    image
    then it should just ignore them instead of reapeating them. it make sense if either key/value is same but value/key is different then do numbering like item (1).

I currently do this sort of thing in Grasshopper using Modify Attributes in the Elefront Plugin. In Rhino 8, this capability will be available through the new native components for modifying rhino objects.

There’s (almost) definitely a way to script this, but that’s out of my wheelhouse.

but opening grashopper each time is not a solution, grashopper is useful when you complete whole project then adding properties one by one. but i want to do it instantly after its creation so i can use different selection methods in my next commands.

Well, as I said, scripting is a bit out of my wheelhouse, but fortunately ChatGPT has a pretty good handle on it, and with a bit of debugging I got to the following:

! _NoEcho _-Runscript (
'Written by ChatGPT using gpt4
'With a bit of coaxing by Jo Kamm
Option Explicit

Call AssignUserTextFromCSV()

Sub AssignUserTextFromCSV()
    Dim objs, fd, fso, file, reader, line, key, value, obj

    ' Get currently selected objects or prompt user to select objects
    objs = Rhino.SelectedObjects()
    If IsNull(objs) Then
        objs = Rhino.GetObject("Select objects to assign UserText", , True, True)
        If IsNull(objs) Then Exit Sub
    End If

    ' Ensure objs is an array (in case only a single object was selected)
    If Not IsArray(objs) Then objs = Array(objs)
    
    ' Prompt user for CSV file
    fd = Rhino.OpenFileName("Open", "CSV Files (*.csv)|*.csv||")
    If IsNull(fd) Then Exit Sub
    
    ' Create a FileSystemObject
    Set fso = CreateObject("Scripting.FileSystemObject")
    
    ' Open the CSV file for reading
    Set file = fso.OpenTextFile(fd, 1)  ' 1 = ForReading
    
    Do Until file.AtEndOfStream
        line = file.ReadLine
        reader = Split(line, ",")
        If UBound(reader) <> 1 Then Exit Sub
        
        key = Trim(reader(0))
        value = Trim(reader(1))
        
        ' Assign key-value pairs to UserText of each object
        For Each obj In objs
            Rhino.SetUserText obj, key, value
        Next
    Loop
    
    file.Close
    Set file = Nothing
    Set fso = Nothing
End Sub
)

Paste this into the Command portion of a new button, and it seems to work. No guarantees though - it might have some poor exception handling if it gets something it’s not expecting.

thanks buddy for giving time, i will try it.
is it in python ?

While you’re at it, you can add a companion script to the RMB Click for the button as a shortcut to Export UserText.

! _NoEcho _-Runscript (
'Written by ChatGPT using gpt4
'With a bit of coaxing by Jo Kamm
Option Explicit

Call ExportUserTextToCSV()

Sub ExportUserTextToCSV()
    Dim obj, fd, fso, file, keys, i, key, value

    ' Prompt user to select a single object
    obj = Rhino.GetObject("Select object to export UserText", , True, False)
    If IsNull(obj) Then Exit Sub
    
    ' Prompt user for CSV file
    fd = Rhino.SaveFileName("Save", "CSV Files (*.csv)|*.csv||")
    If IsNull(fd) Then Exit Sub
    
    ' Create a FileSystemObject
    Set fso = CreateObject("Scripting.FileSystemObject")
    
    ' Open the CSV file for writing
    Set file = fso.OpenTextFile(fd, 2, True)  ' 2 = ForWriting, True = create if doesn't exist
    
    ' Get the keys of the UserText
    keys = Rhino.GetUserText(obj)
    If IsNull(keys) Then
        Rhino.Print "No UserText found."
        Exit Sub
    End If
    
    ' Write each key-value pair to the CSV file
    For i = 0 To UBound(keys)
        key = keys(i)
        value = Rhino.GetUserText(obj, key)
        file.WriteLine key & "," & value
    Next
    
    file.Close
    Set file = Nothing
    Set fso = Nothing
End Sub
)

Still not quite a Command or Alias, like you asked, but that’s only a step away

Nope, RhinoScript, so it will run directly in a button.


But it should be possible to pull out the Rhinoscript, save that out and turn it into a command without too much fuss. Haven’t done that in a while so it would probably take a bit to figure out again.

yes it is exactly doing what i am intended,
can we make alias for that or open any specific folder.
is there any difference between GPT 3 and 4

yes, gpt4 is several orders of magnitude “larger” than gpt3 from a parameter perspective. Not sure about gpt 3.5. Functionally, that seems to make gpt4 better at following instructions.

Currently, it seems to revert to the last folder that was opened due to windows functionality.

There could be a way to set a default folder to open (perhaps a command-line option that either uses the most recent or allows that to change). Then, if it’s made into a command, the command line options could be automated to custom needs (including a specific folder) using a macro.

your commad works fine and maybe someone will suggest to do that specific folder option.

well, here’s a plugin with the two commands as they currently stand so that they can at least be added to an Alias. It would take not much more to add the folder option…
CSVimport.rhp (10 KB)

1 Like

what is the command to call the functions inside this CSVimport.rhp

ImportUserTextCSV
ExportUserTextCSV
You can find the names of any command in a plugin through the plugin manager, too.

yes i got it. do you have any previous background in coding or that can be possible with gpt and have understanding of rhino working.

the cool thing is that, it even discard repeated values. very thankful for your effort, even making script.

1 Like

Thanks - this was a fun exercise!
If you feel this is sufficiently resolved, you can mark the message with the plug-in as “Solution”

do you have any previous background in coding or that can be possible with gpt and have understanding of rhino working.

Yes, I have some experience coding. GPT is good but not great at coding in my experience. Look to some of the other conversations ppl are having about it here: