The project I'm working on in Blender has objects that aren't desired from the mesh rips I've pulled from PPSSPP using a tool called NinjaRipper. I'm looking to put together a script that targets meshes with a specific texture(and any iterations like "Texture.png.01") and have the objects deleted from all collections it is linked to. I have been looking at the python api documentation and found bpy.ops.object.delete(use_global=False, confirm=True)
which seems to be the end goal of the script but I'm unsure how to go about targeting objects sharing the same material/texture and their iterations, selecting them, and deleting them.
How do I create a script to delete all objects sharing the same texture/material in all collections?
$\begingroup$
$\endgroup$
1
1 Answer
$\begingroup$
$\endgroup$
You need to create a list of the relevant textures, something like this:
texture_names = [
"texture1.jpg",
"texture2.jpg",
"Metal007_1K_Color.jpg"
]
replacing my three sample strings with whatever textures you want. Once you've don that, this code will
- First find all of the materials that use any of the textures in your list and
- Then remove any object that uses any of the named materials.
import bpy
texture_names = [
"texture1.jpg",
"texture2.jpg",
"Metal007_1K_Color.jpg"
]
wanted = []
for material in bpy.data.materials:
node_tree = material.node_tree
if node_tree:
for node in node_tree.nodes:
if node.type == "TEX_IMAGE" and node.image.name in texture_names:
wanted.append(material)
for object in bpy.data.objects:
for slot in object.material_slots:
if slot.material in wanted:
print(object.name, slot.material.name)
bpy.data.objects.remove(object)
To do this from the known materials, use a slightly different setup
import bpy
material_names = [
#Fill this with strings of material names
]
for object in bpy.data.objects:
for slot in object.material_slots:
if slot.material.name in material_names:
print(object.name, slot.material.name)
bpy.data.objects.remove(object)
```
bpy.data.materials
searching the node trees for image textures. Each time you find an image texture check the image. If it's one you want to delete, add the material to a list. Then iterate overbpy.data.objects
searching the material slots of each object. If any of the material slots have any of the materials you found in the first search, delete the object. $\endgroup$