cntrl/tools/blender/bake_cgtrader_concrete.py

111 lines
3.9 KiB
Python

import math
import os
import subprocess
import sys
import bpy
REPO = "/home/dandovlatov/Documents/cntrl"
SRC = os.path.join(REPO, "ConcreteMaterial.blend")
TEX = os.path.join(REPO, "assets", "textures", "concrete")
if not os.path.exists(SRC):
sys.exit("missing " + SRC + " — download ConcreteMaterial.blend from CGTrader into the repo root")
if os.path.basename(bpy.data.filepath) != "ConcreteMaterial.blend":
sys.exit("run with: blender -b ConcreteMaterial.blend --python tools/blender/bake_cgtrader_concrete.py")
sc = bpy.context.scene
mat = bpy.data.materials["Concrete"]
nt = mat.node_tree
nodes, links = nt.nodes, nt.links
uv = nodes.new("ShaderNodeUVMap")
uv.uv_map = "UVMap"
sep = nodes.new("ShaderNodeSeparateXYZ")
links.new(uv.outputs["UV"], sep.inputs["Vector"])
def math_node(op, source, value=None):
n = nodes.new("ShaderNodeMath")
n.operation = op
links.new(source, n.inputs[0])
if value is not None:
n.inputs[1].default_value = value
return n
mu = math_node("MULTIPLY", sep.outputs["X"], 2.0 * math.pi)
mv = math_node("MULTIPLY", sep.outputs["Y"], 2.0 * math.pi)
cu, su, cv, sv = (math_node(op, m.outputs[0]) for op, m in
(("COSINE", mu), ("SINE", mu), ("COSINE", mv), ("SINE", mv)))
comb = nodes.new("ShaderNodeCombineXYZ")
links.new(cu.outputs[0], comb.inputs["X"])
links.new(su.outputs[0], comb.inputs["Y"])
links.new(cv.outputs[0], comb.inputs["Z"])
for link in [l for l in links if l.from_node.type == "TEX_COORD"]:
links.new(comb.outputs["Vector"], link.to_socket)
for n in nodes:
if n.type == "TEX_NOISE":
n.noise_dimensions = "4D"
n.inputs["Detail"].default_value = min(n.inputs["Detail"].default_value, 6.0)
links.new(sv.outputs[0], n.inputs["W"])
elif n.type == "TEX_VORONOI":
n.voronoi_dimensions = "4D"
links.new(sv.outputs[0], n.inputs["W"])
bump = next(n for n in nodes if n.name == "Bump")
bw = nodes.new("ShaderNodeRGBToBW")
links.new(bump.inputs["Height"].links[0].from_socket, bw.inputs["Color"])
rng = nodes.new("ShaderNodeMapRange")
rng.inputs["To Min"].default_value = 0.28
rng.inputs["To Max"].default_value = 0.78
links.new(bw.outputs[0], rng.inputs["Value"])
bsdf = next(n for n in nodes if n.type == "BSDF_PRINCIPLED")
links.new(rng.outputs["Result"], bsdf.inputs["Roughness"])
bpy.ops.mesh.primitive_plane_add(size=2.0)
plane = bpy.context.object
plane.data.materials.append(mat)
bpy.ops.object.select_all(action="DESELECT")
plane.select_set(True)
bpy.context.view_layer.objects.active = plane
sc.render.engine = "CYCLES"
sc.cycles.samples = 1
sc.cycles.use_denoising = False
sc.render.resolution_x = sc.render.resolution_y = 1024
sc.render.resolution_percentage = 100
sc.render.bake.use_pass_direct = False
sc.render.bake.use_pass_indirect = False
sc.render.bake.use_pass_color = True
sc.render.bake.use_selected_to_active = False
sc.render.bake.margin = 8
bake_node = nodes.new("ShaderNodeTexImage")
bake_node.location = (600, -400)
def bake(name, bake_type):
img = bpy.data.images.new(name, 1024, 1024, alpha=False)
img.colorspace_settings.name = "sRGB" if name == "albedo" else "Non-Color"
bake_node.image = img
nodes.active = bake_node
bake_node.select = True
print("[bake]", name, bake_type, flush=True)
bpy.ops.object.bake(type=bake_type)
img.filepath_raw = "/tmp/opencode/" + name + ".png"
img.file_format = "PNG"
img.save()
bake_node.select = False
bake("albedo", "DIFFUSE")
bake("normal", "NORMAL")
bake("roughness", "ROUGHNESS")
cracks = next(n for n in nodes if n.name == "ColorRamp.002")
bw2 = nodes.new("ShaderNodeRGBToBW")
links.new(cracks.outputs["Color"], bw2.inputs["Color"])
rng2 = nodes.new("ShaderNodeMapRange")
links.new(bw2.outputs[0], rng2.inputs["Value"])
links.new(rng2.outputs["Result"], bsdf.inputs["Roughness"])
bake("height_voronoi", "ROUGHNESS")
subprocess.run(["python3", os.path.join(REPO, "tools", "blender",
"process_concrete_maps.py")], check=True)