Join Meshes Improvements, better error handling

This commit is contained in:
Yusarina
2024-07-25 22:24:41 +01:00
parent bb108d28a0
commit fef46cd567
2 changed files with 43 additions and 14 deletions
+32 -6
View File
@@ -18,32 +18,46 @@ class JoinAllMeshes(Operator):
return armature is not None and is_valid_armature(armature) return armature is not None and is_valid_armature(armature)
def execute(self, context: Context) -> Set[str]: def execute(self, context: Context) -> Set[str]:
try:
self.join_all_meshes(context) self.join_all_meshes(context)
return {'FINISHED'} return {'FINISHED'}
except Exception as e:
self.report({'ERROR'}, f"{t('Optimization.join_error')}: {str(e)}")
return {'CANCELLED'}
def join_all_meshes(self, context: Context) -> None: def join_all_meshes(self, context: Context) -> None:
if not select_current_armature(context): if not select_current_armature(context):
self.report({'WARNING'}, t("Optimization.no_armature_selected")) raise ValueError(t("Optimization.no_armature_selected"))
return
armature = get_selected_armature(context) armature = get_selected_armature(context)
bpy.ops.object.mode_set(mode='OBJECT') bpy.ops.object.mode_set(mode='OBJECT')
bpy.ops.object.select_all(action='DESELECT') bpy.ops.object.select_all(action='DESELECT')
meshes: List[Object] = get_all_meshes(context) meshes: List[Object] = get_all_meshes(context)
if not meshes:
raise ValueError(t("Optimization.no_meshes_found"))
for mesh in meshes: for mesh in meshes:
mesh.select_set(True) mesh.select_set(True)
if bpy.context.selected_objects: if bpy.context.selected_objects:
bpy.context.view_layer.objects.active = bpy.context.selected_objects[0] bpy.context.view_layer.objects.active = bpy.context.selected_objects[0]
try:
bpy.ops.object.join() bpy.ops.object.join()
except RuntimeError as e:
raise RuntimeError(f"{t('Optimization.join_operation_failed')}: {str(e)}")
try:
bpy.ops.object.transform_apply(location=True, rotation=True, scale=True) bpy.ops.object.transform_apply(location=True, rotation=True, scale=True)
except RuntimeError as e:
raise RuntimeError(f"{t('Optimization.transform_apply_failed')}: {str(e)}")
fix_uv_coordinates(context) fix_uv_coordinates(context)
bpy.ops.object.mode_set(mode='OBJECT') bpy.ops.object.mode_set(mode='OBJECT')
bpy.ops.object.select_all(action='DESELECT') bpy.ops.object.select_all(action='DESELECT')
self.report({'INFO'}, t("Optimization.meshes_joined")) self.report({'INFO'}, t("Optimization.meshes_joined"))
else: else:
self.report({'WARNING'}, t("Optimization.no_mesh_selected")) raise ValueError(t("Optimization.no_mesh_selected"))
context.view_layer.objects.active = armature context.view_layer.objects.active = armature
@@ -59,15 +73,18 @@ class JoinSelectedMeshes(Operator):
return context.mode == 'OBJECT' and len([obj for obj in context.selected_objects if obj.type == 'MESH']) > 1 return context.mode == 'OBJECT' and len([obj for obj in context.selected_objects if obj.type == 'MESH']) > 1
def execute(self, context: Context) -> Set[str]: def execute(self, context: Context) -> Set[str]:
try:
self.join_selected_meshes(context) self.join_selected_meshes(context)
return {'FINISHED'} return {'FINISHED'}
except Exception as e:
self.report({'ERROR'}, f"{t('Optimization.join_error')}: {str(e)}")
return {'CANCELLED'}
def join_selected_meshes(self, context: Context) -> None: def join_selected_meshes(self, context: Context) -> None:
selected_objects: List[Object] = [obj for obj in bpy.context.selected_objects if obj.type == 'MESH'] selected_objects: List[Object] = [obj for obj in bpy.context.selected_objects if obj.type == 'MESH']
if len(selected_objects) < 2: if len(selected_objects) < 2:
self.report({'WARNING'}, t("Optimization.select_at_least_two_meshes")) raise ValueError(t("Optimization.select_at_least_two_meshes"))
return
bpy.ops.object.mode_set(mode='OBJECT') bpy.ops.object.mode_set(mode='OBJECT')
bpy.ops.object.select_all(action='DESELECT') bpy.ops.object.select_all(action='DESELECT')
@@ -77,11 +94,20 @@ class JoinSelectedMeshes(Operator):
if bpy.context.selected_objects: if bpy.context.selected_objects:
bpy.context.view_layer.objects.active = bpy.context.selected_objects[0] bpy.context.view_layer.objects.active = bpy.context.selected_objects[0]
try:
bpy.ops.object.join() bpy.ops.object.join()
except RuntimeError as e:
raise RuntimeError(f"{t('Optimization.join_operation_failed')}: {str(e)}")
try:
bpy.ops.object.transform_apply(location=True, rotation=True, scale=True) bpy.ops.object.transform_apply(location=True, rotation=True, scale=True)
except RuntimeError as e:
raise RuntimeError(f"{t('Optimization.transform_apply_failed')}: {str(e)}")
fix_uv_coordinates(context) fix_uv_coordinates(context)
bpy.ops.object.mode_set(mode='OBJECT') bpy.ops.object.mode_set(mode='OBJECT')
bpy.ops.object.select_all(action='DESELECT') bpy.ops.object.select_all(action='DESELECT')
self.report({'INFO'}, t("Optimization.selected_meshes_joined")) self.report({'INFO'}, t("Optimization.selected_meshes_joined"))
else: else:
self.report({'WARNING'}, t("Optimization.no_mesh_selected")) raise ValueError(t("Optimization.no_mesh_selected"))
+3
View File
@@ -53,6 +53,9 @@
"Optimization.remove_doubles_completed": "Remove doubles operation completed", "Optimization.remove_doubles_completed": "Remove doubles operation completed",
"Optimization.select_armature": "Please select an armature", "Optimization.select_armature": "Please select an armature",
"Optimization.material_attribute_mismatch": "Attribute mismatch in material {material_name}, skipping", "Optimization.material_attribute_mismatch": "Attribute mismatch in material {material_name}, skipping",
"Optimization.join_error": "Error during mesh joining",
"Optimization.join_operation_failed": "Join operation failed",
"Optimization.transform_apply_failed": "Transform apply failed",
"Tools.select_armature": "Please select an armature", "Tools.select_armature": "Please select an armature",
"Tools.label": "Tools", "Tools.label": "Tools",
"Tools.tools_title.label": "Tools:", "Tools.tools_title.label": "Tools:",