Python: Variable names from input strings

Hi everyone,

I’m fairly new to python and I’ve run into an interesting problem; I’ve got an input text file which has many points from multiple systems. Each system must have a different mesh and a different material. The following is an excerpt from the file:

nodes nodal group
x1 y1 z1

353.7 228 -123.8 MN
353.4 231.5 -132.4 MN
348.6 132.6 -149.2 PAuN
377.9 147.4 -149.5 PG&N
375.7 146 -152.9 PG&N
376.8 174.8 -155.4 PG&N
376.75 194 -165.8 IJNu

(note: the same nodal groups can appear at different points in the list, they are in no particular order)

I can read the data in and create the point-cloud just fine, what I am struggling to do is create an array with the name from the nodal group column, and whenever that name comes up, that point can then be appended to the appropriate array.

how do I extract the name from that column, so I can create a variable named MN, PAuN, PG&N, etc…

At the moment I have pre-defined variables with the names described, but in the future, the list will expand to include new nodal groups and I would much rather have the arrays created when the script is run, instead of pre-defining them.

Sorry for making you read a small novel,
Any help will be much appreciated.


Create a dictionary where the keys are your nodal groups and the value of each is a list that holds one ore more points.

A primer on dictionaries:

thank you,

I’d already read this, but I read it again and realized that I could make the variable associated with the key, an array.

setattr do what you are asking, but it won’t work due “&” characters that can not be part of a variable name.

setattr(object, variable_name, value_to_setup)

class Myobject(object):
     def __init__(self):
         self.x = 32

myobj = Myobject()
myobj.x = 35
setattr(myobj, "myFancyVariable", 34.3)
print "fancy variable is ", myobj.myFancyVariable

# You can also can see what variables has an object with:
print "-- obj dictionary --"
for k, v in myobj.__dict__.iteritems():
	print "key %s, value %s" % (k, v)
print "-- end dictionary --"

you also have: getattr(object, ‘myFancyVariable’) to get the value of the variable.

but maybe you want to use blender syntax: if ‘myFancyVariable’ in myobj: do_the_job

I’m not sure if I understand about how to extract variables… I think that it should be something like this:

class Myobject(object):
     def __init__(self):
         self.x = 0.0
         self.y = 0.0 
         self.z = 0.0
         self.varName = ''

line = "353.7 228 -123.8 MN" #whole line readed from file as a string
params = line.strip().split(' ') # return a list
obj = Myobject()
if len(params) > 2: # list start at index zero, so...
         obj.x = float(params[0])
         obj.y = float(params[1])
         obj.z = float(params[2])
         obj.varName = params[3]