Skip to content

Instantly share code, notes, and snippets.

@manmohanbishnoi
Created December 18, 2020 10:11
Show Gist options
  • Save manmohanbishnoi/742666f1c0b40ba17f593308a4f69a6b to your computer and use it in GitHub Desktop.
Save manmohanbishnoi/742666f1c0b40ba17f593308a4f69a6b to your computer and use it in GitHub Desktop.
Texture map bake script for Blender 2.7x
import bpy
import time
# execute with
# blender .\My3DScene.blend --background --python .\bake_texture_maps.py
ops = bpy.ops
mesh = bpy.ops.mesh
# Scene containing objects
scene = bpy.context.scene
# Set render engine to cycles and set device = "GPU",
# this will ensure the baking happens on GPU only
bpy.context.scene.render.engine = "CYCLES"
bpy.context.scene.cycles.device = "GPU"
perfs = bpy.context.user_preferences.addons['cycles'].preferences
print("Compute Device: " + perfs.compute_device_type)
for d in perfs.devices:
print(" + " + d.name)
# For AMD GPUs
# compute_device_type = "OPENCL"
# For NVIDIA GPUs
# compute_device_type = "CUDA"
bpy.context.user_preferences.addons['cycles'].preferences.compute_device_type = "CUDA"
bpy.context.user_preferences.addons['cycles'].preferences.devices[0].use = True
file_data = bpy.data
def scene_update(context):
if bpy.data.objects.is_updated:
print("objects updated")
if bpy.data.images.is_updated:
print("images updated")
#bpy.app.handlers.scene_update_post.append(scene_update)
# Baked image texture resolution; You can set this to the desired texture resolution
# For lightmaps 128px seems appropriate to use with THREE.js
bake_resolution = 128
for o in file_data.objects:
if o.type == "MESH":
print(o.name)
scene.objects.active = o
ops.object.mode_set(mode="EDIT")
ops.mesh.uv_texture_add()
o.data.uv_layers[1].name = "UVAOMap"
ops.uv.lightmap_pack(PREF_IMG_PX_SIZE=bake_resolution)
###############################
#for m in bpy.data.materials:
# node_tree_2 = m.node_tree
###############################
node_tree = bpy.context.active_object.material_slots[0].material.node_tree
node = node_tree.nodes.new(type="ShaderNodeTexImage")
node.name = "BakeTexNode"
node.select = True
node_tree.nodes.active = node
image = bpy.data.images.new(name= o.name + "_Bake", width=bake_resolution, height= bake_resolution)
node.image = image
#o.data.uv_textures[0].data[0].image = image
file_data.screens["UV Editing"].areas[1].spaces[0].image = image
# Bake maps type,
# -----------------------------------------------------
# | Map Type | Blender Cycles Value |
# -----------------------------------------------------
# | Ambient Occlusion : AO |
# | Shadow : SHADOW |
# | Normal : NORMAL |
# -----------------------------------------------------
ops.object.mode_set(mode="OBJECT")
o.select = True
# Bake the AO map
file_data.scenes["Scene"].render.bake_margin = 4
file_data.worlds["World"].light_settings.use_ambient_occlusion = True
file_data.worlds["World"].light_settings.samples = 10
# TODO: improve the below command with parameters
# use_clear = True ; To clear the image before baking
# uv_layer = "UV_AOMap" ; data.uv_layers[1].name
ops.object.bake(type="AO")
image.filepath_raw = "D:\\0\\" + o.name + "_AO" + ".png"
image.file_format = "PNG"
image.save()
ops.wm.save_as_mainfile(filepath="./My3DScene_baked.blend")
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment