Budget: 5000 UAH Deadline: 1 day
import bpy
import bmesh
import math
from mathutils import Vector
from collections import deque
# Existing functions here...
def cut_mesh_along_loops(bm, loops):
"""
Cuts the mesh along the boundary loops (holes) and creates new objects for each part.
"""
# Ensure the original mesh has faces
bm.faces.ensure_lookup_table()
# We'll store the new objects here
new_objects = []
# Step 1: Process each loop and split the geometry along the hole
for loop in loops:
# Gather the edges of the hole
hole_edges = [e for e in loop]
# Step 2: Remove faces that intersect with the hole
faces_to_remove = []
for face in bm.faces:
for edge in hole_edges:
if any(e in face.edges for e in hole_edges):
faces_to_remove.append(face)
break
# Remove the faces
for face in faces_to_remove:
bm.faces.remove(face)
# Step 3: Create new mesh parts after cutting
new_bm = bmesh.new()
new_bm.from_mesh(bm)
# We will now separate the mesh based on the cut
bmesh.ops.delete(new_bm, geom=faces_to_remove, context="FACES_ONLY")
# Create a new mesh for the separated part
new_mesh = bpy.data.meshes.new(f"Mesh_After_Cutting")
new_bm.to_mesh(new_mesh)
new_bm.free()
# Create the new object and link to the scene
new_obj = bpy.data.objects.new(new_mesh.name, new_mesh)
bpy.context.collection.objects.link(new_obj)
new_objects.append(new_obj)
return new_objects
def process_mesh():
"""
Main function to process the mesh according to the defined steps.
"""
# Get the active object
obj = bpy.context.active_object
if obj is None or obj.type != 'MESH':
print("Active object is not a mesh.")
return
print(f"Processing object: {obj.name}")
mesh = obj.data
print(f"Vertices: {len(mesh.vertices)}, Edges: {len(mesh.edges)}, Faces: {len(mesh.polygons)}")
# Ensure the object is in object mode
if obj.mode != 'OBJECT':
bpy.ops.object.mode_set(mode='OBJECT')
print("Switched to object mode.")
# Create a copy of the mesh to preserve the original object
bpy.ops.object.duplicate()
obj_copy = bpy.context.active_object
obj_copy.name = obj.name + "_Copy"
print(f"Created a copy of the mesh: {obj_copy.name}")
# Find holes
bm = bmesh.new()