I'm working with Python in FreeCAD and to add properties (adjustable variables) to an object we use:
obj.addProperty("App::PropertyFloat","variable","variable label","variable explanation").variable = 2
Using a for loop I want to add multiple properties with multiple variable names. I read how to convert a string into a variable but I didn't find how to access that variable name. In my case: how to use it at the .variable position in my code above.
Say I have the code:
varName = []
for i in range(3):
varName.append('variable%s' %i)
exec("%s = %d" % (varName[i],2))
I know for example that I can use print variable0 to get the value 2. How can I access the name of variable0 and put it on the .variable position? I read about creating a list or dictionary of variables but you can only access the value of the variable, not its name right?
A list can be the input for this object and its contents should be assigned to the variable names. The output should look like this (but than suitable for any length of the input list):
obj.addProperty("App::PropertyFloat","variable0,"variable label","variable explanation").variable0 = input[0]
obj.addProperty("App::PropertyFloat","variable1,"variable label","variable explanation").variable1 = input[1]
etc.
In FreeCAD dynamic properties added from python are exposed as standart python instance attributes. Therefore you can use the normal hasattr, getattr and setattr etc. Hence your loop could look like this: