182 lines
7.1 KiB
Python
182 lines
7.1 KiB
Python
import bpy
|
|
import math
|
|
from pathlib import Path
|
|
|
|
SIZE = 512
|
|
INK = (0.012, 0.018, 0.020, 1.0)
|
|
PAPER = (0.88, 0.87, 0.80, 1.0)
|
|
FONT = {
|
|
'0': ['01110', '10001', '10011', '10101', '11001', '10001', '01110'],
|
|
'1': ['00100', '01100', '00100', '00100', '00100', '00100', '01110'],
|
|
'2': ['01110', '10001', '00001', '00010', '00100', '01000', '11111'],
|
|
'3': ['11110', '00001', '00001', '01110', '00001', '00001', '11110'],
|
|
'4': ['00010', '00110', '01010', '10010', '11111', '00010', '00010'],
|
|
'5': ['11111', '10000', '10000', '11110', '00001', '00001', '11110'],
|
|
'6': ['01110', '10000', '10000', '11110', '10001', '10001', '01110'],
|
|
'7': ['11111', '00001', '00010', '00100', '01000', '01000', '01000'],
|
|
'8': ['01110', '10001', '10001', '01110', '10001', '10001', '01110'],
|
|
'9': ['01110', '10001', '10001', '01111', '00001', '00001', '01110'],
|
|
'T': ['11111', '00100', '00100', '00100', '00100', '00100', '00100'],
|
|
'E': ['11111', '10000', '10000', '11110', '10000', '10000', '11111'],
|
|
'M': ['10001', '11011', '10101', '10101', '10001', '10001', '10001'],
|
|
'P': ['11110', '10001', '10001', '11110', '10000', '10000', '10000'],
|
|
'F': ['11111', '10000', '10000', '11110', '10000', '10000', '10000'],
|
|
'L': ['10000', '10000', '10000', '10000', '10000', '10000', '11111'],
|
|
'O': ['01110', '10001', '10001', '10001', '10001', '10001', '01110'],
|
|
'W': ['10001', '10001', '10001', '10101', '10101', '11011', '10001'],
|
|
'D': ['11110', '10001', '10001', '10001', '10001', '10001', '11110'],
|
|
'G': ['01110', '10001', '10000', '10111', '10001', '10001', '01110'],
|
|
'C': ['01111', '10000', '10000', '10000', '10000', '10000', '01111'],
|
|
'°': ['01100', '10010', '10010', '01100', '00000', '00000', '00000'],
|
|
'%': ['11001', '11010', '00010', '00100', '01000', '01011', '10011'],
|
|
' ': ['00000'] * 7,
|
|
}
|
|
|
|
|
|
def canvas():
|
|
return list(PAPER) * (SIZE * SIZE)
|
|
|
|
|
|
def pixel(data, x, y, color=INK):
|
|
if 0 <= x < SIZE and 0 <= y < SIZE:
|
|
index = (y * SIZE + x) * 4
|
|
data[index:index + 4] = color
|
|
|
|
|
|
def rect(data, x, y, width, height, color=INK):
|
|
for yy in range(max(0, y), min(SIZE, y + height)):
|
|
for xx in range(max(0, x), min(SIZE, x + width)):
|
|
pixel(data, xx, yy, color)
|
|
|
|
|
|
def line(data, x1, y1, x2, y2, width=2, color=INK):
|
|
dx = x2 - x1
|
|
dy = y2 - y1
|
|
length2 = dx * dx + dy * dy
|
|
min_x = max(0, int(min(x1, x2) - width - 1))
|
|
max_x = min(SIZE, int(max(x1, x2) + width + 2))
|
|
min_y = max(0, int(min(y1, y2) - width - 1))
|
|
max_y = min(SIZE, int(max(y1, y2) + width + 2))
|
|
for y in range(min_y, max_y):
|
|
for x in range(min_x, max_x):
|
|
if length2:
|
|
t = max(0, min(1, ((x - x1) * dx + (y - y1) * dy) / length2))
|
|
else:
|
|
t = 0
|
|
px = x1 + t * dx
|
|
py = y1 + t * dy
|
|
if (x - px) ** 2 + (y - py) ** 2 <= (width / 2) ** 2:
|
|
pixel(data, x, y, color)
|
|
|
|
|
|
def text(data, body, center_x, top_y, scale=4, color=INK):
|
|
body = str(body)
|
|
total = (len(body) * 6 - 1) * scale
|
|
start_x = int(center_x - total / 2)
|
|
for index, char in enumerate(body):
|
|
glyph = FONT.get(char.upper(), FONT[' '])
|
|
for row, pattern in enumerate(glyph):
|
|
for column, bit in enumerate(pattern):
|
|
if bit == '1':
|
|
rect(data, start_x + (index * 6 + column) * scale, top_y + row * scale, scale, scale, color)
|
|
|
|
|
|
def draw_gauge(data, label, unit):
|
|
center = SIZE / 2
|
|
for value in range(0, 101, 5):
|
|
angle = math.radians(180 - value * 1.8)
|
|
outer = 226
|
|
inner = 199 if value % 25 == 0 else 211
|
|
line(data, center + inner * math.cos(angle), center - inner * math.sin(angle), center + outer * math.cos(angle), center - outer * math.sin(angle), 4 if value % 25 == 0 else 2)
|
|
if value % 25 == 0:
|
|
text(data, str(value), center + 155 * math.cos(angle), int(center - 155 * math.sin(angle) - 13), 4 if value != 100 else 3)
|
|
text(data, label, center, 307, 5)
|
|
text(data, unit, center, 354, 4)
|
|
|
|
|
|
def draw_rotating(data, maximum, labels, unit):
|
|
center = SIZE / 2
|
|
for value in range(maximum + 1):
|
|
if value % 5:
|
|
continue
|
|
angle = math.radians(90 + value * 270 / maximum)
|
|
outer = 226
|
|
inner = 199 if value % 30 == 0 else 211
|
|
line(data, center + inner * math.cos(angle), center - inner * math.sin(angle), center + outer * math.cos(angle), center - outer * math.sin(angle), 4 if value % 30 == 0 else 2)
|
|
for value, label in labels:
|
|
angle = math.radians(90 + value * 270 / maximum)
|
|
text(data, label, center + 148 * math.cos(angle), int(center - 148 * math.sin(angle) - 12), 4 if len(str(label)) < 3 else 3)
|
|
text(data, unit, center, 343, 4)
|
|
|
|
|
|
def draw_conical(data, maximum):
|
|
step = 10 if maximum == 270 else 5
|
|
major_step = 30 if maximum == 270 else 25
|
|
base_y = SIZE - 1
|
|
for value in range(maximum + 1):
|
|
if value % step:
|
|
continue
|
|
angle = (180 + value * 270 / maximum) % 360
|
|
x = int(angle / 360 * SIZE) % SIZE
|
|
major = value % major_step == 0
|
|
line(data, x, base_y - 4, x, base_y - (220 if major else 136), 10 if major else 5)
|
|
|
|
|
|
def create_dial_texture(kind, filepath):
|
|
data = canvas()
|
|
if kind == 'hot_water_conical':
|
|
draw_conical(data, 270)
|
|
elif kind == 'flow_dial_conical':
|
|
draw_conical(data, 100)
|
|
elif kind == 'hot_water':
|
|
draw_rotating(data, 270, [(0, '0'), (90, '90'), (180, '180'), (270, '270')], 'DEG')
|
|
elif kind == 'flow_dial':
|
|
draw_rotating(data, 100, [(0, '0'), (25, '25'), (50, '50'), (75, '75'), (100, '100')], '%')
|
|
elif kind == 'temperature':
|
|
draw_gauge(data, 'TEMP', '°C')
|
|
elif kind == 'flow_gauge':
|
|
draw_gauge(data, 'FLOW', '%')
|
|
else:
|
|
raise ValueError(kind)
|
|
pixels = []
|
|
for y in range(SIZE - 1, -1, -1):
|
|
start = y * SIZE * 4
|
|
pixels.extend(data[start:start + SIZE * 4])
|
|
image = bpy.data.images.new('CM_' + kind + '_FaceTexture', width=SIZE, height=SIZE, alpha=True)
|
|
image.pixels = pixels
|
|
image.update()
|
|
image.filepath_raw = str(Path(filepath))
|
|
image.file_format = 'PNG'
|
|
image.save()
|
|
image.pack()
|
|
return image
|
|
|
|
|
|
def create_arrow_texture(direction, filepath):
|
|
data = [0.0] * (SIZE * SIZE * 4)
|
|
red = (0.72, 0.025, 0.018, 1.0)
|
|
for y in range(120, 393):
|
|
ratio = abs(y - 256) / 136
|
|
if direction < 0:
|
|
start = int(60 + 320 * ratio)
|
|
end = 380
|
|
else:
|
|
start = 132
|
|
end = int(452 - 320 * ratio)
|
|
for x in range(start, end + 1):
|
|
index = (y * SIZE + x) * 4
|
|
data[index:index + 4] = red
|
|
pixels = []
|
|
for y in range(SIZE - 1, -1, -1):
|
|
start = y * SIZE * 4
|
|
pixels.extend(data[start:start + SIZE * 4])
|
|
kind = 'arrow_left' if direction < 0 else 'arrow_right'
|
|
image = bpy.data.images.new('CM_' + kind + '_DecalTexture', width=SIZE, height=SIZE, alpha=True)
|
|
image.pixels = pixels
|
|
image.update()
|
|
image.filepath_raw = str(Path(filepath))
|
|
image.file_format = 'PNG'
|
|
image.save()
|
|
image.pack()
|
|
return image
|