Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
352 views
in Technique[技术] by (71.8m points)

blender - Automatically import files in python

I am develepong a blender addon which has several submodules

- A/__init__.py
  |
  |- B/__init__.py

In A/init.py I can do import A.B to import the content of submodule B, but I would like to be able to import automatically this files. Is there any way to achieve that?

The idea is that my addon is used to implement several glTF extensions. Each extension is in its own submodule and the glTF exporter expects me to return some classes, one for each extension.

Instead of importing manually each extension and adding the class to the list of extensions, I want that to happen automatically

instead of

from A.B import B_extension

glTF2ExportUserExtensions = [A.B.B_extension]

I want something like

# A.submodules returns all the submodules and extension() returns the extension class. Im assuming each submodule have an extension() function
glTF2ExportUserExtensions = [A.submodules.extension()]

I need to return a list of classes

question from:https://stackoverflow.com/questions/65882708/automatically-import-files-in-python

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)

You can import things from an essential file that you have the imports on.

Example of ./import_file.py

import example
import ...

You can use "*" to import everything from the import file, or you can import something specific.

Example of ./main_file.py

from import_file import *
from import_file import example

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...