My script prints every node group name, how many nodes are inside it, and its users. The node groups are sorted by how many nodes are inside them so I can find possible redundant/duplicated node groups in my project.
--------------------------------------------------
NodeGroup.015
7 nodes
3 users
Material: Marble Faux Brick
Material: Slit Guide
--------------------------------------------------
NodeGroup.025
7 nodes
1 users
Material: [Guard Rails] Travertine Blocks
--------------------------------------------------
NodeGroup.029
7 nodes
2 users
Material: [Stands] Marble Stands
Material: [Temple Stairs] Marble Steps NEW
--------------------------------------------------
But searching for the printed node group in its material brings no results since they go by different names in materials. So while bpy.data.node_groups
gives Nodegroup.015
, it's actually Group.008
when in a material.
How do I match Nodegroup.015
to Group.008
so I can print this alternative name? (Why are there two different unique identifiers?) Or is my whole approach wrong?
import re
import bpy
from bpy.types import bpy_prop_collection
# No idea what this does, stole it from here:
# https://blender.stackexchange.com/questions/230605/how-to-find-the-users-of-a-material
def search(ID):
def users(col):
ret = tuple(repr(o) for o in col if o.user_of_id(ID))
return ret if ret else None
return filter(None, (
users(getattr(bpy.data, p))
for p in dir(bpy.data)
if isinstance(
getattr(bpy.data, p, None),
bpy_prop_collection
)
)
)
def show_materials(user):
material_pattern = re.search(r"(?<=bpy\.data\.materials\[').+", user)
if material_pattern:
material_name = material_pattern.group(0)[:-2]
print(" " * 4 + "Material:", material_name)
return material_name
nodes = bpy.data.node_groups
node_group_names = [node_group.name for node_group in nodes]
contained_nodes = [len(node_group.nodes) for node_group in nodes]
sorted_dictionary = {key: value for key, value in sorted(dict(zip(node_group_names, contained_nodes)).items(), key=lambda item: item[1])}
for node_group, value in sorted_dictionary.items():
print("-" * 50)
print(node_group)
print(value, "nodes")
print(nodes[node_group].users, "users")
for collection_of_users in search(nodes[node_group]):
for user in collection_of_users:
material_name = show_materials(user)
if material_name:
print(bpy.data.materials[material_name].node_tree.nodes[??]) # ??
bpy.data.node_groups
, then they have a unique identifier to each material's node tree frombpy.data.materials["your_material].node_tree.nodes
, then they have a label which can be shard by any number of nodes $\endgroup$