AttributeError:调用操作符“bpy.ops.import_scene.obj”错误,找不到

问题描述 投票:0回答:1

我正在尝试编写一个Python脚本,将三角形网格对象转换为四边形网格对象。

例如,图像 (a) 将是我的输入 (

.obj/.stl
) 文件,图像 (b) 将是输出。

我是网格算法或它们如何协同工作的菜鸟。到目前为止,这是我写的脚本:

import bpy

inp = 'mushroom-shelve-1-merged.obj'


# Load the triangle mesh OBJ file
bpy.ops.import_scene.obj(filepath=inp, 
                        use_smooth_groups=False,
                        use_image_search=False)

# Get the imported mesh
obj = bpy.context.selected_objects[0]

# Convert triangles to quads
# The `beauty` parameter can be set to False if desired
bpy.ops.object.mode_set(mode='EDIT')
bpy.ops.mesh.select_all(action='SELECT')
bpy.ops.mesh.tris_convert_to_quads(beauty=True)
bpy.ops.object.mode_set(mode='OBJECT')

# Export to OBJ with quads
bpy.ops.export_scene.obj(filepath='quad_mesh.obj')

这会导致以下错误:

Traceback (most recent call last):
  File "/home/arrafi/mesh-convert-application/test.py", line 8, in <module>
    bpy.ops.import_scene.obj(filepath=inp, 
  File "/home/arrafi/mesh-convert-application/venv/lib/python3.10/site-packages/bpy/4.0/scripts/modules/bpy/ops.py", line 109, in __call__
    ret = _op_call(self.idname_py(), kw)
AttributeError: Calling operator "bpy.ops.import_scene.obj" error, could not be found

任何有关我在这里做错的事情的帮助将不胜感激。

  • 如果您知道使用 Python 将三角形网格转换为四边形网格的更好方法,请提供您的建议。
  • 如果你们知道我可以用 python 调用任何 API 来进行转换,那也可以。
python python-3.x blender stl-format bpy
1个回答
0
投票

结果

bpy.ops.import_scene.obj
bpy==4
处被删除,这是 python 的最新的 Blender-api,因此出现错误。在
bpy>4
中你必须使用
bpy.ops.wm.obj_import(filepath='')

我刚刚降级为

bpy==3.60
以直接在当前场景中导入对象。

pip install bpy=3.6.0

我还修改了我的脚本,以在三角形网格中输入

.obj
文件,然后将网格转换为四边形,然后导出为
stl
obj
。这是我的工作脚本:

def convert_tris_to_quads(obj_path, export_folder):
    try:
        filename = os.path.basename(obj_path).split('.')[0]
        logging.info(f"Importing {obj_path}")

        bpy.ops.object.select_all(action='DESELECT')
        bpy.ops.object.select_by_type(type='MESH')
        bpy.ops.object.delete()
    
        bpy.ops.import_scene.obj(filepath=obj_path)
        print("current objects in the scene: ", [obj for obj in bpy.context.scene.objects])
        for obj in bpy.context.selected_objects:
            bpy.context.view_layer.objects.active = obj
            
        logging.info("Converting mesh")
        bpy.ops.object.mode_set(mode='EDIT')
        bpy.ops.mesh.select_all(action='SELECT')
        bpy.ops.mesh.tris_convert_to_quads()
        bpy.ops.object.mode_set(mode='OBJECT')

        # Export to OBJ
        obj_export_path = export_folder + filename + '_quad.obj'
        logging.info(f"Exporting OBJ to {obj_export_path}")
        bpy.ops.export_scene.obj(filepath=obj_export_path, use_selection=True)

        # Export to STL
        stl_export_path = export_folder + filename + '_quad.stl'
        logging.info(f"Exporting STL to {stl_export_path}")
        bpy.ops.export_mesh.stl(filepath=stl_export_path, use_selection=True)

    except Exception as e:
        logging.error(f"Error processing {obj_path}: {e}")
        return False

这可能仍然不是最好的方法,所以如果有人知道更好的方法,请告诉我。

© www.soinside.com 2019 - 2024. All rights reserved.