"""Real surfaces for the pressure door, the same way the docking arm was done (models/docking-arm/build/surfaces.py, whose node recipe is copied here): the user's rule is "realistic with good textures" from the first version. * tiling sets from the user's own Season 2 pack (already in the Unreal project), read in place by relative path from models/vessel-interior/textures/ModSci_Engineer/Textures: ScratchedMetal under the paint, StainedSteel for bare steel, Glass_DirtRough as a smudge mask, T_Scratch_Norm for fine scratches on the steel; * one map baked from the model in Cycles into a shared atlas, textures/T_PD_Masks.png (R occlusion, G edges, B crevices): a little shade in crevices, a few chips on exposed edges. The ship is clean: low wear, no rust; * generated here: textures/T_PD_Hazard_BaseColor.png (diagonal red/white stripes, seamless, 0.40 m a repeat) and textures/T_PD_Screen_Emissive.png (the control panel's status screen, no text). UV layers (order matters for Unreal, which builds tangents from the first): UVMap (UV0) unique unwrap inside the mesh's square of the shared atlas (the baked masks); the screens 0..1 UV_Tile (UV1) metres, box-projected, for the tiling sets The paint colour follows the reference pictures (pale steel blue); in Unreal it is one parameter of the material. run() does everything; it is safe to run again. """ import bpy, bmesh, math, os, time import numpy as np FOLDER = r"D:\HADAL - The Descent\models\pressure-door" TEX = os.path.join(FOLDER, "textures") PACK = os.path.normpath(os.path.join(FOLDER, "..", "vessel-interior", "textures", "ModSci_Engineer", "Textures")) SCR = ("Base/ScratchedMetal/T_ScratchedMetal_Diff.png", "Base/ScratchedMetal/T_ScratchedMetal_Rough.png", "Base/ScratchedMetal/T_ScratchedMetal_Normal.png") STN = ("Base/StainedSteel/T_StainSteeled_Diff.png", "Base/StainedSteel/T_StainSteeled_Rough.png", None) DIRT = "Base/Glass/T_Glass_DirtRough.png" SCRATCH_N = "Base/Glass/T_Scratch_Norm.png" ATLAS_SIZE = 4096 ATLAS_RECT = {"SM_PressureDoor_Leaf": (0.0, 0.0, 0.5), "SM_PressureDoor_Frame": (0.5, 0.0, 0.5), "SM_PressureDoor_Housing": (0.0, 0.5, 0.5), "SM_PressureDoor_Bulkhead": (0.5, 0.5, 0.25), "SM_PressureDoor_FloorPlate": (0.75, 0.5, 0.25), "SM_PressureDoor_Wheel": (0.5, 0.75, 0.25), "SM_PressureDoor_Bolt": (0.75, 0.75, 0.25)} EDGE_R = 0.012 # screens: (material, facing y sign, x0, x1, z0, z1) -> UVMap 0..1 on their front faces SCREENS = [(-1, 1.93, 2.19, 1.37, 1.51), (1, 1.74, 1.96, 1.365, 1.495)] SURF = { "M_PD_Paint": dict(set=SCR, tint=(0.105, 0.245, 0.315), metal=0.0, tile=0.90, shade=(0.90, 1.06), rough=(0.50, 0.30), normal=0.30, dirt=0.04, dirt_tile=2.6, grime=0.20, wear=0.35, wear_col=(0.52, 0.53, 0.54), wear_rough=0.38), "M_PD_RedPaint": dict(set=SCR, tint=(0.62, 0.035, 0.025), metal=0.0, tile=0.60, shade=(0.88, 1.06), rough=(0.52, 0.32), normal=0.30, dirt=0.05, dirt_tile=1.4, grime=0.20, wear=0.4, wear_col=(0.50, 0.51, 0.52), wear_rough=0.36), "M_PD_Steel": dict(set=STN, tint=(0.80, 0.81, 0.83), metal=1.0, tile=0.50, shade=(0.85, 1.10), rough=(0.10, 0.24), normal=0.45, normal_map=SCRATCH_N, dirt=0.04, dirt_tile=0.9, grime=0.18, wear=0.0), "M_PD_Dark": dict(set=(STN[0], SCR[1], SCR[2]), tint=(0.032, 0.035, 0.039), metal=0.0, tile=0.70, shade=(0.87, 1.13), rough=(0.58, 0.38), normal=0.30, dirt=0.06, dirt_tile=1.9, grime=0.12, wear=0.0), "M_PD_Rubber": dict(set=(None, SCR[1], None), tint=(0.016, 0.016, 0.017), metal=0.0, tile=0.40, shade=(1.0, 1.0), rough=(0.86, 0.64), normal=0.0, dirt=0.08, dirt_tile=1.2, grime=0.10, wear=0.0, dust=0.03), } GLOW = {"M_PD_LampRed": ((1.0, 0.06, 0.03), 6.0), "M_PD_LampGreen": ((0.08, 1.0, 0.25), 6.0)} # ------------------------------------------------------------------ UV layers def uv_tile_layer(me): """metres, by the face's dominant axis""" uv = me.uv_layers.get("UV_Tile") or me.uv_layers.new(name="UV_Tile") co = np.zeros(len(me.vertices) * 3, np.float32) me.vertices.foreach_get("co", co) co = co.reshape(-1, 3) out = np.zeros((len(me.loops), 2), np.float32) for p in me.polygons: n = p.normal ax = 0 if abs(n.x) >= abs(n.y) and abs(n.x) >= abs(n.z) else (1 if abs(n.y) >= abs(n.z) else 2) for li in range(p.loop_start, p.loop_start + p.loop_total): c = co[me.loops[li].vertex_index] out[li] = (c[1], c[2]) if ax == 0 else ((c[0], c[2]) if ax == 1 else (c[0], c[1])) uv.data.foreach_set("uv", out.ravel()) def uv_atlas_layer(ob, rect): me = ob.data if "UVMap" not in me.uv_layers: me.uv_layers.new(name="UVMap") # UVMap first, UV_Tile second names = [l.name for l in me.uv_layers] if names[0] != "UVMap": keep = {} for l in me.uv_layers: a = np.zeros(len(me.loops) * 2, np.float32) l.data.foreach_get("uv", a) keep[l.name] = a while len(me.uv_layers): me.uv_layers.remove(me.uv_layers[0]) for nm in ("UVMap", "UV_Tile"): me.uv_layers.new(name=nm).data.foreach_set("uv", keep.get(nm, np.zeros(len(me.loops) * 2, np.float32))) me.uv_layers.active = me.uv_layers["UVMap"] me.uv_layers["UVMap"].active_render = True x0, y0, s = rect vl = bpy.context.view_layer for o in vl.objects: o.select_set(False) hidden = ob.hide_get() ob.hide_set(False) ob.select_set(True) vl.objects.active = ob bpy.ops.object.mode_set(mode='EDIT') bpy.ops.mesh.select_all(action='SELECT') margin = 6.0 / (s * ATLAS_SIZE) bpy.ops.uv.smart_project(angle_limit=math.radians(60), island_margin=margin, area_weight=0.0, correct_aspect=True, scale_to_bounds=False) bpy.ops.uv.select_all(action='SELECT') try: bpy.ops.uv.pack_islands(rotate=True, scale=True, margin_method='FRACTION', margin=margin, shape_method='CONCAVE') except Exception as ex: print("pack skipped", ob.name, ex) bpy.ops.object.mode_set(mode='OBJECT') a = np.zeros(len(me.loops) * 2, np.float32) me.uv_layers["UVMap"].data.foreach_get("uv", a) a = a.reshape(-1, 2) pad = 4.0 / (s * ATLAS_SIZE) a = np.array([x0, y0], np.float32) + (pad + np.clip(a, 0, 1) * (1 - 2 * pad)) * s me.uv_layers["UVMap"].data.foreach_set("uv", a.ravel()) ob.hide_set(hidden) def screen_uvs(me, mats): """the screens' fronts take the whole screen texture in UVMap (they are not baked: emissive)""" names = [m.name for m in me.materials] if "M_PD_Screen" not in names: return si = names.index("M_PD_Screen") uv = me.uv_layers["UVMap"] for p in me.polygons: if p.material_index != si: continue for (sg, x0, x1, z0, z1) in SCREENS: if p.normal.y * sg > 0.9: for li in range(p.loop_start, p.loop_start + p.loop_total): c = me.vertices[me.loops[li].vertex_index].co u = (c.x - x0) / (x1 - x0) if sg > 0: u = 1.0 - u # seen from behind uv.data[li].uv = (min(max(u, 0), 1), min(max((c.z - z0) / (z1 - z0), 0), 1)) # ------------------------------------------------------------------ generated textures def save_image(name, arr, colorspace='sRGB'): h, w = arr.shape[:2] im = bpy.data.images.get(name) if im is None or tuple(im.size) != (w, h): if im: bpy.data.images.remove(im) im = bpy.data.images.new(name, w, h, alpha=False) im.colorspace_settings.name = colorspace # before the pixels: changing it regenerates a generated image (black) rgba = np.ones((h, w, 4), np.float32) rgba[..., :3] = arr[..., :3] im.pixels.foreach_set(rgba.ravel()) im.filepath_raw = os.path.join(TEX, name + ".png") im.file_format = 'PNG' im.save() return im def make_hazard(n=512): """seamless 45 degree stripes, red and off-white, values in sRGB""" y, x = np.mgrid[0:n, 0:n].astype(np.float32) period = n / 4.0 ph = ((x + y) % period) / period m = np.clip((np.abs(ph - 0.5) - 0.25) * 40.0 + 0.5, 0, 1) # 1 = white red = np.array([0.70, 0.13, 0.10], np.float32) wht = np.array([0.90, 0.90, 0.88], np.float32) return save_image("T_PD_Hazard_BaseColor", red * (1 - m[..., None]) + wht * m[..., None]) def make_screen(w=512, h=256): """status screen: frame, a padlock, three bars; emissive, values in sRGB. No lettering.""" img = np.zeros((h, w, 3), np.float32) img[...] = (0.02, 0.10, 0.12) yy, xx = np.mgrid[0:h, 0:w] border = (xx < 6) | (xx > w - 7) | (yy < 6) | (yy > h - 7) img[border] = (0.10, 0.55, 0.62) img[(yy > h - 40) & (yy < h - 30) & (xx > 20) & (xx < w - 20)] = (0.10, 0.55, 0.62) # header rule (rows count from the bottom) # padlock: body and shackle cx, cy = 110, 110 body = (np.abs(xx - cx) < 46) & (yy > cy - 60) & (yy < cy + 10) rr = np.hypot(xx - cx, yy - (cy + 10)) shackle = (rr > 24) & (rr < 36) & (yy >= cy + 10) img[body | shackle] = (0.20, 0.85, 0.95) img[(np.hypot(xx - cx, yy - (cy - 20)) < 9)] = (0.02, 0.10, 0.12) for k, frac in enumerate((0.9, 0.62, 0.35)): y0 = 150 - k * 42 x0, x1 = 210, 210 + int(270 * frac) img[(yy > y0) & (yy < y0 + 22) & (xx > 210) & (xx < 480)] = (0.04, 0.22, 0.26) img[(yy > y0) & (yy < y0 + 22) & (xx > x0) & (xx < x1)] = (0.15, 0.75, 0.85) return save_image("T_PD_Screen_Emissive", img) # ------------------------------------------------------------------ bake def use_gpu(): sc = bpy.context.scene sc.render.engine = 'CYCLES' try: prefs = bpy.context.preferences.addons['cycles'].preferences prefs.compute_device_type = 'HIP' prefs.get_devices() for d in prefs.devices: d.use = d.type == 'HIP' sc.cycles.device = 'GPU' return 'HIP' except Exception as ex: print("GPU not set:", ex) sc.cycles.device = 'CPU' return 'CPU' def bake_masks(parts, reset=True): """parts: list of (name_in_atlas, mesh). One emit bake of each mesh alone (occlusion counts only the mesh itself).""" t0 = time.time() kind = use_gpu() sc = bpy.context.scene N = ATLAS_SIZE masks = bpy.data.images.get("T_PD_Masks") if masks is None or tuple(masks.size) != (N, N): if masks: bpy.data.images.remove(masks) masks = bpy.data.images.new("T_PD_Masks", N, N, alpha=False) masks.colorspace_settings.name = 'Non-Color' if reset: masks.pixels.foreach_set(np.tile(np.array((1.0, 0.0, 1.0, 1.0), np.float32), N * N)) mm = bpy.data.materials.get("_bake_masks") or bpy.data.materials.new("_bake_masks") mm.use_nodes = True nt = mm.node_tree nt.nodes.clear() ao = nt.nodes.new('ShaderNodeAmbientOcclusion'); ao.samples = 16; ao.only_local = True; ao.inputs["Distance"].default_value = 0.45 cv = nt.nodes.new('ShaderNodeAmbientOcclusion'); cv.samples = 16; cv.only_local = True; cv.inputs["Distance"].default_value = 0.05 bv = nt.nodes.new('ShaderNodeBevel'); bv.samples = 8; bv.inputs["Radius"].default_value = EDGE_R geo = nt.nodes.new('ShaderNodeNewGeometry') dot = nt.nodes.new('ShaderNodeVectorMath'); dot.operation = 'DOT_PRODUCT' nt.links.new(bv.outputs["Normal"], dot.inputs[0]); nt.links.new(geo.outputs["Normal"], dot.inputs[1]) inv = nt.nodes.new('ShaderNodeMath'); inv.operation = 'SUBTRACT'; inv.inputs[0].default_value = 1.0 nt.links.new(dot.outputs["Value"], inv.inputs[1]) edg = nt.nodes.new('ShaderNodeMath'); edg.operation = 'MULTIPLY'; edg.inputs[1].default_value = 3.5; edg.use_clamp = True nt.links.new(inv.outputs[0], edg.inputs[0]) cc = nt.nodes.new('ShaderNodeCombineColor') nt.links.new(ao.outputs["AO"], cc.inputs[0]); nt.links.new(edg.outputs[0], cc.inputs[1]); nt.links.new(cv.outputs["AO"], cc.inputs[2]) em = nt.nodes.new('ShaderNodeEmission') nt.links.new(cc.outputs[0], em.inputs["Color"]) om = nt.nodes.new('ShaderNodeOutputMaterial') nt.links.new(em.outputs[0], om.inputs["Surface"]) ti = nt.nodes.new('ShaderNodeTexImage'); ti.image = masks nt.nodes.active = ti sc.cycles.samples = 16 sc.cycles.use_denoising = False sc.render.bake.margin = 6 sc.render.bake.use_clear = False skip = {"M_PD_Screen", "M_PD_Hazard", "M_PD_LampRed", "M_PD_LampGreen", "M_PD_Glass"} for name, src in parts: me = src.copy() bm = bmesh.new() bm.from_mesh(me) names = [m.name for m in me.materials] kill = [f for f in bm.faces if names[f.material_index] in skip] bmesh.ops.delete(bm, geom=kill, context='FACES') for f in bm.faces: f.material_index = 0 bm.to_mesh(me) bm.free() me.materials.clear() me.materials.append(mm) me.uv_layers.active = me.uv_layers["UVMap"] me.uv_layers["UVMap"].active_render = True tmp = bpy.data.objects.new("_bake_" + name, me) sc.collection.objects.link(tmp) for o2 in bpy.context.view_layer.objects: o2.select_set(False) tmp.select_set(True) bpy.context.view_layer.objects.active = tmp try: bpy.ops.object.bake(type='EMIT', margin=6, use_clear=False) finally: bpy.data.objects.remove(tmp) bpy.data.meshes.remove(me) masks.filepath_raw = os.path.join(TEX, "T_PD_Masks.png") masks.file_format = 'PNG' masks.save() bpy.data.materials.remove(mm) return masks, dict(device=kind, seconds=round(time.time() - t0, 1), size=N) # ------------------------------------------------------------------ materials (node recipe from the docking arm) def tex_image(path, colorspace="sRGB"): path = os.path.normpath(path) for im in bpy.data.images: if im.filepath and os.path.normpath(bpy.path.abspath(im.filepath)) == path: return im im = bpy.data.images.load(path) im.colorspace_settings.name = colorspace try: im.filepath = bpy.path.relpath(path) except Exception: pass return im _RANGE = {} def tex_range(im, lo=3, hi=97): if im.name not in _RANGE: n, ch = im.size[0] * im.size[1], im.channels px = np.empty(n * ch, np.float32) im.pixels.foreach_get(px) px = px.reshape(n, ch) if ch >= 3: c = px[:, :3] if im.colorspace_settings.name == 'sRGB': c = np.where(c <= 0.04045, c / 12.92, ((c + 0.055) / 1.055) ** 2.4) v = c @ np.array([0.2126, 0.7152, 0.0722], np.float32) else: v = px[:, 0] _RANGE[im.name] = (round(float(np.percentile(v, lo)), 4), round(float(np.percentile(v, hi)), 4)) return _RANGE[im.name] class Tree: def __init__(self, mat): mat.use_nodes = True self.nt = mat.node_tree self.nt.nodes.clear() def node(self, kind, label=None, **props): n = self.nt.nodes.new(kind) if label: n.label = label for k, v in props.items(): setattr(n, k, v) i = len(self.nt.nodes) - 1 n.location = (-2000 + 240 * (i // 4), 500 - 320 * (i % 4)) return n def link(self, a, b): self.nt.links.new(a, b) def put(self, sock, v): if isinstance(v, bpy.types.NodeSocket): self.nt.links.new(v, sock) else: sock.default_value = v def math(self, op, a, b=None, clamp=False, label=None): n = self.node('ShaderNodeMath', label, operation=op, use_clamp=clamp) self.put(n.inputs[0], a) if b is not None: self.put(n.inputs[1], b) return n.outputs[0] def ranged(self, v, lo, hi, label=None, f0=0.0, f1=1.0, smooth=False): n = self.node('ShaderNodeMapRange', label) if smooth: n.interpolation_type = 'SMOOTHSTEP' self.put(n.inputs["Value"], v) n.inputs["From Min"].default_value = f0; n.inputs["From Max"].default_value = f1 n.inputs["To Min"].default_value = lo; n.inputs["To Max"].default_value = hi return n.outputs["Result"] def mixc(self, fac, a, b, label=None): n = self.node('ShaderNodeMix', label, data_type='RGBA') self.put(n.inputs[0], fac) self.put(n.inputs[6], a if isinstance(a, bpy.types.NodeSocket) else tuple(a) + (1.0,)) self.put(n.inputs[7], b if isinstance(b, bpy.types.NodeSocket) else tuple(b) + (1.0,)) return n.outputs[2] def mixf(self, fac, a, b, label=None): n = self.node('ShaderNodeMix', label, data_type='FLOAT') self.put(n.inputs[0], fac); self.put(n.inputs[2], a); self.put(n.inputs[3], b) return n.outputs[0] def uv(self, name): return self.node('ShaderNodeUVMap', uv_map=name).outputs["UV"] def mapped(self, src, scale, label=None): n = self.node('ShaderNodeMapping', label) self.nt.links.new(src, n.inputs["Vector"]) n.inputs["Scale"].default_value = (scale, scale, scale) return n.outputs["Vector"] def tex(self, im, vec, label=None): n = self.node('ShaderNodeTexImage', label, image=im) self.nt.links.new(vec, n.inputs["Vector"]) return n def dress(mat, r, masks=None, base_img=None, base_tile=None): """a coated or bare metal surface: a tiling set + smudges; with the baked masks also shade in crevices and a few chips on exposed edges. base_img replaces the tint (the hazard stripes).""" t = Tree(mat) b = t.node('ShaderNodeBsdfPrincipled') out = t.node('ShaderNodeOutputMaterial') t.link(b.outputs[0], out.inputs["Surface"]) u0 = t.uv("UV_Tile") mp = t.mapped(u0, 1.0 / r["tile"], "one repeat = %.2f m" % r["tile"]) diff, rough, normal = r["set"] dirt = t.tex(tex_image(os.path.join(PACK, DIRT), "Non-Color"), t.mapped(u0, 1.0 / r["dirt_tile"]), "smudges and dust").outputs["Color"] ao = edge = crev = None if masks is not None: sep = t.node('ShaderNodeSeparateColor') t.link(t.tex(masks, t.uv("UVMap"), "baked: R occlusion, G edges, B crevices").outputs["Color"], sep.inputs[0]) ao, edge, crev = sep.outputs[0], sep.outputs[1], sep.outputs[2] tint = tuple(r["tint"]) if base_img is not None: tint_sock = t.tex(base_img, t.mapped(u0, 1.0 / base_tile, "stripes: %.2f m a repeat" % base_tile), "stripes").outputs["Color"] else: tint_sock = None col = tint if tint_sock is None else tint_sock if diff: im = tex_image(os.path.join(PACK, diff)) lo, hi = tex_range(im) bw = t.node('ShaderNodeRGBToBW') t.link(t.tex(im, mp, "surface, diffuse").outputs["Color"], bw.inputs[0]) sh = t.ranged(bw.outputs[0], r["shade"][0], r["shade"][1], "shade the tint", lo, hi) if tint_sock is None: vm = t.node('ShaderNodeVectorMath', "tint x shade", operation='SCALE') vm.inputs[0].default_value = tint t.link(sh, vm.inputs["Scale"]) else: vm = t.node('ShaderNodeVectorMath', "stripes x shade", operation='SCALE') t.link(tint_sock, vm.inputs[0]) t.link(sh, vm.inputs["Scale"]) col = vm.outputs["Vector"] grime = None if crev is not None and r["grime"] > 0: grime = t.math('MULTIPLY', t.math('POWER', t.math('SUBTRACT', 1.0, crev), 1.4), r["grime"], clamp=True, label="shade in crevices") col = t.mixc(grime, col, (0.02, 0.019, 0.018), "darken crevices") if ao is not None: col = t.mixc(t.ranged(ao, 0.35, 0.0, "occlusion", 0.0, 1.0), col, (0.01, 0.01, 0.01), "occlusion") if r.get("dust"): col = t.mixc(t.math('MULTIPLY', dirt, r["dust"]), col, (0.55, 0.53, 0.50), "dust") rim = tex_image(os.path.join(PACK, rough), "Non-Color") rlo, rhi = tex_range(rim) ro = t.tex(rim, mp, "surface, roughness").outputs["Color"] rr = t.ranged(ro, r["rough"][0], r["rough"][1], "roughness range", rlo, rhi) rr = t.math('ADD', rr, t.math('MULTIPLY', dirt, r["dirt"]), clamp=True, label="smudges dull the sheen") if grime is not None: rr = t.math('ADD', rr, t.math('MULTIPLY', grime, 0.35), clamp=True, label="crevices are matte") metal = r["metal"] if edge is not None and r["wear"] > 0: breakup = t.ranged(ro, 1.25, 0.55, "scratches break the wear up", rlo, rhi) exposed = t.ranged(ao, 0.0, 1.0, "only where the part is exposed", 0.72, 0.93, smooth=True) w = t.math('MULTIPLY', t.math('MULTIPLY', t.math('MULTIPLY', edge, breakup), exposed), r["wear"]) w = t.ranged(w, 0.0, 1.0, "chipped edge", 0.38, 0.72, smooth=True) col = t.mixc(w, col, tuple(r["wear_col"]), "metal under the coating") rr = t.mixf(w, rr, r["wear_rough"]) metal = t.mixf(w, r["metal"], 1.0) t.put(b.inputs["Base Color"], col) t.put(b.inputs["Roughness"], rr) t.put(b.inputs["Metallic"], metal) nm_file = r.get("normal_map") or normal if nm_file and r["normal"] > 0: nd = t.node('ShaderNodeNormalMap', "fine relief", uv_map="UV_Tile") nd.inputs["Strength"].default_value = r["normal"] t.link(t.tex(tex_image(os.path.join(PACK, nm_file), "Non-Color"), mp, "surface, normal").outputs["Color"], nd.inputs["Color"]) t.link(nd.outputs["Normal"], b.inputs["Normal"]) mat.diffuse_color = tuple(r["tint"]) + (1.0,) mat.metallic = r["metal"] def dress_glow(mat, color, strength): t = Tree(mat) b = t.node('ShaderNodeBsdfPrincipled') out = t.node('ShaderNodeOutputMaterial') t.link(b.outputs[0], out.inputs["Surface"]) b.inputs["Base Color"].default_value = (0.03, 0.03, 0.03, 1) b.inputs["Roughness"].default_value = 0.2 b.inputs["Emission Color"].default_value = tuple(color) + (1,) b.inputs["Emission Strength"].default_value = strength mat.diffuse_color = tuple(color) + (1.0,) def dress_screen(mat, img): t = Tree(mat) b = t.node('ShaderNodeBsdfPrincipled') out = t.node('ShaderNodeOutputMaterial') t.link(b.outputs[0], out.inputs["Surface"]) tx = t.tex(img, t.uv("UVMap"), "status screen") b.inputs["Base Color"].default_value = (0.01, 0.012, 0.014, 1) b.inputs["Roughness"].default_value = 0.08 t.link(tx.outputs["Color"], b.inputs["Emission Color"]) b.inputs["Emission Strength"].default_value = 3.0 mat.diffuse_color = (0.1, 0.5, 0.6, 1) def dress_glass(mat): t = Tree(mat) b = t.node('ShaderNodeBsdfPrincipled') out = t.node('ShaderNodeOutputMaterial') t.link(b.outputs[0], out.inputs["Surface"]) dirt = t.tex(tex_image(os.path.join(PACK, DIRT), "Non-Color"), t.mapped(t.uv("UV_Tile"), 1.0 / 0.5), "smudges").outputs["Color"] b.inputs["Base Color"].default_value = (0.82, 0.90, 0.93, 1) b.inputs["Transmission Weight"].default_value = 1.0 b.inputs["IOR"].default_value = 1.47 t.put(b.inputs["Roughness"], t.ranged(dirt, 0.03, 0.12)) mat.diffuse_color = (0.6, 0.7, 0.75, 0.4) # ------------------------------------------------------------------ all OBJECTS = ["SM_PressureDoor_Leaf", "SM_PressureDoor_Frame", "SM_PressureDoor_Housing", "SM_PressureDoor_Bulkhead", "SM_PressureDoor_FloorPlate", "SM_PressureDoor_Wheel"] def run(bake=True, uvs=True): os.makedirs(TEX, exist_ok=True) t0 = time.time() meshes = [(n, bpy.data.objects[n]) for n in OBJECTS] bolt_ob = bpy.data.objects["SM_PressureDoor_Bolt_01"] for n, ob in (meshes + [("SM_PressureDoor_Bolt", bolt_ob)] if uvs else []): uv_tile_layer(ob.data) uv_atlas_layer(ob, ATLAS_RECT[n]) screen_uvs(ob.data, None) info = {} if bake: masks, info = bake_masks([(n, ob.data) for n, ob in meshes] + [("SM_PressureDoor_Bolt", bolt_ob.data)]) else: masks = bpy.data.images.get("T_PD_Masks") hz = make_hazard() scr = make_screen() for name, r in SURF.items(): dress(bpy.data.materials[name], r, masks) dress(bpy.data.materials["M_PD_Hazard"], dict(SURF["M_PD_Paint"], tint=(1, 1, 1), shade=(0.92, 1.04), wear=0.0, grime=0.12), masks, base_img=hz, base_tile=0.40) for name, (c, s) in GLOW.items(): dress_glow(bpy.data.materials[name], c, s) dress_screen(bpy.data.materials["M_PD_Screen"], scr) dress_glass(bpy.data.materials["M_PD_Glass"]) info["total_s"] = round(time.time() - t0, 1) return info def bake_one(name, reset=False): """bake a single mesh into the atlas (for short MCP calls); name as in ATLAS_RECT""" src = bpy.data.objects["SM_PressureDoor_Bolt_01"].data if name == "SM_PressureDoor_Bolt" else bpy.data.objects[name].data return bake_masks([(name, src)], reset=reset)[1] def redo_one(name): """after rebuilding one mesh: its two UV layers and its square of the atlas""" ob = bpy.data.objects["SM_PressureDoor_Bolt_01"] if name == "SM_PressureDoor_Bolt" else bpy.data.objects[name] uv_tile_layer(ob.data) uv_atlas_layer(ob, ATLAS_RECT[name]) screen_uvs(ob.data, None) return bake_one(name, reset=False)