-
Notifications
You must be signed in to change notification settings - Fork 0
/
osimViewport.py
89 lines (75 loc) · 3.52 KB
/
osimViewport.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
'''
---------------------------------------------------------------------------
opensim-viewer-backend: osimViewport.py
---------------------------------------------------------------------------
Copyright 2024 Stanford University and the Authors
Author(s): Ayman Habib
Licensed under the Apache License, Version 2.0 (the "License"); you may not
use this file except in compliance with the License. You may obtain a copy
of the License at http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
'''
from pathlib import Path
import osimConverters as osimC
import opensim as osim
import osimConverters.openSimData2Gltf
import osimViewerOptions
'''
osimViewort class is a utility that embodies the creation and manipulation of a view
that will be displayed in a jupyter notebook. the parameters passed in will be used to shape/size and otherwise
customize the viewport in a manner similar to how matplotlib allows users to customize plots at a high level.
One benefit of this approach is that the internals of how the window/canvas is created, are all kept internal to
this class and so can be refactored. For now the customizations will go into one of two places:
- Scene customization: lights, floors, ...
- UI customization: size, labels, ...
As of now this supports a single model, however, there's nothing preventing having multiple opensim models
in the same gltf file.
'''
class osimViewport:
def __init__(self, width=600, hight=300):
self.width = width
self.height = hight
self._label = ""
self._options = osimViewerOptions.osimViewerOptions()
def addModelFile(self, modelFile, options=None):
self._modelFile = modelFile
self._motions = []
self._label = ""
if options is not None:
self._options = options
def addDataFile(self, dataFile, options=None):
self._modelFile = dataFile #this should be fixed as motion rather than model
self._motions = []
self._label = ""
if options is not None:
self._options = options
def addModelAndMotionFiles(self, modelFile, motions, options=None):
self._motions = motions
self._modelFile = modelFile
self._label = ""
if options is not None:
self._options = options
def addSceneCamera(self, sceneCamera):
self._options.addCamera(sceneCamera)
def show(self):
self.saveGltf()
def saveGltf(self, suggestedName=None):
if (len(self._motions)==0):
gltfOutput = osimC.convertNativeFileToGLTF(self._modelFile, self._options)
else:
gltfOutput = osimC.convertNativeFileSetToGLTF(self._modelFile, self._motions, self._options)
# add user provided cameras
for cam in self._options._additionalCameras:
osimConverters.openSimData2Gltf.addCamera(gltfOutput, cam.name, None, cam.position, cam.rotation)
if (suggestedName==None):
shortname = Path(self._modelFile).stem
gltfOutput.save(shortname+'.gltf')
else:
if (suggestedName.endswith(".gltf")):
gltfOutput.save(suggestedName)
else:
gltfOutput.save(suggestedName+'.gltf')