递归 return 列表函数停止了吗?

Recursive return list function stops?

我不确定这里发生了什么,耐心等待我才开始做 python。

import traceback
import bpy

propelAnimated = bpy.data.objects.get("Propel_Animated")

def printHierarchy(object, levels=10):
    def recurse(object, parent, depth):
        if depth > levels:
            return
        print("  " * depth, object.name)

        for child in object.children:
            recurse(child, object,  depth + 1)
    try: recurse(object, object.parent, 0)
    except Exception:
        traceback.print_exc()
        print("exception")
        return

def printHierarchy2(object, levels=10, list=None):
    def recurse(object, parent, depth, list):
        if(list == None):
            list = []
        if(object.type == 'MESH'):
            return object
        if depth > levels:
            print('return list 1')
            return list
        print("  " * depth, object.name)

        for child in object.children:
            list.append(recurse(child, object,  depth + 1, list))
    try: return recurse(object, object.parent, 0, list)
    except Exception:
        traceback.print_exc()
        print("exception")
        return list

if (propelAnimated != None and propelAnimated.type == 'EMPTY' and len(propelAnimated.children) > 0):
    #print(dir(propelAnimated))
    #print("My object exists and I can operate upon it.")

#    printHierarchy(propelAnimated)
    test = printHierarchy2(propelAnimated)

    print(test)

else:
    print("My object does not exist.")

函数 printHierarchy 按预期打印整个层次结构:

Propel_Animated
   PS_1_Grp
     propel_1_5xhi
       TR5_48
       TR5_49
       TR5_50
       TR5_51
       TR5_52
       TR5_53
       TR5_54
       TR5_55
       TR5_56
       TR5_57
       TR5_58
       TR5_59
   PS_2_Grp
     propel_2_5xhi
       TR5_72
       TR5_73
       TR5_74
       TR5_75
       TR5_76
       TR5_77
       TR5_78
       TR5_79
       TR5_80
       TR5_81
       TR5_82
       TR5_83
   PS_3_Grp
     propel_3_5xhi
       TR5_60
       TR5_61
       TR5_62
       TR5_63
       TR5_64
       TR5_65
       TR5_66
       TR5_67
       TR5_68
       TR5_69
       TR5_70
       TR5_71
   PS_4_Grp
     propel_4_5xhi
       TR5_84
       TR5_85
       TR5_86
       TR5_87
       TR5_88
       TR5_89
       TR5_90
       TR5_91
       TR5_92
       TR5_93
       TR5_94
       TR5_95

现在我试图将它添加到 return 类型为 'MESH'printHierarchy2 的对象列表,但它总是 return None 并且我我不确定为什么,因为我至少希望它 return 一个空列表。

你的递归函数中没有 return 语句:

def printHierarchy2(object, levels=10, list=None):    
    def recurse(object, parent, depth, list):
        if(list == None):
            list = []       
        if(object.type == 'MESH'):
            return object
        if depth > levels: 
            print('return list 1')
            return list
        print("  " * depth, object.name)

        for child in object.children:            
            list.append(recurse(child, object,  depth + 1, list))
        # changed here
        return list

    try: return recurse(object, object.parent, 0, list)
    except Exception:
        traceback.print_exc()
        print("exception")       
        return list