import bpy import os import sys import argparse """ Blender Headless Texture Baker This…
- User
import bpy import os import sys import argparse
""" Blender Headless Texture Baker
This script performs albedo and normal map baking from a high-poly model to a low-poly model using Blender's headless mode.
Usage: blender --background --python combined_texture_baker.py -- [arguments]
Arguments: --high_poly: Path to the high poly model (.obj or .glb file) --low_poly: Path to the low poly model (.obj or .glb file) --albedo_output: Output path for the albedo map (default: "albedo_map.png") --normal_output: Output path for the normal map (default: "normal_map.png") --image_size: Image size for the texture maps (default: 2048) --ray_distance: Ray distance for baking (default: 0.8) --extrusion: Extrusion amount for baking (default: 0.01)
Example: blender --background --python combined_texture_baker.py -- --high_poly "files/high.obj" --low_poly "files/low.obj" --albedo_output "albedo.png" --normal_output "normal.png" --image_size 4096 --ray_distance 1.0 --extrusion 0.02 --parallel
Note: Make sure to run this script with Blender's Python interpreter. """
def setup_scene(): bpy.ops.object.select_all(action='SELECT') bpy.ops.object.delete()
codebpy.context.scene.render.engine = 'CYCLES' bpy.context.scene.cycles.device = 'GPU'def import_models(high_poly_path, low_poly_path): # Import high-poly model if high_poly_path.lower().endswith('.glb'): bpy.ops.import_scene.gltf(filepath=high_poly_path) else: bpy.ops.wm.obj_import(filepath=high_poly_path) high_poly = bpy.context.selected_objects[0] high_poly.name = "HighPoly"
code# Import low-poly model if low_poly_path.lower().endswith('.glb'): bpy.ops.import_scene.gltf(filepath=low_poly_path) else: bpy.ops.wm.obj_import(filepath=low_poly_path) # Find the actual mesh object in the selection low_poly = next((obj for obj in bpy.context.selected_objects if obj.type == 'MESH'), None) if not low_poly: raise ValueError("No mesh object found in low poly import.") low_poly.name = "LowPoly" # Check for extra parent and remove it if low_poly.parent and low_poly.parent.type == 'EMPTY': bpy.data.objects.remove(low_poly.parent, do_unlink=True) return high_poly, low_polydef setup_albedo_material(low_poly, image_size): material = bpy.data.materials.new(name="BakeAlbedo") material.use_nodes = True low_poly.data.materials.append(material)
Assistant