(Rhino 7)
Trying to write a python script to make a custom button that will match source object custom user key/value pairs, below is where I am right now, I was able to access and see all existing key/value but wasn’t able to update the modified ones, anything I’m doing wrong here? Thanks in advance:
import Rhino
import scriptcontext as sc
import System.Windows.Forms
def match_all_user_data():
# Prompt the user to select the source object
source_objref = Rhino.Input.RhinoGet.GetOneObject("Select the source object", False, Rhino.DocObjects.ObjectType.AnyObject)
if source_objref[0] != Rhino.Commands.Result.Success:
print("Failed to select source object: ", source_objref[0])
return
print("Source object selected:", source_objref[1])
sc.doc.Objects.UnselectAll()
# Prompt the user to select the target object
target_objref = Rhino.Input.RhinoGet.GetOneObject("Select the target object", False, Rhino.DocObjects.ObjectType.AnyObject)
if target_objref[0] != Rhino.Commands.Result.Success:
print("Failed to select target object: ", target_objref[0])
return
print("Target object selected:", target_objref[1])
# Get the actual Rhino objects from the ObjRef objects
source_obj = source_objref[1].Object()
target_obj = target_objref[1].Object()
# Copy all user text from the source object to the target object
keys = source_obj.Attributes.GetUserStrings().AllKeys
updated = 0
if len(keys) > 0:
for key in keys:
value = source_obj.Attributes.GetUserString(key)
target_obj.Attributes.SetUserString(key, value)
if not sc.doc.Objects.ModifyAttributes(target_objref[1], target_obj.Attributes, False):
print("Failed to update object attributes in the document.")
break
updated += 1
print("Copied ", len(keys), " user text items from source to target.")
sc.doc.Views.Redraw()
System.Windows.Forms.MessageBox.Show("All user text copied successfully from source to target!")
else:
print("No user text found on source object.")
match_all_user_data()