This repository has been archived by the owner on Dec 2, 2020. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 10
/
Copy pathminecraft-renderObj.py
262 lines (224 loc) · 7.85 KB
/
minecraft-renderObj.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
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
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
#www.stuffaboutcode.com
#Raspberry Pi, Minecraft - Create 3D Model from Obj file
#import the minecraft.py module from the minecraft directory
import minecraft.minecraft as minecraft
#import minecraft block module
import minecraft.block as block
#import time, so delays can be used
import time
#import datetime, to get the time!
import datetime
# return maximum of 2 values
def MAX(a,b):
if a > b: return a
else: return b
# return step
def ZSGN(a):
if a < 0: return -1
elif a > 0: return 1
elif a == 0: return 0
# draw point
def point3d(mc, x, y, z, blockType):
mc.setBlock(x,y,z,blockType)
# draw a line in 3d space
def line3d(mc, x1, y1, z1, x2, y2, z2, blockType):
# if the 2 points are the same, draw a single block
if (x1 == x2 and y1 == y2 and z1 == z2):
mc.setBlock(x1, y1, z1, blockType)
# else draw a line in 3d space
else:
dx = x2 - x1
dy = y2 - y1
dz = z2 - z1
ax = abs(dx) << 1
ay = abs(dy) << 1
az = abs(dz) << 1
sx = ZSGN(dx)
sy = ZSGN(dy)
sz = ZSGN(dz)
x = x1
y = y1
z = z1
# x dominant
if (ax >= MAX(ay, az)):
yd = ay - (ax >> 1)
zd = az - (ax >> 1)
loop = True
while(loop):
point3d(mc, x, y, z, blockType)
if (x == x2):
loop = False
if (yd >= 0):
y += sy
yd -= ax
if (zd >= 0):
z += sz
zd -= ax
x += sx
yd += ay
zd += az
# y dominant
elif (ay >= MAX(ax, az)):
xd = ax - (ay >> 1)
zd = az - (ay >> 1)
loop = True
while(loop):
point3d(mc, x, y, z, blockType)
if (y == y2):
loop=False
if (xd >= 0):
x += sx
xd -= ay
if (zd >= 0):
z += sz
zd -= ay
y += sy
xd += ax
zd += az
# z dominant
elif(az >= MAX(ax, ay)):
xd = ax - (az >> 1)
yd = ay - (az >> 1)
loop = True
while(loop):
point3d(mc, x, y, z, blockType)
if (z == z2):
loop=False
if (xd >= 0):
x += sx
xd -= az
if (yd >= 0):
y += sy
yd -= az
z += sz
xd += ax
yd += ay
# load obj into lists
def load_obj(filename) :
V = [] #vertex
T = [] #texcoords
N = [] #normals
F = [] #face indexies
fh = open(filename)
for line in fh :
if line[0] == '#' : continue
line = line.strip().split(' ')
if line[0] == 'v' : #vertex
V.append(line[1:])
elif line[0] == 'vt' : #tex-coord
T.append(line[1:])
elif line[0] == 'vn' : #normal vector
N.append(line[1:])
elif line[0] == 'f' : #face
face = line[1:]
for i in range(0, len(face)) :
face[i] = face[i].split('/')
# OBJ indexies are 1 based not 0 based hence the -1
# convert indexies to integer
for j in range(0, len(face[i])) :
if face[i][j] != "":
face[i][j] = int(face[i][j]) - 1
F.append(face)
return V, T, N, F
# strips the x,y,z co-ords from a vertex line, scales appropriately, rounds and converts to int
def getVertexXYZ(vertexLine, scale, startCoord, swapYZ):
# convert, round and scale
x = int((float(vertexLine[0]) * scale) + 0.5)
y = int((float(vertexLine[1]) * scale) + 0.5)
z = int((float(vertexLine[2]) * scale) + 0.5)
# add startCoord to x,y,z
x = x + startCoord.x
y = y + startCoord.y
z = z + startCoord.z
# swap y and z coord if needed
if swapYZ == True:
swap = y
y = z
z = swap
return x, y, z
# main program
if __name__ == "__main__":
print datetime.datetime.now()
#Connect to minecraft by creating the minecraft object
# - minecraft needs to be running and in a game
mc = minecraft.Minecraft.create()
mc.player.setPos(-131,25,98)
#Post a message to the minecraft chat window
mc.postToChat("Hi, Minecraft 3d model maker, www.stuffaboutcode.com")
#Load objfile and set constants
# COORDSSCALE = factor to scale the co-ords by
# STARTCOORD = where to start the model, the relative position 0
# CLEARAREA1/2 = 2 points the program should clear an area in between to put the model in
# SWAPYZ = True to sway the Y and Z dimension
# BLOCKTYPE = type of block to build the model in
# Cube
#COORDSSCALE = 10
#STARTCOORD = minecraft.Vec3(0,10,0)
#BLOCKTYPE = block.STONE
#SWAPYZ = False
#vertices,textures,normals,faces = load_obj("cube.obj")
# Shuttle
#COORDSSCALE = 4
#STARTCOORD = minecraft.Vec3(-60,0,20)
#CLEARAREA1 = minecraft.Vec3(-30, 5, -30)
#CLEARAREA2 = minecraft.Vec3(-90, 30, 30)
#BLOCKTYPE = block.WOOL
#SWAPYZ = True
#vertices,textures,normals,faces = load_obj("shuttle.obj")
# Shyscraper
#COORDSSCALE = 1.4
#STARTCOORD = minecraft.Vec3(0,10,15)
#CLEARAREA1 = minecraft.Vec3(-30, -3, -15)
#CLEARAREA2 = minecraft.Vec3(30, 65, 35)
#BLOCKTYPE = block.IRON_BLOCK
#SWAPYZ = False
#vertices,textures,normals,faces = load_obj("skyscraper.obj")
# Head
#COORDSSCALE = 3
#STARTCOORD = minecraft.Vec3(0,-431,-60)
#CLEARAREA1 = minecraft.Vec3(-30, -30, -30)
#CLEARAREA2 = minecraft.Vec3(30, 65, -110)
#BLOCKTYPE = block.GOLD_BLOCK
#SWAPYZ = False
#vertices,textures,normals,faces = load_obj("head.obj")
# Cessna
#COORDSSCALE = 2
#STARTCOORD = minecraft.Vec3(-75, 25, -60)
#CLEARAREA1 = minecraft.Vec3(-30, 15, -30)
#CLEARAREA2 = minecraft.Vec3(-100, 65, -90)
#BLOCKTYPE = block.WOOD_PLANKS
#SWAPYZ = False
#vertices,textures,normals,faces = load_obj("cessna.obj")
# New York
COORDSSCALE = 0.1
STARTCOORD = minecraft.Vec3(-185, 0, 135)
CLEARAREA1 = minecraft.Vec3(-130, 0, -130)
CLEARAREA2 = minecraft.Vec3(130, 65, 130)
BLOCKTYPE = block.IRON_BLOCK
SWAPYZ = False
vertices,textures,normals,faces = load_obj("NY_LIL.obj")
print "obj file loaded"
# clear a suitably large area
mc.setBlocks(CLEARAREA1.x, CLEARAREA1.y, CLEARAREA1.z, CLEARAREA2.x, CLEARAREA2.y, CLEARAREA2.z, block.AIR)
time.sleep(10)
# loop through faces
for face in faces:
#persist the first vertex of the face
firstVertex = face[0]
#get the x,y,z co-ords of the first vertex
firstVertexX, firstVertexY, firstVertexZ = getVertexXYZ(vertices[firstVertex[0]], COORDSSCALE, STARTCOORD, SWAPYZ)
#last vertex is current none
lastVertex = None
# loop through vertex's in face and draw lines between them
for vertex in face:
vertexX, vertexY, vertexZ = getVertexXYZ(vertices[vertex[0]], COORDSSCALE, STARTCOORD, SWAPYZ)
if lastVertex != None:
# got 2 vertices, draw a line between them
line3d(mc, lastVertexX, lastVertexY, lastVertexZ, vertexX, vertexY, vertexZ, BLOCKTYPE)
#persist the last vertex found
lastVertex = vertex
lastVertexX, lastVertexY, lastVertexZ = vertexX, vertexY, vertexZ
# draw a line between the last and first vertex's
line3d(mc, lastVertexX, lastVertexY, lastVertexZ, firstVertexX, firstVertexY, firstVertexZ, BLOCKTYPE)
mc.postToChat("Model complete, www.stuffaboutcode.com")
print datetime.datetime.now()