0
$\begingroup$

So I'm importing an obj file that comes in as hundreds of separate meshes in the default collection.

Many of the meshes are imported with a similar name, but with a different ID number added in brackets at the end.

I would like to cycle through the default collection to find meshes with these similar names and add them to a new collection named after them.

My end goal would be to have these separated collections and be able to edit one objects materials and the ability to link all at once without scrolling through the long list that is default with the import.

    import bpy

"""
Input: Objects in Collection named "Collection"

Output: Like Named objects in separate collections with the same name

"""

"Initial setup; gets number of objects in Collection to cycle through"

Object_number=500;

"Compares one selected object from the outliner to another next to it"


for i in range(0,Object_number+1):
    a=bpy.context.object.name[0:10]

    bpy.ops.collection.objects_remove(collection="Collection")
    bpy.ops.outliner.select_walk(direction='DOWN')
    bpy.ops.outliner.item_activate(extend=False, deselect_all=True)
    b=bpy.context.object.name[0:10]
    bpy.ops.collection.objects_remove(collection="Collection")
    print(a)
    print(b)
    if a = b:
        bpy.ops.collection.create(name = a)
        bpy.ops.outliner.select_walk(direction='UP')
        bpy.ops.collection.object_add_active(collection = a)
    else
    i=i+1

My initial code scrolled through the outliner and compared the names of two adjacent objects. If the first 10 characters of the string were the same then it would attempt to add both to the collection of the same 10 character name.

Incredibly new to all this and would appreciate the knowledge behind scripting in blender!

$\endgroup$

1 Answer 1

2
$\begingroup$

You can do it this way. The code is fully commented but ask if something is not clear enough.

import bpy

#Get the collection the objects are
original_collection = bpy.data.collections['Collection']

#Get the root collection
scene_collection = bpy.context.scene.collection

#This dictionary will contain the collections that will be created
#The key is the object's name
dictionary = {}

#Loop over the objects
for obj in original_collection.objects:
    name = obj.name[0:10] # Get the root name (the way you need to)
    collection = dictionary.get(name) #Look if there is already a collection
    if not collection:
        #If not create it
        collection = bpy.data.collections.new(name)
        #Link it to the scene
        scene_collection.children.link(collection)
        #Keep it in the dictionary
        dictionary[name] = collection
    #Link the object to the new collection
    collection.objects.link(obj)
    #Unlink it from the original one
    original_collection.objects.unlink(obj)
$\endgroup$
1
  • $\begingroup$ Thank you so much! I am so new to blender python so still not aware of most of the commands. Much appreciated lesson. $\endgroup$
    – Bodog01
    Mar 13, 2020 at 17:33

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .