0
1

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

Unreal Engine (UE)Advent Calendar 2024

Day 20

【Python】フォルダ検索&取得

Posted at

この記事はUnreal Engine (UE) Advent Calendar 2024 シリーズ4の20日目の記事です。

こちらの記事は、シリーズ2の19日目の内容を、Pythonで書き直したものです。
Pythonだと、こういう書き方を出来ますという参考にしていただければと思います。

検証環境

UnrealEngine5.3.2

ブループリントのサンプル

実行テストに使ったブループリントの図です。赤いノードの中身だけ書き換えてテストしています。

実行したブループリントの図.png

サンプルコード①

FindFolder をPythonに直したサンプルコードです。

python
#
# FindFolder
#
def FindFolder(level_sequence, folder_name):
    rootFolders = level_sequence.get_root_folders_in_sequence() 
    
    for rootFolder in rootFolders :
        name = rootFolder.get_folder_name()
        if folder_name == name:
            return rootFolder
        
        for childFolder in rootFolder.get_child_folders():
            name = childFolder.get_folder_name()
            if folder_name == name:
                return childFolder
    return None

サンプルコード②

FindFolder_v2 をPythonに直したサンプルコードです。

python
#
# FindFolder_v2
#
def FindFolder_v2(level_sequence, folder_name):    
    rootFolders = level_sequence.get_root_folders_in_sequence() 
    
    for rootFolder in rootFolders :
        name = rootFolder.get_folder_name()
        if folder_name == name:
            return rootFolder
        
        for childFolder in rootFolder.get_child_folders():
            name = childFolder.get_folder_name()
            if folder_name == name:
                return childFolder
    
    # フォルダを新規作成
    return level_sequence.add_root_folder_to_sequence(folder_name)

サンプルコード③

FindFolder_v3SerchChildFolder をPythonに直したサンプルコードです。

python
#
# FindFolder_v3
#
def SerchChildFolder(scene_folder, folder_name):
    resultFolder = None
    
    if scene_folder != None:
        for childFolder  in scene_folder.get_child_folders():
            name = childFolder.get_folder_name()
            if folder_name == name:
                return childFolder
            else:
                resultFolder = SerchChildFolder(childFolder, folder_name)
                if resultFolder != None:
                    return resultFolder
    
    return resultFolder

def FindFolder_v3(level_sequence, folder_name):
    rootFolders = level_sequence.get_root_folders_in_sequence() 
    
    for rootFolder in rootFolders :
        name = rootFolder.get_folder_name()
        if folder_name == name:
            return rootFolder
        
        childFolder = SerchChildFolder(rootFolder, folder_name)
        if childFolder != None:
            return childFolder
    
    return None

最後に

理解度の向上も兼ねて、C++のコードをPythonに置き換えてみました。
Pythonの方を書く人も結構居るみたいなので、こちらの方が、需要があるかも知れません。

0
1
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
0
1

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?