2026-01-01
This commit is contained in:
@@ -426,12 +426,24 @@ def light_objects(light_key):
|
||||
|
||||
def material_all(material_key):
|
||||
# returns a list of keys of every data-block that uses this material
|
||||
return material_objects(material_key) + \
|
||||
material_geometry_nodes(material_key)
|
||||
# Use comprehensive custom detection that covers all usage contexts
|
||||
users = []
|
||||
|
||||
# Check direct object usage (material slots)
|
||||
users.extend(material_objects(material_key))
|
||||
|
||||
# Check Geometry Nodes usage (materials in node groups used by objects)
|
||||
users.extend(material_geometry_nodes(material_key))
|
||||
|
||||
# Check node group usage (materials in node groups used elsewhere)
|
||||
users.extend(material_node_groups(material_key))
|
||||
|
||||
return distinct(users)
|
||||
|
||||
|
||||
def material_geometry_nodes(material_key):
|
||||
# returns a list of object keys that use the material via Geometry Nodes
|
||||
# Only counts objects that are in scene collections (recursive check)
|
||||
|
||||
users = []
|
||||
material = bpy.data.materials[material_key]
|
||||
@@ -440,17 +452,104 @@ def material_geometry_nodes(material_key):
|
||||
from ..utils import compat
|
||||
|
||||
for obj in bpy.data.objects:
|
||||
# Skip library-linked and override objects
|
||||
if compat.is_library_or_override(obj):
|
||||
continue
|
||||
|
||||
# Check if object is in any scene collection (reuse object_all logic)
|
||||
# This ensures recursive checking: if the object using the material isn't in a scene,
|
||||
# the material isn't considered used
|
||||
obj_scenes = object_all(obj.name)
|
||||
is_in_scene = bool(obj_scenes)
|
||||
|
||||
if not is_in_scene:
|
||||
continue # Skip objects not in scene collections
|
||||
|
||||
if hasattr(obj, 'modifiers'):
|
||||
for modifier in obj.modifiers:
|
||||
if compat.is_geometry_nodes_modifier(modifier):
|
||||
ng = compat.get_geometry_nodes_modifier_node_group(modifier)
|
||||
if ng:
|
||||
# Check if this node group or any nested node groups contain the material
|
||||
if node_group_has_material(ng.name, material.name):
|
||||
users.append(obj.name)
|
||||
|
||||
return distinct(users)
|
||||
|
||||
|
||||
def material_node_groups(material_key):
|
||||
# returns a list of keys indicating where the material is used via node groups
|
||||
# This checks if the material is used in any node group, and if that node group
|
||||
# is itself used anywhere. This complements material_geometry_nodes() by checking
|
||||
# additional usage contexts (materials, other node groups, compositor, etc.)
|
||||
# Note: Geometry Nodes usage is already checked by material_geometry_nodes()
|
||||
# Optimized to return early when usage is found
|
||||
|
||||
from ..utils import compat
|
||||
material = bpy.data.materials[material_key]
|
||||
|
||||
# Check all node groups to see if they contain this material
|
||||
for node_group in bpy.data.node_groups:
|
||||
# Skip library-linked and override node groups
|
||||
if compat.is_library_or_override(node_group):
|
||||
continue
|
||||
if node_group_has_material(node_group.name, material.name):
|
||||
# This node group contains the material, check if the node group is used
|
||||
# Check usage contexts in order of likelihood, return early when found
|
||||
|
||||
# First check: is it used in Geometry Nodes modifiers? (most common case)
|
||||
# Note: material_geometry_nodes() already checks this, but we verify here too
|
||||
obj_users = node_group_objects(node_group.name)
|
||||
if obj_users:
|
||||
return obj_users # Return immediately - material is used
|
||||
|
||||
# Second check: is it used in materials?
|
||||
mat_users = node_group_materials(node_group.name)
|
||||
if mat_users:
|
||||
return mat_users # Return immediately - material is used
|
||||
|
||||
# Third check: is it used in compositor?
|
||||
comp_users = node_group_compositors(node_group.name)
|
||||
if comp_users:
|
||||
return comp_users # Return immediately - material is used
|
||||
|
||||
# Fourth check: is it used in textures?
|
||||
tex_users = node_group_textures(node_group.name)
|
||||
if tex_users:
|
||||
return tex_users # Return immediately - material is used
|
||||
|
||||
# Fifth check: is it used in worlds?
|
||||
world_users = node_group_worlds(node_group.name)
|
||||
if world_users:
|
||||
return world_users # Return immediately - material is used
|
||||
|
||||
# Last check: is it used in other node groups? (recursive, but only if needed)
|
||||
ng_users = node_group_node_groups(node_group.name)
|
||||
if ng_users:
|
||||
# Check if any parent node groups are used (quick check only)
|
||||
for parent_ng_name in ng_users:
|
||||
# Quick check: see if parent is used in objects (most common)
|
||||
parent_obj_users = node_group_objects(parent_ng_name)
|
||||
if parent_obj_users:
|
||||
return parent_obj_users
|
||||
# Quick check: see if parent is used in materials
|
||||
parent_mat_users = node_group_materials(parent_ng_name)
|
||||
if parent_mat_users:
|
||||
return parent_mat_users
|
||||
# Also check if parent is used in compositor, textures, worlds
|
||||
parent_comp_users = node_group_compositors(parent_ng_name)
|
||||
if parent_comp_users:
|
||||
return parent_comp_users
|
||||
parent_tex_users = node_group_textures(parent_ng_name)
|
||||
if parent_tex_users:
|
||||
return parent_tex_users
|
||||
parent_world_users = node_group_worlds(parent_ng_name)
|
||||
if parent_world_users:
|
||||
return parent_world_users
|
||||
|
||||
return [] # Material not used in any node groups
|
||||
|
||||
|
||||
def material_objects(material_key):
|
||||
# returns a list of object keys that use this material
|
||||
|
||||
@@ -760,22 +859,122 @@ def node_group_has_material(node_group_key, material_key):
|
||||
# returns true if a node group contains this material (directly or nested)
|
||||
|
||||
has_material = False
|
||||
node_group = bpy.data.node_groups[node_group_key]
|
||||
material = bpy.data.materials[material_key]
|
||||
try:
|
||||
node_group = bpy.data.node_groups[node_group_key]
|
||||
material = bpy.data.materials[material_key]
|
||||
except (KeyError, AttributeError):
|
||||
return False
|
||||
|
||||
for node in node_group.nodes:
|
||||
# base case: nodes with a material property (e.g., Set Material)
|
||||
if hasattr(node, 'material') and node.material:
|
||||
if node.material.name == material.name:
|
||||
has_material = True
|
||||
try:
|
||||
for node in node_group.nodes:
|
||||
try:
|
||||
# Explicitly check for GeometryNodeSetMaterial nodes first
|
||||
# This is the most reliable way to detect Set Material nodes in Geometry Nodes
|
||||
if hasattr(node, 'bl_idname'):
|
||||
try:
|
||||
if node.bl_idname == 'GeometryNodeSetMaterial':
|
||||
# Geometry Nodes Set Material nodes use input sockets, not a direct material property
|
||||
# Check the material input socket
|
||||
try:
|
||||
# Try to access the Material input socket directly by name
|
||||
if hasattr(node, 'inputs') and 'Material' in node.inputs:
|
||||
try:
|
||||
material_socket = node.inputs['Material']
|
||||
# Check the default_value (for unlinked materials)
|
||||
if hasattr(material_socket, 'default_value'):
|
||||
socket_material = material_socket.default_value
|
||||
if socket_material and hasattr(socket_material, 'name'):
|
||||
if (socket_material.name == material.name or
|
||||
socket_material == material):
|
||||
has_material = True
|
||||
except (KeyError, AttributeError, ReferenceError, RuntimeError, TypeError):
|
||||
pass
|
||||
|
||||
# Also check all inputs as fallback (in case socket name differs)
|
||||
if not has_material:
|
||||
for input_socket in getattr(node, 'inputs', []):
|
||||
try:
|
||||
# Check socket type - material sockets are typically 'MATERIAL' type
|
||||
socket_type = getattr(input_socket, 'type', '')
|
||||
if socket_type == 'MATERIAL' or 'material' in str(input_socket).lower():
|
||||
# Check if this socket has a default_value that is a material
|
||||
if hasattr(input_socket, 'default_value') and input_socket.default_value:
|
||||
socket_material = input_socket.default_value
|
||||
if socket_material and hasattr(socket_material, 'name'):
|
||||
if (socket_material.name == material.name or
|
||||
socket_material == material):
|
||||
has_material = True
|
||||
break # Found it, no need to continue
|
||||
except (AttributeError, ReferenceError, RuntimeError, TypeError):
|
||||
continue # Skip this socket if we can't access it
|
||||
|
||||
# Also check if the node has a direct material property (fallback for some versions)
|
||||
if not has_material and hasattr(node, 'material'):
|
||||
try:
|
||||
if node.material:
|
||||
if (node.material.name == material.name or
|
||||
node.material == material):
|
||||
has_material = True
|
||||
break # Found it, no need to continue
|
||||
except (AttributeError, ReferenceError, RuntimeError):
|
||||
pass # Skip if material access fails
|
||||
|
||||
if has_material:
|
||||
break # Break outer loop if we found it
|
||||
except (AttributeError, ReferenceError, RuntimeError):
|
||||
pass # Skip if Set Material node input access fails
|
||||
except (AttributeError, ReferenceError, RuntimeError):
|
||||
pass # Skip if bl_idname access fails
|
||||
|
||||
# Fallback: Check for any node with a material property (e.g., Set Material)
|
||||
# This catches other node types that might have materials
|
||||
if not has_material and hasattr(node, 'material'):
|
||||
try:
|
||||
if node.material:
|
||||
# Check both by name and by direct reference for robustness
|
||||
if (node.material.name == material.name or
|
||||
node.material == material):
|
||||
has_material = True
|
||||
break # Found it, no need to continue
|
||||
except (AttributeError, ReferenceError, RuntimeError):
|
||||
pass # Skip if material access fails
|
||||
|
||||
# Also check node type by substring for Set Material nodes (backup check)
|
||||
if not has_material and hasattr(node, 'bl_idname'):
|
||||
try:
|
||||
node_type = node.bl_idname
|
||||
# Check for Geometry Nodes Set Material node type (substring match)
|
||||
if 'SetMaterial' in node_type or 'SET_MATERIAL' in node_type.upper():
|
||||
if hasattr(node, 'material'):
|
||||
try:
|
||||
if node.material:
|
||||
if (node.material.name == material.name or
|
||||
node.material == material):
|
||||
has_material = True
|
||||
break
|
||||
except (AttributeError, ReferenceError, RuntimeError):
|
||||
pass # Skip if material access fails
|
||||
except (AttributeError, ReferenceError, RuntimeError):
|
||||
pass # Skip if bl_idname access fails
|
||||
|
||||
# recurse case: nested node groups
|
||||
elif hasattr(node, 'node_tree') and node.node_tree:
|
||||
has_material = node_group_has_material(
|
||||
node.node_tree.name, material.name)
|
||||
# recurse case: nested node groups
|
||||
# Check this separately (not elif) in case we need to recurse
|
||||
if not has_material and hasattr(node, 'node_tree'):
|
||||
try:
|
||||
if node.node_tree:
|
||||
has_material = node_group_has_material(
|
||||
node.node_tree.name, material.name)
|
||||
except (KeyError, AttributeError, ReferenceError, RuntimeError):
|
||||
continue # Skip invalid node groups
|
||||
|
||||
if has_material:
|
||||
break
|
||||
if has_material:
|
||||
break
|
||||
except (AttributeError, ReferenceError, RuntimeError):
|
||||
# Skip nodes that cause errors (e.g., invalid/corrupted nodes)
|
||||
continue
|
||||
except (AttributeError, ReferenceError, RuntimeError):
|
||||
# If we can't even iterate nodes, return False
|
||||
return False
|
||||
|
||||
return has_material
|
||||
|
||||
@@ -969,6 +1168,75 @@ def texture_particles(texture_key):
|
||||
return distinct(users)
|
||||
|
||||
|
||||
def object_all(object_key):
|
||||
# returns a list of scene names where the object is used
|
||||
# An object is "used" if it's in any collection that's part of any scene's collection hierarchy
|
||||
|
||||
users = []
|
||||
obj = bpy.data.objects[object_key]
|
||||
|
||||
# Get all collections that contain this object
|
||||
for collection in obj.users_collection:
|
||||
# Check if this collection is in any scene's hierarchy
|
||||
for scene in bpy.data.scenes:
|
||||
if _scene_collection_contains(scene.collection, collection):
|
||||
if scene.name not in users:
|
||||
users.append(scene.name)
|
||||
|
||||
return distinct(users)
|
||||
|
||||
|
||||
def armature_all(armature_key):
|
||||
# returns a list of object names that use the armature
|
||||
# Checks direct usage, modifier usage, and constraint usage
|
||||
# Only counts objects that are actually in scene collections (recursive check)
|
||||
|
||||
users = []
|
||||
armature = bpy.data.armatures[armature_key]
|
||||
|
||||
# Check all objects - but only count those that are in scene collections
|
||||
for obj in bpy.data.objects:
|
||||
# Skip library-linked and override objects
|
||||
from ..utils import compat
|
||||
if compat.is_library_or_override(obj):
|
||||
continue
|
||||
|
||||
# Check if object is in any scene collection (reuse object_all logic)
|
||||
obj_scenes = object_all(obj.name)
|
||||
is_in_scene = bool(obj_scenes)
|
||||
|
||||
# Check for usage regardless of scene status (we'll filter later)
|
||||
found_usage = False
|
||||
|
||||
# 1. Direct usage: ARMATURE objects where object.data == armature
|
||||
if obj.type == 'ARMATURE' and obj.data == armature:
|
||||
found_usage = True
|
||||
|
||||
# 2. Modifier usage: Armature modifiers where modifier.object.data == armature
|
||||
if not found_usage and hasattr(obj, 'modifiers'):
|
||||
for modifier in obj.modifiers:
|
||||
if modifier.type == 'ARMATURE':
|
||||
if hasattr(modifier, 'object') and modifier.object:
|
||||
if modifier.object.type == 'ARMATURE' and modifier.object.data == armature:
|
||||
found_usage = True
|
||||
break
|
||||
|
||||
# 3. Constraint usage: Constraints that target ARMATURE objects using this armature
|
||||
if not found_usage and hasattr(obj, 'constraints'):
|
||||
for constraint in obj.constraints:
|
||||
if hasattr(constraint, 'target') and constraint.target:
|
||||
if constraint.target.type == 'ARMATURE' and constraint.target.data == armature:
|
||||
found_usage = True
|
||||
break
|
||||
|
||||
# Only add to users if the object is actually in a scene
|
||||
# This implements recursive checking: if the user object is unused, it doesn't count
|
||||
if found_usage and is_in_scene:
|
||||
users.append(obj.name)
|
||||
|
||||
return distinct(users)
|
||||
|
||||
|
||||
def distinct(seq):
|
||||
# returns a list of distinct elements
|
||||
|
||||
|
||||
Reference in New Issue
Block a user