A
A
Andrey Gorbatsevich2017-12-29 15:14:24
Python
Andrey Gorbatsevich, 2017-12-29 15:14:24

How to merge objects in Blender using Python?

Problem: objects are not merged (Warning: Active object is not a selected mesh)
There is a large map with a huge number of objects on it.
5a462ddf19c33347329199.png
It was necessary to merge objects that have the same beginning of the name
{"name", "name 1", (...),"name N"} -> "name"
Doing this by hand is quite long and dreary, so I decided to take the opportunity to write for python script blender.
Through trial and error, two methods were created:

import collections, time
def selector(): # выделяет объекты, названия которых начинаются одинаково
    c = collections.Counter([x.name.split()[0] for x in C.scene.objects])
    _nr = [x for x in dict(c) if dict(c)[x] > 1]
    del c
    for nr in _nr:
        bpy.ops.object.select_all(action='DESELECT')
        print("Selecting all", nr)
        for obj in C.scene.objects:
            if not obj.name.startswith(nr): continue
            bpy.data.objects[obj.name].select = True
            print("Selected", obj.name)
        joiner()

def joiner(): # должен объединять объекты
    bpy.ops.object.join()
    time.sleep(.5)
    bpy.ops.object.select_all(action='DESELECT')

When calling the selector() method, heaps of objects with the same name beginnings should be combined, but not:
5a462fe9c6f81732957318.png
I noticed that when selector() selects objects, all of them are highlighted in orange:
5a46305bda420967101977.png
At the same time, if you call joiner() yourself, then it writes in the console :
5a4630efbc027369869764.png
Joining via Ctrl+J causes the same error (but it is displayed in the toolbar).
If you select manually, then there is always one element highlighted in yellow:
5a4630b389a72395411867.png
In this case, the call to joiner (), as well as joining via Ctrl + J, work with a bang. All objects merge into the one that was highlighted in yellow.
Can anyone come across?

Answer the question

In order to leave comments, you need to log in

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question