-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathbatch_translucent_materials.py
More file actions
87 lines (67 loc) · 2.83 KB
/
batch_translucent_materials.py
File metadata and controls
87 lines (67 loc) · 2.83 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
r"""
batch_translucent_materials.py
Run from UE5 Output Log Python input bar:
exec(open(r"C:/Users/Kyron/OneDrive/Documents/Unreal Projects/ProjectSpiritless/batch_translucent_materials.py").read())
Patches every Material in /Game/sA_StylizedForest_Environment to Masked blend
mode and adds a ScalarParameter("Opacity", 1.0) wired to OpacityMask, so the
camera-occlusion C++ system can hide occluding forest geometry at runtime.
Masked is used instead of Translucent because the forest pack uses Nanite meshes
and Nanite does not support Translucent blend mode.
Already-Masked materials are skipped so re-runs are safe.
"""
import unreal
mat_lib = unreal.MaterialEditingLibrary
asset_lib = unreal.EditorAssetLibrary
reg = unreal.AssetRegistryHelpers.get_asset_registry()
FOLDER = "/Game/sA_StylizedForest_Environment"
PARAM_NAME = "Opacity"
filt = unreal.ARFilter(
class_names = ["Material"],
package_paths = [FOLDER],
recursive_paths = True,
)
asset_data_list = reg.get_assets(filt)
total = len(asset_data_list)
updated = 0
skipped = 0
failed = 0
with unreal.ScopedSlowTask(total, "Patching forest materials…") as task:
task.make_dialog(True)
for asset_data in asset_data_list:
if task.should_cancel():
unreal.log_warning("[BatchMaterials] Cancelled by user.")
break
task.enter_progress_frame(1, str(asset_data.package_name))
mat = asset_data.get_asset()
if not isinstance(mat, unreal.Material):
skipped += 1
continue
# Skip already-Masked — already processed
if mat.get_editor_property("blend_mode") == unreal.BlendMode.BLEND_MASKED:
skipped += 1
continue
try:
# Add ScalarParameter("Opacity", 1.0) wired directly to OpacityMask.
# The C++ occlusion system drives this from 1.0 (visible) to 0.0 (hidden).
opacity_node = mat_lib.create_material_expression(
mat,
unreal.MaterialExpressionScalarParameter,
-400, 400,
)
opacity_node.set_editor_property("parameter_name", PARAM_NAME)
opacity_node.set_editor_property("default_value", 1.0)
mat_lib.connect_material_property(
opacity_node, "",
unreal.MaterialProperty.MP_OPACITY_MASK,
)
mat.set_editor_property("blend_mode", unreal.BlendMode.BLEND_MASKED)
mat_lib.recompile_material(mat)
asset_lib.save_loaded_asset(mat, False)
updated += 1
except Exception as e:
unreal.log_error(f"[BatchMaterials] Failed on {asset_data.package_name}: {e}")
failed += 1
unreal.log(
f"[BatchMaterials] Done — {updated} updated, {skipped} skipped, {failed} failed "
f"(out of {total} total)."
)