Download

[SOLVED]Import Alembic and Assign Material

I have written a script to batch import alembic files, add them to the world, assign the geometry cache and apply a custom made material.

import unreal
import random
import colorsys

ell = unreal.EditorLevelLibrary
eal = unreal.EditorAssetLibrary

def alembicBatchImport():
    destination_path = "/Game/Crowd/Agents"
    
    #Import known images as a list of Texture2D objects.
    lst_image_files = [
        r'S:\Houdini_Scenes\textures\clothing_001.jpg',
        r'S:\Houdini_Scenes\textures\clothing_002.jpg',
        r'S:\Houdini_Scenes\textures\clothing_003.jpg',
        r'S:\Houdini_Scenes\textures\clothing_004.jpg',
        r'S:\Houdini_Scenes\textures\clothing_005.jpg',
        r'S:\Houdini_Scenes\textures\clothing_006.jpg',
        r'S:\Houdini_Scenes\textures\clothing_007.jpg',
        r'S:\Houdini_Scenes\textures\clothing_008.jpg',
        r'S:\Houdini_Scenes\textures\clothing_009.jpg',
        r'S:\Houdini_Scenes\textures\clothing_010.jpg'
    ]
     
    data = unreal.AutomatedAssetImportData()
    data.set_editor_property('destination_path', "%s/Materials" % destination_path)
    data.set_editor_property('filenames', lst_image_files)
    lst_texture2D = unreal.AssetToolsHelpers.get_asset_tools().import_assets_automated(data)
        
    task = unreal.AssetImportTask()
    agent_count = 1
    for i in range(agent_count):
        task.filename = r"S:\Houdini_Scenes\geo\crowd\tops_agent_%s.abc" % i		#"C:\\Path\\To\\foo.abc"
        task.destination_path = destination_path
        task.destination_name = "GC_agent_%s" % i
        task.replace_existing = True
        task.automated = True

        task.options = unreal.AbcImportSettings()
        task.options.import_type = unreal.AlembicImportType.GEOMETRY_CACHE
        task.options.normal_generation_settings = unreal.AbcNormalGenerationSettings(recompute_normals=True)
        task.options.compression_settings = unreal.AbcCompressionSettings(merge_meshes=True)

        asset_tools = unreal.AssetToolsHelpers.get_asset_tools()
        asset_tools.import_asset_tasks([task])
        
        # Add asset to the world.
        mesh_actor = ell.spawn_actor_from_class(unreal.GeometryCacheActor.static_class(), unreal.Vector(0, 0, 0), unreal.Rotator(0, 0, 0))
        mesh_comp = mesh_actor.get_component_by_class(unreal.GeometryCacheComponent.static_class())
        geo_cache = eal.load_asset("GeometryCache'%s/%s'" % (task.destination_path, task.destination_name))
        mesh_comp.set_geometry_cache(geo_cache)
        
        # Make a material per-agent.
        mat_name = "M_agent_%s" % i
        assetTools = unreal.AssetToolsHelpers.get_asset_tools()
        mf = unreal.MaterialFactoryNew()
        mat_closure = assetTools.create_asset(mat_name, "%s/Materials" % destination_path, unreal.Material, mf)
        
        # Node input connection types.
        # MP_BASE_COLOR, MP_NORMAL, MP_ROUGHNESS, MP_SPECULAR, MP_EMISSIVE_COLOR, MP_OPACITY, MP_AMBIENT_OCCLUSION
        
        mat_node_diffuse = unreal.MaterialEditingLibrary.create_material_expression(mat_closure,unreal.MaterialExpressionTextureSample,-384,-200)
        unreal.MaterialEditingLibrary.connect_material_property(mat_node_diffuse, "RGBA", unreal.MaterialProperty.MP_BASE_COLOR)
        if lst_texture2D[0]!=None:
            mat_node_diffuse.texture = lst_texture2D[random.randint(0, 9)]	#Pick from the 10 agent clothing textures.
        
        # Crude color mixer, can't figure out how to script a MaterialBlendOverlay function yet.
        mat_node_multiply = unreal.MaterialEditingLibrary.create_material_expression(mat_closure,unreal.MaterialExpressionMultiply,-125,50)
        mat_node_color = unreal.MaterialEditingLibrary.create_material_expression(mat_closure,unreal.MaterialExpressionConstant3Vector,-384,150)
        
        # A random bright color.
        r = random.uniform(0.420, 1.0)
        g = random.uniform(0.420, 1.0)
        b = random.uniform(0.420, 1.0)
        value = unreal.LinearColor(r,g,b,1.0)
        mat_node_color.set_editor_property("constant", value)
        
        # Connect nodes.
        unreal.MaterialEditingLibrary.connect_material_expressions(mat_node_diffuse, '', mat_node_multiply, 'a')
        unreal.MaterialEditingLibrary.connect_material_expressions(mat_node_color, '', mat_node_multiply, 'b')
        unreal.MaterialEditingLibrary.connect_material_property(mat_node_multiply, "", unreal.MaterialProperty.MP_BASE_COLOR)
        
        # Assign the material to the mesh component.
        material = eal.load_asset("Material'%s/Materials/%s'" % (task.destination_path, mat_name))
        mesh_comp.set_editor_property("override_materials", [material])
        
alembicBatchImport()