2
$\begingroup$

If I understand the way it works, accessing a property by string (data["propname"] = 0) requires the system to search for it. Iterating over all of the properties in data, comparing the id-string until the correct one is found. But what about when we are already iterating over every property?

for key in a.keys():
    a[key] = b[key]

This type of situation seems like it would be very inefficient. Especially if every access requires a search. Even if some type of hashing is automatically kicking in, it seems pretty wasteful. Is it possible to do this in a way that makes more sense? Some type of property iterator? Or any type of index or reference to the property that relates to the next one in the same structure?

If not, would it be possible to build any type of lookup table or helper utility for a situation where you need to iterate the same set of properties for many objects?

$\endgroup$
3
  • 2
    $\begingroup$ IMO code example adds to confusion re what you are asking? eg what is b does it have same keys as a... is it meant to be the other way around b[key] = a[key] ?wonder if using the key value pairs of items() iterator, eg for key, value in a.items(): might be of use $\endgroup$
    – batFINGER
    Commented Oct 26, 2019 at 5:13
  • $\begingroup$ a and b were meant to be identical objects. Yeah, you're right. Using values instead of keys would help a little. What I'm hoping for is some way to iterate properties without using string searches for each access. $\endgroup$
    – Robert
    Commented Oct 26, 2019 at 12:10
  • $\begingroup$ Actually, iterating over the values may help more than a little if I change some things around a bit. Thanks $\endgroup$
    – Robert
    Commented Oct 26, 2019 at 12:20

1 Answer 1

3
$\begingroup$

It is correct that accessing values by key using .get() on bpy.types.struct iterates through the elements. The function that gets called through the Python API is pyrna_struct_get() (bpy_rna.c). It uses IDP_GetPropertyFromGroup() (idprop.c) which in turn calls BLI_findstring(listbase.c). As the name already suggests it performs string comparisons by iterating through a list.

It should be avoided iterating over the keys and then accessing the values, both in Blender and also Python software that uses actual dictionaries as data structure. As batFINGER suggested you could use a.items() instead.

for key, value in a.items():
    # do something with either key or value
    pass
$\endgroup$

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .