def _write_frame(self, frames_grp: h5py.Group, resp: List[bytes], frame_num: int) -> \ Tuple[h5py.Group, h5py.Group, dict, bool]: frame, objs, tr, done = super()._write_frame(frames_grp=frames_grp, resp=resp, frame_num=frame_num) particles_group = frame.create_group("particles") velocities_group = frame.create_group("velocities") for r in resp[:-1]: if FlexParticles.get_data_type_id(r) == "flex": f = FlexParticles(r) flex_dict = dict() for i in range(f.get_num_objects()): flex_dict.update({ f.get_id(i): { "par": f.get_particles(i), "vel": f.get_velocities(i) } }) # Add the Flex data. for o_id in self.object_ids: if o_id not in flex_dict: continue particles_group.create_dataset(str(o_id), data=flex_dict[o_id]["par"]) velocities_group.create_dataset( str(o_id), data=flex_dict[o_id]["vel"]) return frame, objs, tr, done
def get_per_frame_commands(self, resp: List[bytes], frame) -> List[dict]: commands = [] # Apply a force. if frame < self._num_force_frames: for r in resp[:-1]: if FlexParticles.get_data_type_id(r) == "flex": fp = FlexParticles(r) for i in range(fp.get_num_objects()): # Find the cloth. if fp.get_id(i) == self.cloth_id: forces = [] p_id = 0 for p in fp.get_particles(i): # Add a force if this is a "corner particle". if np.abs(np.linalg.norm(p[:-1] - self._corner) ) <= self._corner_radius: # Calculate the force. pos = np.array(p[:-1]) force = ((pos - self._corner) / np.linalg.norm(pos - self._corner)) * self.\ _force_per_frame # Add the force and particle ID. forces.extend(force) forces.append(p_id) p_id += 1 # Encode and send the force. commands.extend([{ "$type": "apply_forces_to_flex_object_base64", "forces_and_ids_base64": TDWUtils.get_base64_flex_particle_forces( forces), "id": self.cloth_id }]) return commands
def run(self): self.load_streamed_scene(scene="tdw_room_2018") self.communicate({"$type": "set_time_step", "time_step": 0.02}) # Create the container. self.communicate({"$type": "create_flex_container", "particle_size": 0.1, "collision_distance": 0.025, "solid_rest": 0.1}) # Create the avatar. self.communicate(TDWUtils.create_avatar(position={"x": -1.5, "y": 0.85, "z": -0.5})) # Add the object. object_id = self.add_object("linbrazil_diz_armchair", position={"x": 0.0, "y": 2.0, "z": 0.0}, rotation={"x": 25.0, "y": 45.0, "z": -40.0}, library="models_core.json") # Set the object to kinematic. # Set the soft actor. # Assign the actor's container. self.communicate([{"$type": "set_kinematic_state", "id": object_id}, {"$type": "set_flex_soft_actor", "id": object_id, "skinning_falloff": 0.5, "volume_sampling": 1.0, "mass_scale": 1.0, "cluster_stiffness": 0.2, "cluster_spacing": 0.2, "cluster_radius": 0.2, "link_radius": 0, "link_stiffness": 1.0, "particle_spacing": 0.025}, {"$type": "assign_flex_container", "id": object_id, "container_id": 0} ]) # Send particles data. resp = self.communicate([{"$type": "send_flex_particles", "frequency": "always"}]) # Output example data. particles = FlexParticles(resp[0]) for j in range(particles.get_num_objects()): print(particles.get_id(j)) print(particles.get_velocities(j)) print(particles.get_particles(j)) for i in range(1000): # Look at the object. self.communicate({"$type": "look_at", "avatar_id": "a", "object_id": object_id, "use_centroid": True})
def run(self): self.start() object_id = self.get_unique_id() resp = self.communicate([ TDWUtils.create_empty_room(12, 12), { "$type": "create_flex_container", "particle_size": 0.1, "collision_distance": 0.025, "solid_rest": 0.1 }, # {"$type": "create_flex_container", # "collision_distance": 0.001, # "static_friction": 1.0, # "dynamic_friction": 1.0, # "radius": 0.1875, # "max_particles": 200000}, self.get_add_object(model_name="linbrazil_diz_armchair", position={ "x": 0.0, "y": 2.0, "z": 0.0 }, rotation={ "x": 25.0, "y": 45.0, "z": -40.0 }, object_id=object_id), { "$type": "set_kinematic_state", "id": object_id }, { "$type": "set_flex_solid_actor", "id": object_id, "mesh_expansion": 0, "particle_spacing": 0.025, "mass_scale": 1 }, # {"$type": "set_flex_soft_actor", # "id": object_id, # "skinning_falloff": 0.5, # "volume_sampling": 1.0, # "mass_scale": 1.0, # "cluster_stiffness": 0.2, # "cluster_spacing": 0.2, # "cluster_radius": 0.2, # "link_radius": 0, # "link_stiffness": 1.0, # "particle_spacing": 0.025}, { "$type": "assign_flex_container", "id": object_id, "container_id": 0 }, { '$type': 'load_primitive_from_resources', 'id': 2, 'primitive_type': 'Cube', 'position': { 'x': 0, 'y': 0.5, 'z': 0 }, 'orientation': { 'x': 0, 'y': 0, 'z': 0 } }, { "$type": "scale_object", "id": 2, "scale_factor": { "x": 0.5, "y": 0.5, "z": 0.5 } }, { '$type': 'set_kinematic_state', 'id': 2, 'is_kinematic': False }, { "$type": "set_flex_solid_actor", "id": 2, "mesh_expansion": 0, "particle_spacing": 0.025, "mass_scale": 1 }, # {'$type': 'set_flex_soft_actor', # 'id': 2, # 'draw_particles': False, # 'particle_spacing': 0.125, # 'cluster_stiffness': 0.22055267521432875}, { '$type': 'assign_flex_container', 'id': 2, 'container_id': 0 }, { '$type': 'set_flex_particles_mass', 'id': 2, 'mass': 15.625 }, { "$type": "send_flex_particles", "frequency": "always" }, { "$type": "send_collisions", "enter": True, "stay": True, "exit": True, "collision_types": ["obj", "env"] } ]) for i in range(100): for j in range(len(resp) - 1): r_id = OutputData.get_data_type_id(resp[j]) if r_id == "flex": flex = FlexParticles(resp[j]) print(i, "flex", flex.get_id(0)) elif r_id == "enco": enco = EnvironmentCollision(resp[j]) print(i, "enco", enco.get_state()) if r_id == "coll": coll = Collision(resp[j]) print(i, "coll", coll.get_state()) if coll.get_state() == 'enter': print("BAM!") resp = self.communicate([])
def run(self): o_id = 0 self.start() # 1. Create a room. # 2. Create a Flex container. # 3. Add a cloth object and set the cloth actor. # 4. Request Flex particle output data (this frame only). commands = [ TDWUtils.create_empty_room(12, 12), { "$type": "create_flex_container", "collision_distance": 0.001, "static_friction": 1.0, "dynamic_friction": 1.0, "iteration_count": 12, "substep_count": 12, "radius": 0.1875, "damping": 0, "drag": 0 }, self.get_add_object("cloth_square", library="models_special.json", position={ "x": 0, "y": 2, "z": 0 }, rotation={ "x": 0, "y": 0, "z": 0 }, object_id=o_id), { "$type": "set_flex_cloth_actor", "id": o_id, "mesh_tesselation": 1, "stretch_stiffness": 0.5620341548096974, "bend_stiffness": 0.6528988964052056, "tether_stiffness": 0.7984931184979334, "tether_give": 0, "pressure": 0, "mass_scale": 1 }, { "$type": "assign_flex_container", "container_id": 0, "id": o_id }, { "$type": "send_flex_particles", "frequency": "once" } ] # Add to the list of commands: create the avatar, teleport it to a position, look at a position. commands.extend( TDWUtils.create_avatar(position={ "x": -2.75, "y": 2.3, "z": -2 }, look_at={ "x": 0, "y": 0.25, "z": 0 })) # Send the commands. resp = self.communicate(commands) for r in resp[:-1]: r_id = OutputData.get_data_type_id(r) # This is FlexParticles data. if r_id == "flex": fp = FlexParticles(resp[0]) # The maximum distance between a particle and the center of the cloth. max_d = 0 # The ID of the particle at the maximum distance. max_id = 0 p_id = 0 for p in fp.get_particles(0): # Get the particle that is furthest from the center. d = np.linalg.norm(p[:-1] - 1) if d > max_d: max_d = d max_id = p_id p_id += 1 # Set the particle as "fixed". self.communicate({ "$type": "set_flex_particle_fixed", "is_fixed": True, "particle_id": max_id, "id": o_id }) for i in range(800): self.communicate([])
def run(self): # Load the streamed scene and add controller rig. self.load_streamed_scene(scene="tdw_room_2018") # Create the container. self.communicate({ "$type": "create_flex_container", "collision_distance": 0.001, "static_friction": 1.0, "dynamic_friction": 1.0, "iteration_count": 3, "substep_count": 8, "radius": 0.1875, "damping": 0, "drag": 0 }) self.communicate({"$type": "create_vr_rig"}) # Add the table object. table_id = self.add_object("trunck", position={ "x": 0, "y": 0, "z": 0.5 }) # Add the vase object and make it graspable. graspable_id_box = self.add_object("woven_box", position={ "x": 0.2, "y": 1, "z": 0.5 }, library="models_core.json") self.communicate([{ "$type": "set_kinematic_state", "id": table_id, "is_kinematic": True }, { "$type": "set_kinematic_state", "id": graspable_id_box, "is_kinematic": True }]) # Assign the object a FlexActor. # Assign the object a Flex container. self.communicate([{ "$type": "set_flex_solid_actor", "id": graspable_id_box, "mass_scale": 100.0, "particle_spacing": 0.035 }, { "$type": "assign_flex_container", "id": graspable_id_box, "container_id": 0 }]) self.communicate([{ "$type": "set_flex_solid_actor", "id": table_id, "mass_scale": 120.0, "particle_spacing": 0.035 }, { "$type": "assign_flex_container", "id": table_id, "container_id": 0 }]) # Set the objects as graspable. self.communicate([{ "$type": "set_graspable", "id": table_id }, { "$type": "set_graspable", "id": graspable_id_box }]) # Send particles data for the box. resp = self.communicate({ "$type": "send_flex_particles", "frequency": "always", "ids": [graspable_id_box] }) while True: particles = FlexParticles(resp[0]) for j in range(particles.get_num_objects()): # Print just the first five particles on the box. print(particles.get_particles(j)[:5]) resp = self.communicate({"$type": "do_nothing"})