Files
Avatar-Toolkit/functions/tools/mesh_separation.py
T
989onan 6d9f751a16 Housekeeping (bug fixes)
NEW FEATURES:
- added apply shapekey to basis from Cats
  - now that pesky thing I keep going back to cats for is in Avatar Toolkit.

BUG FIXES:
- now we push armature santizers into functions where they are needed
  - this prevents the methods from mirroring changes while working, causing them to blow up when mirror mode is on
  - more changes to come for armature setting santitizers
- fixed error reporting
  - now methods when catching errors will return full error tracebacks
  - this will help make debugging and finding user issues easier.
2025-07-10 18:44:42 -04:00

71 lines
2.6 KiB
Python

import bpy
from bpy.types import Operator, Context
from ...core.translations import t
from ...core.common import get_active_armature
from ...core.armature_validation import validate_armature
import traceback
class AvatarToolKit_OT_SeparateByMaterials(Operator):
"""Operator to separate mesh by materials"""
bl_idname = "avatar_toolkit.separate_materials"
bl_label = t("Tools.separate_materials")
bl_description = t("Tools.separate_materials_desc")
bl_options = {'REGISTER', 'UNDO'}
@classmethod
def poll(cls, context: Context) -> bool:
"""Check if operator can be executed"""
armature = get_active_armature(context)
if not armature:
return False
valid, _, _ = validate_armature(armature)
return (context.active_object and
context.active_object.type == 'MESH' and
valid)
def execute(self, context: Context) -> set[str]:
"""Execute the separation operation"""
try:
obj = context.active_object
bpy.ops.object.mode_set(mode='EDIT')
bpy.ops.mesh.select_all(action='SELECT')
bpy.ops.mesh.separate(type='MATERIAL')
bpy.ops.object.mode_set(mode='OBJECT')
self.report({'INFO'}, t("Tools.separate_materials_success"))
return {'FINISHED'}
except Exception:
self.report({'ERROR'}, traceback.format_exc())
return {'CANCELLED'}
class AvatarToolKit_OT_SeparateByLooseParts(Operator):
"""Operator to separate mesh by loose parts"""
bl_idname = "avatar_toolkit.separate_loose"
bl_label = t("Tools.separate_loose")
bl_description = t("Tools.separate_loose_desc")
bl_options = {'REGISTER', 'UNDO'}
@classmethod
def poll(cls, context: Context) -> bool:
"""Check if operator can be executed"""
armature = get_active_armature(context)
if not armature:
return False
valid, _, _ = validate_armature(armature)
return (context.active_object and
context.active_object.type == 'MESH' and
valid)
def execute(self, context: Context) -> set[str]:
"""Execute the separation operation"""
try:
obj = context.active_object
bpy.ops.object.mode_set(mode='EDIT')
bpy.ops.mesh.select_all(action='SELECT')
bpy.ops.mesh.separate(type='LOOSE')
bpy.ops.object.mode_set(mode='OBJECT')
self.report({'INFO'}, t("Tools.separate_loose_success"))
return {'FINISHED'}
except Exception:
self.report({'ERROR'}, traceback.format_exc())
return {'CANCELLED'}