GetUserText

Having an issue with either GetUserText, or SetUserText, but not sure which.

If i try to print the usertext by Key, i get nothing, but if leave the key out, my user-text prints just fine. what am i doing wrong here?

import rhinoscriptsyntax as rs
   
def getset_material(object):
   if object:
       mat_label = rs.IsUserText(object)
       if mat_label != 1: 
            material = rs.GetString (message="Please set a material for this \ 
            object (i.e. hardwood, oak, etc.)", defaultString=None, strings=None)

            rs.SetUserText(object, "Material", material, attach_to_geometry=False)
   else:
        print rs.GetUserText (object, "Material")  #returns nothing
        print rs.GetUserText (object) #returns ['oak']


base_geo = rs.GetObjects ( message=None, filter=1073741840, group=False, \ 
preselect=True, select=True, objects=None, minimum_count=1, \ 
maximum_count=1, custom_filter=None )


getset_material(base_geo)

Hi Adam,

This seems to work here:

import rhinoscriptsyntax as rs
import random

def GetRandomWoodType():
    wood = ['Ash', 'Birch', 'Cherry', 'Maple', 'Oak', 'Pine', 'Walnut']
    return random.choice(wood)

def GetSetWoodType(object_id):
    if object_id:
        key = "WoodType"
        if 1 != rs.IsUserText(object_id):
            value = GetRandomWoodType()
            rs.SetUserText(object_id, key, value, False)
        print rs.GetUserText(object_id, key, False)
        
object_ids = rs.AllObjects()
for object_id in object_ids: 
    GetSetWoodType(object_id)

Does this help?

In looking at your code:

1.) rs.GetObjects returns a list of of object ids, not one. Thus getset_material(base_geo) is passing a list to getset_material which is expecting just one.

2.) Watch your indenting - Python is very particular about this.

This is great Dale, thanks a bunch.

I’m pretty sure the issue was with the list return on GetObjects. I do intend do set this up to iterate through lists of objects eventually, so i want to keep it that way.

calling
getset_material(base_geo[0]) solves the problem.

i think my indent issues are more to do with sucking at forums than python :P.

here’s my new working code. Thanks again.

import rhinoscriptsyntax as rs
   
    
def getset_material(object_id):

    if object_id:
            mat_label = rs.IsUserText(object_id)
            if mat_label != 1:
                value = raw_input("Please set a material for this obect (i.e. hardwood, oak, etc.)")
                key = "Material"
                rs.SetUserText(object_id, key, value)

            else:
                print rs.GetUserText (object_id, "Material")  #returns nothing


base_geo = rs.GetObjects ( message=None, filter=1073741840, group=False, preselect=True, select=True, objects=None, minimum_count=1, maximum_count=1, custom_filter=None )

getset_material(base_geo[0])