I am trying to write a python script that will convert triangular-mesh objects to quad-mesh objects.

For example, image (a) will be my input (.obj/.stl) file and image (b) will be the output.
I am a noob with mesh-algorithms or how they work all together. So, far this is the script I have written:
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')
This results in the following error:
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
Any help with what I am doing wrong here would be greatly appreciated.
Turns out bpy.ops.import_scene.obj was removed at bpy==4 which is the latest blender-api for python, hence the error. In bpy>4 you have to use bpy.ops.wm.obj_import(filepath='')
I just downgraded to bpy==3.60 to import object directly in the current scene.
pip install bpy==3.6.0
I also modified my script to take input of .obj files in triangular-mesh and then convert the mesh to quadrilateral, then export as both stl and obj. Here's my working script:
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
This still might not be the best approach to this, so do let me know if anyone know any better approach.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With