Advertisement
Guest User

Blender Convert GLB

a guest
Dec 10th, 2024
24
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.60 KB | None | 0 0
  1. import bpy
  2. import os
  3.  
  4. # Input .glb file path
  5. input_file = r"path_to_your_file.glb" # Replace with your .glb file path (use raw string `r""` for Windows paths)
  6.  
  7. # Normalize the path to use forward slashes
  8. input_file = input_file.replace("\\", "/")
  9.  
  10. # Automatically determine output paths
  11. base_dir = os.path.dirname(input_file)
  12. file_name = os.path.splitext(os.path.basename(input_file))[0]
  13. output_texture_dir = os.path.join(base_dir, file_name + "_textures").replace("\\", "/")
  14. output_file = os.path.join(base_dir, file_name + ".fbx").replace("\\", "/")
  15.  
  16. # Ensure the texture directory exists
  17. if not os.path.exists(output_texture_dir):
  18. os.makedirs(output_texture_dir)
  19.  
  20. # Clear existing scene data
  21. bpy.ops.wm.read_factory_settings(use_empty=True)
  22.  
  23. # Import the .glb file
  24. bpy.ops.import_scene.gltf(filepath=input_file)
  25.  
  26. # Extract textures from materials
  27. for material in bpy.data.materials:
  28. if material.use_nodes: # Ensure material uses node-based system
  29. for node in material.node_tree.nodes:
  30. if node.type == 'TEX_IMAGE': # Look for image texture nodes
  31. image = node.image
  32. if image:
  33. # Generate texture path
  34. texture_path = os.path.join(output_texture_dir, f"{image.name}.png").replace("\\", "/")
  35.  
  36. if image.filepath:
  37. # Try to copy texture from its existing path
  38. try:
  39. original_path = bpy.path.abspath(image.filepath).replace("\\", "/")
  40. if os.path.exists(original_path):
  41. texture_path = os.path.join(output_texture_dir,
  42. os.path.basename(original_path)).replace("\\", "/")
  43. if not os.path.exists(texture_path):
  44. with open(original_path, "rb") as src, open(texture_path, "wb") as dst:
  45. dst.write(src.read())
  46. print(f"Texture copied: {texture_path}")
  47. except Exception as e:
  48. print(f"Failed to copy texture {image.name}: {e}")
  49. else:
  50. # Save rendered texture if no file path is found
  51. image.save_render(texture_path)
  52. print(f"Texture saved: {texture_path}")
  53.  
  54. # Export the scene to .fbx
  55. bpy.ops.export_scene.fbx(filepath=output_file)
  56. print(f"FBX file saved: {output_file}")
  57. print(f"Textures saved in folder: {output_texture_dir}")
  58.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement