简体   繁体   中英

Find baked textures 3D model in Blender/Python

From a dataset of 3D models, I need to identify automatically which models have baked textures and which don't. I'm using Blender-Python to manipulate the models, but I'm open to suggestions.

(There are too many models to open one by one)

Well first, we need a way to identify whether an object is using a baked texture. Let's say all baked textures use an image with "baked" in its name, so let's look for image texture nodes.

The following will find all objects in the current blend file that uses an image texture with "baked" in the name.

import bpy

for obj in bpy.data.objects:
    # does object have a material?
    if len(obj.material_slots) < 1: continue
    for slot in obj.material_slots:
        # skip empty slots and mats that don't use nodes
        if not slot.material or not slot.material.use_nodes: continue
        for n in slot.material.node_tree.nodes:
            if n.type == 'TEX_IMAGE' and 'baked' in n.image.name:
                print(f'{obj.name} uses baked image {n.image.name}')

As blender will clear out scripts as new blend files are opened, we need a script that will tell blender to open a file and run the previous script, then repeat for each file. To keep cross platform, we can use python for this as well.

from glob import glob
from subprocess import call

for blendFile in glob('*.blend'):
    arglist = [
    'blender',
    '--factory-startup',
    '-b',
    blendFile,
    '--python',
    'check_baked.py',
    ]
    print(f'Checking {blendFile}...')
    call(arglist)

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM