Utility_Apps/Blender/simple scripts/select all mesh with the same material base name/select all mesh with the same material base name.py
2025-07-11 08:27:54 -05:00

35 lines
1 KiB
Python

import bpy
import re
def get_base_material_name(name):
return re.sub(r"\.\d{3}$", "", name)
def select_objects_with_same_base_material():
active = bpy.context.active_object
if not active or not active.type == 'MESH':
print("Select a mesh object first.")
return
if not active.data.materials:
print("Active object has no materials.")
return
# Get the base names of all materials on the active object
base_names = {get_base_material_name(mat.name) for mat in active.data.materials if mat}
# Deselect all first
bpy.ops.object.select_all(action='DESELECT')
for obj in bpy.data.objects:
if obj.type != 'MESH':
continue
for mat in obj.data.materials:
if mat and get_base_material_name(mat.name) in base_names:
obj.select_set(True)
break
# Optionally make the original object active again
bpy.context.view_layer.objects.active = active
active.select_set(True)
select_objects_with_same_base_material()