What is the preferred way of handling multiple global variables in blender?

(Heads up I am new(er) to python) Me and someone else I spoke to were both struggling a little bit with multiple global variables in blender. The methods I know about and could find on stack really just didnt work. Blender would just throw back one error or another. Is there a way to do it in blender that you suggest or if there is an alternatively more advisable way of doing it, can you tell me what method you like to use? I am now over 25 variables that are needed all over the place and its just becoming a pain not knowing how to do multiple globals correctly in blender.

I dont know if it works in blender but one method I saw someone use was to wrap the variables in a class and then calling the class in the functions.

It really depends on what you mean by globals and how you use themā€¦

Do they need to be saved? If so, do they need to be saved per blend file? Are their values changed a lot or mostly static?

In my case all entirely static. I am using them mostly to deal with combining strings from text inputs dot whenever I need them itā€™s not bpy.context.scene.property1 + ā€œ/ā€œ + bpy.context.scene.property2 when I know I am going to need it 20 times in different functions. Just doesnā€™t make much sense to keep writing those sorts of things out each time. Itā€™s also much easier with variables to remember what is doing what. Problem is I still have a LOT of them. And, I need 6-15 per function or class, but itā€™s very redundant. Putting all of the variables in all of the classes or functions is just tiring

Iā€™m not sure Iā€™m completely understanding what youā€™re doing. Can you post a couple of examples of what the values of these globals are?

This is the way to do it if you want to store globally accessible values https://docs.blender.org/api/current/bpy.props.html In fact this is the advisable way to do it. These properties are saved with the .blend file except the wm ones which do not store values across sessions but they are good for the current session.

If this is about just typing them over and over again you can do something like

sc=bpy.context.scene

sc.property2
sc.property3 
....

Show some code so that other devs can help out.

1 Like

I guess technically these are constantsā€¦

@kkar That is probably the right way to go then. I dont know how ā€œā€œcorrectā€ā€ it is per se but I started doing it in a package and its getting the job done, but it since I am newer to python, it may be better to just do that instead if for no other reason than best practices if you think that is the right way to go based on the code illustration I have below. Below is an example of what it is and what I want it to look like(ish).

What it is now, and you can see the redundancy that I am talking about that is bothering me.

def first_function_example():
    exampleTwo = some.example #first time we use this var
    exampleSix = some.example #first time we use this var
    exampleSeven = some.example #first time we use this var

    #just for example
    print(exampleTwo + exampleSix + exampleSeven)

def second_function_example():
    exampleTwo = some.example #second time we use this var
    exampleSix = some.example #second time we use this var
    exampleEight = some.example #first time we use this var
    exampleTen = some.example #first time we use this var

    #just for example
    print(exampleTwo + exampleSix + exampleEight + exampleTen)

def third_function_example():
    exampleTwo = some.example #third time we use this var
    exampleSeven = some.example #second time we use this var
    exampleEight = some.example #second time we use this var
    exampleTen = some.example #second time we use this var

    #just for example
    print(exampleTwo + exampleSeven + exampleEight + exampleTen)

You can see I am using the same variable a lot over and over again in different functions and it means the same thing in all of them. Lets just say exampleTwo = 5ā€¦ it will equal 5 in all of these. That is a bad example of one of my actual uses because 5 is shorter than the variable name but in my case they are usually file paths or things like that that take up much more space than just a single word that I can use to call that file path whenever I need it.

This is an example of what I would prefer it look like. Dont pay too much attention to the formatting of the variables up at the top of the page or if I set them global. It is just to illustrate that I want to make them once and use them wherever I need within the file.

#variables located somewhere else
exampleOne = some.example
exampleTwo = some.example
exampleThree = some.example
exampleFour = some.example
exampleFive = some.example
exampleSix = some.example
exampleSeven = some.example
exampleEight = some.example
exampleNine = some.example
exampleTen = some.example
#------------------------------#


def first_function_example():

    #just for example
    print(exampleTwo + exampleSix + exampleSeven)

def second_function_example():

    #just for example
    print(exampleTwo + exampleSix + exampleEight + exampleTen)

def third_function_example():

    #just for example
    print(exampleTwo + exampleSeven + exampleEight + exampleTen)

So I would say that actually the question isnā€™t about globals in blender, but rather how you might want to have these global constants in python. Since as far as I can tell, it doesnā€™t really matter that they are in blender or not?

What I would suggest then is that you make a python module (this can just be a single file). In it you declare all these things and their values. Then when ever you want to use them somewhere you ā€˜import moduleā€™ in that file which will then include them all as module.constant_name in that scope.

This is how it should be done. And how it is done for things storing pi instead of writing it out to how ever many decimal places you want each time, etcā€¦

1 Like

I usually make a file called constants.py that I can import into any of my other files.
One thing to note is that the accepted python convention for naming constants is to use all uppercase characters to name them, so the contents of constants.py usually looks something like this:

ADDON_VERSION = (1,4,0)
MY_CONSTANT = 5
1 Like

Yup already done. I was really afraid that may not have been the right way to go but I was just sorta (ā€¦interjectionā€¦ I come from more of a web dev background, and thatā€™s the extent of my dev experience, and even that I am not an expert in. Just have family who is so I picked up a bit) but I am just used to keeping all my javascript, css, etc, all in their own file and referencing the file when I need it. Python is almost confusing me with how not technical it is and I see so many people taking a lot of liberty with how they arrange their projects as I am trying to learn, I am just like, ok, but what is best practice? Someone referred me to the PEP8 style guide and that has been very useful but sometimes I am just missing the info to point me where to reference in the guide to know that I am looking at the information that is useful to me if that makes sense?

I didnt even realize the are writen differently. Thank you! Yeah now that you pointed it out I did a search for constants in the style guide and I see what you are saying. I (incorrectly) assumed that because they are writen the same way that they would have similar formatting. Guess not haha. Thank you so much!

Ok, so this then is more a question of why you would or wouldnt want to do this. Does it matter at all that I have my constants in one file that I am importing into the __ init __.py file of the directory so I can just ref the directory its in, or I see some people writing it right inside the directories init file. Does it at all really matter? Is there a time where doing it one way or the other makes more sense? I dont really see a difference for the most part but if they have a good reason for doing it right inside the init file I would love to get an idea of what it is.