2026-02-16
This commit is contained in:
@@ -26,6 +26,7 @@ as determined by stats.users.py
|
||||
import bpy
|
||||
from .. import config
|
||||
from ..utils import compat
|
||||
from ..utils import version
|
||||
from . import users
|
||||
|
||||
|
||||
@@ -90,20 +91,72 @@ def images_deep():
|
||||
# this list also exists in images_shallow()
|
||||
do_not_flag = ["Render Result", "Viewer Node", "D-NOISE Export"]
|
||||
|
||||
total_images = len(bpy.data.images)
|
||||
config.debug_print(f"[Atomic Debug] images_deep(): Starting, total images: {total_images}")
|
||||
checked = 0
|
||||
|
||||
for image in bpy.data.images:
|
||||
# Skip library-linked and override datablocks
|
||||
if compat.is_library_or_override(image):
|
||||
continue
|
||||
|
||||
checked += 1
|
||||
config.debug_print(f"[Atomic Debug] images_deep(): Checking image {checked}/{total_images}: '{image.name}'")
|
||||
|
||||
# First check: standard unused detection
|
||||
config.debug_print(f"[Atomic Debug] images_deep(): Calling users.image_all('{image.name}')...")
|
||||
if not users.image_all(image.name):
|
||||
|
||||
config.debug_print(f"[Atomic Debug] images_deep(): Image '{image.name}' is unused (first check)")
|
||||
# check if image has a fake user or if ignore fake users
|
||||
# is enabled
|
||||
if not image.use_fake_user or config.include_fake_users:
|
||||
|
||||
# if image is not in our do not flag list
|
||||
if image.name not in do_not_flag:
|
||||
unused.append(image.name)
|
||||
else:
|
||||
# Second check: image is used, but check if it's ONLY used by unused objects
|
||||
# This fixes issue #5: images used by unused objects should be marked as unused
|
||||
# Get all objects that use this image (directly or indirectly)
|
||||
objects_using_image = []
|
||||
|
||||
# Check materials that use the image
|
||||
config.debug_print(f"[Atomic Debug] images_deep(): Getting materials for '{image.name}'...")
|
||||
mat_names = users.image_materials(image.name)
|
||||
config.debug_print(f"[Atomic Debug] images_deep(): Found {len(mat_names)} materials using '{image.name}'")
|
||||
for mat_name in mat_names:
|
||||
# Get objects using this material
|
||||
config.debug_print(f"[Atomic Debug] images_deep(): Getting objects for material '{mat_name}'...")
|
||||
objects_using_image.extend(users.material_objects(mat_name))
|
||||
# Also check Geometry Nodes usage
|
||||
config.debug_print(f"[Atomic Debug] images_deep(): Getting Geometry Nodes objects for material '{mat_name}'...")
|
||||
objects_using_image.extend(users.material_geometry_nodes(mat_name))
|
||||
|
||||
# Check Geometry Nodes directly
|
||||
config.debug_print(f"[Atomic Debug] images_deep(): Getting Geometry Nodes objects for '{image.name}'...")
|
||||
objects_using_image.extend(users.image_geometry_nodes(image.name))
|
||||
|
||||
# Remove duplicates
|
||||
objects_using_image = list(set(objects_using_image))
|
||||
config.debug_print(f"[Atomic Debug] images_deep(): Found {len(objects_using_image)} objects using '{image.name}'")
|
||||
|
||||
# If image is only used by objects, and ALL those objects are unused, mark image as unused
|
||||
# Check each object individually to avoid recursion issues
|
||||
if objects_using_image:
|
||||
config.debug_print(f"[Atomic Debug] images_deep(): Checking if all {len(objects_using_image)} objects are unused...")
|
||||
all_objects_unused = all(not users.object_all(obj_name) for obj_name in objects_using_image)
|
||||
if all_objects_unused:
|
||||
config.debug_print(f"[Atomic Debug] images_deep(): All objects are unused, marking '{image.name}' as unused")
|
||||
# Check if image has a fake user or if ignore fake users is enabled
|
||||
if not image.use_fake_user or config.include_fake_users:
|
||||
# if image is not in our do not flag list
|
||||
if image.name not in do_not_flag:
|
||||
unused.append(image.name)
|
||||
config.debug_print(f"[Atomic Debug] images_deep(): Added '{image.name}' to unused list")
|
||||
else:
|
||||
config.debug_print(f"[Atomic Debug] images_deep(): Some objects are used, '{image.name}' is not unused")
|
||||
config.debug_print(f"[Atomic Debug] images_deep(): Finished checking '{image.name}'")
|
||||
|
||||
config.debug_print(f"[Atomic Debug] images_deep(): Complete, checked {checked} images, found {len(unused)} unused")
|
||||
return unused
|
||||
|
||||
|
||||
@@ -160,12 +213,36 @@ def materials_deep():
|
||||
# Skip library-linked and override datablocks
|
||||
if compat.is_library_or_override(material):
|
||||
continue
|
||||
|
||||
# Check if material is used by brushes - these should always be ignored
|
||||
if users.material_brushes(material.name):
|
||||
continue
|
||||
|
||||
# First check: standard unused detection
|
||||
if not users.material_all(material.name):
|
||||
|
||||
# check if material has a fake user or if ignore fake users
|
||||
# is enabled
|
||||
if not material.use_fake_user or config.include_fake_users:
|
||||
unused.append(material.name)
|
||||
else:
|
||||
# Second check: material is used, but check if it's ONLY used by unused objects
|
||||
# This fixes issue #5: materials used by unused objects should be marked as unused
|
||||
# Get all objects that use this material
|
||||
objects_using_material = []
|
||||
objects_using_material.extend(users.material_objects(material.name))
|
||||
objects_using_material.extend(users.material_geometry_nodes(material.name))
|
||||
|
||||
# Remove duplicates
|
||||
objects_using_material = list(set(objects_using_material))
|
||||
|
||||
# If material is only used by objects, and ALL those objects are unused, mark material as unused
|
||||
# Check each object individually to avoid recursion issues
|
||||
if objects_using_material:
|
||||
all_objects_unused = all(not users.object_all(obj_name) for obj_name in objects_using_material)
|
||||
if all_objects_unused:
|
||||
# Check if material has a fake user or if ignore fake users is enabled
|
||||
if not material.use_fake_user or config.include_fake_users:
|
||||
unused.append(material.name)
|
||||
|
||||
return unused
|
||||
|
||||
@@ -174,24 +251,147 @@ def materials_shallow():
|
||||
# returns a list of keys of unused material that may be
|
||||
# incomplete, but is significantly faster than doing a deep search
|
||||
|
||||
return shallow(bpy.data.materials)
|
||||
unused_materials = shallow(bpy.data.materials)
|
||||
|
||||
# Filter out materials used by brushes - these should always be ignored
|
||||
filtered = []
|
||||
for key in unused_materials:
|
||||
material = bpy.data.materials.get(key)
|
||||
if material and not users.material_brushes(key):
|
||||
filtered.append(key)
|
||||
|
||||
return filtered
|
||||
|
||||
|
||||
def _is_compositor_node_tree(node_group):
|
||||
"""
|
||||
Check if a node group is a compositor node tree.
|
||||
In Blender 5.0+, each scene has a compositing_node_tree that should be ignored.
|
||||
|
||||
Args:
|
||||
node_group: The node group to check
|
||||
|
||||
Returns:
|
||||
bool: True if the node group is a compositor node tree
|
||||
"""
|
||||
# Check if this node group is any scene's compositor node tree
|
||||
# Use compat function to handle version differences properly
|
||||
for scene in bpy.data.scenes:
|
||||
if scene.use_nodes:
|
||||
node_tree = compat.get_scene_compositor_node_tree(scene)
|
||||
config.debug_print(f"[Atomic Debug] _is_compositor_node_tree: scene='{scene.name}', use_nodes={scene.use_nodes}, node_tree={node_tree}")
|
||||
if node_tree:
|
||||
config.debug_print(f"[Atomic Debug] _is_compositor_node_tree: node_tree.name='{node_tree.name}', checking against '{node_group.name}'")
|
||||
# Check by reference, not just name (in case user renamed it)
|
||||
if node_tree == node_group:
|
||||
config.debug_print(f"[Atomic Debug] _is_compositor_node_tree: '{node_group.name}' is scene '{scene.name}' compositor node tree")
|
||||
return True
|
||||
else:
|
||||
config.debug_print(f"[Atomic Debug] _is_compositor_node_tree: node_tree != node_group (reference comparison failed)")
|
||||
|
||||
# Also check if it's used as a node within any compositor (via node_group_compositors)
|
||||
# This handles the case where the node group is used within a compositor, not just as the tree itself
|
||||
comp_users = users.node_group_compositors(node_group.name)
|
||||
if comp_users:
|
||||
config.debug_print(f"[Atomic Debug] _is_compositor_node_tree: '{node_group.name}' is used in compositor: {comp_users}")
|
||||
return True
|
||||
|
||||
config.debug_print(f"[Atomic Debug] _is_compositor_node_tree: '{node_group.name}' is NOT a compositor node tree")
|
||||
return False
|
||||
|
||||
|
||||
def node_groups_deep():
|
||||
# returns a list of keys of unused node_groups
|
||||
|
||||
unused = []
|
||||
# Track which node groups we've already determined are unused (to avoid infinite recursion)
|
||||
_unused_node_groups_cache = set()
|
||||
|
||||
def _is_node_group_unused(ng_name, visited=None):
|
||||
"""Recursively check if a node group is unused.
|
||||
Returns True if the node group is only used by unused materials/objects/node_groups."""
|
||||
if visited is None:
|
||||
visited = set()
|
||||
|
||||
# Avoid infinite recursion
|
||||
if ng_name in visited:
|
||||
return False
|
||||
visited.add(ng_name)
|
||||
|
||||
# Check cache first
|
||||
if ng_name in _unused_node_groups_cache:
|
||||
return True
|
||||
|
||||
node_group = bpy.data.node_groups.get(ng_name)
|
||||
if not node_group:
|
||||
return False
|
||||
|
||||
# Skip library-linked and override datablocks
|
||||
if compat.is_library_or_override(node_group):
|
||||
return False
|
||||
# Skip compositor node trees
|
||||
if _is_compositor_node_tree(node_group):
|
||||
return False
|
||||
|
||||
# First check: node group has no users at all
|
||||
all_users = users.node_group_all(ng_name)
|
||||
config.debug_print(f"[Atomic Debug] _is_node_group_unused: '{ng_name}' - all_users = {all_users}")
|
||||
if not all_users:
|
||||
if not node_group.use_fake_user or config.include_fake_users:
|
||||
config.debug_print(f"[Atomic Debug] _is_node_group_unused: '{ng_name}' has no users, marking as unused")
|
||||
_unused_node_groups_cache.add(ng_name)
|
||||
return True
|
||||
|
||||
# Second check: node group is used, but check if it's ONLY used by unused materials/objects/node_groups
|
||||
# Get all materials and objects that use this node group
|
||||
materials_using_ng = users.node_group_materials(ng_name)
|
||||
objects_using_ng = users.node_group_objects(ng_name)
|
||||
parent_node_groups = users.node_group_node_groups(ng_name)
|
||||
|
||||
# Collect all objects that use this node group (directly or via materials)
|
||||
all_objects_using_ng = list(objects_using_ng) # Direct object usage via geometry nodes
|
||||
|
||||
# For each material using this node group, get objects using that material
|
||||
for mat_name in materials_using_ng:
|
||||
# Get objects using this material
|
||||
objects_using_mat = users.material_objects(mat_name)
|
||||
objects_using_mat.extend(users.material_geometry_nodes(mat_name))
|
||||
all_objects_using_ng.extend(objects_using_mat)
|
||||
|
||||
# Remove duplicates
|
||||
all_objects_using_ng = list(set(all_objects_using_ng))
|
||||
|
||||
# Check if all objects are unused
|
||||
all_objects_unused = True
|
||||
if all_objects_using_ng:
|
||||
all_objects_unused = all(not users.object_all(obj_name) for obj_name in all_objects_using_ng)
|
||||
|
||||
# Check if all parent node groups are unused (recursive)
|
||||
all_parent_ngs_unused = True
|
||||
if parent_node_groups:
|
||||
for parent_ng_name in parent_node_groups:
|
||||
if not _is_node_group_unused(parent_ng_name, visited.copy()):
|
||||
all_parent_ngs_unused = False
|
||||
break
|
||||
|
||||
# If node group is only used by unused objects and unused parent node groups, mark it as unused
|
||||
if all_objects_unused and all_parent_ngs_unused:
|
||||
if not node_group.use_fake_user or config.include_fake_users:
|
||||
_unused_node_groups_cache.add(ng_name)
|
||||
return True
|
||||
|
||||
return False
|
||||
|
||||
for node_group in bpy.data.node_groups:
|
||||
# Skip library-linked and override datablocks
|
||||
if compat.is_library_or_override(node_group):
|
||||
continue
|
||||
if not users.node_group_all(node_group.name):
|
||||
|
||||
# check if node group has a fake user or if ignore fake users
|
||||
# is enabled
|
||||
if not node_group.use_fake_user or config.include_fake_users:
|
||||
unused.append(node_group.name)
|
||||
# Skip compositor node trees (Blender 5.0+ creates one per file)
|
||||
if _is_compositor_node_tree(node_group):
|
||||
continue
|
||||
|
||||
if _is_node_group_unused(node_group.name):
|
||||
unused.append(node_group.name)
|
||||
|
||||
return unused
|
||||
|
||||
@@ -200,7 +400,16 @@ def node_groups_shallow():
|
||||
# returns a list of keys of unused node groups that may be
|
||||
# incomplete, but is significantly faster than doing a deep search
|
||||
|
||||
return shallow(bpy.data.node_groups)
|
||||
unused = shallow(bpy.data.node_groups)
|
||||
|
||||
# Filter out compositor node trees (Blender 5.0+ creates one per file)
|
||||
filtered = []
|
||||
for node_group_name in unused:
|
||||
node_group = bpy.data.node_groups.get(node_group_name)
|
||||
if node_group and not _is_compositor_node_tree(node_group):
|
||||
filtered.append(node_group_name)
|
||||
|
||||
return filtered
|
||||
|
||||
|
||||
def particles_deep():
|
||||
@@ -263,21 +472,27 @@ def textures_shallow():
|
||||
|
||||
def worlds():
|
||||
# returns a full list of keys of unused worlds
|
||||
|
||||
config.debug_print(f"[Atomic Debug] unused.worlds(): Starting, total worlds: {len(bpy.data.worlds)}")
|
||||
unused = []
|
||||
checked = 0
|
||||
|
||||
for world in bpy.data.worlds:
|
||||
# Skip library-linked and override datablocks
|
||||
if compat.is_library_or_override(world):
|
||||
continue
|
||||
|
||||
checked += 1
|
||||
config.debug_print(f"[Atomic Debug] unused.worlds(): Checking '{world.name}' (users={world.users}, fake_user={world.use_fake_user})")
|
||||
|
||||
# if data-block has no users or if it has a fake user and
|
||||
# ignore fake users is enabled
|
||||
if world.users == 0 or (world.users == 1 and
|
||||
world.use_fake_user and
|
||||
config.include_fake_users):
|
||||
config.debug_print(f"[Atomic Debug] unused.worlds(): '{world.name}' is unused, adding to list")
|
||||
unused.append(world.name)
|
||||
|
||||
config.debug_print(f"[Atomic Debug] unused.worlds(): Complete, checked {checked} worlds, found {len(unused)} unused")
|
||||
return unused
|
||||
|
||||
|
||||
|
||||
Reference in New Issue
Block a user