Skip to content

Instantly share code, notes, and snippets.

@andybak
Created March 11, 2025 14:12
Show Gist options
  • Save andybak/890a9efc19689b5b755e9d5c26315c73 to your computer and use it in GitHub Desktop.
Save andybak/890a9efc19689b5b755e9d5c26315c73 to your computer and use it in GitHub Desktop.
@tool
extends EditorScenePostImport
var MATERIAL_REPLACEMENTS := {}
var TEXTURES := {}
func _init():
setup_textures()
setup_materials()
func setup_textures():
TEXTURES["Material_001"] = load("res://addons/gltf_material_swapper/textures/material_001.png")
TEXTURES["Material_002"] = load("res://addons/gltf_material_swapper/textures/material_002.png")
TEXTURES["Metallic"] = load("res://addons/gltf_material_swapper/textures/metallic.png")
func setup_materials():
MATERIAL_REPLACEMENTS["Material_001"] = StandardMaterial3D.new()
MATERIAL_REPLACEMENTS["Material_001"].albedo_color = Color(1, 0, 0)
MATERIAL_REPLACEMENTS["Material_001"].albedo_texture = TEXTURES.get("Material_001")
MATERIAL_REPLACEMENTS["Material_002"] = StandardMaterial3D.new()
MATERIAL_REPLACEMENTS["Material_002"].albedo_color = Color(0, 1, 0)
MATERIAL_REPLACEMENTS["Material_002"].albedo_texture = TEXTURES.get("Material_002")
MATERIAL_REPLACEMENTS["Metallic"] = StandardMaterial3D.new()
MATERIAL_REPLACEMENTS["Metallic"].metallic = 1.0
MATERIAL_REPLACEMENTS["Metallic"].metallic_texture = TEXTURES.get("Metallic")
func _post_import(scene: Node) -> Object:
if not get_source_file().ends_with(".gltf") and not get_source_file().ends_with(".glb"):
return scene # Skip non-GLTF imports
var meta := get_import_metadata()
if meta.has("materials_swapped") and meta["materials_swapped"] == true:
return scene # Skip if already processed
swap_materials(scene)
meta["materials_swapped"] = true
return scene
func swap_materials(node: Node):
if node is MeshInstance3D:
var mesh_instance := node as MeshInstance3D
for i in range(mesh_instance.get_surface_material_count()):
var material = mesh_instance.get_surface_material(i)
if material:
var material_name = material.resource_name
if material_name in MATERIAL_REPLACEMENTS and mesh_instance.get_surface_material(i) != MATERIAL_REPLACEMENTS[material_name]:
mesh_instance.set_surface_material(i, MATERIAL_REPLACEMENTS[material_name])
for child in node.get_children():
swap_materials(child)
@tool
extends EditorPlugin
var gltf_post_import_plugin
func _enter_tree():
gltf_post_import_plugin = load("res://addons/gltf_material_swapper/importer.gd").new()
add_scene_post_import_plugin(gltf_post_import_plugin)
func _exit_tree():
if gltf_post_import_plugin:
remove_scene_post_import_plugin(gltf_post_import_plugin)
gltf_post_import_plugin = null
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment