2
$\begingroup$

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.

$\endgroup$
1
  • $\begingroup$ iterate over 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 over bpy.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$ Commented Apr 18, 2022 at 13:47

1 Answer 1

2
$\begingroup$

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)
```
$\endgroup$

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .