22import glfw
33import numpy as np
44import time
5+ import pathlib
6+ import yaml
57from .callbacks import Callbacks
68
79
@@ -27,6 +29,9 @@ def __init__(
2729 # keep true while running
2830 self .is_alive = True
2931
32+ self .CONFIG_PATH = pathlib .Path .joinpath (
33+ pathlib .Path .home (), ".config/mujoco_viewer/config.yaml" )
34+
3035 # glfw init
3136 glfw .init ()
3237
@@ -68,6 +73,43 @@ def __init__(
6873 self .ctx = mujoco .MjrContext (
6974 self .model , mujoco .mjtFontScale .mjFONTSCALE_150 .value )
7075
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+
71113 # get viewport
72114 self .viewport = mujoco .MjrRect (
73115 0 , 0 , framebuffer_width , framebuffer_height )
0 commit comments