2
$\begingroup$

I was making a modal operator and I needed some kind of global variable I could use as a "user preference" if you will. Whenever the operator is run, it checks if the value is True or False and runs the necessary code. If it doesn't exist then it creates it with a default value.

I managed by using something like this (an ID property?) which is great because I think it's saved with the scene and not per object:

context.scene["My Property"] = False

My code is already working just fine, no problems there. However, I discovered I can also do this (with optional extras I imagine are useful/necessary for UI Panels):

my_property : BoolProperty(default=False, name..., min... max...)

I just use this property to remember to turn on the wireframe for a selected object while the operator is running, that's it. I don't need to display it in any menu (at least not at the moment) and I don't need to save it in any file; it's just a temporary, non-persistant user preference.

My Question:

Is there's any benefit of one method over the other if I just use it like a global variable?

I've read about Custom vs ID Properties but I'm too much of a layman to understand when to use different things.

$\endgroup$
2
  • $\begingroup$ The first won't work if the current scene is deleted. You'd usually want a collection of properties for your add-on. $\endgroup$ Commented May 14, 2020 at 20:04
  • $\begingroup$ Yes of course but its just a temporary user preference I want to save for that particular session. It just remembers to turn on the wireframe for the selected object while the operator is running (if the var is True). $\endgroup$ Commented May 15, 2020 at 4:00

1 Answer 1

1
$\begingroup$

I decided to do this because it just works:

my_property : BoolProperty(default=False)
...
if button_pres_event: # Not real code
    self.my_property = not self.my_property
    do some stuff

I prefer this over the Scene ID Property I had before because it automatically creates it when the operator runs (no more checking if it exists first) and I can easily give it a starting value.

Basically, it simplifies the code. I'm sure there are other benefits but simpler/easier code is a good enough excuse for me.

$\endgroup$

You must log in to answer this question.

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