我必须马上说我不懂 Python,只懂最基本的。
可以写这样一个类
bl_info = {
"name": "Outliner sorting",
"version": (1, 1),
"blender": (2, 80, 0),
"author": "Igor Yuchimenko"
}
import bpy
class OutlinerSort(bpy.types.Operator):
"""Tooltip"""
bl_idname = "outliner.sort"
bl_label = "Sort alphabetic"
def sort_collection(collection, case = False):
if collection.children is None: return
children = sorted (
collection.children,
key = lambda c: c.name if case else c.name.lower()
)
for child in children:
collection.children.unlink(child)
collection.children.link(child)
sort_collection(child)
def execute(self, context):
for scene in bpy.data.scenes:
sort_collection(scene.collection, False)
return {'FINISHED'}
def draw_method(self, context):
self.layout.operator(OutlinerSort.bl_idname)
def register():
bpy.utils.register_class(OutlinerSort)
bpy.types.OUTLINER_MT_context_menu.prepend(draw_method)
def unregister():
bpy.types.OUTLINER_MT_context_menu.remove(draw_method)
bpy.utils.unregister_class(OutlinerSort)
if __name__ == "__main__":
register()
当您按下菜单按钮时,会进行调用execute
,并且已经通过此方法获得所需的排序功能。Blender 抛出一个它sort_collection
不存在的错误,但是如果我替换为self.sort_collection(scene.collection, False)
,则会出现另一个错误,即指定了 3 个参数而不是 2 个,当我sort_collection(self, collection, case = False)
再次添加时,我得到一个错误,该方法不存在。怎么了?我没有在其他语言中看到这种行为。