1
$\begingroup$

It seems difficult to find any information about enum-flag usage and workflow. I'm familiar with bitflags and related operations, but the blender/python specifics are fun to figure out.

Is there any clever (or possibly obvious in my case) mechanism to convert enum flags into a string? It is primarily for debugging, but user friendly would be a plus.

The way enum properties are defined in Blender makes me wonder if the flags are not already defined as a string in some way.

Edit: For example, my flags are defined like this:

FBXG_Mode=[
            # mode flags
            ('GA', "Asset", "...", 1),
            ('LP', "Low", "...", 2),
            ('HP', "High", "...", 4),

            # system flags
            ('EXCLUDE',"","",8),
            ('RENAMED',"","",16),
            ('DELETE',"","",32),
            ]

And it seems we can check for flags using strings like this..

if 'GA' in MyFlags:

By the way, how exactly does this work? Does the environment process this correctly because it associates the MyFlags variable with the FBXG_Mode list?

I'm very new to Python, so I have a couple questions if anyone knows the answers:

  1. What tells the environment the attribute order of my FBXG_Mode list? Eg, how does it know that I placed the flag names into the second column/entry? Is this just hard-coded somewhere in the Blender or Python environment? Where would I find documentation about this? I'm not sure what this is called.

  2. How do I access an element of FBXG_Mode in a way that allows me to access the name, description, value, etc? Would it be FBXG_Mode[0][1]? I'm looking for something more like FBXG_Mode['GA'].Description, or at least FBXG_Mode['GA'][2]. I would like to avoid the hard-coded indices if possible.

  3. Is it possible to access the strings and/or properties of the FBXG_Mode list using only the MyFlagsvariable after it is assigned or associated with it? Does a flags variable have any type of association after it is assigned, or is the association only available during the assignment?

  4. Is there any quick/automatic way to extract the toggled flags of a variable in string form, such as for printing text? This seems like something a generic function could be written to do, so I'm wondering if one already exists? Especially since Blender can generate UI controls basically the same way.

Thanks for any help!

$\endgroup$
0
$\begingroup$

It is sometime later after posting this question, and I have learned a lot about how Blender properties work. Here are the nuts and bolts that should answer my older question. Below, instance represents a scene element or property group, and Prop represents any property defined for the instance.

  • All Blender properties are stored in memory as two different value types - complex and basic. Some properties (like int) are the same type, but are still stored in two locations. Some others (like enum flags) are stored as very different types in each location.
  • Using syntax instance.Prop (same as getattr(instance,"Prop") or setattr(instance,"Prop",*)) reads and/or writes the property using the complex value types.
  • Using syntax instance["Prop"] reads and/or writes the property using basic value types.
  • Assigning either version of the two property value types will instantly and automatically update the other version to the same value of its respective type. You can literally assign a set of strings to the complex version of an enum flags property and, on the very next line, read the basic version of it to convert complex -> basic. Or do the opposite to convert the other way.
  • Be careful when reading the basic version of any property (syntax instance["Prop"]). It will not be generated for a specific instance (eg, property group) until the property is assigned for the first time (either type version). You can check to see if the basic version of a property has been generated by using if "Prop" in instance: or if "Prop" in instance.keys():. You can also use instance.get("Prop",DEFAULT_BASIC_VALUE) to safely request the basic version without knowing if it has been assigned yet.
  • The basic version of an enum flags property (instance["MyFlags"]) is stored as an int, which represents all of the binary flag values ORed together. The complex version of an enum flags property (instance.MyFlags) is represented as a set of strings, where each string represents the bit or mask you assigned to it.
  • To convert an enum flags property into a string, you can literally just convert the complex value into a string: str( instance.MyFlags )
$\endgroup$

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.