1
This commit is contained in:
1 parent
0a69e82952
commit
72e08f024f
997 files changed
+345
-1
No files matched your search
@@ -0,0 +1,181 @@
|
||||
# Export every showcase ornament as a Roblox-ready .glb (one MeshPart each).
|
||||
# Run: blender -b --factory-startup --python export_roblox.py
|
||||
#
|
||||
# Per ornament:
|
||||
# * body = merged exposed faces of the voxel grid (no hidden faces, no bevels) -> low triangle count
|
||||
# * colours baked into a tiny palette PNG (Roblox ignores vertex colours); faces UV to their texel
|
||||
# * gold hook + ring joined into the same mesh, UV'd to the gold texel
|
||||
# * scaled so the ornament is ~4-5 studs tall, origin at the ornament centre, transforms applied
|
||||
import bpy, bmesh, math, os
|
||||
|
||||
HERE = r"C:\roblox\Ornament Generator RNG\ornament_showcase"
|
||||
OUT = os.path.join(HERE, "roblox_export")
|
||||
SCALE = 0.05 # 1 voxel = 5 cm -> ornament ~1.3 m (~4.6 studs)
|
||||
TEX = 64 # palette texture size (TEX x TEX texels; per-pixel colour jitter needs room)
|
||||
|
||||
ns = {"__name__": "showcase_lib"}
|
||||
exec(open(os.path.join(HERE, "ornament_showcase.py"), encoding="utf-8").read(), ns)
|
||||
|
||||
def to_srgb(c):
|
||||
return 12.92 * c if c <= 0.0031308 else 1.055 * (c ** (1 / 2.4)) - 0.055
|
||||
|
||||
N6 = ((1, 0, 0), (-1, 0, 0), (0, 1, 0), (0, -1, 0), (0, 0, 1), (0, 0, -1))
|
||||
captured = {}
|
||||
|
||||
def capture_voxels(name, cells, coll, loc, bevel=0.0):
|
||||
"""Replacement for ornament_showcase.voxel_object: builds the merged exposed-face mesh."""
|
||||
palette = captured.setdefault("palette", {})
|
||||
def texel(colour):
|
||||
key = tuple(round(v, 3) for v in colour)
|
||||
if key not in palette:
|
||||
palette[key] = len(palette)
|
||||
i = palette[key]
|
||||
return ((i % TEX) + 0.5) / TEX, ((i // TEX) + 0.5) / TEX
|
||||
bm = bmesh.new()
|
||||
uv = bm.loops.layers.uv.new("UVMap")
|
||||
vc = {}
|
||||
def vert(p):
|
||||
k = (round(p[0], 4), round(p[1], 4), round(p[2], 4))
|
||||
if k not in vc:
|
||||
vc[k] = bm.verts.new(k)
|
||||
return vc[k]
|
||||
for (x, y, z), (kind, colour) in cells.items():
|
||||
for a in range(3):
|
||||
for s in (-1, 1):
|
||||
n = [x, y, z]; n[a] += s
|
||||
if tuple(n) in cells:
|
||||
continue
|
||||
o = [i for i in range(3) if i != a]
|
||||
quad = []
|
||||
for uu, vv in ((-0.5, -0.5), (0.5, -0.5), (0.5, 0.5), (-0.5, 0.5)):
|
||||
p = [float(x), float(y), float(z)]
|
||||
p[a] += 0.5 * s
|
||||
p[o[0]] += uu
|
||||
p[o[1]] += vv
|
||||
quad.append(vert(p))
|
||||
if s < 0:
|
||||
quad.reverse()
|
||||
if a == 1:
|
||||
quad.reverse() # keep outward winding for the Y axis permutation
|
||||
f = bm.faces.new(quad)
|
||||
t = texel(colour)
|
||||
for lp in f.loops:
|
||||
lp[uv].uv = t
|
||||
bmesh.ops.recalc_face_normals(bm, faces=list(bm.faces))
|
||||
me = bpy.data.meshes.new(name)
|
||||
bm.to_mesh(me)
|
||||
bm.free()
|
||||
ob = bpy.data.objects.new(name, me)
|
||||
ob.location = loc
|
||||
coll.objects.link(ob)
|
||||
captured["body"] = ob
|
||||
return ob
|
||||
|
||||
ns["voxel_object"] = capture_voxels
|
||||
|
||||
def mesh_from(ob, gold_uv):
|
||||
"""Evaluate (curve/torus) object to a mesh and point all its UVs at the gold texel."""
|
||||
dg = bpy.context.evaluated_depsgraph_get()
|
||||
me = bpy.data.meshes.new_from_object(ob.evaluated_get(dg))
|
||||
me.transform(ob.matrix_world)
|
||||
if not me.uv_layers:
|
||||
me.uv_layers.new(name="UVMap")
|
||||
for d in me.uv_layers[0].data:
|
||||
d.uv = gold_uv
|
||||
return me
|
||||
|
||||
def export_one(fn, coll, mat):
|
||||
captured.clear()
|
||||
for o in list(coll.objects):
|
||||
bpy.data.objects.remove(o, do_unlink=True)
|
||||
fn(coll, (0, 0, 0))
|
||||
body = captured["body"]
|
||||
palette = captured["palette"]
|
||||
gold_key = tuple(round(v, 3) for v in ns["GOLD_A"])
|
||||
if gold_key not in palette:
|
||||
palette[gold_key] = len(palette)
|
||||
gi = palette[gold_key]
|
||||
gold_uv = (((gi % TEX) + 0.5) / TEX, ((gi // TEX) + 0.5) / TEX)
|
||||
if len(palette) > TEX * TEX:
|
||||
raise RuntimeError("palette overflow")
|
||||
|
||||
# join hook + ring into the body mesh
|
||||
bm = bmesh.new()
|
||||
bm.from_mesh(body.data)
|
||||
for o in list(coll.objects):
|
||||
if o is body:
|
||||
continue
|
||||
hm = mesh_from(o, gold_uv)
|
||||
bm.from_mesh(hm)
|
||||
bpy.data.meshes.remove(hm)
|
||||
bpy.data.objects.remove(o, do_unlink=True)
|
||||
bm.to_mesh(body.data)
|
||||
bm.free()
|
||||
|
||||
# palette texture
|
||||
name = body.name.replace("Ornament_", "")
|
||||
img = bpy.data.images.new(name + "_Palette", TEX, TEX, alpha=False)
|
||||
px = [0.0] * (TEX * TEX * 4)
|
||||
for colour, i in palette.items():
|
||||
px[i * 4:i * 4 + 4] = [to_srgb(colour[0]), to_srgb(colour[1]), to_srgb(colour[2]), 1.0]
|
||||
img.pixels = px
|
||||
img.filepath_raw = os.path.join(OUT, name + "_Palette.png")
|
||||
img.file_format = "PNG"
|
||||
img.save()
|
||||
|
||||
m = mat.copy()
|
||||
m.name = name + "_Mat"
|
||||
tex = next(n for n in m.node_tree.nodes if n.type == "TEX_IMAGE")
|
||||
tex.image = img
|
||||
body.data.materials.clear()
|
||||
body.data.materials.append(m)
|
||||
|
||||
# centre (body centre stays at origin), scale, apply transforms
|
||||
body.data.transform(__import__("mathutils").Matrix.Scale(SCALE, 4))
|
||||
body.name = name
|
||||
tris = sum(len(p.vertices) - 2 for p in body.data.polygons)
|
||||
|
||||
bpy.ops.object.select_all(action="DESELECT")
|
||||
body.select_set(True)
|
||||
bpy.context.view_layer.objects.active = body
|
||||
path = os.path.join(OUT, name + ".glb")
|
||||
bpy.ops.export_scene.gltf(filepath=path, export_format="GLB", use_selection=True,
|
||||
export_apply=True, export_yup=True)
|
||||
dims = [round(v, 3) for v in body.dimensions]
|
||||
return {"file": os.path.basename(path), "tris": tris, "colors": len(palette), "size_m": dims}
|
||||
|
||||
def main():
|
||||
os.makedirs(OUT, exist_ok=True)
|
||||
sc = bpy.context.scene
|
||||
for o in list(bpy.data.objects):
|
||||
bpy.data.objects.remove(o, do_unlink=True)
|
||||
coll = bpy.data.collections.new("Export")
|
||||
sc.collection.children.link(coll)
|
||||
MATS = ns["MATS"]
|
||||
for k in ns["KINDS"]:
|
||||
MATS[k] = ns["attr_material"]("Ornament_" + k.capitalize(), k)
|
||||
MATS["hook"] = ns["plain_gold"]()
|
||||
|
||||
base = bpy.data.materials.new("Palette_Base")
|
||||
try:
|
||||
base.use_nodes = True
|
||||
except Exception:
|
||||
pass
|
||||
nt = base.node_tree
|
||||
nt.nodes.clear()
|
||||
bs = nt.nodes.new("ShaderNodeBsdfPrincipled")
|
||||
bs.inputs["Roughness"].default_value = 0.35
|
||||
tx = nt.nodes.new("ShaderNodeTexImage")
|
||||
tx.interpolation = "Closest"
|
||||
out = nt.nodes.new("ShaderNodeOutputMaterial")
|
||||
nt.links.new(tx.outputs["Color"], bs.inputs["Base Color"])
|
||||
nt.links.new(bs.outputs["BSDF"], out.inputs["Surface"])
|
||||
|
||||
report = []
|
||||
for fn in ns["_builders"]():
|
||||
ns["random"].seed(3)
|
||||
report.append(export_one(fn, coll, base))
|
||||
return report
|
||||
|
||||
for r in main():
|
||||
print("EXPORT", r)
|
||||
Reference in new issue
Block a user