Blender3D:Python 脚本问题“ImportError:没有名为 MyModule 的模块”

发布于 2024-12-12 04:53:25 字数 577 浏览 2 评论 0原文

您好,我正在尝试在 Blender3D 脚本中使用多个 python 文件。 (例如 MyScript.py 和 MyModule.py)。 MyScript.py 看起来像:

import bpy
import math
from add_utils import AddObjectHelper, add_object_data
import mathutils

# this line doesn't work
import MyModule

#### REGISTER ####

def register():
    pass

def unregister():
    print("Finished")

if __name__ == '__main__':
    register()

在 Blender3D 中运行脚本,我收到错误:

"ImportError: No module named MyModule"

我已将 Blender3D 的“脚本”文件夹设置为指向包含我的脚本和 MyModule.py 的文件夹。

感谢您的任何帮助。

J

Hi I'm trying to use multiple python files in my Blender3D script. (eg. MyScript.py and MyModule.py). MyScript.py looks like:

import bpy
import math
from add_utils import AddObjectHelper, add_object_data
import mathutils

# this line doesn't work
import MyModule

#### REGISTER ####

def register():
    pass

def unregister():
    print("Finished")

if __name__ == '__main__':
    register()

Running the script within Blender3D, I get the error:

"ImportError: No module named MyModule"

I have setup Blender3D's "script" folder to point at the folder containing my script and MyModule.py.

Thanks for any help.

J

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

灯角 2024-12-19 04:53:25

必须向 sys.path 变量添加正确的路径。我不知道 Blender 的具体情况,但一般来说,您可以按如下方式添加它:

sys.path.append(r'.\mymodule')            # Relative path
sys.path.append(r'C:\path\to\my\module')  # Absolute path

要使用的脚本取决于您运行的条件:如果您的脚本位于某些(对于 Blender)专用脚本文件夹中,您可以使用第一个。如果您从(例如)Program Files 文件夹运行它,请使用绝对路径。

Something has to add the proper path to the sys.path variable. I don't know it for Blender specifically, but in general, you could add it as follows:

sys.path.append(r'.\mymodule')            # Relative path
sys.path.append(r'C:\path\to\my\module')  # Absolute path

The one to use depends on the conditions you are running under: if your script is in some (for Blender) dedicated scripts folder, you could use the first one. If you run it from (for example) a Program Files folder, use the absolute path.

小…楫夜泊 2024-12-19 04:53:25

另一件对我有用的事情是使用以下终端命令运行我的 python 脚本

/usr/local/bin/python2.7 path/to/script.py

Another thing that worked for me was running my python script with the following terminal command

/usr/local/bin/python2.7 path/to/script.py
傾旎 2024-12-19 04:53:25

当我的外部模块与 .blend 文件位于同一目录中时,我使用的另一个陈词滥调是:

basedir =os.path.dirname(bpy.data.filepath) 
if basedir not in sys.path:
    sys.path.append(basedir)

import marchingCubes

# this next part forces a reload in case you edit the source after you first start the blender session
import imp
imp.reload(marchingCubes)

我从 http://www.blender.org/documentation/blender_python_api_2_59_2/info_tips_and_tricks.html 实际上有一个错误,当您使用 os.path.basename确实需要os.path.dirname

Another cliche I use when I have external modules that live in the same directory with the .blend file is:

basedir =os.path.dirname(bpy.data.filepath) 
if basedir not in sys.path:
    sys.path.append(basedir)

import marchingCubes

# this next part forces a reload in case you edit the source after you first start the blender session
import imp
imp.reload(marchingCubes)

I derived it from http://www.blender.org/documentation/blender_python_api_2_59_2/info_tips_and_tricks.html which actually has a bug in that it uses os.path.basename when you really need os.path.dirname.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文