Deleting UserText Keys and data from a script

Hi,
I’m a bit stuck I’m trying to clear objects of any keys and data associated through a script.
I’ve tried to use the DeleteObjectData method frome Rhino Script but the keys are not deleted. ( This method is not available in Python (Rhino 5.0)

Option Explicit
Call Main()
Sub Main()
	Dim arrlistObj,strObject
	
	arrlistObj = Rhino.GetObjects("SelectObjects")
	For Each strObject In arrlistObj
		If Not IsNull(strObject) Then
			Call Rhino.DeleteObjectData(strObject)
		End If
	Next 
End Sub

I’ve also tried using the command option suggested by Helvetosaur and Pacal, setting the key to “” in a Python script. This does not work either.

import rhinoscriptsyntax as rs

listObj = rs.GetObjects("Select objects")
for obj in listObj:
    listKeys = rs.GetUserText(obj)
    if listKeys:
        for key in listKeys:
            rs.SelectObject(obj)
            empty = '""'
            strDel = "_SetUserText "+key+" "+empty
            rs.Command(strDel)
        rs.Command("_SelNone")

If you have any suggestions or if you can see what I’m doing wrong here.
Cheers
Phil

@philshapiro, please post a file so we can check if your user text is applied as attribute or geometry type.

Do you want to do it in Rhino, RhinoScript or Python ?

_
c.

Thanks for the reply, I have not found out where to place a Rhino File, the data is user Text on objects not attributes.
And I would rather have a python solution.
Cheers
Phil

When typing a reply either drag&drop the file into the text editor of the reply or use the upload button:

image

Hi Phil,

This should work, but I haven’t tested it thoroughly… Removes both attribute and object texts…

"""Removes attribute and/or object user text from selected objects.
Pass optional False arguments if you want to retain one or the other.
Script by Mitch Heynick 29.01.18"""

import rhinoscriptsyntax as rs
import scriptcontext as sc
import Rhino

def RemoveUserObjAndAttrText(rem_attr_txt=True,rem_obj_txt=True):
    objIDs=rs.GetObjects("Select objects to remove user text",preselect=True)
    if not objIDs: return
    
    for objID in objIDs:
        obj=sc.doc.Objects.Find(objID)
        if rem_attr_txt:
            obj.Attributes.DeleteAllUserStrings()
        if rem_obj_txt:
            user_strings=obj.Geometry.GetUserStrings()
            for item in user_strings: obj.Geometry.SetUserString(item,"")
RemoveUserObjAndAttrText()

Edit: added option to delete attribute or object text separately…
Let me know how it works… --Mitch

Thanks

Hi Helvetosaur, (Mitch ;o)
Thanks, I tried after commenting out the attributes section since that crashed the script, but unfortunately it doen’t work on my objects. I’m uploading a sample.
Cheers
PhilTestUserText.3dm (132.5 KB)

@philshapiro, try to delete all attribute user text with this:

import rhinoscriptsyntax as rs

def DoSomething():
    
    id = rs.GetObject("Object", 0, True, True)
    if not id: return
    
    keys = rs.GetUserText(id, None, False)
    for key in keys:
        value = rs.GetUserText(id, key, False)
        rs.SetUserText(id, key, None, False)
        
DoSomething()

_
c.

Hi Phil,

Hmmm, I am seeing something different.

First off, the reason the attributes part is throwing an error is probably because Attributes.DeleteAllUserStrings() doesn’t exist in RhinoCommon for V5 - I am testing in V6… I added a section for V5 in the revised script below.

No need to comment anything out, in the last parentheses just put:

RemoveUserObjAndAttrText(False,True) if you want to delete just object text
or
RemoveUserObjAndAttrText(True,False) if you want to delete just attribute text

or no argument if you want to delete both…

Second, I don’t see any object user text on your 3 box objects, just attribute text:

Command: GetUserText
Text key <All keys>:
2 attributes user strings.
  <TYPE> BOX.
  <LOCATION> LEFT.

I tried attributing some object user texts using SetUserText and AttachTo=Object ; they seem to be deleted when running the script…

"""Removes attribute and/or object user text from selected objects.
Pass optional False arguments if you want to retain one or the other.
Script by Mitch Heynick 29.01.18
Revised - added V5 switch, Attributes.DeleteAllUserStrings() is only in V6"""

import rhinoscriptsyntax as rs
import scriptcontext as sc
import Rhino

def RemoveUserObjAndAttrText(rem_attr_txt=True,rem_obj_txt=True):
    objIDs=rs.GetObjects("Select objects to remove user text",preselect=True)
    if not objIDs: return
    
    for objID in objIDs:
        obj=sc.doc.Objects.Find(objID)
        if rem_attr_txt:
            if rs.ExeVersion<6:
                attr_strings=obj.Attributes.GetUserStrings()
                if attr_strings:
                    for item in attr_strings:
                        obj.Attributes.SetUserString(item,"")
            else:
                obj.Attributes.DeleteAllUserStrings()
        if rem_obj_txt:
            obj_strings=obj.Geometry.GetUserStrings()
            if obj_strings:
                for item in obj_strings: obj.Geometry.SetUserString(item,"")
RemoveUserObjAndAttrText()