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.
I'm no expert on FreeCAD so this is something of a guess, but..., it looks like
addPropertytakes a name, type and description for a variable and creates one with a default value for you. The examples show creation / update in one step, like in your example. The problem is what to do when you don't know the name of the variable in advance. In python, you handle "variable attribute names" withgetattrandsetattr. So, if you have a name and a value you can:There are various ways to generate variable names. In your example,
Or maybe you already have them in a dict somewhere
I am really puzzled by the FreeCAD property implementation. The property object should have a single well-known name to access it's value, not some tricky name-of-the-property thing.