0
$\begingroup$

I'm building a module that contains only functions, which I named functions.py, it's in the same directory as the addon's __init__.py file. So I have various solutions:

Solution 1:

from . import functions

Now I can call the functions contained in functions.py module like so:

functions.my_function_one()
functions.my_function_two()
#Etc.

Solution 2:

from .functions import *

Now I can call functions like this:

my_function_one()
my_function_two()
#Etc.

I particularly like the use of the second solution (But I don't know which is the best idea)

Anyway the question is: when I run bpy.ops.script.reload() Changes made inside functions.py module will not be reloaded, Only by restarting Blender will these take effect.

So I'd like to understand how to go about solving Solution 1 and 2

I want to clarify that I ask this, as it is quite frustrating to have to restart the blender at every minimum change of the modules.

$\endgroup$
5
  • 1
    $\begingroup$ from xxx import * isn't recommended, can lead to problems down the road. you could use import functions as f to make the first solution shorter to write $\endgroup$ – wilks Sep 29 '20 at 10:03
  • $\begingroup$ Hello @wilks I really appreciate the comment, and would love to know why you suggest this. I still don't have the skills to fully understand this (Unfortunately) $\endgroup$ – Noob Cat Sep 29 '20 at 12:11
  • 1
    $\begingroup$ Importing * in Python. | Medium. This post explained well, basically it's fine if you know exactly what is imported inside your code. But most of the time, write out them explicitly will be better since most of the packages/modules you used are not written by you. And also, after a month, you will forget what you have written in your functions.py. $\endgroup$ – HikariTW Sep 29 '20 at 12:58
  • $\begingroup$ @HikariTW Sounds cool, you convinced me to avoid using solution 2, this makes a lot of sense. $\endgroup$ – Noob Cat Sep 29 '20 at 14:26
  • 1
    $\begingroup$ blender.stackexchange.com/a/158787/15543 $\endgroup$ – batFINGER Oct 8 '20 at 22:21
2
$\begingroup$

It is not that bad to use the second method if you're not collaborate and distribute your project with other. In other words, personal usage is fine if you do take care of naming confliction.

The problem when you reload script is a common mistake that you assume that all object you imported from that module is reloaded by the reload method (imp.reload()). Instead reload all relative script, Python will only reload the required script, which is your module, not the * objects.

You can some how use Python built-in reload function to specify the updated script that you want to use and reimport * again:

from imp import reload
reload(function)  # bpy.ops.script.reload() seems like a sugar macro that handle the dependencies

from functions import *
# This is required since you don't reload e.g. `foo()` and `bar()` in your functions

And also if you want to use Method 1 for correctness:

from . import functions as fn
fn.foo('bar')

or

# You should take care about relative import path issue
from ./functions import (
    foo,
    bar,
    a_very_loooong_name_function,
)
foo("cool")
bar("hello")
$\endgroup$
2
  • $\begingroup$ each time you reload any module, the from import objects need to be reassign again to update its content $\endgroup$ – HikariTW Sep 29 '20 at 10:52
  • $\begingroup$ Ok,sounds cool, but I don't understand how I have to organize, at the beginning of the script, this, so that I can reload everything using bpy.ops.script.reload() . I decided to use my solution 1, as it actually seems much more reliable, so as not to create possible conflicts of variables. $\endgroup$ – Noob Cat Sep 30 '20 at 11:19

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.