1
$\begingroup$

I'm wanting to iterate properties and get/set values. Example code:

for prop in self.rna_type.properties.keys():

    # get property name
    name = str( prop )

    # skip properties if first ident character is lower case (auto added properties)
    if name[0].islower():
        continue

    # assign new value
    self[name] = value

Edit note: There was no need for me to convert prop into a string with str() at the top of this loop. All elements inside of keys() are identifier strings.

$\endgroup$
3
  • $\begingroup$ If I'm understanding you right, you could simply iterate through all properties and check if their values are of type bool. If they are, then set them to the desired value. $\endgroup$ – Robert Gützkow Sep 22 '19 at 14:40
  • $\begingroup$ @rjg: Yeah, that's what I'm after. I'm just not sure how to use that object/list. I'm currently trying to use dir() to figure it out, but looking for any type of information about dealing with them. Primarily what each member variable represents. $\endgroup$ – Robert Sep 22 '19 at 17:11
  • $\begingroup$ Annotations? Suggested here: blender.stackexchange.com/a/152816/31447 $\endgroup$ – brockmann Sep 22 '19 at 17:57
1
$\begingroup$

You can iterate through the annotations to get and set each prop.

>>> settings = C.scene.my_addon
>>> settings.__annotations__.keys()
>>> for p in settings.__annotations__.keys():
...     getattr(settings, p)
...     setattr(settings, p, True)
$\endgroup$
2
  • $\begingroup$ Think I'm finally starting to understand how some of this works. So keys() is just a list of property identifier strings. values() is a list property values, and items() is sort of a combination of both. When you showed me __annotations__ before, I assumed it was some component specific to Blender's preference class. A specific nested type. That's on me, sorry. Also appreciate the usage example. $\endgroup$ – Robert Sep 22 '19 at 22:44
  • $\begingroup$ Glad I could help @Robert Yep, keys() returns the keys and values() the values of an usual key-value pair (aka dict). $\endgroup$ – brockmann Sep 23 '19 at 20:24

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.