-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathbrenderlib.py
88 lines (70 loc) · 1.85 KB
/
brenderlib.py
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
#
# Imports
#
import sys, getopt, os
import struct, numpy
#
# BRender file constants
#
# file types (for reading)
BR_FILE_VERSION = 2
BR_FILE_MODELS = 64206
BR_FILE_PIXELMAPS = 2
BR_FILE_TERMINATOR = 0
# file types (for writing)
BR_FILE_VERSION_B = b"\x00\x00\x00\x02"
BR_FILE_MODELS_B = b"\x00\x00\xFA\xCE"
BR_FILE_PIXELMAPS_B = b"\x00\x00\x00\x02"
BR_FILE_TERMINATOR_B = b"\x00\x00\x00\x00"
# top-level chunk types
BR_CHUNK_PIXELMAP = 3
BR_CHUNK_FILEINFO = 18
BR_CHUNK_PIXELDATA = 33
BR_CHUNK_MODEL = 54
# model subchunk types
BR_CHUNK_VERTICES = 23
BR_CHUNK_MATERIALS = 22
BR_CHUNK_UVS = 24
BR_CHUNK_MATERIAL_ASSIGNMENTS = 26
BR_CHUNK_FACES = 53
# bitmap types
BR_PIXELMAP_8BIT = 3
BR_PIXELMAP_ARGB = 7
#
# BRenderLib class
#
class BRenderLib:
#
# initialization
#
def __init__(self, outpath):
self.outpath = outpath
#
# file I/O operations
#
# open file and write header chunk
def OpenDataFile(self, filetype):
self.file = open(self.outpath, "wb")
self.WriteChunk(BR_CHUNK_FILEINFO, filetype)
# close file when done
def CloseDataFile(self):
self.file.close()
#
# writing data to file
#
# writing chunks to file
def WriteChunk(self, identifier, data):
self.file.write(struct.pack(">i", identifier))
self.file.write(struct.pack(">i", len(data)))
self.file.write(struct.pack(">" + "B" * len(data), *data))
def WriteTerminator(self):
self.file.write(struct.pack(">" + "B" * len(BR_FILE_TERMINATOR_B), *BR_FILE_TERMINATOR_B))
# write model
def WriteModel(self, identifier, vertices, uvs, faces, materials, material_assignments):
self.WriteChunk(BR_CHUNK_MODEL, identifier)
self.WriteChunk(BR_CHUNK_VERTICES, vertices)
self.WriteChunk(BR_CHUNK_UVS, uvs)
self.WriteChunk(BR_CHUNK_FACES, faces)
self.WriteChunk(BR_CHUNK_MATERIALS, materials)
self.WriteChunk(BR_CHUNK_MATERIAL_ASSIGNMENTS, material_assignments)
self.WriteTerminator()