-
Notifications
You must be signed in to change notification settings - Fork 2
/
wxMmdModelViewer.pyw
1533 lines (1302 loc) · 50.4 KB
/
wxMmdModelViewer.pyw
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
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
#!/usr/bin/env python
# -*- coding: utf-8 -*-
###############################################################################
# The MIT License (MIT)
#
# Copyright (c) <year> <copyright holders>
#
# Permission is hereby granted, free of charge, to any person obtaining a copy
# of this software and associated documentation files (the "Software"), to deal
# in the Software without restriction, including without limitation the rights
# to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
# copies of the Software, and to permit persons to whom the Software is
# furnished to do so, subject to the following conditions:
#
# The above copyright notice and this permission notice shall be included in
# all copies or substantial portions of the Software.
#
# THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
# IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
# FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
# AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
# LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
# OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
# THE SOFTWARE.
###############################################################################
from __future__ import unicode_literals
from __future__ import division
import os
import sys
import time
import random
import StringIO
import codecs
import json
import locale
rcdomain = 'wxViewMMD'
localedir = os.path.realpath('i18n')
os.environ['LANG'] = locale.getdefaultlocale()[0]
import gettext
_ = gettext.gettext
import wx
import wx.aui
import wx.lib.platebtn as platebtn
# if wx.GetApp().GetComCtl32Version() >= 600 and wx.DisplayDepth() >= 32:
# # Use the 32-bit images
# wx.SystemOptions.SetOption("msw.remap", 2)
WIN_SIZE = (800, 800)
from pandac.PandaModules import *
# need to be before the Direct Start Import
loadPrcFileData('startup', 'window-type none')
loadPrcFileData('', 'window-title MMD PMX/PMX Model Viewer')
loadPrcFileData('', 'icon-filename mmdviewer.png')
loadPrcFileData('', 'win-size %d %d' % WIN_SIZE)
loadPrcFileData('', 'window-type none')
loadPrcFileData('', 'text-encoding utf8')
loadPrcFileData('', 'textures-power-2 none')
loadPrcFileData('', 'geom-cache-size 10')
loadPrcFileData('', 'coordinate-system zup-right')
# loadPrcFileData('', 'coordinate-system yup-right')
loadPrcFileData('', 'clock-mode limited')
loadPrcFileData('', 'clock-frame-rate 60')
loadPrcFileData('', 'show-frame-rate-meter 1')
loadPrcFileData('', 'egg-emulate-bface 1')
loadPrcFileData('', 'graphics-memory-limit 67108864')
# loadPrcFileData('', 'texture-scale 0.5')
# loadPrcFileData('', 'compressed-textures 1')
loadPrcFileData('', 'model-cache-compressed-textures true')
# loadPrcFileData('', 'notify-level warning')
# loadPrcFileData('', 'default-directnotify-level warning')
# loadPrcFileData('', 'want-pstats 1')
from panda3d.core import ConfigVariableString
from panda3d.core import Shader
from panda3d.core import Filename
from panda3d.core import Material
from panda3d.core import VBase4
from panda3d.ode import *
from panda3d.bullet import ZUp
from panda3d.bullet import BulletWorld
from panda3d.bullet import BulletDebugNode
from panda3d.bullet import BulletPlaneShape
from panda3d.bullet import BulletRigidBodyNode
from panda3d.bullet import BulletSoftBodyNode
from panda3d.bullet import BulletPlaneShape
from panda3d.bullet import BulletBoxShape
from panda3d.bullet import BulletSphereShape
from panda3d.bullet import BulletCylinderShape
from panda3d.bullet import BulletCapsuleShape
from panda3d.bullet import BulletConeShape
from panda3d.bullet import BulletCharacterControllerNode
from direct.showbase.ShowBase import ShowBase
from direct.showbase.ShowBase import WindowControls
from direct.showbase.DirectObject import DirectObject
# from direct.directtools.DirectGrid import DirectGrid
from direct.directtools.DirectCameraControl import *
from direct.directtools.DirectSelection import *
from direct.directtools.DirectGeometry import *
from direct.directtools.DirectGlobals import *
# from direct.gui.DirectGui import *
from direct.actor.Actor import Actor
from direct.filter.CommonFilters import CommonFilters
from direct.interval.IntervalGlobal import *
from direct.task import Task
from direct.wxwidgets.ViewPort import *
from utils.DrawPlane import *
from utils.common import *
from utils.pmx import *
from utils.pmd import *
from main_ui import *
# Get the location of the 'py' file I'm running:
CWD = os.path.abspath(sys.path[0])
SHOW_LIGHT_POS = True
SHOW_LIGHT_POS = False
SHOW_SHADOW = True
SHOW_SHADOW = False
SHOW_AXIS = True
# SHOW_AXIS = False
lastModel = None
ID_GRIDPLANE = 20000
ID_RECENTFILES = 21000
ID_EXPRESSION = 30000
CurrentApp = None
class TestFrame(wx.Frame):
def __init__(self, *args, **kwargs):
wx.Frame.__init__(self, *args, **kwargs)
self.auiManager = wx.aui.AuiManager(self)
# self.panda = Viewport.makeFront(self)
# self.panda = Viewport.makePerspective(self)
self.panda = Viewport.make(self, vpType=CREATENEW)
pandaInfo = wx.aui.AuiPaneInfo().Name("panda3d")
pandaInfo.Center().CloseButton(False).MaximizeButton(True).CaptionVisible(False)
self.auiManager.AddPane(self.panda, pandaInfo)
self.auiManager.Update()
def onQuit(self, evt):
self.auiManager.UnInit()
self.Close()
pass
class Frame(MainForm):
def __init__(self, *args, **kwargs):
MainForm.__init__(self, *args, **kwargs)
self.auiManager = wx.aui.AuiManager(self)
# self.panda = Viewport.makeFront(self)
# self.panda = Viewport.makePerspective(self)
self.panda = Viewport('panda3dviewport', self)
pandaInfo = wx.aui.AuiPaneInfo().Name("panda3d")
pandaInfo.Center().CloseButton(False).MaximizeButton(True).CaptionVisible(False)
self.auiManager.AddPane(self.panda, pandaInfo)
self.auiManager.Update()
def onQuit(self, evt):
self.auiManager.UnInit()
self.Close()
pass
class Stage(object):
lights = None
@staticmethod
def setAxis(render, update=False):
def CreateAxis(axisStage):
grid = ThreeAxisGrid(gridstep=10, subdiv=10, xy=True, yz=False, xz=False, z=False)
gridnodepath = grid.create()
grid.showPlane(XY=True)
grid.showAxis(Z=False)
gridnodepath.writeBamFile(axisStage)
return(gridnodepath)
axisStage = u'./stages/default_axis.bam'
if not update:
try:
gridnodepath = loader.loadModel(axisStage)
gridnodepath = gridnodepath.getChild(0)
except:
gridnodepath = CreateAxis(axisStage)
else:
gridnodepath = CreateAxis(axisStage)
gridnodepath.reparentTo(render)
pass
@staticmethod
def setStudioLight(render):
lightsStage = u'./stages/default_lights.bam'
# lightsStage = ''
if os.path.isfile(os.path.abspath(lightsStage)) and (not SHOW_LIGHT_POS):
# try:
lights = loader.loadModel(lightsStage)
lights = lights.getChild(0)
# except:
else:
print('--> Gen Lights...')
lights = NodePath(PandaNode('StageLights'))
alight = AmbientLight('alight')
alight.setColor(VBase4(1, 1, 1, 0.9))
alnp = render.attachNewNode(alight)
alnp.reparentTo(lights)
dlight_key = PointLight('key dlight')
# dlight_key = DirectionalLight('key dlight')
dlight_key.setAttenuation( Vec3( 0., 0., 0.0003 ) )
dlnp_key = render.attachNewNode(dlight_key)
dlnp_key.setX(-40)
dlnp_key.setY(-50)
dlnp_key.setZ(40)
if SHOW_LIGHT_POS:
dlnp_key.node().showFrustum()
dlnp_key.reparentTo(lights)
dlight_fill = PointLight('fill dlight')
# dlight_fill = DirectionalLight('fill dlight')
dlight_fill.setAttenuation( Vec3( 0., 0., 0.0006 ) )
dlnp_fill = render.attachNewNode(dlight_fill)
dlnp_fill.setX(40)
dlnp_fill.setY(-50)
dlnp_fill.setZ(50)
if SHOW_LIGHT_POS:
dlnp_fill.node().showFrustum()
dlnp_fill.reparentTo(lights)
dlight_back = PointLight('back dlight')
# dlight_outline = DirectionalLight('back dlight')
dlight_back.setAttenuation( Vec3( 0., 0., 0.0006 ) )
dlnp_back = render.attachNewNode(dlight_back)
dlnp_back.setX(40)
dlnp_back.setY(50)
dlnp_back.setZ(50)
if SHOW_LIGHT_POS:
dlnp_back.node().showFrustum()
dlnp_back.reparentTo(lights)
if SHOW_SHADOW:
# lights.setShaderAuto()
# lights.setShadowCaster(True, 512, 512)
for lnp in lights.getChildren():
light = lnp.node()
lnp.setShaderAuto()
if not light.isAmbientLight():
light.setShadowCaster(True, 512, 512)
if not os.path.exists(os.path.abspath(lightsStage)):
lights.writeBamFile(lightsStage)
if SHOW_LIGHT_POS:
lights.reparentTo(render)
Stage.lights = lights
return(lights)
pass
@staticmethod
def lightAtNode(node=None, lights=None, on=True):
if not lights:
return
if not node:
for light in lights.getChildren():
# if light.node().isAmbientLight():
# render.setLight(light)
# continue
if on:
render.setLight(light)
else:
render.clearLight(light)
light.setPythonTag('On', on)
lights.setPythonTag('On', on)
elif isinstance(node, NodePathCollection):
for np in node:
for light in lights.getChildren():
# if light.node().isAmbientLight():
# np.setLight(light)
# continue
if on:
np.setLight(light)
else:
np.clearLight(light)
light.setPythonTag('On', on)
lights.setPythonTag('On', on)
elif isinstance(node, NodePath):
for light in lights.getChildren():
# if light.node().isAmbientLight():
# node.setLight(light)
# continue
try:
if on:
node.setLight(light)
else:
node.clearLight(light)
light.setPythonTag('On', on)
except:
continue
lights.setPythonTag('On', on)
if on:
print('--> lights on')
else:
print('--> lights off')
pass
@staticmethod
def getCamera():
pos = base.trackball.node().getPos()
hpr = base.trackball.node().getHpr()
return(pos, hpr)
@staticmethod
# def setCamera(x=0, y=0, z=0, h=0, p=0, r=0, fov=46.8, focal=50, oobe=False):
def setCamera(x=None, y=None, z=None, h=None, p=None, r=None, fov=46.8, focal=50, oobe=False):
base.camLens.setNearFar(0.1, 11000.0)
base.camLens.setFov(fov)
base.camLens.setFocalLength(focal)
old_pos = base.trackball.node().getPos()
old_hpr = base.trackball.node().getHpr()
if x is None: x = old_pos.x
if y is None: y = old_pos.y
if z is None: z = old_pos.z
if h is None: h = old_hpr.x
if p is None: p = old_hpr.y
if r is None: r = old_hpr.z
base.trackball.node().setPos(x, y, z)
base.trackball.node().setHpr(h, p, r)
# base.useDrive()
if oobe:
# base.oobe()
base.oobeCull()
pass
@staticmethod
def resetCamera(model=None):
WIN_SIZE = (500, 500)
node_fov = 50
if model:
lens = base.camLens
fov_old = render.getPythonTag('lensFov')
fov_new = lens.getFov()
minFov_old = min(fov_old.getX(), fov_old.getY())
maxFov_old = max(fov_old.getX(), fov_old.getY())
minFov_new = min(fov_new.getX(), fov_new.getY())
maxFov_new = max(fov_new.getX(), fov_new.getY())
scaleFOV = minFov_new / maxFov_old
body_min_point = LPoint3f()
body_max_point = LPoint3f()
body = model.find('**/Body')
if body:
body.calcTightBounds(body_min_point, body_max_point)
else:
model.calcTightBounds(body_min_point, body_max_point)
body_size = LPoint3f(body_max_point.x-body_min_point.x, body_max_point.y-body_min_point.y, body_max_point.z-body_min_point.z)
model_min_point = LPoint3f()
model_max_point = LPoint3f()
model.calcTightBounds(model_min_point, model_max_point)
model_size = LPoint3f(model_max_point.x-model_min_point.x, model_max_point.y-model_min_point.y, model_max_point.z-model_min_point.z)
# print(model_size.z, body_size.z)
# if model_size.z - body_size.z > 10:
if model_size.z == 0: model_size.z = 0.00001
if body_size.z == 0: body_size.z = 0.00001
if abs(model_size.z / body_size.z) < 1.5:
node_size = model_size
min_point = model_min_point
max_point = model_max_point
else:
node_size = body_size
min_point = body_min_point
max_point = body_max_point
camPosX = 0
camPosY = 1.5 * max(node_size.z, node_size.x) / scaleFOV + ((min_point.x + max_point.x)/2)
# camPosY = 7/5*node_size.z/(scaleFOV*scaleModel)
camPosZ = 0.5*node_size.z + min_point.z
else:
camPosX = 0
camPosY = 68
camPosZ = 20
Stage.setCamera(x=camPosX, y=camPosY, z=-camPosZ, h=0, p=0, r=0, oobe=False)
pass
pass
class Utils(object):
@staticmethod
def loadMmdModel(modelname):
p3dnode = None
world = render.getPythonTag('World')
engine = render.getPythonTag('Engine')
lastModel = render.getPythonTag('lastModel')
if lastModel:
node = lastModel.find('**/+ModelRoot')
if node:
loader.unloadModel(node)
else:
loader.unloadModel(lastModel)
lastModel.removeNode()
if world:
for rigid in world.getRigidBodies():
world.remove(rigid)
for cs in world.getConstraints():
world.remove(cs)
render.setPythonTag('lastModel', None)
try:
modelname = os.path.relpath(modelname, CWD)
except:
pass
ext = os.path.splitext(modelname)[1].lower()
if os.path.altsep:
modelname = modelname.replace(os.path.sep, os.path.altsep)
# modelname = modelname.replace('\\', os.path.altsep)
log('Loading Model:"%s" data...' % os.path.basename(modelname), force=True)
if ext in ['.pmx']:
p3dnode = loadPmxModel(modelname, world, engine)
elif ext in ['.pmd']:
p3dnode = loadPmdModel(modelname)
else:
p3dnode = loader.loadModel(Filename.fromOsSpecific(modelname))
p3dnode.setPythonTag('name', modelname)
if p3dnode:
p3dnode.reparentTo(render)
render.setPythonTag('lastModel', p3dnode)
app.SetTitle(p3dnode.getPythonTag('name'))
Stage.lightAtNode(p3dnode, Stage.lights)
Stage.resetCamera(model=p3dnode)
app.addExpressionMenu(Utils.getExpressionList(p3dnode))
app.updateConfig(modelname)
physicsBody = p3dnode.find('**/Physics')
if physicsBody:
if render.getPythonTag('Engine').lower() == 'bullet':
for np in physicsBody.getChildren():
node = np.node()
if isinstance(node, BulletRigidBodyNode):
# app.worldNP.attachNewNode(node)
app.debugNP.attachNewNode(node)
app.world.attachRigidBody(node)
for cs in physicsBody.getPythonTag('Joints'):
app.world.attachConstraint(cs)
return(p3dnode)
@staticmethod
def processVertexData(vdata_src, vdata_dst, morphOn=True, strength=1.0):
vertex_src = GeomVertexWriter(vdata_src, 'vertex')
vertex_dst = GeomVertexReader(vdata_dst, 'vertex')
vindex_dst = GeomVertexReader(vdata_dst, 'vindex')
# vmorph_dst = GeomVertexReader(vdata_dst, 'v.morph')
vmorph_dst = GeomVertexReader(vdata_dst, 'vmorph')
# vertex_src.setColumn('vertex')
while not vertex_dst.isAtEnd():
i_dst = vindex_dst.getData1i()
v_dst = vertex_dst.getData3f()
m_dst = vmorph_dst.getData3f()
vertex_src.setRow(i_dst)
if morphOn:
vertex_src.setData3f(v_dst.getX()+m_dst.getX()*strength, v_dst.getY()+m_dst.getY()*strength, v_dst.getZ()+m_dst.getZ()*strength)
else:
vertex_src.setData3f(v_dst.getX(), v_dst.getY(), v_dst.getZ())
# print(vertex_src.getWriteRow())
del vertex_src, vertex_dst, vindex_dst, vmorph_dst
@staticmethod
def processGeomNode(geomNode, morphNode, morphOn=True, strength=1.0):
geom = geomNode.modifyGeom(0)
morphgeom = morphNode.getGeom(0)
vdata_src = geom.modifyVertexData()
vdata_dst = morphgeom.getVertexData()
result = Utils.processVertexData(vdata_src, vdata_dst, morphOn, strength)
del vdata_src, vdata_dst, geom, morphgeom
return(result)
@staticmethod
def morphVertex(nodepath, morphnodepath, morphOn=True, strength=1.0):
morphNode = morphnodepath.node()
geomNodeCollection = nodepath.findAllMatches('**/Body/+GeomNode')
idx = 0
for nodePath in geomNodeCollection:
log(u'%s' % nodePath.getName())
geomNode = nodePath.node()
Utils.processGeomNode(geomNode, morphNode, morphOn, strength)
# idx += 1
del geomNode
del morphNode, geomNodeCollection
pass
@staticmethod
def morphMaterial(nodepath, morphData, morphOn=True, strength=1.0):
idx = 0
print(u'-> Morph Material Count : %d' % len(morphData))
for data in morphData:
material_index = data.getPythonTag('materialIndex')
calc_mode = data.getPythonTag('calcMode')
edge_color = data.getPythonTag('edge_color')
edge_size = data.getPythonTag('edge_size')
texture_factor = data.getPythonTag('texture_factor')
sphere_texture_factor = data.getPythonTag('sphere_texture_factor')
toon_texture_factor = data.getPythonTag('toon_texture_factor')
mat = data.getMaterial()
print(u'--> Target Material Count : %d' % len(morphData))
print(u'--> Target Material Index : %d' % material_index)
nps = nodepath.findAllMatches('**/Body/*')
for np in nps:
log(u'---> %s, %d, %s' % (np.getName(), np.getPythonTag('material_index'), str(morphOn)), force=True)
matIndex = np.getPythonTag('material_index')
if material_index < 0:
pass
elif matIndex != material_index:
continue
tex_name = u'%s_morph_%04d' % (np.getName(), idx)
ts_morph = TextureStage(tex_name)
tsSphereName = u'%s_sphere' % np.getName()
tsSphere = np.findTextureStage(tsSphereName)
tsToonName = u'%s_toon' % np.getName()
tsToon = np.findTextureStage(tsToonName)
if morphOn:
np.setMaterial(mat, 1)
if calc_mode == 0: # *
np.setColorScale(texture_factor.r, texture_factor.g, texture_factor.b, texture_factor.a, matIndex)
elif calc_mode == 1: # +
np.setColorScale(texture_factor.r, texture_factor.g, texture_factor.b, texture_factor.a, matIndex)
else:
np.setColorScaleOff(matIndex)
np.clearMaterial()
np.setMaterial(np.getPythonTag('material'), 1)
if tsSphere:
if morphOn:
np.setPythonTag('sphere_combine_mode', (
tsSphere.getCombineRgbMode(),
tsSphere.getCombineRgbSource0(),
tsSphere.getCombineRgbOperand0(),
tsSphere.getCombineRgbSource1(),
tsSphere.getCombineRgbOperand1(),
tsSphere.getCombineRgbSource2(),
tsSphere.getCombineRgbOperand2(),
tsSphere.getCombineAlphaMode(),
tsSphere.getCombineAlphaSource0(),
tsSphere.getCombineAlphaOperand0(),
tsSphere.getCombineAlphaSource1(),
tsSphere.getCombineAlphaOperand1(),
tsSphere.getCombineAlphaSource2(),
tsSphere.getCombineAlphaOperand2(),
tsSphere.getMode()
))
if calc_mode == 0: # *
color = VBase4(sphere_texture_factor.r*strength/10, sphere_texture_factor.g*strength/10, sphere_texture_factor.b*strength/10, sphere_texture_factor.a*strength/10)
tsSphere.setColor(color)
tsSphere.setCombineRgb(TextureStage.CMModulate, TextureStage.CSPrevious, TextureStage.COSrcColor, TextureStage.CSTexture, TextureStage.COSrcColor)
tsSphere.setCombineAlpha(TextureStage.CMModulate, TextureStage.CSPrevious, TextureStage.COSrcAlpha, TextureStage.CSTexture, TextureStage.COSrcAlpha)
elif calc_mode == 1: # +
color = VBase4(-sphere_texture_factor.r*strength, -sphere_texture_factor.g*strength, -sphere_texture_factor.b*strength, -sphere_texture_factor.a*strength)
tsSphere.setColor(color)
tsSphere.setCombineRgb(TextureStage.CMSubtract, TextureStage.CSTexture, TextureStage.COSrcColor, TextureStage.CSConstant, TextureStage.COSrcColor)
tsSphere.setCombineAlpha(TextureStage.CMSubtract, TextureStage.CSTexture, TextureStage.COSrcAlpha, TextureStage.CSConstant, TextureStage.COSrcAlpha)
else:
cMode = np.getPythonTag('sphere_combine_mode')
# tsSphere.setCombineRgb(cMode[0], cMode[1], cMode[2], cMode[3], cMode[4], cMode[5], cMode[6])
# tsSphere.setCombineAlpha(cMode[7], cMode[8], cMode[9], cMode[10], cMode[11], cMode[12], cMode[13])
tsSphere.setMode(cMode[14])
pass
pass
if tsToon:
if morphOn:
np.setPythonTag('toon_combine_mode', (
tsToon.getCombineRgbMode(),
tsToon.getCombineRgbSource0(),
tsToon.getCombineRgbOperand0(),
tsToon.getCombineRgbSource1(),
tsToon.getCombineRgbOperand1(),
tsToon.getCombineRgbSource2(),
tsToon.getCombineRgbOperand2(),
tsToon.getCombineAlphaMode(),
tsToon.getCombineAlphaSource0(),
tsToon.getCombineAlphaOperand0(),
tsToon.getCombineAlphaSource1(),
tsToon.getCombineAlphaOperand1(),
tsToon.getCombineAlphaSource2(),
tsToon.getCombineAlphaOperand2(),
tsToon.getMode()
))
if calc_mode == 0: # *
color = VBase4(toon_texture_factor.r*strength/5, toon_texture_factor.g*strength/5, toon_texture_factor.b*strength/5, toon_texture_factor.a*strength/5)
tsToon.setColor(color)
tsToon.setCombineRgb(TextureStage.CMModulate, TextureStage.CSTexture, TextureStage.COSrcColor, TextureStage.CSPrevious, TextureStage.COSrcColor)
tsToon.setCombineAlpha(TextureStage.CMModulate, TextureStage.CSTexture, TextureStage.COSrcAlpha, TextureStage.CSPrevious, TextureStage.COSrcAlpha)
elif calc_mode == 1: # +
color = VBase4(toon_texture_factor.r*strength/5, toon_texture_factor.g*strength/5, toon_texture_factor.b*strength/5, toon_texture_factor.a*strength/5)
tsToon.setColor(color)
print(color)
tsToon.setCombineRgb(TextureStage.CMSubtract, TextureStage.CSPrevious, TextureStage.COSrcColor, TextureStage.CSConstant, TextureStage.COSrcColor)
tsToon.setCombineAlpha(TextureStage.CMSubtract, TextureStage.CSPrevious, TextureStage.COSrcAlpha, TextureStage.CSConstant, TextureStage.COSrcAlpha)
else:
cMode = np.getPythonTag('toon_combine_mode')
print(cMode)
# tsToon.setCombineRgb(cMode[0], cMode[1], cMode[2], cMode[3], cMode[4], cMode[5], cMode[6])
# tsToon.setCombineAlpha(cMode[7], cMode[8], cMode[9], cMode[10], cMode[11], cMode[12], cMode[13])
tsToon.setMode(cMode[14])
pass
pass
idx += 1
pass
@staticmethod
def setExpression(model, expression, morphOn=True, strength=1.0, default=True):
if strength < 0: strength = 0.0;
if strength > 1: strength = 1.0;
strength = 0.99
morph = model.find('**/Morphs*')
if len(expression)==0 or expression.lower()=='default':
for item in morph.getChildren():
if item.getPythonTag('show'):
Utils.morphVertex(model, item, morphOn=False, strength=strength)
item.setPythonTag('show', False)
else:
for item in morph.getChildren():
if len(expression)==0 or expression.lower()=='default':
Utils.morphVertex(model, item, morphOn=False, strength=strength)
item.setPythonTag('show', False)
continue
if item.getName() == expression:
log(u'===================\n%s\n===================' % expression, force=True)
lastExpression = model.getPythonTag('lastExpression')
morphType = item.getPythonTag('morph_type')
morphPanel = item.getPythonTag('panel')
print('Morph Type : ', morphType)
# print('Morph Panel : ', morphPanel)
if morphType == 1:
if lastExpression and default:
Utils.morphVertex(model, lastExpression, morphOn=False, strength=strength)
lastExpression.setPythonTag('show', False)
state = False if item.getPythonTag('show') else True
Utils.morphVertex(model, item, morphOn=state, strength=strength)
item.setPythonTag('show', state)
model.setPythonTag('lastExpression', item)
# print(expression, morphOn)
elif morphType == 8:
state = False if item.getPythonTag('show') else True
morphData = item.getPythonTag('morph_data')
# Utils.morphMaterial(model, morphData, morphOn=state, strength=strength)
# item.setPythonTag('show', state)
item.setPythonTag('show', False)
else:
log('not vertex/material expression', force=True)
break
pass
pass
pass
@staticmethod
def getExpressionList(model):
expressions = []
if model:
morph = model.find('**/Morphs*')
if morph:
for item in morph.getChildren():
expression_name = item.getName()
if item.getPythonTag('show'):
expression_state = True
else:
expression_state = False
expressions.append({'name':expression_name, 'state':expression_state})
return(expressions)
class MyFileDropTarget(wx.FileDropTarget):
hostApp = None
def __init__(self, window, app):
wx.FileDropTarget.__init__(self)
self.window = window
self.hostApp = app
def OnDropFiles(self, x, y, filenames):
info = "%d file(s) dropped at (%d,%d):\n" % (len(filenames), x, y)
print(info)
for file in filenames:
modelname = file
break
p3dnode = Utils.loadMmdModel(modelname)
pass
class MmdViewerApp(ShowBase):
wp = None
modelidx = 0
appConfig = dict()
title = None
def __init__(self):
self.loadConfig()
self.base = ShowBase.__init__(self, fStartDirect=False, windowType=None)
self.startWx()
# self.wxApp.Bind(wx.EVT_QUERY_END_SESSION, self.OnCloseWindow)
# self.wxApp.Bind(wx.EVT_CLOSE, self.OnCloseWindow)
self.wxApp.Bind(wx.EVT_CLOSE, self.OnClose)
self.wxApp.Bind(wx.EVT_SIZE, self.OnResize)
# self.frame = TestFrame(None)
self.frame = Frame(None)
outx, outy = self.frame.GetSize() - self.frame.GetClientSize()
diffx, diffy = self.frame.GetClientSize() - wx.Size(500, 500)
self.frame.SetSize(self.frame.GetSize() - wx.Size(diffx, diffy))
self.wp = WindowProperties()
self.wp.setOrigin(0,0)
self.wp.setSize(self.frame.panda.GetSize()[0], self.frame.panda.GetSize()[1])
self.wp.setParentWindow(self.frame.panda.GetHandle())
base.openMainWindow(type='onscreen', props=self.wp)
self.setupUI(self.frame)
self.setupGL()
self.setupWorld()
# icon = wx.Icon('viewpmx.ico', wx.BITMAP_TYPE_ICO)
icon = wx.Icon('mmdviewer.png', wx.BITMAP_TYPE_ANY)
self.frame.SetIcon(icon)
self.title = self.frame.GetTitle()
# self.frame.SetTopWindow(self.frame)
self.frame.Show(True)
pass
def OnResize(self, event=None):
if self.wp:
w, h = self.frame.panda.GetSize()
self.wp.setSize(w, h)
base.win.requestProperties(self.wp)
base.messenger.send(base.win.getWindowEvent(), [base.win])
pass
def OnClose(self, event=None):
self.onDestroy(event)
try:
self.saveConfig()
base
except NameError:
self.saveConfig()
sys.exit()
base.userExit()
pass
def OnCloseWindow(self, event=None):
self.saveConfig()
self.frame.Close()
pass
def loadConfig(self):
fn = os.path.splitext(__file__)
fn = os.path.join(CWD, fn[0]+'.config')
print('--> Reading config from %s ...' % os.path.relpath(fn, CWD))
if os.path.isfile(fn):
with codecs.open(fn, 'r', encoding='utf8') as f:
try:
self.appConfig = json.load(f, encoding='utf8')
except:
pass
if not 'recent' in self.appConfig:
self.appConfig['recent'] = []
if not 'hana2eng' in self.appConfig:
self.appConfig['hana2eng'] = loadJ2ETable(u'和英変換.txt')
# print(self.appConfig['hana2eng'])
pass
def saveConfig(self):
fn = os.path.splitext(__file__)
fn = os.path.join(CWD, fn[0]+'.config')
print('--> Writing config to %s ...' % fn)
if os.path.altsep and 'lastModel' in self.appConfig:
self.appConfig['lastModel'] = self.appConfig['lastModel'].replace(os.path.sep, os.path.altsep)
self.appConfig['recent'] = self.appConfig['recent'][:20]
for idx in xrange(len(self.appConfig['recent'])):
if os.path.altsep:
self.appConfig['recent'][idx] = self.appConfig['recent'][idx].replace(os.path.sep, os.path.altsep)
with codecs.open(fn, 'w', encoding='utf8') as f:
json.dump(self.appConfig, f, indent=2, encoding='utf8', ensure_ascii=False)
pass
def setupUI(self, win):
# win.Bind(wx.EVT_QUERY_END_SESSION, self.OnCloseWindow)
# win.Bind(wx.EVT_END_SESSION, self.OnCloseWindow)
#
# Create Drag&Drop response
#
self.dt = MyFileDropTarget(win, self)
win.SetDropTarget(self.dt)
self.frame.toolbar.EnableTool(ID_SAVE, False)
#
# Create Axis Grid Plane View Menu
#
self.menuPlane = wx.Menu()
for idx in xrange(self.frame.menuView.GetMenuItemCount()):
item = self.frame.menuView.FindItemByPosition(idx)
text = item.GetText()
help = item.GetHelp()
if text=='':
self.menuPlane.AppendSeparator()
else:
mItem = self.menuPlane.AppendCheckItem(ID_GRIDPLANE+idx, text, help)
if item.IsCheckable():
mItem.Check(item.IsChecked())
win.Bind(wx.EVT_MENU, self.OnPlanePopupItemSelected, id=mItem.GetId())
win.Bind(wx.EVT_MENU, self.OnPlanePopupItemSelected, id=item.GetId())
self.btnPlane = self.frame.toolbar.AddLabelTool( ID_GRIDPLANE, _(u"Plane"), wx.Bitmap( u"icons/gridplane.png", wx.BITMAP_TYPE_ANY ), wx.NullBitmap, wx.ITEM_DROPDOWN, _('Axis Plane Grid'), wx.EmptyString, None )
self.frame.toolbar.SetDropdownMenu(ID_GRIDPLANE, self.menuPlane)
self.frame.toolbar.AddSeparator()
#
# Init the expressions dropdown button
#
self.menuExpression = wx.Menu()
self.menuExpression.Append(ID_EXPRESSION, _('None'), '')
self.btnExpression = self.frame.toolbar.AddLabelTool(ID_EXPRESSION, _(u"Expression"), wx.Bitmap( u"icons/expression.png", wx.BITMAP_TYPE_ANY ), wx.NullBitmap, wx.ITEM_DROPDOWN, _('Expression List'), wx.EmptyString, None )
self.frame.toolbar.SetDropdownMenu(ID_EXPRESSION, self.menuExpression)
#
# Create 'Recent Files' menu
#
self.btnRecentFiles = self.frame.toolbar.InsertLabelTool(0, ID_RECENTFILES, _(u"Recent Files"), wx.Bitmap( u"icons/open.png", wx.BITMAP_TYPE_ANY ), wx.NullBitmap, wx.ITEM_DROPDOWN, _('Open File \nRecent Files List'), wx.EmptyString, None )
self.menuRecentFiles = wx.Menu()
idx = 1
for recent in self.appConfig['recent']:
text = os.path.basename(recent)
help = recent
mItem = self.menuRecentFiles.Append(ID_RECENTFILES+idx, text, help)
mruItem = self.frame.menuMRU.Append(ID_RECENTFILES+idx, text, help)
win.Bind(wx.EVT_MENU, self.OnRecentFilesItemSelected, id=mItem.GetId())
win.Bind(wx.EVT_MENU, self.OnRecentFilesItemSelected, id=mruItem.GetId())
idx += 1
win.Bind(wx.EVT_TOOL, self.OnRecentFilesItemSelected, id=ID_RECENTFILES)
self.frame.toolbar.SetDropdownMenu(ID_RECENTFILES, self.menuRecentFiles)
#
# Realize the toolbar to display right
#
self.frame.toolbar.Realize()
#
# Bind other events
#
win.Bind(wx.EVT_MENU_OPEN, self.OnMenuPopup)
win.Bind(wx.EVT_TOOL, self.OnResetCamera, id=ID_CAMERARESET)
win.Bind(wx.EVT_MENU, self.OnResetCamera, id=win.menuResetCamera.GetId())
# for test load model only.
win.Bind(wx.EVT_TOOL, self.OnOpenFileTest, id=ID_OPEN)
win.Bind(wx.EVT_MENU, self.OnOpenFile, id=win.menuOpen.GetId())
win.Bind(wx.EVT_MENU, self.OnCloseWindow, id=win.menuExit.GetId())
win.Bind(wx.EVT_TOOL, self.OnSnapshot, id=ID_SNAPSHOT)
win.Bind(wx.EVT_MENU, self.OnSnapshot, id=win.menuSnapshot.GetId())
pass
def setupGL(self):
FPS = 60
self.globalClock = ClockObject.getGlobalClock()
self.globalClock.setMode(ClockObject.MLimited)
self.globalClock.setFrameRate(FPS)
globalClock.setFrameTime(globalClock.getRealTime())
base.setSleep(.01)
# base.setFrameRateMeter(True)
base.camLens.setNearFar(0.1, 550.0)
base.camLens.setFov(45.0)
base.camLens.setFocalLength(50)
Stage.setAxis(render)
self.lights = Stage.setStudioLight(render)
# Stage.lightAtNode(lights=Stage.lights)
Stage.resetCamera()
render.setAntialias(AntialiasAttrib.MAuto)
fov = base.camLens.getFov()
render.setPythonTag('lensFov', LVecBase2f(fov.getX(), fov.getY()))
#
# setup object picker
#
# Create a traverser that Panda3D will automatically use every frame.
base.cTrav = CollisionTraverser()
# base.cTrav.showCollisions(render)
# Create a handler for the events.
self.collHandler = CollisionHandlerQueue()
self.pickerNode = CollisionNode('mouseRay')
self.pickerNP = camera.attachNewNode(self.pickerNode)
self.pickerNode.setFromCollideMask(GeomNode.getDefaultCollideMask())
self.pickerRay = CollisionRay()
self.pickerNode.addSolid(self.pickerRay)
self.cTrav.addCollider(self.pickerNP, self.collHandler)
self.lastPickedObj = None
#
# input accept
self.do = DirectObject()
self.do.accept('f1', self.toggleDebug)
self.do.accept('f2', self.toggleModel)
self.do.accept('f3', base.toggleWireframe)
self.do.accept('f4', base.toggleTexture)
self.do.accept('f5', self.toggleBone)
self.do.accept('l', self.toggleLight)
self.do.accept('t', self.testFunc)
self.do.accept('wheel_up', self.OnMouseWheelUp)
self.do.accept('wheel_down', self.OnMouseWheelDown)
def setupWorld(self, engine='bullet'):
self.engine = engine
if engine.lower() == 'ode':
# World
self.worldNP = render.attachNewNode('World')
# Setup our physics world