0

我一直在使用javascript,最近开始使用python来使用blender-python库。(bpy)所以由于缺乏语言行为知识,这个问题可能是我的一些误解。

此函数使用 bpy。我进行了测试,它成功地将 .gltf 文件转换为 .fbx。

import os
import bpy

path = os.getcwd()
UPLOAD_FOLDER = os.path.join(path, 'uploads')
DOWNLOAD_FOLDER = os.path.join(path, 'downloads')

def gltf_to_fbx(filename):
    path = os.path.join(UPLOAD_FOLDER, filename)
    bpy.ops.import_scene.gltf(filepath = path)
    portion = os.path.splitext(filename)
    newname = portion[0] + ".fbx"
    path_to_file = os.path.join(DOWNLOAD_FOLDER, newname)
    bpy.ops.export_scene.fbx(filepath = path_to_file)
    bpy.ops.object.delete()

gltf_to_fbx("1.gltf")

但是,当我像这样在烧瓶应用程序路由器中运行它时,它会引发错误

from flask import Flask, flash, request

app = Flask(__name__)

@app.route('/', methods=['POST'])
def upload_file():
    file = request.files['model']
    filename = secure_filename(file.filename)
    save_path = os.path.join(UPLOAD_FOLDER, filename)
    file.save(save_path)
    gltf_to_fbx(filename)
    flash('File successfully uploaded')
    return "Success"

if __name__ == "__main__":
    app.run(host="localhost", port = 5000)

这是错误信息

...
  File "index.py", line 62, in upload_file
    gltf_to_fbx("1_1.gltf")
  File "index.py", line 32, in gltf_to_fbx
    bpy.ops.import_scene.gltf(filepath = path)
  File "C:\ProgramData\Blender Foundation\Blender\2.82\scripts\modules\bpy\ops.py", line 201, in __call__       
    ret = op_call(self.idname_py(), None, kw)
RuntimeError: Error: Traceback (most recent call last):
  File "C:\ProgramData\Blender Foundation\Blender\2.82\scripts\addons\io_scene_gltf2\__init__.py", line 850, in 
execute
    return self.import_gltf2(context)
  File "C:\ProgramData\Blender Foundation\Blender\2.82\scripts\addons\io_scene_gltf2\__init__.py", line 869, in 
import_gltf2
    return self.unit_import(self.filepath, import_settings)
  File "C:\ProgramData\Blender Foundation\Blender\2.82\scripts\addons\io_scene_gltf2\__init__.py", line 887, in 
unit_import
    BlenderGlTF.create(self.gltf_importer)
  File "C:\ProgramData\Blender Foundation\Blender\2.82\scripts\addons\io_scene_gltf2\blender\imp\gltf2_blender_gltf.py", line 41, in create
    BlenderScene.create(gltf, scene_idx)
  File "C:\ProgramData\Blender Foundation\Blender\2.82\scripts\addons\io_scene_gltf2\blender\imp\gltf2_blender_scene.py", line 53, in create
    bpy.context.window.scene = bpy.data.scenes[gltf.blender_scene]
AttributeError: 'NoneType' object has no attribute 'scene'

location: C:\ProgramData\Blender Foundation\Blender\2.82\scripts\modules\bpy\ops.py:201

错误消息似乎是说它无法导入文件,因为它......错了?但这没有意义,因为它在烧瓶应用程序之外运行时没有抛出错误。

我想也许文件没有保存,直到 bpy 尝试加载它,但没有。我还尝试使用gltf_to_fbx已保存的文件路径运行该函数,但它仍然会引发相同的错误。我能想象的唯一原因是 bpy 在全局范围内有效,但在烧瓶应用程序中无效。这对我来说听起来很奇怪。

任何意见都值得赞赏。谢谢!

编辑 我刚刚注释掉了 bpy 模块中的错误行。然后它又抛出了另一个错误,我也将其注释掉了。现在它起作用了!不过,我并不为这种解决方案感到自豪。我的假设是 bpy 仅设计为在 Blender 控制台上运行。所以import_scene.gltf函数假设系统有运行 Blender 上下文,在这种情况下它没有。(仍然存在问题;它是如何在全局范围内运行而没有错误的?)我发现这个文档解释了如何在外部构建模块看起来充满希望的搅拌机。https://wiki.blender.org/wiki/Building_Blender/Windows

4

2 回答 2

0

我意识到我安装的 bpy 模块不正确。我认为 bpy 应该作为一个完整的模块工作,因为我在 pip 中找到了它(我是使用 安装的pip install bpy),但实际上它假定该模块是在 Blender 的应用内控制台上运行的。因此,不是通过运行应用程序python index.py,而是首先在后台运行 Blender 并在其上运行 python 解决了这个问题。blender --background python index.py

于 2021-01-20T07:58:00.423 回答
0

用于生成路径path = os.getcwd()gltf_to_fbx函数行path = os.path.join(UPLOAD_FOLDER, filename)中覆盖全局名称的可能问题,尝试更改为这样的,然后将其传递给这样的UPLOAD_FOLDERpath = os.getcwd()file_pathfile_path = os.path.join(UPLOAD_FOLDER, filename)file_pathbpybpy.ops.import_scene.gltf(filepath=file_path)

这可能会有所帮助,因为错误表明NoneType没有scene可能对提供给bpy.

bpy.context.window.scene = bpy.data.scenes[gltf.blender_scene]
AttributeError: 'NoneType' object has no attribute 'scene'
于 2021-01-12T14:41:02.060 回答