0
$\begingroup$

Not sure if this is a normal python behaviour or a Blender thing

I have a file named operators.py and a file named functions.py. also a __innit__.py that calls the operators.

Method 1 :

in functions.py:

def someFunction(self, context):
    print("I did it!")

in operators.py:

from functions import someFunction

and then later I call the function with:

someFunction(self, context)

The above resulted in:

ImportError: cannot import name 'someFunction'

Method 2 :

in functions.py:

def someFunction(self, context):
    print("I did it!")

in operators.py:

import functions

and then later I call the function with:

functions.someFunction(self, context)

The above resulted in:

AttributeError: module 'functions' has no attribute 'someFunction'

So blender did get and import the module but it can't get the functions working.

$\endgroup$
  • $\begingroup$ How are you running the script? are you opening the files in blender's text editor and running them? or telling blender to run the script from the cli? is the script in the current dir when you start blender? $\endgroup$ – sambler May 30 '18 at 4:39
  • $\begingroup$ opening the files in blender's text editor and running them. Everytime I hv to press run script to on each file to get it up and running. Am I doing it wrong? $\endgroup$ – adrian li May 30 '18 at 11:13
1
$\begingroup$

Your function seems to be in a class (guessed from the self parameter).

Method using self are meant to be used on instances.

You should either instanciate the class it belongs to in order to have an instance on which to call the method, or make the function static, or make the function a module function (out of a class definition).

Also the package init file should be named __init__.py, not __innit__.py.

$\endgroup$
  • $\begingroup$ thanks i think that's the reason. But i though if i call the function from some other classes, as long i called it with (self, context) parameter it will run the same. for example i have to call a self.report() inside the function, how do I achieve that without having the (self) parameter? $\endgroup$ – adrian li May 30 '18 at 11:04
  • $\begingroup$ I have changed the file name to __init__.py. On another matter, when I have multiple .py files in the blender text editor, I must pressrun script on every single one of them in order to get everything up and running, is this normal or am I doing something wrong? $\endgroup$ – adrian li May 30 '18 at 11:06
  • $\begingroup$ I am trying to call the def from another class that also have the (self, context) parameter, the def itself is not written inside a class. is it not ok? $\endgroup$ – adrian li May 30 '18 at 11:15

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.