UE4+Python(导出资源)

虚幻引擎:4.25

Python:官方2.7版本

一个StaticMesh,通过查找它的refrences,导出整个关联关系的StaticMesh。可以做个素材管理,将公用的素材独立出来,这样的话一个文件夹下的素材就是一个完整的StaticMesh,包括Material, Texture,和材质实例。

export_asset.py

#!/usr/bin/python
# -*- coding: utf-8 -*-

import unreal, os, shutil, uuid, zipfile
import sys
reload(sys)
sys.setdefaultencoding('utf-8')

asset_package_name_array = []
    
def get_export_asset_array(package_name):
    global asset_package_name_array
    asset_data_array = unreal.AssetRegistryHelpers.get_asset_registry().get_assets_by_package_name(str(package_name))

    if(len(asset_data_array) > 0):
        asset_data = asset_data_array[0]
        unreal.log(asset_data)

        asset_class = asset_data.get_editor_property('asset_class')
        unreal.log(asset_class)

        # Recursion material and texture
        if asset_class == 'Material' or asset_class == 'MaterialInstanceConstant':
            # check other depend
            asset_package_name = asset_data.get_editor_property('package_name')
            unreal.log(asset_package_name)

            asset_registry_dependency_options = unreal.AssetRegistryDependencyOptions()

            refrencer_package_name_array = unreal.AssetRegistryHelpers.get_asset_registry().get_dependencies(asset_package_name, asset_registry_dependency_options)
            unreal.log(len(refrencer_package_name_array))

            for refrencer_package_name in refrencer_package_name_array:
                unreal.log(refrencer_package_name)
                get_export_asset_array(refrencer_package_name)
            
            asset_package_name_array.append(package_name)

        if asset_class == 'Texture2D':
            asset_package_name_array.append(package_name)


def export_asset_data():
    global asset_package_name_array

    # root package path
    root_package_path = '/Game/Assets/Library/md_models/Mesh'

    # get all sub package path of root package path
    package_path_array = unreal.AssetRegistryHelpers.get_asset_registry().get_sub_paths(root_package_path, True)
    unreal.log(len(package_path_array))

    for asset_path in package_path_array:
        unreal.log(asset_path)

        # get all asset in asset path
        asset_data_array = unreal.AssetRegistryHelpers.get_asset_registry().get_assets_by_path(str(asset_path))
        unreal.log(len(asset_data_array))

        for asset_data in asset_data_array:
            # get asset class
            asset_class = asset_data.get_editor_property('asset_class')
            unreal.log(asset_class)

            # if asset class is static mesh
            if asset_class == 'StaticMesh':
                package_path = asset_data.get_editor_property('package_path')
                unreal.log(package_path)

                asset_name = asset_data.get_editor_property('asset_name')
                unreal.log(asset_name)

                package_name = asset_data.get_editor_property('package_name')
                unreal.log(package_name)

                # get asset dependencies
                asset_registry_dependency_options = unreal.AssetRegistryDependencyOptions()

                refrencer_package_name_array = unreal.AssetRegistryHelpers.get_asset_registry().get_dependencies(package_name, asset_registry_dependency_options)
                unreal.log(len(refrencer_package_name_array))

                # for loop asset dependencies
                for refrencer_package_name in refrencer_package_name_array:
                    unreal.log(refrencer_package_name)
                    get_export_asset_array(refrencer_package_name)

                asset_package_name_array.append(package_name)

                # export asset fbx
                # unreal.AssetToolsHelpers.get_asset_tools().export_assets(asset_package_name_array, asset_export_path)
        
                # export asset os path
                base_dir = 'E:/Export-Asset/' + str(uuid.uuid1())

                unreal.log('---export---')
                for asset_package_name in asset_package_name_array:
                    unreal.log(asset_package_name)
                    
                    asset_data_array = unreal.AssetRegistryHelpers.get_asset_registry().get_assets_by_package_name(str(asset_package_name))

                    if(len(asset_data_array) > 0):
                        asset_data = asset_data_array[0]
                        unreal.log(asset_data)

                        package_path = asset_data.get_editor_property('package_path')
                        unreal.log(package_path)

                        asset_name = asset_data.get_editor_property('asset_name')
                        unreal.log(asset_name)

                        if str(package_path).startswith('/Game'):
                            # create destination dir
                            destination_dir = base_dir + str(package_path)[5:] + '/'
                            unreal.log(destination_dir)

                            if os.path.exists(destination_dir):
                                pass
                            else:
                                os.makedirs(destination_dir)

                            # unreal project path
                            root_dir = unreal.SystemLibrary.get_project_directory()

                            # source asset path
                            source_asset_path = root_dir + 'Content' + str(asset_package_name)[5:] + '.uasset'
                            unreal.log(source_asset_path)

                            # destination asset path
                            destination_asset_path = destination_dir + str(asset_name) + '.uasset'
                            unreal.log(destination_asset_path)

                            # shutil copy file
                            shutil.copyfile(unicode(source_asset_path), unicode(destination_asset_path))

                # clear export asset package name array 
                asset_package_name_array = []


if __name__ == "__main__":
    export_asset_data()

在编辑器Python命令行下执行export_asset.py,会将指定目录下的资源逐个导出到指定路径下

你可能感兴趣的:(UE4,Python,UE4,Python)