def resetStaticMeshMaterial(self, package_path): # def __init__(self, package_names=[], package_paths=[], object_paths=[], class_names=[], recursive_classes_exclusion_set=[], recursive_paths=False, recursive_classes=False, include_only_on_disk_assets=False): filter_staticmesh = unreal.ARFilter( [], [package_path], [], [unreal.StaticMesh.static_class().get_name()], [], True) filter_materialIns = unreal.ARFilter( [], [package_path], [], [unreal.MaterialInstanceConstant.static_class().get_name()], [], True) AssetRegistry = unreal.AssetRegistryHelpers().get_asset_registry() MaterialInsDataArr = AssetRegistry.get_assets(filter_materialIns) StaticMeshAssetDataArr = AssetRegistry.get_assets(filter_staticmesh) print('MaterialInsDataArr len is {}, StaticMeshAssetDataArr is {}'. format(len(MaterialInsDataArr), len(StaticMeshAssetDataArr))) for StaticMeshAssetData in StaticMeshAssetDataArr: # print StaticMeshAssetData StaticMeshStr = str(StaticMeshAssetData.package_name) # print StaticMeshStr StaticMeshAsset = unreal.StaticMesh.cast( unreal.load_asset(StaticMeshStr)) if (StaticMeshAsset != None): for MaterialInsData in MaterialInsDataArr: # print MaterialInsData.asset_name materialIndex = StaticMeshAsset.get_material_index( MaterialInsData.asset_name) if (materialIndex != -1): MaterialInsStr = str(MaterialInsData.package_name) targetMaterial = unreal.MaterialInstance.cast( unreal.load_asset(MaterialInsStr)) StaticMeshAsset.set_material(materialIndex, targetMaterial) print MaterialInsStr
def get_material_template(self, material): print(material) template_material_inst = material.split('_') if len(template_material_inst ) < 2 or template_material_inst[-1] != 'Inst': return None, None material_inst = template_material_inst[ -2] + '_' + template_material_inst[-1] package_path = '/Game/ZHAssets/MaterialTemplate/' filter_staticmesh = unreal.ARFilter( [], [package_path], [], [unreal.MaterialInstanceConstant.static_class().get_name()], [], True) AssetRegistry = unreal.AssetRegistryHelpers().get_asset_registry() MaterialInsDataArr = AssetRegistry.get_assets(filter_staticmesh) for material in MaterialInsDataArr: if str(material.package_name).split('/')[-1] == material_inst: return str(material.package_name), material_inst return None, None
def exportMyAsset(assets_path, output_path): asset_data = unreal.AssetData(assets_path) fbx_path = output_path + str(asset_data.asset_name) + ".fbx" asset_obj = unreal.AssetRegistryHelpers().get_asset(asset_data) print asset_obj options = buildStaticMeshExportOptions() task_fbx = buildExportTask(asset_obj, output_path, options) a = unreal.Exporter.run_asset_export_task(task_fbx) print a
def add_slots(self, asset_path): filter_skeletalmesh = unreal.ARFilter( [], [asset_path], [], [unreal.SkeletalMesh.static_class().get_name()], [], True) AssetRegistry = unreal.AssetRegistryHelpers().get_asset_registry() MaterialInsDataArr = AssetRegistry.get_assets(filter_skeletalmesh) print(len(MaterialInsDataArr)) for material in MaterialInsDataArr: print('material.package_name is xxx{}'.format( str(material.get_full_name()).split(' ')[1])) unreal.PythonCallLibrary.create_socket( str(material.get_full_name()).split(' ')[1], self.Bone_list)
def resetSkeletonMeshMaterial(self, package_path): # def __init__(self, package_names=[], package_paths=[], object_paths=[], class_names=[], recursive_classes_exclusion_set=[], recursive_paths=False, recursive_classes=False, include_only_on_disk_assets=False): filter_skeletalMesh = unreal.ARFilter( [], [package_path], [], [unreal.SkeletalMesh.static_class().get_name()]) filter_materialIns = unreal.ARFilter( [], [package_path], [], [unreal.MaterialInstanceConstant.static_class().get_name()]) AssetRegistry = unreal.AssetRegistryHelpers().get_asset_registry() MaterialInsDataArr = AssetRegistry.get_assets(filter_materialIns) SkeletalMeshAssetDataArr = AssetRegistry.get_assets( filter_skeletalMesh) for SkeletalMeshAssetData in SkeletalMeshAssetDataArr: # print StaticMeshAssetData SkeletalMeshStr = str(SkeletalMeshAssetData.package_name) # print StaticMeshStr SkeletalMeshAsset = unreal.SkeletalMesh.cast( unreal.load_asset(SkeletalMeshStr)) materials = SkeletalMeshAsset.materials if (SkeletalMeshAsset != None): for MaterialInsData in MaterialInsDataArr: # print MaterialInsData.asset_name materialIndex = SkeletalMeshAsset.get_material_index( MaterialInsData.asset_name) if (materialIndex != -1): MaterialInsStr = str(MaterialInsData.package_name) targetMaterial = unreal.MaterialInstance.cast( unreal.load_asset(MaterialInsStr)) # SkeletalMeshAsset.set_material(materialIndex,targetMaterial) for SkeletalMeshMaterialIndex in range(materials): if (materials[SkeletalMeshMaterialIndex]. imported_material_slot_name == MaterialInsData.asset_name): targetSkeltalMaterial = unreal.SkeletalMaterial( targetMaterial, materials[SkeletalMeshMaterialIndex]. material_slot_name(), materials[SkeletalMeshMaterialIndex]. uv_channel_data()) materials[ SkeletalMeshMaterialIndex] = targetSkeltalMaterial print MaterialInsStr
import os import unreal editor_utility = unreal.EditorUtilityLibrary() asset_editor = unreal.AssetEditorSubsystem() editor_asset = unreal.EditorAssetLibrary() asset_registry_helper = unreal.AssetRegistryHelpers() gameplay_statics = unreal.GameplayStatics() editor_level = unreal.EditorLevelLibrary() def open_asset(): assets = [] selected_assets = editor_utility.get_selected_assets() for asset in selected_assets: assets.append(asset) asset_editor.open_editor_for_assets(assets) def setup_output(): with open("D:/NekoNeko/Plugins/BitBaker/Content/Python/ConsoleLog.txt".format(os.path.dirname(__file__)), "r") as Log: broken_assets = [] lines = Log.readlines() for line in lines: asset_paths = line.rstrip() asset_paths = asset_paths.split('|') print(asset_paths) # Checks if there's a Map file extension in the errors, if so don't try to load it. # Do this by splitting the '.' and getting the last string in the asset_paths get_extension = asset_paths[0].split('.') if get_extension[-1] != 'umap':
def importAsset(self, targetDir): import xml.etree.ElementTree as ET root = ET.parse(targetDir + '\\Description.xml').getroot() # print textureTargetNameList textureTargetNameList = [] material_template_lists = list() for elem in root.iter(): if (elem.tag == "Material"): material = elem.attrib.get('matName') material_template_path, material_template = self.get_material_template( material) if not material_template: continue if material_template not in material_template_lists: material_template_lists.append(material_template) else: continue Minslots = set() for elem_Inst in elem.iter(): for Pic in elem_Inst.iter(): Usage = Pic.attrib.get('Usage') if Usage: Minslots.add(Usage) self.MaterialsTemplateArr.append({ 'mat_inst': material_template, 'mat_inst_path': material_template_path, 'Minslots': list(Minslots) }) # for template in self.MaterialsTemplateArr: # print('xx' + str(template)) # return # targetDir = 'M:\\DLQ2\\asset_work\\props\\hw\\Model\\texture\\publish' root = ET.parse(targetDir + '\\Description.xml').getroot() picList = [] destination_path = "/Game" + targetDir.replace("\\", "/").split(":")[1] importType = 0 # print os.path.exists('M:\\DLQ2\\asset_work\\props\\hw\\Model\\texture\\publish\\Description.xml') # print root,root.tag, root.attrib # for child_of_root in root: # print child_of_root.tag, child_of_root.attrib MeshFileName = "" is_character_type = False is_Combine_meshs = False dict_static_type = { 'Character': False, 'Environment': False, 'Props': False } for elem in root.iter(): # print elem.tag, elem.attrib if (elem.tag == "Pic"): Pic_Path = elem.attrib.get('Path') # print Pic_Path picList.append(Pic_Path) # destination_path = "/"+os.path.dirname(Pic_Path).replace('M:',"Game") elif (elem.tag == "StaticMesh"): MeshFileName = elem.attrib.get('Path') print('MeshFileName is {}'.format(MeshFileName)) static_type = elem.attrib.get('AssetType') dict_static_type[static_type] = True importType = 1 elif (elem.tag == "SkeletalMesh"): MeshFileName = elem.attrib.get('Path') print('MeshFileName is {}'.format(MeshFileName)) if elem.attrib.get('AssetType') == 'character': is_character_type = True importType = 2 # print "importType" + str(importType) self.importIMGAsset(picList, destination_path) #// by chenganggui EditorAssetLibrary = unreal.EditorAssetLibrary() AssetRegistry = unreal.AssetRegistryHelpers().get_asset_registry() # print "destination_path.replace" + destination_path if not EditorAssetLibrary.does_directory_exist(destination_path): EditorAssetLibrary.make_directory(destination_path) AssetRegistryDataArr = AssetRegistry.get_assets_by_path( destination_path) texArr = [] # print AssetRegistryDataArr for AssetRegistryData in AssetRegistryDataArr: # print AssetRegistryData.package_name # print 'AssetRegistryData.package_name '+ AssetRegistryData.package_name texArr.append(str(AssetRegistryData.package_name)) # print destination_path # print texArr for elem in root.iter(): if (elem.tag == "Material"): Material_matName = elem.attrib.get('matName') # print "Material_matName:: "+Material_matName Material_TemplateList = self.Material_Template.split("/") # print('Material_matName is {} '.format(Material_matName)) Pic_destination_path = destination_path self.create_material_instance(Material_matName, Pic_destination_path, texArr) # add by chenganggui # for f in os.listdir(targetDir): # # print f # if (f.find(".fbx") != -1): # MeshFileName = targetDir + "\\" + f # print MeshFileName if MeshFileName == "": print('canot find fbx file') return if importType == 1: package_path = "/Game" + os.path.dirname(targetDir).replace( "\\", "/").split(":")[1] package_path = package_path.replace(".", "_") # EditorAssetLibrary = unreal.EditorAssetLibrary() # if (EditorAssetLibrary.does_directory_exist(package_path)): # EditorAssetLibrary.delete_directory(package_path) if dict_static_type['Environment']: print('not conbine' + '**' * 10) self.importStaticMesh(MeshFileName, destination_path) # add by chenganggui else: print('need conbine' + '**' * 10) self.importStaticMesh( MeshFileName, destination_path, bCombine_meshs=True) # add by chenganggui print('package_path is {}'.format(package_path)) self.resetStaticMeshMaterial( package_path ) # '/Game/DLQ2/asset_work/props/hw/Model/texture/publish/image/hw_Texture_V01_fbx' # unreal.EditorLoadingAndSavingUtils.save_dirty_packages(True, True) elif importType == 2: package_path = "/Game" + os.path.dirname(targetDir).replace( "\\", "/").split(":")[1] package_path = package_path.replace(".", "_") # EditorAssetLibrary = unreal.EditorAssetLibrary() # if (EditorAssetLibrary.does_directory_exist(package_path)): # EditorAssetLibrary.delete_directory(package_path) print('import skeletal mesh') self.importSkeletalMesh(MeshFileName, destination_path) # self.resetSkeletonMeshMaterial( # package_path) # '/Game/DLQ2/asset_work/props/hw/Model/texture/publish/image/hw_Texture_V01_fbx' unreal.EditorLoadingAndSavingUtils.save_dirty_packages(True, True) #判断是否是character类型,所以为skeletalmesh 添加插槽 if is_character_type: print('this is is_character_type') self.add_slots(package_path)