2
2
import glfw
3
3
import numpy as np
4
4
import time
5
+ import pathlib
6
+ import yaml
5
7
from .callbacks import Callbacks
6
8
7
9
@@ -27,6 +29,9 @@ def __init__(
27
29
# keep true while running
28
30
self .is_alive = True
29
31
32
+ self .CONFIG_PATH = pathlib .Path .joinpath (
33
+ pathlib .Path .home (), ".config/mujoco_viewer/config.yaml" )
34
+
30
35
# glfw init
31
36
glfw .init ()
32
37
@@ -68,6 +73,43 @@ def __init__(
68
73
self .ctx = mujoco .MjrContext (
69
74
self .model , mujoco .mjtFontScale .mjFONTSCALE_150 .value )
70
75
76
+ # load camera from configuration (if available)
77
+ pathlib .Path (
78
+ self .CONFIG_PATH .parent ).mkdir (
79
+ parents = True ,
80
+ exist_ok = True )
81
+ pathlib .Path (self .CONFIG_PATH ).touch (exist_ok = True )
82
+ with open (self .CONFIG_PATH , "r" ) as f :
83
+ try :
84
+ cam_config = {
85
+ "type" : self .cam .type ,
86
+ "fixedcamid" : self .cam .fixedcamid ,
87
+ "trackbodyid" : self .cam .trackbodyid ,
88
+ "lookat" : self .cam .lookat .tolist (),
89
+ "distance" : self .cam .distance ,
90
+ "azimuth" : self .cam .azimuth ,
91
+ "elevation" : self .cam .elevation
92
+ }
93
+ load_config = yaml .safe_load (f )
94
+ if isinstance (load_config , dict ):
95
+ for key , val in load_config .items ():
96
+ if key in cam_config .keys ():
97
+ cam_config [key ] = val
98
+ if cam_config ["type" ] == mujoco .mjtCamera .mjCAMERA_FIXED :
99
+ if cam_config ["fixedcamid" ] < self .model .ncam :
100
+ self .cam .type = cam_config ["type" ]
101
+ self .cam .fixedcamid = cam_config ["fixedcamid" ]
102
+ if cam_config ["type" ] == mujoco .mjtCamera .mjCAMERA_TRACKING :
103
+ if cam_config ["trackbodyid" ] < self .model .nbody :
104
+ self .cam .type = cam_config ["type" ]
105
+ self .cam .trackbodyid = cam_config ["trackbodyid" ]
106
+ self .cam .lookat = np .array (cam_config ["lookat" ])
107
+ self .cam .distance = cam_config ["distance" ]
108
+ self .cam .azimuth = cam_config ["azimuth" ]
109
+ self .cam .elevation = cam_config ["elevation" ]
110
+ except yaml .YAMLError as e :
111
+ print (e )
112
+
71
113
# get viewport
72
114
self .viewport = mujoco .MjrRect (
73
115
0 , 0 , framebuffer_width , framebuffer_height )
0 commit comments