Join Meshes Improvements, better error handling
This commit is contained in:
+40
-14
@@ -18,32 +18,46 @@ class JoinAllMeshes(Operator):
|
||||
return armature is not None and is_valid_armature(armature)
|
||||
|
||||
def execute(self, context: Context) -> Set[str]:
|
||||
self.join_all_meshes(context)
|
||||
return {'FINISHED'}
|
||||
try:
|
||||
self.join_all_meshes(context)
|
||||
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:
|
||||
if not select_current_armature(context):
|
||||
self.report({'WARNING'}, t("Optimization.no_armature_selected"))
|
||||
return
|
||||
raise ValueError(t("Optimization.no_armature_selected"))
|
||||
|
||||
armature = get_selected_armature(context)
|
||||
bpy.ops.object.mode_set(mode='OBJECT')
|
||||
bpy.ops.object.select_all(action='DESELECT')
|
||||
|
||||
meshes: List[Object] = get_all_meshes(context)
|
||||
if not meshes:
|
||||
raise ValueError(t("Optimization.no_meshes_found"))
|
||||
|
||||
for mesh in meshes:
|
||||
mesh.select_set(True)
|
||||
|
||||
if bpy.context.selected_objects:
|
||||
bpy.context.view_layer.objects.active = bpy.context.selected_objects[0]
|
||||
bpy.ops.object.join()
|
||||
bpy.ops.object.transform_apply(location=True, rotation=True, scale=True)
|
||||
try:
|
||||
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)
|
||||
except RuntimeError as e:
|
||||
raise RuntimeError(f"{t('Optimization.transform_apply_failed')}: {str(e)}")
|
||||
|
||||
fix_uv_coordinates(context)
|
||||
bpy.ops.object.mode_set(mode='OBJECT')
|
||||
bpy.ops.object.select_all(action='DESELECT')
|
||||
self.report({'INFO'}, t("Optimization.meshes_joined"))
|
||||
else:
|
||||
self.report({'WARNING'}, t("Optimization.no_mesh_selected"))
|
||||
raise ValueError(t("Optimization.no_mesh_selected"))
|
||||
|
||||
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
|
||||
|
||||
def execute(self, context: Context) -> Set[str]:
|
||||
self.join_selected_meshes(context)
|
||||
return {'FINISHED'}
|
||||
try:
|
||||
self.join_selected_meshes(context)
|
||||
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:
|
||||
selected_objects: List[Object] = [obj for obj in bpy.context.selected_objects if obj.type == 'MESH']
|
||||
|
||||
if len(selected_objects) < 2:
|
||||
self.report({'WARNING'}, t("Optimization.select_at_least_two_meshes"))
|
||||
return
|
||||
raise ValueError(t("Optimization.select_at_least_two_meshes"))
|
||||
|
||||
bpy.ops.object.mode_set(mode='OBJECT')
|
||||
bpy.ops.object.select_all(action='DESELECT')
|
||||
@@ -77,11 +94,20 @@ class JoinSelectedMeshes(Operator):
|
||||
|
||||
if bpy.context.selected_objects:
|
||||
bpy.context.view_layer.objects.active = bpy.context.selected_objects[0]
|
||||
bpy.ops.object.join()
|
||||
bpy.ops.object.transform_apply(location=True, rotation=True, scale=True)
|
||||
try:
|
||||
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)
|
||||
except RuntimeError as e:
|
||||
raise RuntimeError(f"{t('Optimization.transform_apply_failed')}: {str(e)}")
|
||||
|
||||
fix_uv_coordinates(context)
|
||||
bpy.ops.object.mode_set(mode='OBJECT')
|
||||
bpy.ops.object.select_all(action='DESELECT')
|
||||
self.report({'INFO'}, t("Optimization.selected_meshes_joined"))
|
||||
else:
|
||||
self.report({'WARNING'}, t("Optimization.no_mesh_selected"))
|
||||
raise ValueError(t("Optimization.no_mesh_selected"))
|
||||
|
||||
|
||||
Reference in New Issue
Block a user