import unreal def ensure_directory(path): if not unreal.EditorAssetLibrary.does_directory_exist(path): unreal.EditorAssetLibrary.make_directory(path) def create_led_material(): package_path = "/Game/LedVolume/Materials" asset_name = "M_LedVoxel" asset_path = f"{package_path}/{asset_name}" ensure_directory("/Game/LedVolume") ensure_directory(package_path) if unreal.EditorAssetLibrary.does_asset_exist(asset_path): material = unreal.EditorAssetLibrary.load_asset(asset_path) usage = getattr(unreal.MaterialUsage, "MATUSAGE_INSTANCED_STATIC_MESHES", None) if usage is not None: unreal.MaterialEditingLibrary.set_base_material_usage(material, usage, True) unreal.MaterialEditingLibrary.recompile_material(material) unreal.EditorAssetLibrary.save_loaded_asset(material) return material asset_tools = unreal.AssetToolsHelpers.get_asset_tools() material = asset_tools.create_asset(asset_name, package_path, unreal.Material, unreal.MaterialFactoryNew()) material.set_editor_property("shading_model", unreal.MaterialShadingModel.MSM_UNLIT) material.set_editor_property("blend_mode", unreal.BlendMode.BLEND_OPAQUE) material.set_editor_property("two_sided", True) mel = unreal.MaterialEditingLibrary usage = getattr(unreal.MaterialUsage, "MATUSAGE_INSTANCED_STATIC_MESHES", None) if usage is not None: mel.set_base_material_usage(material, usage, True) custom_r = mel.create_material_expression(material, unreal.MaterialExpressionPerInstanceCustomData, -700, -220) custom_r.set_editor_property("data_index", 0) custom_g = mel.create_material_expression(material, unreal.MaterialExpressionPerInstanceCustomData, -700, -120) custom_g.set_editor_property("data_index", 1) custom_b = mel.create_material_expression(material, unreal.MaterialExpressionPerInstanceCustomData, -700, -20) custom_b.set_editor_property("data_index", 2) append_rg = mel.create_material_expression(material, unreal.MaterialExpressionAppendVector, -470, -170) append_rgb = mel.create_material_expression(material, unreal.MaterialExpressionAppendVector, -250, -100) mel.connect_material_expressions(custom_r, "", append_rg, "A") mel.connect_material_expressions(custom_g, "", append_rg, "B") mel.connect_material_expressions(append_rg, "", append_rgb, "A") mel.connect_material_expressions(custom_b, "", append_rgb, "B") mel.connect_material_property(append_rgb, "", unreal.MaterialProperty.MP_EMISSIVE_COLOR) mel.recompile_material(material) unreal.EditorAssetLibrary.save_loaded_asset(material) return material def create_gpu_led_material(): package_path = "/Game/LedVolume/Materials" asset_name = "M_LedVoxelGpu" asset_path = f"{package_path}/{asset_name}" ensure_directory("/Game/LedVolume") ensure_directory(package_path) if unreal.EditorAssetLibrary.does_asset_exist(asset_path): material = unreal.EditorAssetLibrary.load_asset(asset_path) usage = getattr(unreal.MaterialUsage, "MATUSAGE_INSTANCED_STATIC_MESHES", None) if material and usage is not None: unreal.MaterialEditingLibrary.set_base_material_usage(material, usage, True) unreal.MaterialEditingLibrary.recompile_material(material) unreal.EditorAssetLibrary.save_loaded_asset(material) if material: return material asset_tools = unreal.AssetToolsHelpers.get_asset_tools() material = asset_tools.create_asset(asset_name, package_path, unreal.Material, unreal.MaterialFactoryNew()) material.set_editor_property("shading_model", unreal.MaterialShadingModel.MSM_UNLIT) material.set_editor_property("blend_mode", unreal.BlendMode.BLEND_OPAQUE) material.set_editor_property("two_sided", True) mel = unreal.MaterialEditingLibrary usage = getattr(unreal.MaterialUsage, "MATUSAGE_INSTANCED_STATIC_MESHES", None) if usage is not None: mel.set_base_material_usage(material, usage, True) world_pos = mel.create_material_expression(material, unreal.MaterialExpressionWorldPosition, -920, -260) time_expr = mel.create_material_expression(material, unreal.MaterialExpressionTime, -920, -120) spacing = mel.create_material_expression(material, unreal.MaterialExpressionScalarParameter, -920, 20) spacing.set_editor_property("parameter_name", "VoxelSpacing") spacing.set_editor_property("default_value", 8.0) speed = mel.create_material_expression(material, unreal.MaterialExpressionScalarParameter, -920, 140) speed.set_editor_property("parameter_name", "AnimationSpeed") speed.set_editor_property("default_value", 1.0) pattern = mel.create_material_expression(material, unreal.MaterialExpressionScalarParameter, -920, 260) pattern.set_editor_property("parameter_name", "PatternMode") pattern.set_editor_property("default_value", 2.0) brightness = mel.create_material_expression(material, unreal.MaterialExpressionScalarParameter, -920, 380) brightness.set_editor_property("parameter_name", "Brightness") brightness.set_editor_property("default_value", 18.0) gamma = mel.create_material_expression(material, unreal.MaterialExpressionScalarParameter, -920, 500) gamma.set_editor_property("parameter_name", "Gamma") gamma.set_editor_property("default_value", 1.0) solid = mel.create_material_expression(material, unreal.MaterialExpressionVectorParameter, -920, 620) solid.set_editor_property("parameter_name", "LedColor") solid.set_editor_property("default_value", unreal.LinearColor(1.0, 0.2, 0.05, 1.0)) custom = mel.create_material_expression(material, unreal.MaterialExpressionCustom, -520, 120) custom.set_editor_property("description", "GPU LED volumetric test pattern") custom.set_editor_property("output_type", unreal.CustomMaterialOutputType.CMOT_FLOAT3) custom.set_editor_property("code", """ float safeSpacing = max(VoxelSpacing, 0.001); float3 p = WorldPos / safeSpacing; float t = TimeValue * AnimationSpeed; float mode = PatternMode; float3 solid = saturate(SolidColor.rgb); float3 color = solid; if (mode > 0.5 && mode < 1.5) { color = frac(abs(p) / 128.0); } else if (mode >= 1.5 && mode < 2.5) { float phase = dot(p, float3(0.071, 0.053, 0.041)) + t; color = 0.5 + 0.5 * sin(float3(phase, phase + 2.0943951, phase + 4.1887902)); } else if (mode >= 2.5) { float wave = sin(t) * 0.5 + 0.5; float radius = lerp(12.0, 52.0, wave); float d = abs(length(p) - radius); float shell = saturate(1.0 - d / 4.0); float phase = dot(p, float3(0.09, 0.04, 0.07)) + t; color = shell * (0.35 + 0.65 * (0.5 + 0.5 * sin(float3(phase, phase + 2.0943951, phase + 4.1887902)))); } float safeGamma = max(GammaValue, 0.001); return pow(max(color, 0.0), safeGamma) * BrightnessValue; """) def custom_input(name): item = unreal.CustomInput() item.set_editor_property("input_name", name) return item custom.set_editor_property("inputs", [ custom_input("WorldPos"), custom_input("TimeValue"), custom_input("VoxelSpacing"), custom_input("AnimationSpeed"), custom_input("PatternMode"), custom_input("BrightnessValue"), custom_input("GammaValue"), custom_input("SolidColor"), ]) mel.connect_material_expressions(world_pos, "", custom, "WorldPos") mel.connect_material_expressions(time_expr, "", custom, "TimeValue") mel.connect_material_expressions(spacing, "", custom, "VoxelSpacing") mel.connect_material_expressions(speed, "", custom, "AnimationSpeed") mel.connect_material_expressions(pattern, "", custom, "PatternMode") mel.connect_material_expressions(brightness, "", custom, "BrightnessValue") mel.connect_material_expressions(gamma, "", custom, "GammaValue") mel.connect_material_expressions(solid, "", custom, "SolidColor") mel.connect_material_property(custom, "", unreal.MaterialProperty.MP_EMISSIVE_COLOR) mel.recompile_material(material) unreal.EditorAssetLibrary.save_loaded_asset(material) return material def create_raymarch_volume_material(): package_path = "/Game/LedVolume/Materials" asset_name = "M_LedVolumeRaymarch" asset_path = f"{package_path}/{asset_name}" ensure_directory("/Game/LedVolume") ensure_directory(package_path) if unreal.EditorAssetLibrary.does_asset_exist(asset_path): material = unreal.EditorAssetLibrary.load_asset(asset_path) if material: return material asset_tools = unreal.AssetToolsHelpers.get_asset_tools() material = asset_tools.create_asset(asset_name, package_path, unreal.Material, unreal.MaterialFactoryNew()) material.set_editor_property("shading_model", unreal.MaterialShadingModel.MSM_UNLIT) material.set_editor_property("blend_mode", unreal.BlendMode.BLEND_ADDITIVE) material.set_editor_property("two_sided", False) mel = unreal.MaterialEditingLibrary usage = getattr(unreal.MaterialUsage, "MATUSAGE_INSTANCED_STATIC_MESHES", None) if usage is not None: mel.set_base_material_usage(material, usage, True) world_pos = mel.create_material_expression(material, unreal.MaterialExpressionWorldPosition, -980, -320) camera_pos = mel.create_material_expression(material, unreal.MaterialExpressionCameraPositionWS, -980, -200) time_expr = mel.create_material_expression(material, unreal.MaterialExpressionTime, -980, -80) def scalar_param(name, default, y): node = mel.create_material_expression(material, unreal.MaterialExpressionScalarParameter, -980, y) node.set_editor_property("parameter_name", name) node.set_editor_property("default_value", default) return node def vector_param(name, color, y): node = mel.create_material_expression(material, unreal.MaterialExpressionVectorParameter, -980, y) node.set_editor_property("parameter_name", name) node.set_editor_property("default_value", color) return node spacing = scalar_param("VoxelSpacing", 8.0, 40) speed = scalar_param("AnimationSpeed", 1.0, 160) pattern = scalar_param("PatternMode", 2.0, 280) brightness = scalar_param("Brightness", 4.0, 400) gamma = scalar_param("Gamma", 1.0, 520) radius = scalar_param("PixelRadiusFraction", 0.5, 640) shape = scalar_param("VoxelShapeMode", 1.0, 700) solid = vector_param("LedColor", unreal.LinearColor(1.0, 0.2, 0.05, 1.0), 760) center = vector_param("VolumeCenter", unreal.LinearColor(0.0, 0.0, 150.0, 1.0), 880) extent = vector_param("VolumeExtent", unreal.LinearColor(512.0, 512.0, 512.0, 1.0), 1000) custom = mel.create_material_expression(material, unreal.MaterialExpressionCustom, -520, 220) custom.set_editor_property("description", "GPU raymarched LED volume") custom.set_editor_property("output_type", unreal.CustomMaterialOutputType.CMOT_FLOAT3) custom.set_editor_property("code", """ float3 center = VolumeCenter.xyz; float3 extent = max(VolumeExtent.xyz, 1.0); float3 ro = CameraPos; float3 rd = normalize(WorldPos - CameraPos); float3 bmin = center - extent; float3 bmax = center + extent; float3 invR = 1.0 / max(abs(rd), 0.00001) * sign(rd); float3 tbot = (bmin - ro) * invR; float3 ttop = (bmax - ro) * invR; float3 tmin3 = min(ttop, tbot); float3 tmax3 = max(ttop, tbot); float t0 = max(max(tmin3.x, tmin3.y), tmin3.z); float t1 = min(min(tmax3.x, tmax3.y), tmax3.z); if (t1 <= max(t0, 0.0)) { return 0.0; } t0 = max(t0, 0.0); float safeSpacing = max(VoxelSpacing, 0.001); float radius = saturate(PixelRadiusFraction) * 0.22; float t = TimeValue * AnimationSpeed; float3 accum = 0.0; float coverage = 0.0; const int Steps = 128; float stepLen = (t1 - t0) / Steps; for (int i = 0; i < Steps; ++i) { float3 p = ro + rd * (t0 + (i + 0.5) * stepLen); float3 grid = (p - bmin) / safeSpacing; float3 cell = abs(frac(grid) - 0.5); float sphereD = length(cell); float cubeD = max(max(cell.x, cell.y), cell.z); float lensD = max(length(cell.xy), cell.z * 2.8); float shapeMode = VoxelShapeMode; float d = sphereD; if (shapeMode > 0.5 && shapeMode < 1.5) { d = cubeD; } else if (shapeMode > 1.5 && shapeMode < 2.5) { d = lensD; } float edge = max(radius * 0.12, 0.01); float led = 1.0 - smoothstep(radius - edge, radius + edge, d); float3 color = saturate(SolidColor.rgb); if (PatternMode > 0.5 && PatternMode < 1.5) { color = saturate((p - bmin) / max(extent * 2.0, 1.0)); } else if (PatternMode >= 1.5 && PatternMode < 2.5) { float phase = dot(grid, float3(0.071, 0.053, 0.041)) + t; color = 0.5 + 0.5 * sin(float3(phase, phase + 2.0943951, phase + 4.1887902)); } else if (PatternMode >= 2.5) { float shellRadius = lerp(8.0, 54.0, sin(t) * 0.5 + 0.5); float shell = saturate(1.0 - abs(length(grid - 64.0) - shellRadius) / 4.0); float phase = dot(grid, float3(0.09, 0.04, 0.07)) + t; color = shell * (0.35 + 0.65 * (0.5 + 0.5 * sin(float3(phase, phase + 2.0943951, phase + 4.1887902)))); } float sampleWeight = led * 0.18; accum = max(accum, color * sampleWeight); coverage = max(coverage, led); } accum = pow(max(accum, 0.0), max(GammaValue, 0.001)) * BrightnessValue * saturate(coverage); return accum; """) def custom_input(name): item = unreal.CustomInput() item.set_editor_property("input_name", name) return item custom.set_editor_property("inputs", [ custom_input("WorldPos"), custom_input("CameraPos"), custom_input("TimeValue"), custom_input("VoxelSpacing"), custom_input("AnimationSpeed"), custom_input("PatternMode"), custom_input("BrightnessValue"), custom_input("GammaValue"), custom_input("PixelRadiusFraction"), custom_input("VoxelShapeMode"), custom_input("SolidColor"), custom_input("VolumeCenter"), custom_input("VolumeExtent"), ]) for node, name in [ (world_pos, "WorldPos"), (camera_pos, "CameraPos"), (time_expr, "TimeValue"), (spacing, "VoxelSpacing"), (speed, "AnimationSpeed"), (pattern, "PatternMode"), (brightness, "BrightnessValue"), (gamma, "GammaValue"), (radius, "PixelRadiusFraction"), (shape, "VoxelShapeMode"), (solid, "SolidColor"), (center, "VolumeCenter"), (extent, "VolumeExtent"), ]: mel.connect_material_expressions(node, "", custom, name) mel.connect_material_property(custom, "", unreal.MaterialProperty.MP_EMISSIVE_COLOR) mel.recompile_material(material) unreal.EditorAssetLibrary.save_loaded_asset(material) return material def create_physical_solid_material(asset_name, color, translucent=False, opacity=1.0): package_path = "/Game/LedVolume/Materials" asset_path = f"{package_path}/{asset_name}" ensure_directory("/Game/LedVolume") ensure_directory(package_path) if unreal.EditorAssetLibrary.does_asset_exist(asset_path): material = unreal.EditorAssetLibrary.load_asset(asset_path) if material: return material asset_tools = unreal.AssetToolsHelpers.get_asset_tools() material = asset_tools.create_asset(asset_name, package_path, unreal.Material, unreal.MaterialFactoryNew()) material.set_editor_property("shading_model", unreal.MaterialShadingModel.MSM_DEFAULT_LIT) material.set_editor_property("blend_mode", unreal.BlendMode.BLEND_TRANSLUCENT if translucent else unreal.BlendMode.BLEND_OPAQUE) material.set_editor_property("two_sided", True) mel = unreal.MaterialEditingLibrary usage = getattr(unreal.MaterialUsage, "MATUSAGE_INSTANCED_STATIC_MESHES", None) if usage is not None: mel.set_base_material_usage(material, usage, True) color_param = mel.create_material_expression(material, unreal.MaterialExpressionVectorParameter, -500, -120) color_param.set_editor_property("parameter_name", "WireColor" if translucent else "BaseColor") color_param.set_editor_property("default_value", color) mel.connect_material_property(color_param, "", unreal.MaterialProperty.MP_BASE_COLOR) roughness = mel.create_material_expression(material, unreal.MaterialExpressionScalarParameter, -500, 20) roughness.set_editor_property("parameter_name", "Roughness") roughness.set_editor_property("default_value", 0.88) mel.connect_material_property(roughness, "", unreal.MaterialProperty.MP_ROUGHNESS) if translucent: opacity_param = mel.create_material_expression(material, unreal.MaterialExpressionScalarParameter, -500, 160) opacity_param.set_editor_property("parameter_name", "WireOpacity") opacity_param.set_editor_property("default_value", opacity) mel.connect_material_property(opacity_param, "", unreal.MaterialProperty.MP_OPACITY) mel.recompile_material(material) unreal.EditorAssetLibrary.save_loaded_asset(material) return material def main(): material = create_led_material() unreal.log(f"Created LED material: {material.get_path_name()}") gpu_material = create_gpu_led_material() unreal.log(f"Created GPU LED material: {gpu_material.get_path_name()}") raymarch_material = create_raymarch_volume_material() unreal.log(f"Created GPU raymarch material: {raymarch_material.get_path_name()}") cabinet_material = create_physical_solid_material("M_PhysicalCabinet", unreal.LinearColor(0.005, 0.005, 0.004, 1.0), False, 1.0) unreal.log(f"Created physical cabinet material: {cabinet_material.get_path_name()}") wire_material = create_physical_solid_material("M_PhysicalWire", unreal.LinearColor(0.92, 0.92, 0.86, 0.62), True, 0.62) unreal.log(f"Created physical wire material: {wire_material.get_path_name()}") fixer_material = create_physical_solid_material("M_PhysicalFixer", unreal.LinearColor(0.72, 0.72, 0.68, 0.86), True, 0.86) unreal.log(f"Created physical fixer material: {fixer_material.get_path_name()}") if __name__ == "__main__": main()