Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- import bpy
- import os
- # Input .glb file path
- input_file = r"path_to_your_file.glb" # Replace with your .glb file path (use raw string `r""` for Windows paths)
- # Normalize the path to use forward slashes
- input_file = input_file.replace("\\", "/")
- # Automatically determine output paths
- base_dir = os.path.dirname(input_file)
- file_name = os.path.splitext(os.path.basename(input_file))[0]
- output_texture_dir = os.path.join(base_dir, file_name + "_textures").replace("\\", "/")
- output_file = os.path.join(base_dir, file_name + ".fbx").replace("\\", "/")
- # Ensure the texture directory exists
- if not os.path.exists(output_texture_dir):
- os.makedirs(output_texture_dir)
- # Clear existing scene data
- bpy.ops.wm.read_factory_settings(use_empty=True)
- # Import the .glb file
- bpy.ops.import_scene.gltf(filepath=input_file)
- # Extract textures from materials
- for material in bpy.data.materials:
- if material.use_nodes: # Ensure material uses node-based system
- for node in material.node_tree.nodes:
- if node.type == 'TEX_IMAGE': # Look for image texture nodes
- image = node.image
- if image:
- # Generate texture path
- texture_path = os.path.join(output_texture_dir, f"{image.name}.png").replace("\\", "/")
- if image.filepath:
- # Try to copy texture from its existing path
- try:
- original_path = bpy.path.abspath(image.filepath).replace("\\", "/")
- if os.path.exists(original_path):
- texture_path = os.path.join(output_texture_dir,
- os.path.basename(original_path)).replace("\\", "/")
- if not os.path.exists(texture_path):
- with open(original_path, "rb") as src, open(texture_path, "wb") as dst:
- dst.write(src.read())
- print(f"Texture copied: {texture_path}")
- except Exception as e:
- print(f"Failed to copy texture {image.name}: {e}")
- else:
- # Save rendered texture if no file path is found
- image.save_render(texture_path)
- print(f"Texture saved: {texture_path}")
- # Export the scene to .fbx
- bpy.ops.export_scene.fbx(filepath=output_file)
- print(f"FBX file saved: {output_file}")
- print(f"Textures saved in folder: {output_texture_dir}")
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement