Commits

German Larrain committed c402ae1

Program: new _screenshot_recorder attribute, and new create_screenshot_recorder, record_frame methods.
Simulation: new num_frame and screenshot_recorder_callback attributes, and the necessary changes in on_idle.

Comments (0)

Files changed (2)

ars/app/__init__.py

 
 import sys
 from abc import abstractmethod
+import math
 
 import ars.graphics.adapters as gp
 from ars.model.simulator import Simulation
 		
 		self.key_press_functions = None
 		self.sim = None
+		self._screenshot_recorder = None
 		
 		# (key -> action) mapping
 		self.set_key_2_action_mapping()
 	
 	def on_pre_frame(self):
 		raise NotImplementedError()
+	
+	def create_screenshot_recorder(self, base_filename, period):
+		self._screenshot_recorder = gp.ScreenshotRecorder(base_filename, self.gAdapter)
+		self._screenshot_recorder.period = period
+		self.sim.screenshot_recorder_callback = self.record_frame
+	
+	def record_frame(self):
+		"""
+		Records a frame using a screenshot recorder. If frames are meant to be
+		written periodically, a new one will be recorded only if enough time
+		has elapsed, otherwise it will return False. The filename index =
+		math.ceil(time / period).
+		If frames are not meant to be written periodically, then index =
+		simulator's frame number.
+		"""
+		if self._screenshot_recorder is None:
+			raise Exception('Screenshot recorder is not initialized')
+		#=======================================================================
+		# elif self._screenshot_recorder.image_getter is None:
+		#	print('screenshot_recorder.image_getter is None')
+		#	self._screenshot_recorder.set_input(self.gAdapter.renWin)
+		#	print('screenshot_recorder input was set')
+		#=======================================================================
+			
+		try:
+			time = self.sim.sim_time
+			last_time = self._screenshot_recorder.last_write_time
+			period = self._screenshot_recorder.period
+			
+			if period is None:
+				self._screenshot_recorder.write(self.sim.num_frame)
+			else:
+				if last_time is None:
+					last_time = 0.0
+					index = 0
+				elif time - last_time + self.sim.get_time_step() >= period:
+					index = int(math.ceil(time / period))
+				else:
+					return False
+				self._screenshot_recorder.write(index, time)
+		except Exception as e:
+			raise Exception('Could not record frame', e)
 
 # TODO: see if it is better that this is derived from dictionary or another builtin class
 class ActionMap:

ars/model/simulator/__init__.py

 		self.paused = False
 		self.sim_time = 0.0
 		self.num_iter = 0
+		self.num_frame = 0
 
 		self._contact_group = None
 		self._world = None
 		# stores functions to be called on each step of a specific frame. e.g. addTorque
 		self.all_frame_steps_callbacks = []
 		
+		self.screenshot_recorder_callback = None
+		
 	def add_basic_simulation_objects(self):
 		''' create the basic simulation objects '''
 		self._contact_group = phs.create_joint_group() # for the contact joints generated during collisions
 		self._space = coll.Space()
 
 	def on_idle(self):
+		self.num_frame += 1
 		try:
-			self.pre_frame_callback()
-		except:
-			pass
+			if self.screenshot_recorder_callback is not None:
+				self.screenshot_recorder_callback()
+			if self.pre_frame_callback is not None:
+				self.pre_frame_callback()
+		except Exception as e:
+			print(e)
 		
 		self.perform_sim_steps_per_frame()
 		# clear functions registered to be called
Tip: Filter by directory path e.g. /media app.js to search for public/media/app.js.
Tip: Use camelCasing e.g. ProjME to search for ProjectModifiedEvent.java.
Tip: Filter by extension type e.g. /repo .js to search for all .js files in the /repo directory.
Tip: Separate your search with spaces e.g. /ssh pom.xml to search for src/ssh/pom.xml.
Tip: Use ↑ and ↓ arrow keys to navigate and return to view the file.
Tip: You can also navigate files with Ctrl+j (next) and Ctrl+k (previous) and view the file with Ctrl+o.
Tip: You can also navigate files with Alt+j (next) and Alt+k (previous) and view the file with Alt+o.