def test_spawn_and_delete_one_object(self, client_type): """ Ask Clerk to spawn one object. """ # Get the client for this test. client = self.clients[client_type] # Constants and parameters for this test. objID, templateID = 1, '_templateEmpty' # Spawn a new object from templateID. The new object must have objID=1. init = {'templateID': templateID, 'rbs': {'position': (0, 0, 0)}} ret = client.spawn([init]) assert ret.ok and ret.data == [objID] # Attempt to spawn a non-existing template. assert not client.spawn([{'templateID': 'blah'}]).ok # Send invalid data to 'spawn'. assert not client.spawn([{'blah': 'blah'}]).ok # Exactly one object must exist at this point. ret = client.getAllObjectIDs() assert (ret.ok, ret.data) == (True, [objID]) # Attempt to delete a non-existing object. This must silently fail. assert client.removeObject(100).ok ret = client.getAllObjectIDs() assert (ret.ok, ret.data) == (True, [objID]) # Delete an existing object. assert client.removeObject(objID).ok ret = client.getAllObjectIDs() assert (ret.ok, ret.data) == (True, [])
def run(self): # Return immediately if no resets are required. if self.period == -1: return # Establish connection to Azrael. client = azrael.client.Client() # Query all objects in the scene. These are the only objects that will # survive the reset. ret = client.getAllObjectIDs() assert ret.ok ret = client.getRigidBodies(ret.data) assert ret.ok allowed_objIDs = {k: v["rbs"] for k, v in ret.data.items() if v is not None} print("Took simulation snapshot for reset: ({} objects)".format(len(allowed_objIDs))) # Periodically reset the SV values. Set them several times because it # is well possible that not all State Variables reach Leonard in the # same frame, which means some objects will be reset while other are # not. This in turn may cause strange artefacts in the next physics # update step, especially when the objects now partially overlap. while True: # Wait until the timeout expires. time.sleep(self.period) # Remove all newly added objects. ret = client.getAllObjectIDs() for objID in ret.data: if objID not in allowed_objIDs: client.removeObject(objID) # Forcefully reset the position and velocity of every object. Do # this several times since network latency may result in some # objects being reset sooner than others. for ii in range(5): for objID, SV in allowed_objIDs.items(): tmp = { "position": SV.position, "velocityLin": SV.velocityLin, "velocityRot": SV.velocityRot, "rotation": SV.rotation, } assert client.setRigidBodies({objID: tmp}).ok time.sleep(0.1)
def resetSimulation(host, port=5555): """ Delete all objects in the scene. Azrael does not have a command to do that (yet), which is why this function queries all objects and the deletes them one-by-one. The `host` and `port` parameters specify the location of the Azrael API. """ # Connect to Azrael. client = azrael.client.Client(host, port=port) # Query IDs of all objects in the simulation. ret = client.getAllObjectIDs() assert ret.ok allIDs = ret.data # Delete all objects. for objID in allIDs: assert client.removeObject(objID).ok