Files
generate_ashmaw.py
Code
1 322
generate_ashmaw.py
import math
import os
import random
import bpy
from mathutils import Vector
OUTPUT = os.path.dirname(os.path.abspath(__file__))
TEXTURE_SIZE = 64
TILE_WIDTH = 16
TILE_HEIGHT = 32
model_objects = []
def make_atlas():
image = bpy.data.images.new("Ashmaw_64x64", width=64, height=64, alpha=True)
rng = random.Random(731)
pixels = [0.0] * (64 * 64 * 4)
def put(x, y, color):
offset = (y * 64 + x) * 4
pixels[offset:offset + 4] = [color[0] / 255, color[1] / 255, color[2] / 255, 1.0]
for tile in range(5):
col, row = tile % 4, tile // 4
ox, oy = col * TILE_WIDTH, row * TILE_HEIGHT
for y in range(TILE_HEIGHT):
for x in range(TILE_WIDTH):
noise = rng.randint(-9, 9)
if tile == 0:
shade = 43 + noise + (7 if (x + y * 3) % 13 == 0 else 0)
color = (shade, shade + 2, shade + 5)
elif tile == 1:
shade = 34 + noise
color = (shade, shade + 2, shade + 5)
crack_x = (4 + y // 7 + (1 if y % 5 > 2 else 0)) % TILE_WIDTH
second_x = (12 - y // 9) % TILE_WIDTH
if x in (crack_x, second_x) or (y % 11 == 0 and 5 <= x <= 8):
color = (255, 91 + rng.randrange(60), 9)
elif abs(x - crack_x) == 1 or abs(x - second_x) == 1:
color = (100, 37, 12)
elif tile == 2:
shade = 17 + noise
color = (shade + 3, shade, shade + 12)
if (x * 7 + y * 11) % 37 == 0:
color = (78, 48, 92)
elif tile == 3:
strand = (x // 2) % 2
shade = (31 if strand else 20) + noise
color = (shade + 6, shade + 2, shade)
else:
color = (255, 105 + rng.randrange(70), 12 + rng.randrange(18))
if (x + y) % 9 == 0:
color = (255, 190, 45)
put(ox + x, oy + y, color)
image.pixels.foreach_set(pixels)
image.pack()
image.filepath_raw = os.path.join(OUTPUT, "ashmaw_atlas.png")
image.file_format = "PNG"
image.save()
return image
def make_material(image):
material = bpy.data.materials.new("Ashmaw_Pixel_Atlas")
material.use_nodes = True
nodes = material.node_tree.nodes
links = material.node_tree.links
shader = nodes.get("Principled BSDF")
texture = nodes.new("ShaderNodeTexImage")
texture.image = image
texture.interpolation = "Closest"
texture.extension = "REPEAT"
links.new(texture.outputs["Color"], shader.inputs["Base Color"])
emission = shader.inputs.get("Emission Color") or shader.inputs.get("Emission")
if emission:
links.new(texture.outputs["Color"], emission)
strength = shader.inputs.get("Emission Strength")
if strength:
strength.default_value = 0.16
shader.inputs["Roughness"].default_value = 0.82
shader.inputs["Metallic"].default_value = 0.12
return material
def tile_uv(tile):
col, row = tile % 4, tile // 4
return (
(col * TILE_WIDTH + 1) / 64,
(row * TILE_HEIGHT + 1) / 64,
((col + 1) * TILE_WIDTH - 1) / 64,
((row + 1) * TILE_HEIGHT - 1) / 64,
)
def bind_to_bone(obj, bone_name):
group = obj.vertex_groups.new(name=bone_name)
group.add(list(range(len(obj.data.vertices))), 1.0, "REPLACE")
modifier = obj.modifiers.new("Ashmaw_Rig", "ARMATURE")
modifier.object = rig
obj.parent = rig
model_objects.append(obj)
def make_box(name, location, dimensions, tile, bone_name, rotation=(0, 0, 0)):
hx, hy, hz = (value / 2 for value in dimensions)
corners = [
(-hx, -hy, -hz), (hx, -hy, -hz), (hx, hy, -hz), (-hx, hy, -hz),
(-hx, -hy, hz), (hx, -hy, hz), (hx, hy, hz), (-hx, hy, hz),
]
faces = [
(0, 3, 2, 1), (4, 5, 6, 7), (0, 1, 5, 4),
(3, 7, 6, 2), (0, 4, 7, 3), (1, 2, 6, 5),
]
rot = Vector(rotation)
from mathutils import Euler
matrix = Euler(rot, "XYZ").to_matrix()
origin = Vector(location)
vertices = [tuple(matrix @ Vector(point) + origin) for point in corners]
mesh = bpy.data.meshes.new(name + "_Mesh")
mesh.from_pydata(vertices, [], faces)
mesh.update()
uv_layer = mesh.uv_layers.new(name="AtlasUV")
u0, v0, u1, v1 = tile_uv(tile)
face_uvs = ((u0, v0), (u1, v0), (u1, v1), (u0, v1))
for polygon in mesh.polygons:
for loop_index, uv in zip(polygon.loop_indices, face_uvs):
uv_layer.data[loop_index].uv = uv
mesh.materials.append(atlas_material)
obj = bpy.data.objects.new(name, mesh)
bpy.context.collection.objects.link(obj)
bind_to_bone(obj, bone_name)
return obj
def make_tapered(name, base, tip, radii, tile, bone_name, sides=7, curve=0.0):
start, end = Vector(base), Vector(tip)
axis = (end - start).normalized()
reference = Vector((1, 0, 0)) if abs(axis.x) < 0.9 else Vector((0, 1, 0))
right = axis.cross(reference).normalized()
forward = axis.cross(right).normalized()
vertices = []
for ring_index, radius in enumerate(radii):
t = ring_index / (len(radii) - 1)
center = start.lerp(end, t) + Vector((0, -curve * math.sin(math.pi * t), 0))
for side in range(sides):
angle = 2 * math.pi * side / sides
point = center + radius * (math.cos(angle) * right + math.sin(angle) * forward)
vertices.append(tuple(point))
faces = []
for ring in range(len(radii) - 1):
for side in range(sides):
a = ring * sides + side
b = ring * sides + (side + 1) % sides
faces.append((a, b, b + sides, a + sides))
faces.append(tuple(range(sides - 1, -1, -1)))
faces.append(tuple((len(radii) - 1) * sides + side for side in range(sides)))
mesh = bpy.data.meshes.new(name + "_Mesh")
mesh.from_pydata(vertices, [], faces)
mesh.update()
uv_layer = mesh.uv_layers.new(name="AtlasUV")
u0, v0, u1, v1 = tile_uv(tile)
face_uvs = ((u0, v0), (u1, v0), (u1, v1), (u0, v1))
for polygon in mesh.polygons:
for loop_index, uv in zip(polygon.loop_indices, face_uvs):
uv_layer.data[loop_index].uv = face_uvs[loop_index % 4]
mesh.materials.append(atlas_material)
obj = bpy.data.objects.new(name, mesh)
bpy.context.collection.objects.link(obj)
bind_to_bone(obj, bone_name)
return obj
def make_rig():
global rig
data = bpy.data.armatures.new("Ashmaw_Skeleton")
rig = bpy.data.objects.new("Ashmaw_Rig", data)
bpy.context.collection.objects.link(rig)
bpy.context.view_layer.objects.active = rig
rig.select_set(True)
bpy.ops.object.mode_set(mode="EDIT")
bones = {
"root": ((0, 0, 0), (0, 0, 1)),
"body": ((0, 0.2, 1.15), (0, 0.2, 2.7)),
"head": ((0, -1.1, 1.9), (0, -2.1, 2.2)),
}
for side, x in (("L", -0.82), ("R", 0.82)):
for position, y in (("F", -1.02), ("B", 1.02)):
bones[f"leg_{position}{side}"] = ((x, y, 1.5), (x, y, 0.18))
edit_bones = {}
for name, (head, tail) in bones.items():
bone = data.edit_bones.new(name)
bone.head, bone.tail = head, tail
edit_bones[name] = bone
edit_bones["body"].parent = edit_bones["root"]
edit_bones["head"].parent = edit_bones["body"]
for name in bones:
if name.startswith("leg_"):