简体   繁体   English

递归返回列表功能停止?

[英]Recursive return list function stops?

I am not sure what is going on here, bear with me I just began doing python.我不确定这里发生了什么,请耐心等待我刚开始做 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.")

The function printHierarchy prints the whole hierarchy as intended:函数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

Now I was trying to get it to return a list of objects of type 'MESH' with printHierarchy2 but it always return None and I am not sure why because I would at least expect it to return an empty list.现在我试图让它返回一个带有printHierarchy2 'MESH'类型的对象列表,但它总是返回None ,我不知道为什么,因为我至少希望它返回一个空列表。

There's no return statement within your recurse function:您的递归函数中没有 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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM