[bug-16904]Behavior of perspective custom properties vs [pj]ython data structures

Is it legal to initialize/set perspective custom properties out of a python dictionary? I’ve got mixed results and even seemingly bugs, for example with the following code (put it in a perspective button “onClick” event):

session = self.session
# this *sometimes* sets session.custom.foo to BAR, sometimes leaves it at its previous value
session.custom = {'foo': "BAR"}
# this *sometimes* erases all properties, sometimes leaves foo
session.custom = {}

If what I’m doing isn’t reliable, what’s a good way to bulk set dozens of perspective custom properties starting from a python dictionary or array?

Also, ho can I access a custom property whose name I have in a variable? For example, I have session.custom.foo and session.custom.bar, and x = ‘bar’.
How do I reference session.custom.$x (not valid of course, I’m looking for the right syntax) ?

Try getattr(session.custom, x) and setattr(session.custom, x, newValue).

Thanks, how does this work with complex nested hierarchies? Do I just do getattr(session.custom, 'foo.bar.baz[0].quu') or should I first get baz = session.custom.foo.bar.baz[0] (if that’s the right syntax) and then getattr(baz, 'quu') ?

Python’s getattr function works one level at a time. Supply the deepest constant level as the first argument, with the corresponding next level of dynamic property name. Perform array subscript lookups directly on that level of object.

1 Like

I’ve got it to work more or less, thanks. Still, I don’t seem to be able to completely delete all custom properties; the following all fail for various reasons:

session.custom = {}      # no change at all
setattr(session, 'custom', {})      # no change at all
delattr(session, 'custom')   # object has no attribute "custom"

EDIT: Now, this seems to work to delete all properties:

for prop in session.custom:
    delattr(session.custom, prop)

Still it feels a bit awkward to have to go through all these contortions. It would be much better if just assigning a dictionary worked out of the box (I haven’t found the contrary explicitly stated, and instead found references in this forum suggesting that it should work; yet it doesn’t).

Looking at the code, both of your first examples “should” work - I think there’s a bug in the way the property tree tries to ‘merge’ values between what it already has and what it’s getting assigned. I’ll file a ticket for it.
I wouldn’t expect delattr to work on the top level (an actual scope of custom, props, etc), because you really can’t get to a state where session.custom doesn’t mean anything, so allowing scripting to “fake” that would just be more confusing.

It is important generally to recognize that access to the property tree wrappers is inherently going into a queue - operations from different threads could theoretically put things into that queue at different times, which may account for any odd behavior you initially noticed.

1 Like

Might be, however during debug I had trimmed down the environment to a single perspective window with a single button, and was still getting odd behaviors, so I'd be leaning more towards the bug possibility. Let's see what you find. Thanks!

1 Like

Hi there. Under the category of “better late than never” - we believe this has been fixed. Please let us know if you’re still experiencing this behavior in the latest version (8.1.14)

1 Like