Skip to content

Instantly share code, notes, and snippets.

@p2or
Last active May 21, 2023 19:14
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 1 You must be signed in to fork a gist
  • Save p2or/87fc80a85f958bce64861211b0751e55 to your computer and use it in GitHub Desktop.
Save p2or/87fc80a85f958bce64861211b0751e55 to your computer and use it in GitHub Desktop.
Groups by materials #Blender
import bpy
data = {}
for obj in bpy.data.objects: # bpy.context.selected_objects
mat_slots = obj.material_slots.keys()
if mat_slots:
data_key = "-".join(mat_slots)
else:
data_key = "None"
data.setdefault(data_key, []).append(obj)
for key, value in data.items():
group_name = "{}".format(key)
if group_name not in bpy.data.groups:
bpy.ops.group.create(name=group_name)
grp = bpy.data.groups.get(group_name)
for obj in value:
if len(obj.users_group) == 0:
grp.objects.link(obj)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment