class TestFunction(bpy.types.Operator):
"""TestFunction"""
bl_idname = "object.test_function"
bl_label = "Test Function"
bl_options = {'REGISTER', 'UNDO'}
def merge_hierarchy(obj):
"""
递归地合并层级结构中的所有子对象
"""
# 如果对象没有子对象,直接返回
if not obj.children:
return
# 找到所有非EMPTY的子对象
objects_to_merge = [child for child in obj.children if child.type != 'EMPTY']
# 如果有多个非EMPTY的子对象,合并它们
if len(objects_to_merge) > 1:
# 选择所有要合并的对象
for child in objects_to_merge:
child.select_set(True)
# 设置第一个对象为活动对象
bpy.context.view_layer.objects.active = objects_to_merge[0]
# 执行合并操作
bpy.ops.object.join()
# 重新命名合并后的对象为父节点的名称
merged_object = bpy.context.active_object
merged_object.name = obj.name
# 清除选择
bpy.ops.object.select_all(action='DESELECT')
# 递归处理每个子对象
for child in obj.children:
TestFunction.merge_hierarchy(child)
def execute(self, context):
"""
合并所有场景中的层级结构
"""
# 获取所有顶层对象(没有父对象的对象)
top_level_objects = [obj for obj in bpy.data.objects if obj.parent is None]
for obj in top_level_objects:
TestFunction.merge_hierarchy(obj)
return {'FINISHED'}
Whenever I run the aforementioned code to process models in an engineering file, the software consistently crashes after a while. The scene contains about 6,000 models. I'm using Blender version 3.4.1, which should be paired with Python version 3.10.8. However, when I select multiple objects in Blender and right-click to merge them, the software completes the operation swiftly. Why does it crash when these actions are turned into code for batch execution? I monitored the CPU usage during code execution and noticed a peak utilization rate of 80%. The rate typically fluctuated between 20% and 45%, with significant variations. Despite this, the software remained stable. However, after continued operation, it unexpectedly crashed without prior indications.(The running computer CPU is i9-13900ks) There are many models under the empty object node I selected on the right. I want to merge all models under each hierarchical structure into a single one, ensuring only one object remains under each node.