How to select a variable by input ?

More of a Python problem than a Blender problem, really…

I have a group of objects in Blender. Each has many different properties (about 50) stored in a text file. I can select objects based on their properties without problems. However, what I really want to do is allow the user to choose which property they want to select by. The important bit of the code is :


for i in range(1,889):     #888 lines in file
   line = infile.readline()
   Object,vcc,alfa,delta,mag,a,b,vel,dist,type.... = line.split()
   if float(type) >= float(Textbox2.val):
      for ob in gObjects:
          if ob.name.startswith(str(Object)):
            ob.select(1)

Where Textbox2.val is the value of, in this case, “type” to select the object by. gObjects is a list of all objects in Blender. I’ve tried replacing “float(type)” with “float(Parameter)” where Parameter is a string from another textbox, but I get “invalid literal for float(): type” as an error. Maybe there’s a better way to do this ?

Is Textbox2 a string button?

Also, ‘type’ is an inbuilt python function, so there could be a namespace clash (but I think your local variable overrides it).

How I would do this is have a button with string and then some switch statements (if, elif, else) to determine what they want to change it by and execute the correct code based on that (A menu may be better for this than a textbox). Inside the switch statements you could have a certain value stored that y


#Scenario One
myVal = 0
if Textbox2.val == 'Object':  #could be a better more case insensitive way of doing this
     myVal = 1
elif Textbox2.val == 'a':
     myVal = 2
...
Later
...
if myVal == 1:
     Execute Code for Object
...etc.

#Scenario Two
if Textbox2.val == 'Object'
     Execute Code for Object
elif Textbox2.val == 'a'
     Execute Code for a
...etc.

#Scenario Three Use a Menu and choose one of the above methods (this is probably the best method, actually)
#See the API for seeing how create a menu

if menu.val == 1:
     do code
...etc. etc. ad nauseum

Yes, Textbox2 is a string. I can change type to something else, no problem there.

If possible, I’d prefer to avoid using an if statement for every condition, it would be rather unwieldly with over 40 choices. Also, I’d like to be able to be able to choose by a subtraction of two values (e.g. select everything with a - b greater than 0.1). This would lead to many hundreds of possible combinations. I could always just change the script, but it would be more convienient if I could simply type in the parameters from the GUI.

What about this?


propertynames = ("Object","vcc","alfa","delta","mag","a","b","vel","dist","type", ...)
selectionproperty = 9  #selecting for "type"
for i in range(1,889):     #888 lines in file
   line = infile.readline()
   properties = line.split()
   if float(properties[selectionproperty]) >= float(Textbox2.val):
      for ob in gObjects:
          if ob.name.startswith(str(properties[0])):
            ob.select(1)

Then make another button that controls the value of selectionproperty.
Also, “propertynames[selectionproperty]” will return the name of the property that you are selecting for.

As for your second problem, this might do:


def filter1(a,b):
  return float(a) >= float(b)
def filter2(a,b):
  return float(a) - float(b) >= 0.1

propertynames = ("Object","vcc","alfa","delta","mag","a","b","vel","dist","type", ...)
selectionproperty = 9  #selecting for "type"

filters = (filter1, filter2)
currfilter = 0

for i in range(1,889):     #888 lines in file
   line = infile.readline()
   properties = line.split()
   filterfunc = filters[currfilter]
   if filterfunc(properties[selectionproperty], Textbox2.val):
      for ob in gObjects:
          if ob.name.startswith(str(properties[0])):
            ob.select(1)


and add a button to control currfilter

Sjoerd, that seems just what I’m after. I’ll give it a go. Thanks !