|
| 1 | +import bpy |
| 2 | + |
| 3 | +class CleanReduceIfcOperator(bpy.types.Operator): |
| 4 | + """Clean and reduce IFC""" |
| 5 | + bl_idname = "object.clean_reduce_ifc_operator" |
| 6 | + bl_label = "Clean and Reduce IFC" |
| 7 | + bl_options = {'REGISTER', 'UNDO'} |
| 8 | + |
| 9 | + def execute(self, context): |
| 10 | + clean_reduce_ifc(self, context) |
| 11 | + self.report({'INFO'}, "IFC Clean and Reduce Completed") |
| 12 | + return {'FINISHED'} |
| 13 | + |
| 14 | +def clean_and_link_mesh_data(context): |
| 15 | + # Check if there are selected objects |
| 16 | + if not context.selected_objects: |
| 17 | + print("No objects selected. Exiting function.") |
| 18 | + return # Exit the function if no objects are selected |
| 19 | + |
| 20 | + # Check if there's an active object; if not, set the first selected object as active |
| 21 | + if not context.active_object or context.active_object not in context.selected_objects: |
| 22 | + context.view_layer.objects.active = context.selected_objects[0] |
| 23 | + |
| 24 | + # A dictionary to track original mesh data and their objects |
| 25 | + mesh_to_objects = {} |
| 26 | + for obj in bpy.context.selected_objects: |
| 27 | + if obj.type == 'MESH': |
| 28 | + if obj.data not in mesh_to_objects: |
| 29 | + mesh_to_objects[obj.data] = [obj] |
| 30 | + else: |
| 31 | + mesh_to_objects[obj.data].append(obj) |
| 32 | + |
| 33 | + total_objects = len(context.selected_objects) |
| 34 | + processed_objects = 0 |
| 35 | + |
| 36 | + # Ensure we're in object mode to start |
| 37 | + bpy.ops.object.mode_set(mode='OBJECT') |
| 38 | + |
| 39 | + # Process each unique mesh data block |
| 40 | + for mesh_data, objects in mesh_to_objects.items(): |
| 41 | + original_object = objects[0] # Select the first object for modification |
| 42 | + |
| 43 | + # Ensure the object has a unique mesh data block |
| 44 | + original_object.data = original_object.data.copy() |
| 45 | + |
| 46 | + # Make the original object active |
| 47 | + bpy.context.view_layer.objects.active = original_object |
| 48 | + |
| 49 | + # Ensure all objects are deselected before selecting the active object |
| 50 | + bpy.ops.object.select_all(action='DESELECT') |
| 51 | + original_object.select_set(True) |
| 52 | + |
| 53 | + # Enter Edit mode and select all vertices |
| 54 | + bpy.ops.object.mode_set(mode='EDIT') |
| 55 | + bpy.ops.mesh.select_all(action='SELECT') |
| 56 | + |
| 57 | + # Apply the necessary mesh operations |
| 58 | + bpy.ops.mesh.remove_doubles() |
| 59 | + bpy.ops.mesh.dissolve_limited(angle_limit=0.0872665) # Example angle limit |
| 60 | + |
| 61 | + # Go back to Object mode |
| 62 | + bpy.ops.object.mode_set(mode='OBJECT') |
| 63 | + |
| 64 | + # Add and apply the Decimate modifier |
| 65 | + mod = original_object.modifiers.new(name="DecimateMod", type='DECIMATE') |
| 66 | + mod.ratio = context.window_manager.decimate_ratio |
| 67 | + bpy.ops.object.modifier_apply(modifier=mod.name) |
| 68 | + |
| 69 | + # Link the modified mesh data to the other objects |
| 70 | + for other_object in objects[1:]: # Skip the first object since it's already modified |
| 71 | + other_object.data = original_object.data |
| 72 | + |
| 73 | + processed_objects += 1 |
| 74 | + |
| 75 | + # Update progress message every 10 objects |
| 76 | + if processed_objects % 15 == 0 or processed_objects == total_objects: |
| 77 | + progress_message = f"Processing {processed_objects} of {total_objects} objects..." |
| 78 | + context.scene.clean_progress = progress_message |
| 79 | + bpy.ops.wm.redraw_timer(type='DRAW_WIN_SWAP', iterations=1) |
| 80 | + |
| 81 | + # Clear the status text when done |
| 82 | + context.scene.clean_progress = "Ready to clean and reduce IFC" |
| 83 | + bpy.context.workspace.status_text_set(None) |
| 84 | + |
| 85 | +def clean_reduce_ifc(self, context): |
| 86 | + clean_and_link_mesh_data(context) |
| 87 | + |
| 88 | +def register(): |
| 89 | + bpy.utils.register_class(CleanReduceIfcOperator) |
| 90 | + |
| 91 | +def unregister(): |
| 92 | + bpy.utils.unregister_class(CleanReduceIfcOperator) |
| 93 | + |
| 94 | +if __name__ == "__main__": |
| 95 | + register() |
0 commit comments