acoolrocket

Blender - Bulk change OpenGL to DirectX

Oct 9th, 2026
16
0
Never
1
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.37 KB | None | 0 0
  1. """Convert OpenGL normal-map nodes to DirectX normal-map nodes in this scene.
  2.  
  3. Run this script from Blender's Text Editor. It updates every Normal Map node in
  4. materials assigned to objects in the active scene, including nodes inside nested
  5. shader node groups.
  6. """
  7.  
  8. import bpy
  9.  
  10.  
  11. def iter_material_node_trees(material):
  12. """Yield the material node tree and any nested shader node-group trees once."""
  13. visited_trees = set()
  14.  
  15. def visit(node_tree):
  16. if node_tree is None or node_tree.as_pointer() in visited_trees:
  17. return
  18.  
  19. visited_trees.add(node_tree.as_pointer())
  20. yield node_tree
  21.  
  22. for node in node_tree.nodes:
  23. if node.type == 'GROUP' and node.node_tree is not None:
  24. yield from visit(node.node_tree)
  25.  
  26. yield from visit(material.node_tree)
  27.  
  28.  
  29. def scene_materials(scene):
  30. """Yield each material assigned to an object in *scene* once."""
  31. seen_materials = set()
  32.  
  33. for obj in scene.objects:
  34. for material_slot in obj.material_slots:
  35. material = material_slot.material
  36. if material is None or material.as_pointer() in seen_materials:
  37. continue
  38.  
  39. seen_materials.add(material.as_pointer())
  40. yield material
  41.  
  42.  
  43. def convert_opengl_normal_maps_to_directx(scene=None):
  44. """Set OpenGL Normal Map node conventions to DirectX and return counts."""
  45. scene = scene or bpy.context.scene
  46. material_count = 0
  47. converted_count = 0
  48.  
  49. for material in scene_materials(scene):
  50. if not material.use_nodes or material.node_tree is None:
  51. continue
  52.  
  53. material_count += 1
  54. for node_tree in iter_material_node_trees(material):
  55. for node in node_tree.nodes:
  56. if node.type != 'NORMAL_MAP':
  57. continue
  58.  
  59. # Blender 5.2's Normal Map node Mode dropdown; this does not
  60. # modify the RGB values in the source image.
  61. if node.convention == 'OPENGL':
  62. node.convention = 'DIRECTX'
  63. converted_count += 1
  64.  
  65. print(
  66. f"OpenGL to DirectX Normal Map: converted {converted_count} node(s) "
  67. f"across {material_count} scene material(s)."
  68. )
  69. return converted_count, material_count
  70.  
  71.  
  72. if __name__ == "__main__":
  73. convert_opengl_normal_maps_to_directx()
Advertisement
Comments
  • User was banned
Add Comment
Please, Sign In to add comment