# Bright pixel-voxel Christmas ornament showcase, in the style of the ornaments-*-sheet.png renders. # Run: blender -b --factory-startup --python ornament_showcase.py (or exec() inside Blender) # # Every ornament is a 2D pixel design given depth (domed / bevelled relief), built from bevelled # cubes. Each cube carries its own colour in the "Col" colour attribute, so a handful of shared # materials (paint / gold metal / glow) give per-pixel colour like the sheets. import bpy, bmesh, math, os, random from mathutils import Matrix, Vector OUT_DIR = r"C:\roblox\Ornament Generator RNG\ornament_showcase" SPACING = 19.0 # pixel units between ornaments ROW_GAP = 29.0 # vertical distance between the two rows GLOW_STRENGTH = 6.0 # emission strength of gems / glowing pixels OUTLINE_PX = 5.0 # Freestyle outline thickness (pixels) random.seed(3) # ---------------------------------------------------------------- colours (linear RGB) RED_A, RED_B, RED_HI, RED_LO = (0.80, 0.01, 0.02), (0.58, 0.0, 0.01), (1.0, 0.18, 0.12), (0.36, 0.0, 0.01) GOLD_A, GOLD_B, GOLD_HI, GOLD_LO = (1.0, 0.62, 0.03), (0.92, 0.45, 0.0), (1.0, 0.85, 0.25), (0.62, 0.28, 0.0) ICE_W, ICE_B, ICE_C = (0.85, 0.93, 1.0), (0.38, 0.70, 1.0), (0.12, 0.85, 1.0) PUR_A, PUR_B, PUR_HI = (0.26, 0.04, 0.85), (0.16, 0.02, 0.62), (0.45, 0.12, 1.0) WHITE, CANE_R = (0.95, 0.95, 0.98), (0.85, 0.02, 0.04) GREEN_A, GREEN_B = (0.05, 0.55, 0.08), (0.02, 0.35, 0.05) CYAN_GEM, RUBY_GEM, PINK_GEM = (0.05, 0.85, 1.0), (1.0, 0.08, 0.15), (1.0, 0.3, 0.8) def jit(c, a=0.035): k = 1.0 + random.uniform(-a, a) return tuple(min(1.0, max(0.0, v * k)) for v in c) # ---------------------------------------------------------------- scene def setup_scene(): sc = bpy.context.scene for o in list(bpy.data.objects): bpy.data.objects.remove(o, do_unlink=True) for c in list(bpy.data.collections): bpy.data.collections.remove(c) orn = bpy.data.collections.new("Ornaments") studio = bpy.data.collections.new("Studio") sc.collection.children.link(orn) sc.collection.children.link(studio) return sc, orn, studio # ---------------------------------------------------------------- materials def _fresh(name): m = bpy.data.materials.get(name) or bpy.data.materials.new(name) try: m.use_nodes = True except Exception: pass m.node_tree.nodes.clear() return m def attr_material(name, kind): """kind: 'paint' (glossy plastic + fresnel rim), 'metal' (gold), 'glow' (emissive gems).""" m = _fresh(name) N, L = m.node_tree.nodes, m.node_tree.links out = N.new("ShaderNodeOutputMaterial") at = N.new("ShaderNodeAttribute"); at.attribute_name = "Col" bs = N.new("ShaderNodeBsdfPrincipled") L.new(at.outputs["Color"], bs.inputs["Base Color"]) if "Specular IOR Level" in bs.inputs: bs.inputs["Specular IOR Level"].default_value = 0.6 if kind == "paint": bs.inputs["Roughness"].default_value = 0.3 if "Coat Weight" in bs.inputs: bs.inputs["Coat Weight"].default_value = 0.6 bs.inputs["Coat Roughness"].default_value = 0.08 # fresnel rim: LayerWeight.Facing^3 * 0.6 * colour -> emission lw = N.new("ShaderNodeLayerWeight"); lw.inputs["Blend"].default_value = 0.3 pw = N.new("ShaderNodeMath"); pw.operation = "POWER"; pw.inputs[1].default_value = 3.0 mu = N.new("ShaderNodeMath"); mu.operation = "MULTIPLY"; mu.inputs[1].default_value = 0.6 L.new(lw.outputs["Facing"], pw.inputs[0]); L.new(pw.outputs["Value"], mu.inputs[0]) em = N.new("ShaderNodeEmission") L.new(at.outputs["Color"], em.inputs["Color"]); L.new(mu.outputs["Value"], em.inputs["Strength"]) add = N.new("ShaderNodeAddShader") L.new(bs.outputs["BSDF"], add.inputs[0]); L.new(em.outputs["Emission"], add.inputs[1]) L.new(add.outputs["Shader"], out.inputs["Surface"]) return m if kind == "metal": bs.inputs["Metallic"].default_value = 0.8 bs.inputs["Roughness"].default_value = 0.22 L.new(at.outputs["Color"], bs.inputs["Emission Color"]) bs.inputs["Emission Strength"].default_value = 0.18 else: # glow bs.inputs["Roughness"].default_value = 0.08 L.new(at.outputs["Color"], bs.inputs["Emission Color"]) bs.inputs["Emission Strength"].default_value = GLOW_STRENGTH L.new(bs.outputs["BSDF"], out.inputs["Surface"]) return m def plain_gold(name="Gold_Hook"): m = _fresh(name) N, L = m.node_tree.nodes, m.node_tree.links bs = N.new("ShaderNodeBsdfPrincipled") bs.inputs["Base Color"].default_value = (*GOLD_A, 1) bs.inputs["Metallic"].default_value = 0.8 bs.inputs["Roughness"].default_value = 0.2 bs.inputs["Emission Color"].default_value = (*GOLD_A, 1) bs.inputs["Emission Strength"].default_value = 0.18 L.new(bs.outputs["BSDF"], N.new("ShaderNodeOutputMaterial").inputs["Surface"]) return m MATS = {} KINDS = ("paint", "metal", "glow") # ---------------------------------------------------------------- voxel object N6 = ((1, 0, 0), (-1, 0, 0), (0, 1, 0), (0, -1, 0), (0, 0, 1), (0, 0, -1)) def voxel_object(name, cells, coll, loc, bevel=0.09): """cells: {(x, y, z): (kind, colour)} — y is depth, front = -y. Only visible cubes are built.""" bm = bmesh.new() col_layer = bm.loops.layers.float_color.new("Col") for (x, y, z), (kind, colr) in cells.items(): if all((x + dx, y + dy, z + dz) in cells for dx, dy, dz in N6): continue ret = bmesh.ops.create_cube(bm, size=1.0, matrix=Matrix.Translation((x, y, z))) c4 = (*colr, 1.0) for f in {f for v in ret["verts"] for f in v.link_faces}: f.material_index = KINDS.index(kind) for lp in f.loops: lp[col_layer] = c4 me = bpy.data.meshes.new(name) bm.to_mesh(me) bm.free() for k in KINDS: me.materials.append(MATS[k]) for p in me.polygons: p.use_smooth = True ob = bpy.data.objects.new(name, me) ob.location = loc bv = ob.modifiers.new("Bevel", "BEVEL") bv.width, bv.segments, bv.limit_method = bevel, 2, "ANGLE" bv.harden_normals = True bv.use_clamp_overlap = True coll.objects.link(ob) return ob def extrude(mask, depth_fn, colour_fn): """2D pixel mask {(x, z)} -> 3D cells, column depth from depth_fn(x, z).""" cells = {} for (x, z) in mask: d = depth_fn(x, z) for y in range(-d, d + 1): cells[(x, y, z)] = colour_fn(x, y, z, d) return cells def edge_distance(mask): """Chebyshev-ish distance of each pixel to the outside (for bevelled relief).""" dist = {} for (x, z) in mask: r = 0 while all((x + dx, z + dz) in mask for dx in range(-r - 1, r + 2) for dz in range(-r - 1, r + 2) if abs(dx) + abs(dz) <= r + 1): r += 1 if r > 6: break dist[(x, z)] = r return dist # ---------------------------------------------------------------- cap + hook (shared) def add_cap(cells, cx, top): """Gold stepped cap with two cyan gems sitting on the highest pixel row.""" for x in range(cx - 2, cx + 3): for y in (-1, 0, 1): cells[(x, y, top + 1)] = ("metal", jit(GOLD_A if (x + y) % 2 else GOLD_B)) for x in range(cx - 1, cx + 2): for y in (-1, 0, 1): cells[(x, y, top + 2)] = ("metal", jit(GOLD_HI if x == cx - 1 else GOLD_A)) for x in (cx - 1, cx + 1): cells[(x, -2, top + 1)] = ("glow", CYAN_GEM) return top + 2.5 # top face of the cap HOOK_SCALE = 0.62 def make_hook(name, coll, loc, cx, base): gold = MATS["hook"] k = HOOK_SCALE # ring through the cap bpy.ops.mesh.primitive_torus_add(major_radius=1.0 * k + 0.2, minor_radius=0.38, location=(0, 0, 0), rotation=(math.radians(90), 0, 0)) ring = bpy.context.active_object ring.name = name + "_Ring" ring.data.materials.append(gold) for c in list(ring.users_collection): c.objects.unlink(ring) ring.location = Vector(loc) + Vector((cx, 0, base + 0.9 * k + 0.25)) coll.objects.link(ring) # thick "?" hook cu = bpy.data.curves.new(name + "_Curve", "CURVE") cu.dimensions = "3D" cu.bevel_depth = 0.5 cu.bevel_resolution = 4 cu.use_fill_caps = True sp = cu.splines.new("NURBS") pts = [(0, 0, 1.6), (0, 0, 2.8), (1.2, 0, 3.9), (2.5, 0, 5.0), (2.6, 0, 6.8), (1.3, 0, 8.1), (-0.7, 0, 8.2), (-2.2, 0, 7.3), (-2.4, 0, 6.0)] sp.points.add(len(pts) - 1) for p, c in zip(sp.points, pts): p.co = (c[0] * k, c[1], c[2] * k + 0.4, 1.0) sp.order_u, sp.use_endpoint_u, sp.resolution_u = 3, True, 20 cu.materials.append(gold) hook = bpy.data.objects.new(name + "_Hook", cu) hook.location = Vector(loc) + Vector((cx, 0, base)) coll.objects.link(hook) def finish(name, cells, coll, loc, cx=0, top=None): if top is None: top = max(z for (x, y, z) in cells if x == cx) base = add_cap(cells, cx, top) ob = voxel_object(name, cells, coll, loc) make_hook(name, coll, loc, cx, base) return ob # ---------------------------------------------------------------- ornaments def make_bauble(coll, loc): R = 6.6 mask = {(x, z) for x in range(-7, 8) for z in range(-7, 8) if x * x + z * z <= R * R} def depth(x, z): return int(round(3.6 * math.sqrt(max(0.0, 1 - (x * x + z * z) / (R * R))))) def colour(x, y, z, d): if z == 0: return ("metal", jit(GOLD_HI if x < 0 else GOLD_A)) if -x + z > 5 and x * x + z * z < 30: return ("paint", jit(RED_HI)) if x - z > 7: return ("paint", jit(RED_LO)) return ("paint", jit(RED_A if (x + z) % 2 == 0 else RED_B)) cells = extrude(mask, depth, colour) for x in range(-6, 7): # protruding gold band ring d = depth(x, 0) + 1 for y in (-d, d): cells[(x, y, 0)] = ("metal", jit(GOLD_A)) d0 = depth(0, 0) for x in range(-1, 2): # raised gem setting on the band for z in range(-1, 2): for y in (-(d0 + 1), -(d0 + 2)): cells[(x, y, z)] = ("metal", jit(GOLD_HI if (x, z) == (-1, 1) else GOLD_A)) cells[(0, -(d0 + 2), 0)] = ("glow", RUBY_GEM) for sx in (-1, 1): cells[(sx * 4, -(depth(4, 0) + 2), 0)] = ("glow", RUBY_GEM) return finish("Ornament_RedBauble", cells, coll, loc) def make_star(coll, loc): def inside(px, pz): a = math.atan2(px, pz) # 0 = up r = math.hypot(px, pz) seg = (a % (2 * math.pi / 5)) / (2 * math.pi / 5) tri = abs(seg - 0.5) * 2 # 1 at points, 0 between rmax = 4.1 + (8.0 - 4.1) * (tri ** 1.3) return r <= rmax mask = {(x, z) for x in range(-8, 9) for z in range(-8, 9) if inside(x, z + 0.6)} dist = edge_distance(mask) def colour(x, y, z, d): a = math.degrees(math.atan2(x, z)) % 72 if dist[(x, z)] >= 3: return ("paint", jit(GOLD_HI)) return ("paint", jit(GOLD_HI if a < 36 else GOLD_A) if y < 0 else jit(GOLD_B)) cells = extrude(mask, lambda x, z: min(2, dist[(x, z)]), colour) cells[(0, -3, 0)] = ("glow", (1.0, 0.75, 0.2)) return finish("Ornament_GoldStar", cells, coll, loc) def make_snowflake(coll, loc): segs = [] for k in range(6): a = math.radians(90 + 60 * k) d = Vector((math.cos(a), math.sin(a))) segs.append((Vector((0, 0)), d * 7.4)) for t, ln in ((3.6, 2.6), (5.6, 1.7)): p = d * t for off in (-55, 55): b = math.radians(90 + 60 * k + off) segs.append((p, p + Vector((math.cos(b), math.sin(b))) * ln)) def dist_seg(p, a, b): ab = b - a t = max(0.0, min(1.0, (p - a).dot(ab) / ab.length_squared)) return (p - (a + ab * t)).length mask, core = set(), set() for x in range(-8, 9): for z in range(-8, 9): p = Vector((x, z)) dm = min(dist_seg(p, a, b) for a, b in segs) if dm <= 0.72 or p.length <= 1.6: mask.add((x, z)) if p.length <= 1.6: core.add((x, z)) def colour(x, y, z, d): r = math.hypot(x, z) if (x, z) in core: return ("glow", CYAN_GEM) if y < 0 else ("paint", jit(ICE_C)) if r >= 6.6: return ("paint", jit(ICE_C)) return ("paint", jit(ICE_W if (x + z) % 2 == 0 or y < 0 and r < 3 else ICE_B)) cells = extrude(mask, lambda x, z: 2 if (x, z) in core else 1, colour) return finish("Ornament_Snowflake", cells, coll, loc) def make_bell(coll, loc): hw = {6: 1.6, 5: 2.6, 4: 3.1, 3: 3.5, 2: 3.7, 1: 4.0, 0: 4.3, -1: 4.7, -2: 5.3, -3: 6.3, -4: 6.5} cells = {} for z, w in hw.items(): for x in range(-7, 8): for y in range(-5, 6): if x * x + (y / 0.62) ** 2 <= w * w: if z in (-3, -4): c = ("metal", jit(GOLD_HI if x < 0 and y < 0 else GOLD_A)) elif z == 4: c = ("metal", jit(GOLD_A)) elif -x + z > 4: c = ("paint", jit(PUR_HI)) else: c = ("paint", jit(PUR_A if (x + y + z) % 2 == 0 else PUR_B)) cells[(x, y, z)] = c for (x, z) in ((-2, 1), (2, -1), (1, 2), (-1, -2), (0, 0)): # glowing star dots on the front ys = [k[1] for k in cells if k[0] == x and k[2] == z] cells[(x, min(ys), z)] = ("glow", (1.0, 0.85, 0.3)) for (x, y, z) in [(x, y, -5) for x in (-1, 0, 1) for y in (-1, 0, 1)] + [(0, 0, -6), (0, -1, -6), (0, 1, -6)]: cells[(x, y, z)] = ("metal", jit(GOLD_B)) return finish("Ornament_PurpleBell", cells, coll, loc) def make_candy_cane(coll, loc): mask = set() for x in (1, 2, 3): # 3-pixel shaft for z in range(-8, 3): mask.add((x, z)) C = Vector((-1.0, 3.0)) for x in range(-7, 5): for z in range(0, 10): r = (Vector((x, z)) - C).length if 1.2 <= r <= 4.3 and (z >= 3 or x <= -3): mask.add((x, z)) def colour(x, y, z, d): return ("paint", jit(CANE_R if ((x - z) // 2) % 2 == 0 else WHITE)) cells = extrude(mask, lambda x, z: 1, colour) # holly sprig on the front of the shaft: two leaves + glowing berries on a gold clasp for x in (1, 2, 3): cells[(x, -2, -1)] = ("metal", jit(GOLD_A)) for (x, z, c) in ((-1, 0, GREEN_A), (0, 0, GREEN_A), (0, -1, GREEN_B), (-1, -1, GREEN_B), (4, 0, GREEN_A), (5, 0, GREEN_A), (4, -1, GREEN_B), (5, -1, GREEN_B)): cells[(x, -2, z)] = ("paint", jit(c)) for x in (1, 3): cells[(x, -3, 0)] = ("glow", RUBY_GEM) cells[(2, -3, -1)] = ("glow", RUBY_GEM) return finish("Ornament_CandyCane", cells, coll, loc, cx=-1) # ---------------------------------------------------------------- patterned baubles (row 2) BLUE_A, BLUE_B = (0.02, 0.28, 1.0), (0.01, 0.16, 0.80) NAVY_A, NAVY_B = (0.04, 0.03, 0.32), (0.08, 0.02, 0.45) BROWN, BROWN_DK = (0.25, 0.10, 0.03), (0.12, 0.05, 0.02) def pattern_bauble(name, coll, loc, base_fn, pattern, R=6.6): """Domed pixel bauble. base_fn(x, z) -> body colour/kind; pattern(x, z) -> (kind, colour) or None: pattern pixels are painted on the front face and raised by one cube for relief.""" mask = {(x, z) for x in range(-7, 8) for z in range(-7, 8) if x * x + z * z <= R * R} def depth(x, z): return int(round(3.6 * math.sqrt(max(0.0, 1 - (x * x + z * z) / (R * R))))) cells = extrude(mask, depth, lambda x, y, z, d: base_fn(x, z)) for (x, z) in mask: p = pattern(x, z) if p: d = depth(x, z) cells[(x, -d, z)] = p cells[(x, -d - 1, z)] = p return finish(name, cells, coll, loc) def snow_rim(x, z): """White snowy cap / bottom rows used by the blue and green baubles.""" return z >= 5 or z <= -5 def make_blue_snow_bauble(coll, loc): def base(x, z): if snow_rim(x, z): return ("paint", jit(WHITE if (x + z) % 2 else ICE_W)) return ("paint", jit(BLUE_A if (x + z) % 2 == 0 else BLUE_B)) def flake(x, z): ax, az = abs(x), abs(z) if max(ax, az) > 3 or snow_rim(x, z): return None if x == 0 or z == 0 or ax == az: return ("glow", CYAN_GEM) if (x, z) == (0, 0) else ("paint", jit(WHITE)) return None return pattern_bauble("Ornament_BlueSnowBauble", coll, loc, base, flake) def make_tree_bauble(coll, loc): tree = {3: 0, 2: 1, 1: 2, 0: 1, -1: 2, -2: 3} def base(x, z): if snow_rim(x, z): return ("paint", jit(WHITE if (x + z) % 2 else ICE_W)) return ("paint", jit(GREEN_A if (x + z) % 2 == 0 else GREEN_B)) def pat(x, z): if z in tree and abs(x) <= tree[z]: if (x, z) == (0, 3): return ("glow", (1.0, 0.8, 0.2)) # glowing star on top return ("paint", jit(WHITE if abs(x) == tree[z] or z in (1, -2) else (0.55, 0.85, 0.6))) if x == 0 and z in (-3, -4): return ("paint", jit(BROWN)) if (x, z) in ((-4, 2), (4, 0), (-3, -2), (3, 3), (-5, 0), (5, -2)): return ("glow", (0.9, 0.95, 1.0)) # falling snow dots return None return pattern_bauble("Ornament_TreeBauble", coll, loc, base, pat) def make_night_bauble(coll, loc): def base(x, z): return ("paint", jit(NAVY_A if (x + z) % 2 == 0 else NAVY_B)) def pat(x, z): ax, az = abs(x), abs(z) if (x == 0 and az <= 4) or (z == 0 and ax <= 4) or (ax <= 1 and az <= 1): if ax + az <= 1: return ("glow", (1.0, 0.75, 0.2)) return ("metal", jit(GOLD_HI if x <= 0 and z >= 0 else GOLD_A)) if (x, z) in ((-4, 3), (4, -3), (3, 4), (-3, -4), (-5, -1), (5, 1)): return ("glow", (0.95, 0.95, 1.0)) # sparkles return None return pattern_bauble("Ornament_NightStarBauble", coll, loc, base, pat) def make_ruby_bauble(coll, loc): def base(x, z): o = max(abs(x), abs(z), (abs(x) + abs(z)) * 0.72) # octagonal facet rings upper_left = x <= 0 and z >= 0 if o < 1.6: return ("glow", (1.0, 0.2, 0.25)) if o < 3.2: return ("paint", jit(RED_HI if upper_left else (0.95, 0.06, 0.08))) if o < 5.0: return ("paint", jit((0.9, 0.05, 0.08) if upper_left else RED_B)) return ("paint", jit(RED_A if upper_left else RED_LO)) return pattern_bauble("Ornament_RubyBauble", coll, loc, base, lambda x, z: None) REINDEER = [ # facing left; rows from top (z=8) down. x = column - 8 "..A...A.........", "..AA.AA.........", "...AAA..........", "..GGGG..........", ".GGEGG..........", "NGGGGG..........", "..GGGG..........", "..RRRRR.........", "...RGGGGGGGGGG..", "...RGLLGGGGGGGT.", "...RGGGGGGGGGG..", "....GGLLLLLLGG..", "....GG.G..G.GG..", "....HH.H..H.HH..", ] def make_reindeer(coll, loc): palette = { "G": ("paint", GOLD_A), "L": ("paint", GOLD_HI), "A": ("paint", BROWN), "E": ("paint", (0.01, 0.01, 0.02)), "N": ("glow", (1.0, 0.1, 0.12)), "R": ("paint", CANE_R), "T": ("paint", WHITE), "H": ("paint", BROWN_DK), } mask, colours = set(), {} for row, line in enumerate(REINDEER): z = 8 - row for col, ch in enumerate(line): if ch in palette: x = col - 8 mask.add((x, z)) colours[(x, z)] = palette[ch] def colour(x, y, z, d): kind, c = colours[(x, z)] if kind == "paint" and c in (GOLD_A, GOLD_HI) and y > 0: c = GOLD_B # back side a touch darker return (kind, jit(c)) cells = extrude(mask, lambda x, z: 1, colour) for z in (7, 8): # little gold stem joining cap and antlers cells[(-4, 0, z)] = ("metal", jit(GOLD_A)) return finish("Ornament_GoldReindeer", cells, coll, loc, cx=-4, top=8) # ---------------------------------------------------------------- studio def setup_studio(sc, studio, positions): xs = [p[0] for p in positions] zs = [p[1] for p in positions] world = bpy.data.worlds.new("Showcase_World") try: world.use_nodes = True except Exception: pass N, L = world.node_tree.nodes, world.node_tree.links N.clear() tc = N.new("ShaderNodeTexCoord"); sep = N.new("ShaderNodeSeparateXYZ") L.new(tc.outputs["Window"], sep.inputs[0]) inv = N.new("ShaderNodeMath"); inv.operation = "SUBTRACT"; inv.inputs[0].default_value = 1.0 L.new(sep.outputs["Y"], inv.inputs[1]) mix_f = N.new("ShaderNodeMath"); mix_f.operation = "ADD" L.new(sep.outputs["X"], mix_f.inputs[0]); L.new(inv.outputs["Value"], mix_f.inputs[1]) half = N.new("ShaderNodeMath"); half.operation = "MULTIPLY"; half.inputs[1].default_value = 0.5 L.new(mix_f.outputs["Value"], half.inputs[0]) ramp = N.new("ShaderNodeValToRGB") ramp.color_ramp.elements[0].color = (0.30, 0.04, 0.78, 1) # violet (top-left) ramp.color_ramp.elements[1].color = (0.04, 0.10, 0.85, 1) # blue (bottom-right) L.new(half.outputs["Value"], ramp.inputs["Fac"]) bg_cam = N.new("ShaderNodeBackground"); L.new(ramp.outputs["Color"], bg_cam.inputs["Color"]) bg_env = N.new("ShaderNodeBackground") bg_env.inputs["Color"].default_value = (1.0, 0.9, 0.8, 1); bg_env.inputs["Strength"].default_value = 0.45 lp = N.new("ShaderNodeLightPath"); mix = N.new("ShaderNodeMixShader") L.new(lp.outputs["Is Camera Ray"], mix.inputs["Fac"]) L.new(bg_env.outputs["Background"], mix.inputs[1]); L.new(bg_cam.outputs["Background"], mix.inputs[2]) L.new(mix.outputs["Shader"], N.new("ShaderNodeOutputWorld").inputs["Surface"]) sc.world = world # soft magenta halo card behind each ornament halo = _fresh("Halo") HN, HL = halo.node_tree.nodes, halo.node_tree.links tco = HN.new("ShaderNodeTexCoord"); mp = HN.new("ShaderNodeMapping") mp.inputs["Scale"].default_value = (1.9, 1.9, 1.0) # plane local coords are +-0.5 -> soft disc grad = HN.new("ShaderNodeTexGradient"); grad.gradient_type = "SPHERICAL" HL.new(tco.outputs["Object"], mp.inputs["Vector"]); HL.new(mp.outputs["Vector"], grad.inputs["Vector"]) pw = HN.new("ShaderNodeMath"); pw.operation = "POWER"; pw.inputs[1].default_value = 1.6 HL.new(grad.outputs["Fac"], pw.inputs[0]) em = HN.new("ShaderNodeEmission"); em.inputs["Color"].default_value = (0.95, 0.35, 1.0, 1) em.inputs["Strength"].default_value = 1.1 tr = HN.new("ShaderNodeBsdfTransparent"); hm = HN.new("ShaderNodeMixShader") HL.new(pw.outputs["Value"], hm.inputs["Fac"]); HL.new(tr.outputs["BSDF"], hm.inputs[1]); HL.new(em.outputs["Emission"], hm.inputs[2]) HL.new(hm.outputs["Shader"], HN.new("ShaderNodeOutputMaterial").inputs["Surface"]) for attr, val in (("surface_render_method", "BLENDED"), ("blend_method", "BLEND")): try: setattr(halo, attr, val) except Exception: pass for i, (x, z) in enumerate(positions): bm = bmesh.new() bmesh.ops.create_grid(bm, x_segments=1, y_segments=1, size=1.0) me = bpy.data.meshes.new("Halo_%d" % i) bm.to_mesh(me); bm.free() me.materials.append(halo) ob = bpy.data.objects.new("Halo_%d" % i, me) ob.location = (x, 14.0, z + 2.5) ob.rotation_euler = (math.radians(90), 0, 0) ob.scale = (24, 30, 1) ob.visible_shadow = False studio.objects.link(ob) def sun(name, rot, strength, colr, angle=8): L_ = bpy.data.objects.new(name, bpy.data.lights.new(name, "SUN")) L_.data.energy, L_.data.color, L_.data.angle = strength, colr, math.radians(angle) L_.rotation_euler = [math.radians(a) for a in rot] studio.objects.link(L_) sun("Key", (55, 0, -30), 3.2, (1.0, 0.97, 0.92), 12) sun("Fill", (80, 0, 35), 1.0, (0.85, 0.85, 1.0), 20) sun("Rim_Cyan", (70, 0, 150), 2.5, (0.2, 0.85, 1.0)) sun("Rim_Magenta", (70, 0, -150), 2.5, (1.0, 0.3, 0.9)) cam = bpy.data.objects.new("Showcase_Camera", bpy.data.cameras.new("Showcase_Camera")) cam.data.lens = 85 cam.data.clip_end = 2000 span = (max(xs) - min(xs)) + 18 dist = (span / 2) / math.tan(math.atan(18 / 85)) * 1.04 target = Vector((0, 0, (max(zs) + min(zs)) / 2 + 4.0)) az, el = math.radians(9), math.radians(6) cam.location = target + Vector((dist * math.cos(el) * math.sin(az), -dist * math.cos(el) * math.cos(az), dist * math.sin(el))) cam.rotation_euler = (target - cam.location).to_track_quat("-Z", "Y").to_euler() studio.objects.link(cam) sc.camera = cam def setup_render(sc, studio): for eng in ("BLENDER_EEVEE", "BLENDER_EEVEE_NEXT"): try: sc.render.engine = eng break except Exception: pass try: sc.eevee.use_raytracing = True except Exception: pass sc.render.resolution_x, sc.render.resolution_y = 1600, 1200 sc.view_settings.view_transform = "Standard" try: tree = bpy.data.node_groups.new("Showcase_Compositor", "CompositorNodeTree") sc.compositing_node_group = tree rl, gl, go = tree.nodes.new("CompositorNodeRLayers"), tree.nodes.new("CompositorNodeGlare"), tree.nodes.new("NodeGroupOutput") tree.interface.new_socket("Image", in_out="OUTPUT", socket_type="NodeSocketColor") try: gl.glare_type = "BLOOM" except Exception: if "Type" in gl.inputs: gl.inputs["Type"].default_value = "Bloom" for k, v in (("Threshold", 0.95), ("Strength", 0.45), ("Size", 0.55)): if k in gl.inputs: gl.inputs[k].default_value = v tree.links.new(rl.outputs["Image"], gl.inputs["Image"]) tree.links.new(gl.outputs["Image"], go.inputs[0]) except Exception as ex: print("bloom unavailable:", ex) vl = sc.view_layers[0] sc.render.use_freestyle = True sc.render.line_thickness_mode = "ABSOLUTE" fs = vl.freestyle_settings for ls in list(fs.linesets): fs.linesets.remove(ls) ls = fs.linesets.new("Outline") ls.select_by_visibility = True ls.select_by_edge_types = True for a in ("select_silhouette", "select_border", "select_crease", "select_contour", "select_external_contour", "select_material_boundary", "select_edge_mark"): setattr(ls, a, False) ls.select_silhouette = True ls.select_border = True ls.select_by_collection = True ls.collection = studio ls.collection_negation = "EXCLUSIVE" ls.linestyle.color = (0.02, 0.01, 0.06) ls.linestyle.thickness = OUTLINE_PX vl.use_freestyle = True # ---------------------------------------------------------------- main def main(): os.makedirs(OUT_DIR, exist_ok=True) sc, orn, studio = setup_scene() for k in KINDS: MATS[k] = attr_material("Ornament_" + k.capitalize(), k) MATS["hook"] = plain_gold() rows = [ (make_bauble, make_star, make_snowflake, make_bell, make_candy_cane), (make_blue_snow_bauble, make_tree_bauble, make_night_bauble, make_ruby_bauble, make_reindeer), ] positions = [] for r, row in enumerate(rows): z = ROW_GAP / 2 - r * ROW_GAP for i, fn in enumerate(row): x = (i - (len(row) - 1) / 2) * SPACING fn(orn, (x, 0, z)) positions.append((x, z)) setup_studio(sc, studio, positions) setup_render(sc, studio) sc.render.filepath = os.path.join(OUT_DIR, "ornament_showcase.png") bpy.ops.render.render(write_still=True) bpy.ops.wm.save_as_mainfile(filepath=os.path.join(OUT_DIR, "ornament_showcase.blend")) return {"objects": len(orn.objects), "preview": sc.render.filepath} ALL_BUILDERS = None # filled below; used by export_roblox.py def _builders(): return [make_bauble, make_star, make_snowflake, make_bell, make_candy_cane, make_blue_snow_bauble, make_tree_bauble, make_night_bauble, make_ruby_bauble, make_reindeer] if __name__ == "__main__": result = main() print("RESULT", result)