View properties - Issue

Hi guys, if anyone can explain to me what’s going on, I’d appreciate.

I made a view on which I added an object property x, which has an integer member named y.
Then I added a button with the following onClick script:

custom = self.view.custom
x = custom.x
x.y = x.y + 1 
system.perspective.print('x.y: %s' % x.y)
system.perspective.print('custom.x.y: %s' % custom.x.y)

And clicking on the button gives me this…

In other words, after incrementing x.y:

  • x.y returns the previous value.
  • custom.x.y returns the current value.

I don’t get it :s

You are not setting the value back to the actual parameter and are just referencing your local variables. You have to remember that you are not actually getting a reference the custom property when setting your variable to self.view.custom, you are just copying it’s value into a new variable. To get the functionality you desire, do something like the following:

x = self.view.custom.x #Get the value currently in the property 
y = x['y'] #or x.y but I like json notation
y += 1 #Increment your counter
self.view.custom.x.y = y #set the actual property to the new value

What’s troubling me is that assigning y via the x copy will increment self.view.custom.x.y, but not the x copy. As if there’s a redirection on the copy, or the copy is read-only ?

In your example you were never incrementing self.view.custom.x.y at all. You had written:

custom = self.view.custom

Which is creating a copy of the custom property object.

You then had

x = custom.x

which is now a new separate variable, independent of “custom”

When you did

x.y = x.y + 1

you now successfully incremented object y of object x. Since “custom” is still a completely separate variable, the object custom.x.y is still the original value.

In general never assign the entirety of the custom properties to a single variable inside a script since that can get confusing fast. Try to assign each custom property object to it’s own variable:

x = self.view.custom.x

then perform any operation on the object and it’s children

x.y = x.y + 1

Then update the custom property by setting the value to your object

self.view.custom.x = x

Well it’s somehow incrementing the real thing, see the output when I click on the button repeatedly.

I agree I’ll avoid it now that I know the pitfalls. It’s still a strange behavior to me, and I can’t explain to myself how the copy affects the real property but not itself.

It’s basically just a bug.
There’s a lot of smoke and mirrors happening so that these things pretend to be native Python data structures. When you rebind them, that (currently causes this weird behavior.