-
Notifications
You must be signed in to change notification settings - Fork 1
/
previewGlasses.py
285 lines (232 loc) · 9.53 KB
/
previewGlasses.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
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
from direct.showbase.ShowBase import ShowBase
from pathlib import Path
from direct.actor.Actor import Actor
from tkinter.filedialog import askopenfilename
from panda3d.core import Filename, OrthographicLens, GraphicsOutput, WindowProperties, Texture, GraphicsPipe, \
FrameBufferProperties
from direct.gui.DirectGui import *
import sys, os
# We need to import the tkinter library to
# disable the tk window that pops up.
# We use tk for the file path selector.
import tkinter as tk
root = tk.Tk()
root.withdraw()
# Force high quality for our render
from panda3d.core import loadPrcFileData
loadPrcFileData('', 'default-antialias-enable 1')
loadPrcFileData('', 'framebuffer-multisample 1')
loadPrcFileData('', 'win-size 1920 1080')
loadPrcFileData('', 'model-path $RESOURCE_DIR')
"""
Controls:
s = Take screenshot
o = toggle oobe/free camera
r = reload loaded textures
e = reset rotation
b = toggle backface culling
k = move glasses up
l (L) = move glasses down
mouse wheel up = zoom in
mouse wheel down = zoom out
mouse3(middle mouse click) = reset zoom
left arrow = rotate negative heading
right arrow = rotate positive heading
up arrow = rotate positive pitch
down arrow = rotate negative pitch
"""
"""
Todo:
Add onscreen text that displays the offset (camers zoom, clothing rotation, etc.) <-- will be hidden in screenshots
"""
class previewGlasses(ShowBase):
def __init__(self):
ShowBase.__init__(self)
self.fileName = "output" # Output file name
self.fileFormat = ".png"
self.midPointX = 0
self.glassesModel = None
self.glassesTex = None
self.loadedTextures = [None]
self.glasses = None
base.cam.setPos(0, -7, 0.25)
self.defaultCamPos = base.cam.getPos()
base.camera.hide()
self.i = 1
self.defaultH = 180 # todo: hotkey to reset all transformations
self.currentH = self.defaultH
self.defaultP = 0
self.currentP = self.defaultP
self.defaultY = -7
self.currentY = self.defaultY
self.defaultZ = 0.25 # note: negative goes up, positive goes down
self.currentZ = self.defaultZ
# Camera
# 16 : 9 aspect ratio default
scaleMultiplier = 0.25
self.filmSizeX_BASE = 16 * scaleMultiplier
self.filmSizeY_BASE = 9 * scaleMultiplier
self.filmSizeX = 16
self.filmSizeY = 9
self.orthoLens = OrthographicLens()
self.orthoLens.setFilmSize(self.filmSizeX, self.filmSizeY)
self.isOrthoView = False
self.defaultLens = base.cam.node().getLens()
# Just in case we have these enabled in the config...
base.setFrameRateMeter(False)
base.setSceneGraphAnalyzerMeter(False)
base.disableMouse()
self.loadGlasses()
self.loadGUI()
"""
If you want to change the default outfit texture (not desat), you can either
change the texture path of the egg model(s) itself, or, alternatively, you can
directly call to load specific textures, e.g.:
self.loadGlassesModel("path/to/texture.png")
"""
self.accept('s', self.aspect2d.hide) # Hacky b/c hiding and showing in same method no work
self.accept('s-up', self.saveScreenshot)
self.accept('o', base.oobe)
self.accept('b', render.setTwoSided, [True])
self.accept('r', self.reloadTextures)
self.accept('e', self.defaultRotation)
self.accept('c', self.toggleOrthoView)
self.accept('q', sys.exit)
self.accept('wheel_up', self.zoomCamera, [0.1])
self.accept('wheel_down', self.zoomCamera, [-0.1])
self.accept('mouse2', self.defaultCam)
self.accept('arrow_left', self.rotateGlassesH, [-5])
self.accept('arrow_left-repeat', self.rotateGlassesH, [-5])
self.accept('arrow_right', self.rotateGlassesH, [5])
self.accept('arrow_right-repeat', self.rotateGlassesH, [5])
self.accept('arrow_up', self.rotateGlassesP, [5])
self.accept('arrow_up-repeat', self.rotateGlassesP, [5])
self.accept('arrow_down', self.rotateGlassesP, [-5])
self.accept('arrow_down-repeat', self.rotateGlassesP, [-5])
self.accept('k', self.translateGlassesZ, [-0.1])
self.accept('k-repeat', self.translateGlassesZ, [-0.1])
self.accept('l', self.translateGlassesZ, [0.1])
self.accept('l-repeat', self.translateGlassesZ, [0.1])
self.accept('p', print, ["H = {}, P = {}".format(self.defaultH, self.defaultP)])
# self.accept('b', self.leg.showTightBounds)
# most efficient color to use due to antialiasing.
base.setBackgroundColor(0, 0, 0, 0)
def loadGlasses(self, path=None):
self.clearGlasses()
if path is None:
self.glasses = loader.loadModel("assets/glasses.egg")
else:
self.glasses = loader.loadModel(path)
self.glasses.reparentTo(render)
def clearGlasses(self):
if self.glasses is not None:
self.glasses.removeNode()
self.glasses = None
def loadGUI(self):
# Todo: figure out how to reposition buttons when window changes size
# guiFrame = DirectFrame(frameColor=(0, 0, 0, 1),
# frameSize=(-1, 1, -1, 1),
# pos=(1, -1, -1))
self.modelButton = DirectButton(text = ("Change Glasses Model"),
scale = 0.05, pos = (-1.6, 0, -0.4), command = self.openModel)
self.texButton = DirectButton(text = ("Change Glasses Texture"),
scale = 0.05, pos = (-1.6, 0, -0.5), command = self.openTexture)
def saveScreenshot(self):
# intent: Image number would increment if the file already exists just so it doesn't overwrite
self.newfileName = self.fileName
if not (os.path.isfile(self.newfileName)): # wip
self.newfileName = self.fileName + str(self.i)
self.i += 1
filename = self.newfileName + self.fileFormat
base.win.saveScreenshot(Filename(filename))
self.aspect2d.show()
print("Screenshot saved! {}".format(filename))
def reloadTextures(self):
for tex in self.loadedTextures:
if tex:
tex.reload()
# Rotate clothing
def rotateGlassesH(self, value):
self.currentH = self.glasses.getH() + value
self.glasses.setH(self.currentH)
def rotateGlassesP(self, value):
self.currentP = self.glasses.getP() + value
self.glasses.setP(self.currentP)
def defaultRotation(self):
self.currentH = self.defaultH
self.glasses.setH(self.currentH)
self.currentP = self.defaultP
self.glasses.setP(self.currentP)
def translateGlassesZ(self, value):
self.currentZ = self.glasses.getZ() + value
self.glasses.setZ(self.currentZ)
# Camera Modifiers
def defaultCam(self):
base.cam.setPos(self.defaultCamPos)
self.orthoLens.setFilmSize(self.filmSizeX_BASE, self.filmSizeY_BASE)
def zoomCamera(self, value):
self.filmSizeX, self.filmSizeY = self.orthoLens.getFilmSize()
self.orthoLens.setFilmSize(self.filmSizeX + (value * self.filmSizeX_BASE / 2),
self.filmSizeY + (value * self.filmSizeY_BASE / 2))
base.cam.setPos(base.cam.getX(), base.cam.getY() + value, base.cam.getZ())
###
def browseForImage(self):
path = Path(askopenfilename(filetypes = (
("Image Files", "*.jpg;*.jpeg;*.png;*.psd;*.tga"),
("JPEG", "*.jpg;*.jpeg"),
("PNG", "*.png"),
("Photoshop File", "*.psd"),
("Targa", "*.tga"))))
return path
def browseForModel(self):
path = Path(askopenfilename(filetypes = (
("Panda3D Model Files", "*.egg;*.bam"),
("EGG", "*.egg"),
("BAM", "*.bam"))))
return path
def loadGlassesModel(self, file: str):
self.glassesModel = file
self.loadGlasses(self.glassesModel)
def loadGlassesTexture(self, file: str):
# File is an absolute path
# So let's be compatible with both JPG+RGB and PNG variants of textures.
fileExt = os.path.splitext(file)[1]
fileTex = os.path.splitext(file)[0]
if fileExt == '.png':
# No special calls needed
tex = loader.loadTexture(file)
else:
# todo: panda doesn't like loading _a.rgb like seen below
# For now let's just only check for an a_rgb if not PNG
tex = loader.loadTexture(file)
# if os.path.isfile(fileTex + '_a.rgb'):
# tex = loader.loadTexture(file, fileTex + '_a.rgb')
# else:
# tex = loader.loadTexture(file)
self.glasses.setTexture(tex, 1)
self.glassesTex = file
self.loadedTextures[0] = tex
def openModel(self):
filename = self.browseForModel()
if str(filename) == ".":
return
try:
self.loadGlassesModel(filename)
except:
print(str(filename) + " could not be loaded!")
def openTexture(self):
filename = self.browseForImage()
if str(filename) == ".":
return
try:
self.loadGlassesTexture(filename)
except:
print(str(filename) + " could not be loaded!")
def toggleOrthoView(self):
self.isOrthoView = not self.isOrthoView
if self.isOrthoView:
base.cam.node().setLens(self.orthoLens)
else:
base.cam.node().setLens(self.defaultLens)
app = previewGlasses()
app.run()