-
-
Notifications
You must be signed in to change notification settings - Fork 6
Expand file tree
/
Copy pathconvert_model.py
More file actions
116 lines (103 loc) · 3 KB
/
convert_model.py
File metadata and controls
116 lines (103 loc) · 3 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
#!/usr/bin/python3
# Title: Create C model data from an OBJ file exported from Blender
# Author: Dean Belfield
# Created: 03/09/2025
# Last Updated: 04/09/2025
#
# Modinfo:
# 04/09/2025: Removed debug code, fixed bugs, added scale arg
import sys
import os
from datetime import datetime
from pathlib import Path
# The Next palette lookup
# https://wiki.specnext.dev/File:256-palette-2.png
#
palette = {
"Black": "0x00",
"Blue": "0x07",
"Red": "0xE0",
"Magenta": "0xA3",
"Green": "0x5C",
"Cyan": "0x1F",
"Yellow": "0xFC",
"White": "0xFF",
"Grey": "0x6E",
"Light_Grey": "0x6D",
"Dark_Grey": "0X6C",
}
# Open the file for reading
#
name = sys.argv[1] # Get the filename
try:
scale = float(sys.argv[2])
except:
scale = 100
full_path = os.path.expanduser(name) # Expand the full path to the file and
file = open(full_path, "r") # Open it up as a text file
file_stdout = sys.stdout # Store the current stdout file handle for redirect output
sys.stdout = open(Path(full_path).stem + ".h", "w")
now = datetime.now()
line = "" # Storage for line
modelName = "" # The model name
vertices = [] # List of vertices
faces = [] # List of faces
colour = "0x12" # Default colour palette entry on Next
# Iterate through the file
# Use print to output to file
#
while True:
line = file.readline() # Read 1 byte into the buffer data
if len(line) == 0:
break
code = line[:line.find(" ")] # Find the code
data = line[line.find(" ")+1:].rstrip('\n').split(" ")
if code == "o":
modelName = data[0]
elif code == "v": # Vertex data
if len(data) != 3:
sys.exit("Invalid vertex count")
output = []
for item in data:
value = round(float(item)*scale/100)
if value < -128 or value > 127:
sys.exit("Coordinate data out of range")
output.append(str(value))
vertices.append(f" POINT8( {', '.join(output)} ),")
elif code == "vn:": # Normal data
pass
elif code == "usemtl": # Blender material name
colour = palette[data[0]]
elif code == "f": # Face data
if len(data) != 3:
sys.exit("Invalid face count")
output = []
for item in data:
value = item.split("//")
output.append(str(int(value[0])-1)) # Blender indexes vertices from 1, not 0
output.append(colour) # Colour of face
faces.append(f" {{ {', '.join(output)} }},")
elif code == "l": # Line data
pass
else:
pass
print(f"// Generated automatically by convert_model.py on {now.strftime('%m/%d/%Y, %H:%M:%S')}")
print(f"//")
print(f"// Model: {name}")
print(f"// Scale: {scale}")
print(f"//")
print(f"Model_3D {modelName}_m = {{")
print(f" {len(vertices)},");
print(f" {len(faces)},");
print(f" &{modelName}_p,");
print(f" &{modelName}_v,");
print("};")
print(f"Point8_3D {modelName}_p[] = {{")
print("\n".join(vertices))
print("};")
print(f"Vertice_3D {modelName}_v[] = {{")
print("\n".join(faces))
print("};")
file.close() # We've done so close the files
sys.stdout.close() # Close and restore stdout
sys.stdout = file_stdout