blender_asset_tracer/trace/blocks2assets.py

216 lines
7.2 KiB
Python
Raw Permalink Normal View History

2021-10-18 15:54:04 +02:00
# ***** BEGIN GPL LICENSE BLOCK *****
#
# This program is free software; you can redistribute it and/or
# modify it under the terms of the GNU General Public License
# as published by the Free Software Foundation; either version 2
# of the License, or (at your option) any later version.
#
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
# GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with this program; if not, write to the Free Software Foundation,
# Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
#
# ***** END GPL LICENCE BLOCK *****
#
# (c) 2014, Blender Foundation - Campbell Barton
# (c) 2018, Blender Foundation - Sybren A. Stüvel
"""Block walkers.
From a Blend file data block, iter_assts() yields all the referred-to assets.
"""
import functools
import logging
import typing
from blender_asset_tracer import blendfile, bpathlib, cdefs
from blender_asset_tracer.blendfile import iterators
from . import result, modifier_walkers
log = logging.getLogger(__name__)
_warned_about_types = set() # type: typing.Set[bytes]
_funcs_for_code = {} # type: typing.Dict[bytes, typing.Callable]
def iter_assets(block: blendfile.BlendFileBlock) -> typing.Iterator[result.BlockUsage]:
"""Generator, yield the assets used by this data block."""
2023-01-10 11:41:55 +01:00
assert block.code != b"DATA"
2021-10-18 15:54:04 +02:00
try:
block_reader = _funcs_for_code[block.code]
except KeyError:
if block.code not in _warned_about_types:
2023-01-10 11:41:55 +01:00
log.debug("No reader implemented for block type %r", block.code.decode())
2021-10-18 15:54:04 +02:00
_warned_about_types.add(block.code)
return
2023-01-10 11:41:55 +01:00
log.debug("Tracing block %r", block)
2021-10-18 15:54:04 +02:00
yield from block_reader(block)
def dna_code(block_code: str):
"""Decorator, marks decorated func as handler for that DNA code."""
assert isinstance(block_code, str)
def decorator(wrapped):
_funcs_for_code[block_code.encode()] = wrapped
return wrapped
return decorator
def skip_packed(wrapped):
"""Decorator, skip blocks where 'packedfile' is set to true."""
@functools.wraps(wrapped)
def wrapper(block: blendfile.BlendFileBlock, *args, **kwargs):
2023-01-10 11:41:55 +01:00
if block.get(b"packedfile", default=False):
log.debug("Datablock %r is packed; skipping", block.id_name)
2021-10-18 15:54:04 +02:00
return
yield from wrapped(block, *args, **kwargs)
return wrapper
2023-01-10 11:41:55 +01:00
@dna_code("CF")
2021-10-18 15:54:04 +02:00
def cache_file(block: blendfile.BlendFileBlock) -> typing.Iterator[result.BlockUsage]:
"""Cache file data blocks."""
2023-01-10 11:41:55 +01:00
path, field = block.get(b"filepath", return_field=True)
2021-10-18 15:54:04 +02:00
yield result.BlockUsage(block, path, path_full_field=field)
2023-01-10 11:41:55 +01:00
@dna_code("IM")
2021-10-18 15:54:04 +02:00
@skip_packed
def image(block: blendfile.BlendFileBlock) -> typing.Iterator[result.BlockUsage]:
"""Image data blocks."""
# old files miss this
2023-01-10 11:41:55 +01:00
image_source = block.get(b"source", default=cdefs.IMA_SRC_FILE)
if image_source not in {
cdefs.IMA_SRC_FILE,
cdefs.IMA_SRC_SEQUENCE,
cdefs.IMA_SRC_MOVIE,
cdefs.IMA_SRC_TILED,
}:
log.debug("skiping image source type %s", image_source)
2021-10-18 15:54:04 +02:00
return
2023-01-10 11:41:55 +01:00
pathname, field = block.get(b"name", return_field=True)
is_sequence = image_source in {cdefs.IMA_SRC_SEQUENCE, cdefs.IMA_SRC_TILED}
2021-10-18 15:54:04 +02:00
yield result.BlockUsage(block, pathname, is_sequence, path_full_field=field)
2023-01-10 11:41:55 +01:00
@dna_code("LI")
2021-10-18 15:54:04 +02:00
def library(block: blendfile.BlendFileBlock) -> typing.Iterator[result.BlockUsage]:
"""Library data blocks."""
2023-01-10 11:41:55 +01:00
path, field = block.get(b"name", return_field=True)
2021-10-18 15:54:04 +02:00
yield result.BlockUsage(block, path, path_full_field=field)
# The 'filepath' also points to the blend file. However, this is set to the
# absolute path of the file by Blender (see BKE_library_filepath_set). This
# is thus not a property we have to report or rewrite.
2023-01-10 11:41:55 +01:00
@dna_code("ME")
2021-10-18 15:54:04 +02:00
def mesh(block: blendfile.BlendFileBlock) -> typing.Iterator[result.BlockUsage]:
"""Mesh data blocks."""
2023-01-10 11:41:55 +01:00
block_external = block.get_pointer((b"ldata", b"external"), None)
2021-10-18 15:54:04 +02:00
if block_external is None:
2023-01-10 11:41:55 +01:00
block_external = block.get_pointer((b"fdata", b"external"), None)
2021-10-18 15:54:04 +02:00
if block_external is None:
return
2023-01-10 11:41:55 +01:00
path, field = block_external.get(b"filename", return_field=True)
2021-10-18 15:54:04 +02:00
yield result.BlockUsage(block, path, path_full_field=field)
2023-01-10 11:41:55 +01:00
@dna_code("MC")
2021-10-18 15:54:04 +02:00
def movie_clip(block: blendfile.BlendFileBlock) -> typing.Iterator[result.BlockUsage]:
"""MovieClip data blocks."""
2023-01-10 11:41:55 +01:00
path, field = block.get(b"name", return_field=True)
2021-10-18 15:54:04 +02:00
# TODO: The assumption that this is not a sequence may not be true for all modifiers.
yield result.BlockUsage(block, path, is_sequence=False, path_full_field=field)
2023-01-10 11:41:55 +01:00
@dna_code("OB")
2021-10-18 15:54:04 +02:00
def object_block(block: blendfile.BlendFileBlock) -> typing.Iterator[result.BlockUsage]:
"""Object data blocks."""
ctx = modifier_walkers.ModifierContext(owner=block)
# 'ob->modifiers[...].filepath'
for mod_idx, block_mod in enumerate(iterators.modifiers(block)):
2023-01-10 11:41:55 +01:00
block_name = b"%s.modifiers[%d]" % (block.id_name, mod_idx)
mod_type = block_mod[b"modifier", b"type"]
log.debug("Tracing modifier %s, type=%d", block_name.decode(), mod_type)
2021-10-18 15:54:04 +02:00
try:
mod_handler = modifier_walkers.modifier_handlers[mod_type]
except KeyError:
continue
yield from mod_handler(ctx, block_mod, block_name)
2023-01-10 11:41:55 +01:00
@dna_code("SC")
2021-10-18 15:54:04 +02:00
def scene(block: blendfile.BlendFileBlock) -> typing.Iterator[result.BlockUsage]:
"""Scene data blocks."""
# Sequence editor is the only interesting bit.
2023-01-10 11:41:55 +01:00
block_ed = block.get_pointer(b"ed")
2021-10-18 15:54:04 +02:00
if block_ed is None:
return
2023-01-10 11:41:55 +01:00
single_asset_types = {
cdefs.SEQ_TYPE_MOVIE,
cdefs.SEQ_TYPE_SOUND_RAM,
cdefs.SEQ_TYPE_SOUND_HD,
}
2021-10-18 15:54:04 +02:00
asset_types = single_asset_types.union({cdefs.SEQ_TYPE_IMAGE})
for seq, seq_type in iterators.sequencer_strips(block_ed):
if seq_type not in asset_types:
continue
2023-01-10 11:41:55 +01:00
seq_strip = seq.get_pointer(b"strip")
2021-10-18 15:54:04 +02:00
if seq_strip is None:
continue
2023-01-10 11:41:55 +01:00
seq_stripdata = seq_strip.get_pointer(b"stripdata")
2021-10-18 15:54:04 +02:00
if seq_stripdata is None:
continue
2023-01-10 11:41:55 +01:00
dirname, dn_field = seq_strip.get(b"dir", return_field=True)
basename, bn_field = seq_stripdata.get(b"name", return_field=True)
2021-10-18 15:54:04 +02:00
asset_path = bpathlib.BlendPath(dirname) / basename
is_sequence = seq_type not in single_asset_types
2023-01-10 11:41:55 +01:00
yield result.BlockUsage(
seq_strip,
asset_path,
is_sequence=is_sequence,
path_dir_field=dn_field,
path_base_field=bn_field,
)
2021-10-18 15:54:04 +02:00
2023-01-10 11:41:55 +01:00
@dna_code("SO")
2021-10-18 15:54:04 +02:00
@skip_packed
def sound(block: blendfile.BlendFileBlock) -> typing.Iterator[result.BlockUsage]:
"""Sound data blocks."""
2023-01-10 11:41:55 +01:00
path, field = block.get(b"name", return_field=True)
2021-10-18 15:54:04 +02:00
yield result.BlockUsage(block, path, path_full_field=field)
2023-01-10 11:41:55 +01:00
@dna_code("VF")
2021-10-18 15:54:04 +02:00
@skip_packed
def vector_font(block: blendfile.BlendFileBlock) -> typing.Iterator[result.BlockUsage]:
"""Vector Font data blocks."""
2023-01-10 11:41:55 +01:00
path, field = block.get(b"name", return_field=True)
if path == b"<builtin>": # builtin font
2021-10-18 15:54:04 +02:00
return
yield result.BlockUsage(block, path, path_full_field=field)