def run(self): self.start() init_setup_commands = [{ "$type": "set_screen_size", "width": 1280, "height": 962 }, { "$type": "set_render_quality", "render_quality": 5 }] self.communicate(init_setup_commands) # Create an empty room. self.load_streamed_scene(scene="tdw_room_2018") # Disable physics. self.communicate({"$type": "set_gravity", "value": False}) # Add the avatar. self.communicate( TDWUtils.create_avatar(position={ "x": 0, "y": 1.7, "z": 0 }, look_at=TDWUtils.array_to_vector3([4, 0, 0]), avatar_id="avatar")) self.communicate({ "$type": "set_field_of_view", "field_of_view": 68.0, "avatar_id": "avatar" }) self.add_object(model_name="linbrazil_diz_armchair", position={ "x": 3.5, "y": 0, "z": 1.5 }, rotation={ "x": 0, "y": -90, "z": 0 }, library="models_full.json") table_id = self.add_object(model_name="03_106", position={ "x": 2.5, "y": 0, "z": 0 }, rotation={ "x": 0, "y": 90, "z": 0 }, library="models_full.json") table_bounds = self.get_bounds_data(table_id) top = table_bounds.get_top(0) self.add_object(model_name="ficus", position={ "x": 3.5, "y": 0, "z": -1.5 }, rotation={ "x": 0, "y": -90, "z": 0 }, library="models_full.json") self.add_object(model_name="vm_v2_015", position={ "x": 2.5, "y": top[1], "z": 0.5 }, rotation={ "x": 0, "y": 90, "z": 0 }, library="models_full.json") # Enable image capture self.communicate({ "$type": "set_pass_masks", "avatar_id": "avatar", "pass_masks": ["_img", "_id"] }) self.communicate({"$type": "send_images", "frequency": "always"}) scene_data = self.communicate({ "$type": "look_at_position", "avatar_id": "avatar", "position": TDWUtils.array_to_vector3([4, 0, 0]) }) images = Images(scene_data[0]) TDWUtils.save_images(images, "fan_room", output_directory="replicated_images")
def run(self): self.start() init_setup_commands = [{"$type": "set_screen_size", "width": 600, "height": 480}, {"$type": "set_render_quality", "render_quality": 5}] self.communicate(init_setup_commands) # Create an empty room. self.load_streamed_scene(scene="tdw_room_2018") # Disable physics. self.communicate({"$type": "set_gravity", "value": False}) # Add the avatar. self.communicate(TDWUtils.create_avatar(position={"x": -0.8, "y": 1.6, "z": 0.6}, look_at=TDWUtils.array_to_vector3([0, 0.4, 0]), avatar_id="avatar")) # self.communicate({"$type": "set_field_of_view", # "field_of_view": 68.0, # "avatar_id": "avatar"}) table_id = self.add_object(model_name="glass_table_round", position={"x": 0, "y": 0, "z": 0}, library="models_full.json") table_bounds = self.get_bounds_data(table_id) top = table_bounds.get_top(0) self.add_object(model_name="b03_orange", position={"x": 0, "y": top[1], "z": 0}, library="models_full.json") position = TDWUtils.get_random_point_in_circle([0, 0, 0], 0.17) position[1] = top[1] self.add_object(model_name="b03_orange", position={"x": 0, "y": top[1], "z": 0}, library="models_full.json") position = TDWUtils.get_random_point_in_circle([0, 0, 0], 0.4) position[1] = top[1] self.add_object(model_name="b04_banana", position=TDWUtils.array_to_vector3(position), rotation={"x": 0, "y": 40, "z": 0}, library="models_full.json") position = TDWUtils.get_random_point_in_circle([0, 0, 0], 0.4) position[1] = top[1] self.add_object(model_name="b04_banana", position=TDWUtils.array_to_vector3(position), library="models_full.json") position = TDWUtils.get_random_point_in_circle([0, 0, 0], 0.5) position[1] = top[1] self.add_object(model_name="red_apple", position=TDWUtils.array_to_vector3(position), library="models_full.json") position = TDWUtils.get_random_point_in_circle([0, 0, 0], 0.6) position[1] = top[1] self.add_object(model_name="vk0076_fruitfork", position=TDWUtils.array_to_vector3(position), library="models_full.json") # Enable image capture self.communicate({"$type": "set_pass_masks", "avatar_id": "avatar", "pass_masks": ["_img", "_id"]}) self.communicate({"$type": "send_images", "frequency": "always"}) scene_data = self.communicate({"$type": "look_at_position", "avatar_id": "avatar", "position": TDWUtils.array_to_vector3([0, 0.4, 0])}) images = Images(scene_data[0]) TDWUtils.save_images(images, "fruit_table", output_directory="/Users/leonard/Desktop/TDWBase-1.5.0/Python/Leonard/compare_COCO_TDW/replicated_images/interior")
def trial(self): """ Select random objects and collide them to produce impact sounds. """ p = PyImpact(initial_amp=0.5) # Destroy all objects currently in the scene. # Set the screen size. # Adjust physics timestep for more real-time physics behavior. commands = [{ "$type": "destroy_all_objects" }, { "$type": "set_screen_size", "width": 1024, "height": 1024 }, { "$type": "set_time_step", "time_step": 0.02 }] # Create the avatar. commands.extend( TDWUtils.create_avatar(avatar_type="A_Img_Caps_Kinematic", position={ "x": 1, "y": 1.2, "z": 1.2 }, look_at=TDWUtils.VECTOR3_ZERO)) # Add the audio sensor. # Set the target framerate. # Make sure that post-processing is enabled and render quality is set to max. commands.extend([{ "$type": "add_audio_sensor", "avatar_id": "a" }, { "$type": "set_target_framerate", "framerate": 60 }, { "$type": "set_post_process", "value": True }, { "$type": "set_focus_distance", "focus_distance": 2 }, { "$type": "set_render_quality", "render_quality": 5 }]) # Select a random pair of objects. objects = PyImpact.get_object_info() obj1_names = [ "trapezoidal_table", "glass_table_round", "yellow_side_chair", "table_square", "marble_table" ] obj2_names = ["vase_06", "spoon1", "glass3", "jug02"] obj1_name = random.choice(obj1_names) obj2_name = random.choice(obj2_names) obj1_id = 0 obj2_id = 1 # Add the objects. # Set their masses from the audio info data. # Set a physics material for the second object. # Apply a force to the second object. # Listen for collisions, and object properties. commands.extend([ self.get_add_object(model_name=obj1_name, object_id=obj1_id, library=objects[obj1_name].library), { "$type": "set_mass", "id": obj1_id, "mass": objects[obj1_name].mass }, self.get_add_object(model_name=obj2_name, object_id=obj2_id, library=objects[obj2_name].library, rotation={ "x": 135, "y": 0, "z": 30 }, position={ "x": 0, "y": 2, "z": 0 }), { "$type": "set_mass", "id": obj2_id, "mass": objects[obj2_name].mass }, { "$type": "set_physic_material", "id": obj2_id, "bounciness": objects[obj2_name].bounciness, "dynamic_friction": 0.8 }, { "$type": "apply_force_to_object", "force": { "x": 0, "y": -0.01, "z": 0 }, "id": obj2_id }, { "$type": "send_collisions", "enter": True, "stay": False, "exit": False }, { "$type": "send_rigidbodies", "frequency": "always", "ids": [obj2_id, obj1_id] } ]) # Send all of the commands. resp = self.communicate(commands) # Iterate through 200 frames. # Every frame, listen for collisions, and parse the output data. for i in range(200): collisions, environment_collision, rigidbodies = PyImpact.get_collisions( resp) # If there was a collision, create an impact sound. if len(collisions) > 0 and PyImpact.is_valid_collision( collisions[0]): impact_sound_command = p.get_impact_sound_command( collision=collisions[0], rigidbodies=rigidbodies, target_id=obj2_id, target_mat=objects[obj2_name].material.name, target_amp=objects[obj2_name].amp, other_id=obj1_id, other_amp=objects[obj1_name].amp, other_mat=objects[obj1_name].material.name) resp = self.communicate(impact_sound_command) # Continue to run the trial. else: resp = self.communicate([]) # Stop listening for collisions and rigidbodies. self.communicate([{ "$type": "send_collisions", "frequency": "never" }, { "$type": "send_rigidbodies", "frequency": "never" }])
def run(self): self.start() init_setup_commands = [{ "$type": "set_screen_size", "width": 1280, "height": 962 }, { "$type": "set_render_quality", "render_quality": 5 }] self.communicate(init_setup_commands) # Create an empty room. self.load_streamed_scene(scene="tdw_room_2018") # Disable physics. self.communicate({"$type": "set_gravity", "value": False}) # Add the avatar. self.communicate( TDWUtils.create_avatar(position={ "x": 0, "y": 1, "z": 0 }, look_at=TDWUtils.array_to_vector3( [1.5, 0.8, -1.5]), avatar_id="avatar")) # self.communicate({"$type": "set_field_of_view", # "field_of_view": 68.0, # "avatar_id": "avatar"}) chair_id = self.add_object(model_name="chair_willisau_riale", position={ "x": 2, "y": 0, "z": -1.5 }, library="models_full.json") chair_bounds = self.get_bounds_data(chair_id) top = chair_bounds.get_top(0) self.add_object(model_name="b03_cat3smooth", position={ "x": 2, "y": top[1] - 0.34, "z": -1.5 }, rotation=TDWUtils.array_to_vector3([0, -45, 0]), library="models_full.json") table_id = self.add_object(model_name="de_castelli_placas_table", position={ "x": 1.5, "y": 0, "z": -1.5 }, library="models_full.json") table_bounds = self.get_bounds_data(table_id) table_top = table_bounds.get_top(0) self.add_object(model_name="ikea_clock", position={ "x": 1.5, "y": table_top[1], "z": -1.5 }, library="models_full.json") # Enable image capture self.communicate({ "$type": "set_pass_masks", "avatar_id": "avatar", "pass_masks": ["_img", "_id"] }) self.communicate({"$type": "send_images", "frequency": "always"}) scene_data = self.communicate({ "$type": "look_at_position", "avatar_id": "avatar", "position": TDWUtils.array_to_vector3([2, 0.8, -1.5]) }) images = Images(scene_data[0]) TDWUtils.save_images(images, "cat_on_chair", output_directory="replicated_images")
def run(self): self.start() init_setup_commands = [{ "$type": "set_screen_size", "width": 600, "height": 480 }, { "$type": "set_render_quality", "render_quality": 5 }] self.communicate(init_setup_commands) # Create an empty room. self.load_streamed_scene(scene="tdw_room_2018") # Disable physics. self.communicate({"$type": "set_gravity", "value": False}) # Add the avatar. self.communicate( TDWUtils.create_avatar(position={ "x": -1, "y": 0.9, "z": -1 }, look_at=TDWUtils.array_to_vector3( [-0.1, 0.5, -0.1]), avatar_id="avatar")) self.communicate({ "$type": "set_field_of_view", "field_of_view": 68.0, "avatar_id": "avatar" }) table_id = self.add_object(model_name="coffee_table_glass_round", position={ "x": 0, "y": 0, "z": 0 }, library="models_full.json") table_bounds = self.get_bounds_data(table_id) top = table_bounds.get_top(0) self.add_object(model_name="am151_043_00", position={ "x": 0.15, "y": top[1], "z": 0.15 }, rotation={ "x": 0, "y": 30, "z": 0 }, library="models_full.json") self.add_object(model_name="b04_tea_cup", position={ "x": 0.3, "y": top[1], "z": 0.1 }, library="models_full.json") self.add_object(model_name="cgaxis_models_65_06_vray", position={ "x": -0.17, "y": top[1], "z": 0.15 }, rotation={ "x": 0, "y": 24, "z": 0 }, library="models_full.json") # Enable image capture self.communicate({ "$type": "set_pass_masks", "avatar_id": "avatar", "pass_masks": ["_img", "_id"] }) self.communicate({"$type": "send_images", "frequency": "always"}) scene_data = self.communicate({ "$type": "look_at_position", "avatar_id": "avatar", "position": TDWUtils.array_to_vector3([-0.1, 0.45, -0.1]) }) images = Images(scene_data[0]) TDWUtils.save_images( images, "sadnwich1", output_directory= "/Users/leonard/Desktop/TDWBase-1.5.0/Python/Leonard/compare_COCO_TDW/replicated_images/interior" )
def run(self, drag_on_land, angular_drag_on_land): self.start() self.communicate(TDWUtils.create_empty_room(40, 40)) # Create the avatar. self.communicate( TDWUtils.create_avatar(avatar_type="A_Simple_Body", position={ "x": 0, "y": 1, "z": 0 })) # When the y value of the avatar's position is this value, the avatar is on the ground. ground_y = 1 # When the y value of the avatar's position exceeds ground_y by this much, it is in the air. in_air_threshold = 0.05 # If true, the avatar is in the air. is_in_air = False # Set high default drag values. # Set a low mass. # Apply a force. # Request avatar data. resp = self.communicate([{ "$type": "set_avatar_drag", "avatar_id": "a", "drag": 30, "angular_drag": 80 }, { "$type": "send_avatars", "frequency": "always" }, { "$type": "apply_force_to_avatar", "avatar_id": "a", "force": { "x": 900, "y": 1000, "z": 0 } }]) for i in range(500): x, y, z = AvatarSimpleBody(resp[0]).get_position() # If the avatar just launched in to the air, set its drag values to 0. if y > ground_y + in_air_threshold and not is_in_air: is_in_air = True resp = self.communicate({ "$type": "set_avatar_drag", "avatar_id": "a", "drag": 0, "angular_drag": 0 }) print("The avatar is in the air on frame: " + str(Controller.get_frame(resp[-1]))) # If the avatar just landed on the ground, set drag values. elif y <= ground_y + in_air_threshold and is_in_air: resp = self.communicate({ "$type": "set_avatar_drag", "avatar_id": "a", "drag": drag_on_land, "angular_drag": angular_drag_on_land }) is_in_air = False print("The avatar is on the ground on frame: " + str(Controller.get_frame(resp[-1]))) # Wait. else: resp = self.communicate({"$type": "do_nothing"})
def run(self): if system() != "Windows": raise Exception( "Flex fluids are only supported in Windows (see Documentation/misc_frontend/flex.md)" ) self.load_streamed_scene(scene="tdw_room") # Create the container, set up for fluids. self.communicate({ "$type": "create_flex_container", "collision_distance": 0.04, "static_friction": 0.1, "dynamic_friction": 0.1, "particle_friction": 0.1, "viscocity": 0.001, "cohesion": 0.0015, "radius": 0.1, "fluid_rest": 0.05, "damping": 0.01, "substep_count": 5, "iteration_count": 5, "buoyancy": 1.0 }) # Slow down physics so the water can settle without splashing out of the container. self.communicate({"$type": "set_time_step", "time_step": 0.005}) # Create the avatar. self.communicate( TDWUtils.create_avatar(position={ "x": -3.75, "y": 1.5, "z": -0.5 }, look_at={ "x": 0, "y": 0, "z": 0 })) # Load a pool container for the fluid. self.pool_id = self.add_object("fluid_receptacle1x1", position={ "x": -0.35, "y": 0, "z": 0 }, rotation={ "x": 0, "y": 0, "z": 0 }, library="models_special.json") self.communicate([{ "$type": "scale_object", "id": self.pool_id, "scale_factor": { "x": 2.0, "y": 2.0, "z": 2.0 } }, { "$type": "set_kinematic_state", "id": self.pool_id, "is_kinematic": True, "use_gravity": False }]) # Add the fluid actor, using the FluidPrimitive. self.fluid_id = self.get_unique_id() self.communicate({ "$type": "load_flex_fluid_from_resources", "id": self.fluid_id, "orientation": { "x": 0, "y": 0, "z": 0 }, "position": { "x": -0.35, "y": 1.0, "z": 0 } }) # Assign the actor's container and set the Flex scale (this MUST be done, even if the scale is 1,1,1). self.communicate([{ "$type": "create_flex_fluid_object", "id": self.fluid_id, "mass_scale": 1.0, "particle_spacing": 0.05 }, { "$type": "assign_flex_container", "id": self.fluid_id, "container_id": 0, "fluid_container": True }]) # Pause for a while to look at the container while it fills with water (this is not required, simply for demo purposes). for i in range(500): # Look at the object. self.communicate({ "$type": "look_at", "avatar_id": "a", "object_id": self.pool_id, "use_centroid": True }) # Set physics back to a normal rate, for the trials. self.communicate({"$type": "set_time_step", "time_step": 0.03}) # Set up the data for five "trials" and run them. masses = [1.25, 2.5, 4.0, 6.65, 8.5] heights = [3.0, 3.0, 3.0, 3.0, 3.0] stim_times = [170, 170, 170, 170, 170] pause_times = [0.1, 0.1, 0.1, 0.1, 0.1] for mass, height, stim_time, pause_time in zip(masses, heights, stim_times, pause_times): self.do_trial(mass, height, stim_time, pause_time)
def run(self): self.start() init_setup_commands = [{ "$type": "set_screen_size", "width": 600, "height": 480 }, { "$type": "set_render_quality", "render_quality": 5 }] self.communicate(init_setup_commands) # Create an empty room. self.communicate(TDWUtils.create_empty_room(8, 8)) # Disable physics. self.communicate({"$type": "set_gravity", "value": False}) # Add the avatar. self.communicate( TDWUtils.create_avatar(position={ "x": 0, "y": 0.5, "z": 1.3 }, look_at=TDWUtils.array_to_vector3( [0.1, 0.2, 2.5]), avatar_id="avatar")) # self.communicate({"$type": "set_field_of_view", # "field_of_view": 68.0, # "avatar_id": "avatar"}) self.add_object(model_name="b06_cat_1", position={ "x": -0.2, "y": 0, "z": 3.0 }, rotation={ "x": 0, "y": 159, "z": 0 }, library="models_full.json") self.add_object(model_name="labrador_retriever_puppy", position={ "x": 0.25, "y": 0, "z": 2.8 }, rotation={ "x": 0, "y": 30, "z": 0 }, library="models_full.json") self.add_object(model_name="baseball2014", position={ "x": 0.17, "y": 0, "z": 2.5 }, rotation={ "x": 0, "y": 0, "z": 0 }, library="models_full.json") self.add_object(model_name="baseballs", position={ "x": -0.14, "y": 0, "z": 2.6 }, rotation={ "x": 0, "y": 23, "z": 0 }, library="models_full.json") # Enable image capture self.communicate({ "$type": "set_pass_masks", "avatar_id": "avatar", "pass_masks": ["_img", "_id"] }) self.communicate({"$type": "send_images", "frequency": "always"}) scene_data = self.communicate({ "$type": "look_at_position", "avatar_id": "avatar", "position": TDWUtils.array_to_vector3([0.1, 0.2, 2.5]) }) images = Images(scene_data[0]) TDWUtils.save_images( images, "dog_cat", output_directory= "/Users/leonard/Desktop/TDWBase-1.5.0/Python/Leonard/compare_COCO_TDW/replicated_images/interior" )
def trial(self, d_f, s_f, b_f, d_c, s_c, b_c, collision_types): """ Collide a chair with a fridge. :param d_f: The dynamic friction of the fridge. :param s_f: The static friction of the fridge. :param b_f: The bounciness of the fridge. :param d_c: The dynamic friction of the chair. :param s_c: The static friction of the chair. :param b_c: The bounciness of the chair. :param collision_types: The types of collisions to listen for. """ print("###\n\nNew Trial\n\n###\n") fridge_id = 0 chair_id = 1 # Destroy all objects currently in the scene. init_commands = [{"$type": "destroy_all_objects"}] # Create the avatar. init_commands.extend( TDWUtils.create_avatar(position={ "x": 1, "y": 2.5, "z": 5 }, look_at=TDWUtils.VECTOR3_ZERO)) # Add the objects. # Set the masses and physic materials. # Apply a force to the chair. # Receive collision data (note that by setting "stay" to True, you will receive a LOT of data; # see "Performance Optimizations" documentation.) init_commands.extend([ self.get_add_object("fridge_large", object_id=fridge_id), self.get_add_object("chair_billiani_doll", object_id=chair_id, position={ "x": 4, "y": 0, "z": 0 }), { "$type": "set_mass", "id": fridge_id, "mass": 40 }, { "$type": "set_mass", "id": chair_id, "mass": 20 }, { "$type": "set_physic_material", "id": fridge_id, "dynamic_friction": d_f, "static_friction": s_f, "bounciness": b_f }, { "$type": "set_physic_material", "id": chair_id, "dynamic_friction": d_c, "static_friction": s_c, "bounciness": b_c }, { "$type": "apply_force_to_object", "force": { "x": -200, "y": 0, "z": 0 }, "id": chair_id }, { "$type": "send_collisions", "collision_types": collision_types, "enter": True, "exit": True, "stay": True } ]) self.communicate(init_commands) # Iterate through 500 frames. # Every frame, listen for collisions, and parse the output data. for i in range(500): resp = self.communicate([]) if len(resp) > 1: for r in resp[:-1]: r_id = OutputData.get_data_type_id(r) # There was a collision between two objects. if r_id == "coll": collision = Collision(r) print("Collision between two objects:") print("\tEvent: " + collision.get_state()) print("\tCollider: " + str(collision.get_collider_id())) print("\tCollidee: " + str(collision.get_collidee_id())) print("\tRelative velocity: " + str(collision.get_relative_velocity())) print("\tContacts:") for j in range(collision.get_num_contacts()): print( str(collision.get_contact_normal(j)) + "\t" + str(collision.get_contact_point(j))) # There was a collision between an object and the environment. elif r_id == "enco": collision = EnvironmentCollision(r) print( "Collision between an object and the environment:") print("\tEvent: " + collision.get_state()) print("\tCollider: " + str(collision.get_object_id())) print("\tContacts:") for j in range(collision.get_num_contacts()): print( str(collision.get_contact_normal(j)) + "\t" + str(collision.get_contact_point(j))) else: raise Exception(r_id)
def run(self): self.start() positions_list = [] # Stores current model locations and radii scene_dimensions = [] # Store scene/environment dimensions init_setup_commands = [{ "$type": "set_screen_size", "width": 1280, "height": 962 }, { "$type": "set_render_quality", "render_quality": 5 }] self.communicate(init_setup_commands) scene_lib = SceneLibrarian() # Disable physics when adding in new objects (objects float) self.communicate({"$type": "simulate_physics", "value": False}) for scene in scenes[1:]: # Load in scene print("Scene", scene[0]) if scene[3] == "interior" and scene[0] == "box_room_2018": self.start() scene_record = scene_lib.get_record(scene[0]) self.communicate({ "$type": "add_scene", "name": scene_record.name, "url": scene_record.get_url() }) # Gets dimensions of environments (e.g. inside, outside) in the scene # This command returns environment data in the form of a list of serialized byte arrays scene_bytes = self.communicate({ "$type": "send_environments", "frequency": "once" }) # Iterating through data and parsing byte array # Ignoring the last element (the frame count) for b in scene_bytes[:-1]: e = Environments(b) for i in range(e.get_num()): center = e.get_center(i) bounds = e.get_bounds(i) env_id = e.get_id(i) scene_dimensions = [center, bounds, env_id] # Center, bounds are tuples # Must come before set_pass_masks avatar_position = TDWUtils.array_to_vector3([ 0.9 * scene_dimensions[1][0] / 2, scene_dimensions[1][1] / 2, 0 ]) print("Avatar Position:", avatar_position) self.communicate( TDWUtils.create_avatar(avatar_id="avatar", position=avatar_position, look_at={ "x": 0, "y": scene_dimensions[0][1] / 2, "z": 0 })) # Set collision mode self.communicate({ "$type": "set_avatar_collision_detection_mode", "mode": "continuous_speculative", "avatar_id": "avatar" }) # Alter FOV self.communicate({ "$type": "set_field_of_view", "field_of_view": 80, "avatar_id": "avatar" }) # Gets rid of header (Model: Category) objects = models[1:] random.shuffle(objects) obj_count = 0 obj_overlaps = 0 # Number of failed attempts to place object due to over-dense objects area while obj_count < 30 and obj_overlaps < 5: # Need to have random position for Bounds Data to return meaningful info valid_obj_pos = { "x": random.uniform(-1 * scene_dimensions[1][0] / 2, 0.5 * scene_dimensions[1][0] / 2), "y": scene_dimensions[1][1] / 4, "z": random.uniform(-0.9 * scene_dimensions[1][2] / 2, 0.9 * scene_dimensions[1][2] / 2) } # Add in the object at random position # Object will later be removed or updated accordingly after performing collision calculations record = ModelLibrarian( library="models_full.json").get_record( objects[obj_count][0]) self.communicate({ "$type": "add_object", "name": objects[obj_count][0], "url": record.get_url(), "scale_factor": record.scale_factor, "position": valid_obj_pos, "rotation": { "x": 0, "y": 0, "z": 0 }, "category": record.wcategory, "id": obj_count }) # Returns bound data for added object bounds_data = self.communicate({ "$type": "send_bounds", "frequency": "once" }) # Appends object, with information on position and obj_radius, to positions_list # Length of buffer array should be 1 print("Bounds Data:", bounds_data) for b in bounds_data[:-1]: print("Buffer Loop:", b) b_id = OutputData.get_data_type_id(b) if b_id == "boun": print("BOUNDS") o = Bounds(b) print("# of Objects:", o.get_num()) print("# of Failed Attempts:", obj_overlaps) print("Buffer Array:", b) print("Bounds Object:", o) for i in range(o.get_num()): print("Object ID:", o.get_id(i)) print("obj_count:", obj_count) print("Object:", objects[obj_count][0], "Category:", objects[obj_count][1]) print("Object Center:", o.get_center(i)) # Only want to compute valid_position for object we are about to add # Skip any computation if this is not the case if o.get_id(i) != obj_count: continue # Useful for detecting if object fits in environment print( "Calculating if object fits in environment" ) width = distance.euclidean( o.get_left(i), o.get_right(i)) depth = distance.euclidean( o.get_front(i), o.get_back(i)) height = distance.euclidean( o.get_top(i), o.get_bottom(i)) print("Width:", width) print("Depth:", depth) print("Height:", height) # Useful for avoiding object overlap print("Calculating Object Bounds") center_to_top = distance.euclidean( o.get_center(i), o.get_top(i)) center_to_bottom = distance.euclidean( o.get_center(i), o.get_bottom(i)) center_to_left = distance.euclidean( o.get_center(i), o.get_left(i)) center_to_right = distance.euclidean( o.get_center(i), o.get_right(i)) center_to_front = distance.euclidean( o.get_center(i), o.get_front(i)) center_to_back = distance.euclidean( o.get_center(i), o.get_back(i)) # Max object radius (center to diagonal of bounding box) obj_radius = \ max(math.sqrt(center_to_top ** 2 + center_to_left ** 2 + center_to_front ** 2), math.sqrt(center_to_top ** 2 + center_to_right ** 2 + center_to_front ** 2), math.sqrt(center_to_top ** 2 + center_to_left ** 2 + center_to_back ** 2), math.sqrt(center_to_top ** 2 + center_to_right ** 2 + center_to_back ** 2), math.sqrt(center_to_bottom ** 2 + center_to_left ** 2 + center_to_front ** 2), math.sqrt(center_to_bottom ** 2 + center_to_right ** 2 + center_to_front ** 2), math.sqrt(center_to_bottom ** 2 + center_to_left ** 2 + center_to_back ** 2), math.sqrt(center_to_bottom ** 2 + center_to_right ** 2 + center_to_back ** 2)) print("Obj_Radius:", obj_radius) # Set sweeping radius, based on scene plane dimensions l_radius = random.uniform( 0, min(0.9 * scene_dimensions[1][0] / 2, 0.9 * scene_dimensions[1][2] / 2)) # Checking that object fits in scene viewing if (width > min(0.7 * scene_dimensions[1][0], 0.7 * scene_dimensions[1][2]) or depth > min( 0.7 * scene_dimensions[1][0], 0.7 * scene_dimensions[1][2]) or height > 0.7 * scene_dimensions[1][1]): print("Object does not fit in scene") self.communicate([{ "$type": "send_images", "frequency": "never" }, { "$type": "destroy_object", "id": obj_count }]) # Ensures next attempt to load in item is not the same item as before random.shuffle(objects) break # Not possible to find valid object position -- too many overlapping objects elif (not self._get_object_position( scene_dimensions=scene_dimensions, object_positions=positions_list, object_to_add_radius=obj_radius, max_tries=20, location_radius=l_radius)[0]): print( "Could not calculate valid object location" ) self.communicate([{ "$type": "send_images", "frequency": "never" }, { "$type": "destroy_object", "id": obj_count }]) obj_overlaps += 1 # Ensures next attempt to load in item is not the same item as before random.shuffle(objects) break # Find appropriate, non-overlapping object position # Reset object position to the valid position else: print("Object fits in scene") valid_obj_pos = self._get_object_position( scene_dimensions=scene_dimensions, object_positions=positions_list, object_to_add_radius=obj_radius, max_tries=20, location_radius=l_radius)[1] print("Position calculated") positions_list.append( ObjectPosition(valid_obj_pos, obj_radius)) self.communicate([{ "$type": "send_images", "frequency": "never" }, { "$type": "destroy_object", "id": obj_count }]) print("Object ready to reset") self.communicate([{ "$type": "send_images", "frequency": "never" }, { "$type": "add_object", "name": objects[obj_count][0], "url": record.get_url(), "scale_factor": record.scale_factor, "position": valid_obj_pos, "rotation": { "x": 0, "y": 0, "z": 0 }, "category": record.wcategory, "id": obj_count }]) print("Object reset") # Rotate the object randomly print("Rotating object") self.communicate({ "$type": "rotate_object_by", "angle": random.uniform(-45, 45), "axis": "yaw", "id": obj_count, "is_world": True }) # Don't rotate the object if doing so will result in overlap into scene if not (o.get_bottom(i)[1] < 0 or o.get_top(i)[1] > 0.9 * scene_dimensions[1][1]): pitch_angle = random.uniform(-45, 45) self.communicate({ "$type": "rotate_object_by", "angle": pitch_angle, "axis": "pitch", "id": obj_count, "is_world": True }) roll_angle = random.uniform(-45, 45) self.communicate({ "$type": "rotate_object_by", "angle": roll_angle, "axis": "roll", "id": obj_count, "is_world": True }) # Setting random materials/textures # Looping through sub-objects and materials sub_count = 0 for sub_object in record.substructure: # Loop through materials in sub-objects for j in range(len(sub_object)): # Get random material and load in material = random.choice( materials[1:]) self.load_material(material) print("Material loaded") # Set random material on material of sub-object self.communicate({ "$type": "set_visual_material", "material_index": j, "material_name": material[0], "object_name": sub_object['name'], "id": obj_count }) print("Material set") sub_count += 1 if sub_count > 10: break break print("Updating count") obj_count += 1 print("Breaking out of object_id loop") break # Break out of buffer loop print("Breaking out of buffer loop") break # Move onto next iteration of while loop (next object to load in) print("Object added - next while loop iteration") continue # Enable image capture self.communicate({ "$type": "set_pass_masks", "avatar_id": "avatar", "pass_masks": ["_img", "_id"] }) self.communicate({ "$type": "send_images", "frequency": "always" }) # Capture scene scene_data = self.communicate({ "$type": "look_at_position", "avatar_id": "avatar", "position": { "x": 0, "y": scene_dimensions[0][1] / 2, "z": 0 } }) images = Images(scene_data[0]) TDWUtils.save_images(images, TDWUtils.zero_padding(i), output_directory=path)
def run(self): self.start() init_setup_commands = [{ "$type": "set_screen_size", "width": 1280, "height": 962 }, { "$type": "set_render_quality", "render_quality": 5 }] self.communicate(init_setup_commands) # Create an empty room. self.load_streamed_scene(scene="tdw_room_2018") # Disable physics. self.communicate({"$type": "set_gravity", "value": False}) # Add the avatar. self.communicate( TDWUtils.create_avatar(position={ "x": -3.75, "y": 1.7, "z": -5.5 }, look_at=TDWUtils.array_to_vector3( [-3.75, 1.5, -10]), avatar_id="avatar")) self.communicate({ "$type": "set_field_of_view", "field_of_view": 78.0, "avatar_id": "avatar" }) self.add_object(model_name="b03_ka90ivi20r_2013__vray", position={ "x": -5.5, "y": 0, "z": -9 }, rotation={ "x": 0, "y": 90, "z": 0 }, library="models_full.json") table_id = self.add_object(model_name="quatre_dining_table", position={ "x": -3, "y": 0, "z": -9 }, rotation={ "x": 0, "y": 90, "z": 0 }, library="models_full.json") table_bounds = self.get_bounds_data(table_id) top = table_bounds.get_top(0) self.add_object(model_name="stua_onda_stool_max2012", position={ "x": -3, "y": 0, "z": -10 }, library="models_full.json") self.add_object(model_name="stua_onda_stool_max2012", position={ "x": -3, "y": 0, "z": -8 }, rotation={ "x": 0, "y": 180, "z": 0 }, library="models_full.json") # Enable image capture self.communicate({ "$type": "set_pass_masks", "avatar_id": "avatar", "pass_masks": ["_img", "_id"] }) self.communicate({"$type": "send_images", "frequency": "always"}) scene_data = self.communicate({ "$type": "look_at_position", "avatar_id": "avatar", "position": TDWUtils.array_to_vector3([-3.75, 1.5, -10]) }) images = Images(scene_data[0]) TDWUtils.save_images(images, "kitchen1", output_directory="replicated_images")
def run(self): # Setting up image directory output_directory = "multiple_objects_pics" parent_dir = '/Python/Leonard/multiple_objects' path = os.path.join(parent_dir, output_directory) print(path) if os.path.exists(path): shutil.rmtree(path) time.sleep(0.5) os.mkdir(path) # Initialize scene self.start() # Load in scene scene_lib = SceneLibrarian() scene_record = scene_lib.get_record("tdw_room_2018") self.communicate({"$type": "add_scene", "name": scene_record.name, "url": scene_record.get_url()}) # Resize screen; note render_quality 5 is best self.communicate([{"$type": "set_screen_size", "width": 1280, "height": 962}, {"$type": "set_render_quality", "render_quality": 2}]) # Toggle physics physics_on = True self.communicate({"$type": "simulate_physics", "value": physics_on}) # Add in coffee table object table_id = self.add_object("live_edge_coffee_table", position={"x": 1.5, "y": 0, "z": 1.5}, rotation={"x": 0, "y": 0, "z": 0}) # Add two bowls on top of the table bowl_1 = self.add_object(model_name="b04_bowl_smooth", position={"x": 1.25, "y": 0.331, "z": 1.5}, rotation=TDWUtils.VECTOR3_ZERO, library="models_full.json") self.add_object(model_name="b04_bowl_smooth", position={"x": 1.75, "y": 0.331, "z": 1.5}, rotation=TDWUtils.VECTOR3_ZERO, library="models_full.json") # Push one bowl towards the other self.communicate({"$type": "apply_force_to_object", "force": {"x": 5, "y": 0, "z": 0}, "id": bowl_1}) # Load in avatar self.communicate(TDWUtils.create_avatar(avatar_id="avatar", position={"x": 0, "y": 1.5, "z": 0}, look_at={"x": 1.5, "y": 0, "z": 1.5})) # Necessary commands to avoid error in calling save_images() # Second command enables image capture self.communicate({"$type": "set_pass_masks", "avatar_id": "avatar", "pass_masks": ["_img", "_id"]}) self.communicate({"$type": "send_images", "frequency": "always"}) # Capture images for i in range(20): resp = self.communicate({"$type": "look_at", "avatar_id": "avatar", "object_id": table_id, "use_centroid": True}) images = Images(resp[0]) TDWUtils.save_images(images, TDWUtils.zero_padding(i), output_directory=path)
def run(self): # Setting up image directory output_directory = "rotating_guitar_images" parent_dir = '/Users/leonard/Desktop/TDWBase-1.5.0/Python/Leonard/rotating_guitar' path = os.path.join(parent_dir, output_directory) print(path) if os.path.exists(path): shutil.rmtree(path) time.sleep(0.5) os.mkdir(path) # Initialize scene self.start() init_setup_commands = [{ "$type": "set_screen_size", "width": 1280, "height": 962 }, { "$type": "set_render_quality", "render_quality": 5 }] self.communicate(init_setup_commands) scene_lib = SceneLibrarian() scene_record = scene_lib.get_record("tdw_room_2018") self.communicate({ "$type": "add_scene", "name": scene_record.name, "url": scene_record.get_url() }) self.communicate({"$type": "set_gravity", "value": True}) self.communicate({"$type": "simulate_physics", "value": True}) self.communicate( TDWUtils.create_avatar(avatar_id="avatar", position={ "x": 3, "y": 1.5, "z": 3 }, look_at={ "x": 0, "y": 0.8, "z": 0 })) guitar_id = self.add_object(model_name="b01_shovel", position={ "x": 0, "y": 1, "z": 0 }, rotation={ "x": 0, "y": 0, "z": 0 }, library="models_full.json") self.communicate({ "$type": "rotate_object_by", "angle": -15.0, "id": guitar_id, "axis": "roll", "is_world": True }) self.communicate({ "$type": "set_pass_masks", "avatar_id": "avatar", "pass_masks": ["_img", "_id"] }) self.communicate({"$type": "send_images", "frequency": "always"}) for i in range(50): resp = self.communicate({ "$type": "look_at_position", "avatar_id": "avatar", "position": { "x": 0, "y": 0.8, "z": 0 } }) images = Images(resp[0]) TDWUtils.save_images(images, TDWUtils.zero_padding(i), output_directory=path)
def run(self): self.start() init_setup_commands = [{ "$type": "set_screen_size", "width": 600, "height": 480 }, { "$type": "set_render_quality", "render_quality": 5 }] self.communicate(init_setup_commands) # Create an empty room. self.communicate(TDWUtils.create_empty_room(10, 10)) # Disable physics. self.communicate({"$type": "set_gravity", "value": False}) # Add the avatar. self.communicate( TDWUtils.create_avatar(position={ "x": -4, "y": 1.7, "z": 0 }, look_at=TDWUtils.array_to_vector3( [0, 1.0, 0]), avatar_id="avatar")) self.communicate({ "$type": "set_field_of_view", "field_of_view": 68.0, "avatar_id": "avatar" }) self.add_object(model_name="b05_zebra", position={ "x": 0.2, "y": 0, "z": 2.0 }, library="models_full.json") self.add_object(model_name="735737_zebra", position={ "x": 2, "y": 0, "z": 0 }, rotation={ "x": 0, "y": 30, "z": 0 }, library="models_full.json") # Enable image capture self.communicate({ "$type": "set_pass_masks", "avatar_id": "avatar", "pass_masks": ["_img", "_id"] }) self.communicate({"$type": "send_images", "frequency": "always"}) scene_data = self.communicate({ "$type": "look_at_position", "avatar_id": "avatar", "position": TDWUtils.array_to_vector3([0, 1.0, 0]) }) images = Images(scene_data[0]) TDWUtils.save_images( images, "zebras", output_directory= "/Users/leonard/Desktop/TDWBase-1.5.0/Python/Leonard/compare_COCO_TDW/replicated_images/exterior" )
def run(self): self.start() self.communicate(TDWUtils.create_empty_room(12, 12)) # Find the local asset bundle for this platform. url = "file:///" + str( Path("composite_objects/" + platform.system() + "/test_composite_object").resolve()) # Add the local object. o_id = self.get_unique_id() self.communicate([{ "$type": "add_object", "name": "test_composite_object", "url": url, "scale_factor": 1, "id": o_id }, { "$type": "set_mass", "id": o_id, "mass": 100 }]) self.communicate( TDWUtils.create_avatar(position={ "x": 0, "y": 1.49, "z": -2.77 })) # Test that each sub-object has a unique segmentation color. resp = self.communicate({ "$type": "send_segmentation_colors", "ids": [] }) colors = SegmentationColors(resp[0]) segmentation_colors = [] # There are 4 objects: The parent object, the motor, the "base" of the motor, and the light. assert colors.get_num() == 4, colors.get_num() print("Segmentation colors:") for i in range(colors.get_num()): print(colors.get_object_id(i), colors.get_object_color(i)) # Cache the color for the next test. segmentation_colors.append(colors.get_object_color(i)) # Test that each sub-object is visible to the avatar. resp = self.communicate({ "$type": "send_id_pass_segmentation_colors", "frequency": "once" }) colors = IdPassSegmentationColors(resp[0]) # There are three visible objects (the light won't be visible in the _id pass). assert colors.get_num_segmentation_colors() == 3 print("\nObserved colors:") # Test that the colors observed by the avatar are in the cache. for i in range(colors.get_num_segmentation_colors()): observed_color = colors.get_segmentation_color(i) assert observed_color in segmentation_colors print(observed_color) # Get composite objects data. resp = self.communicate({"$type": "send_composite_objects"}) assert len(resp) > 1 assert OutputData.get_data_type_id(resp[0]) == "comp" o = CompositeObjects(resp[0]) assert o.get_num() == 1 # There are 3 sub-objects: The motor, the "base" of the motor, and the light. assert o.get_num_sub_objects(0) == 3 print("\nCompositeObjects: ") commands = [] lights = [] # Iterate through each sub-object. for s in range(o.get_num_sub_objects(0)): sub_object_id = o.get_sub_object_id(0, s) s_type = o.get_sub_object_machine_type(0, s) print(sub_object_id, s_type) # Add commands depending on the type of sub-object. if s_type == "motor": # Start the motor. commands.append({ "$type": "set_motor", "target_velocity": 500, "force": 500, "id": sub_object_id }) elif s_type == "light": commands.append({ "$type": "set_sub_object_light", "is_on": True, "id": sub_object_id }) lights.append(sub_object_id) self.communicate(commands) is_on = True for i in range(1000): commands = [] # Every 50 frames, blink the lights on and off. if i % 50 == 0: is_on = not is_on for light in lights: commands.append({ "$type": "set_sub_object_light", "is_on": is_on, "id": light }) else: commands.append({"$type": "do_nothing"}) self.communicate(commands)
def run(self): # Instantiate the librarians. self.humanoid_animation_librarian = HumanoidAnimationLibrarian() self.humanoid_librarian = HumanoidLibrarian() # Get the records. humanoid_record = self.humanoid_librarian.get_record("man_suit") wading_record = self.humanoid_animation_librarian.get_record( "wading_through_water") fencing_record = self.humanoid_animation_librarian.get_record( "fencing") h_id = 0 # Load the streamed scene. self.load_streamed_scene(scene="tdw_room") # Create the avatar. self.communicate( TDWUtils.create_avatar(position={ "x": -4.42, "y": 1.5, "z": 5.95 }, look_at={ "x": 0, "y": 1.0, "z": -3 })) # Make Unity's framerate match that of the animation clip. self.communicate({ "$type": "set_target_framerate", "framerate": wading_record.framerate }) self.communicate([{ "$type": "add_humanoid", "name": humanoid_record.name, "position": { "x": 0, "y": 0, "z": -4 }, "url": humanoid_record.get_url(), "id": h_id }, { "$type": "add_humanoid_animation", "name": wading_record.name, "url": wading_record.get_url() }, { "$type": "play_humanoid_animation", "name": wading_record.name, "id": h_id }]) # First, play the "wading_through_water" animation, which is looped. We can test for this: if wading_record.loop: num_loops = 4 else: num_loops = 1 for i in range(num_loops): self.communicate([]) self.communicate({ "$type": "play_humanoid_animation", "name": wading_record.name, "id": h_id }) frame = 0 num_frames = wading_record.get_num_frames() while frame < num_frames: self.communicate([]) frame += 1 # Now play the regular, non-looped "fencing" animation # Make Unity's framerate match that of the animation clip. self.communicate({ "$type": "set_target_framerate", "framerate": fencing_record.framerate }) self.communicate([{ "$type": "add_humanoid_animation", "name": fencing_record.name, "url": fencing_record.get_url() }, { "$type": "play_humanoid_animation", "name": fencing_record.name, "id": h_id }]) frame = 0 num_frames = fencing_record.get_num_frames() while frame < num_frames: self.communicate([]) frame += 1
def run(self): print("W, up-arrow = Move forward") print("S, down-arrow = Move backward") print("A, left-arrow = Turn counterclockwise") print("D, right-arrow = Turn clockwise") print("Esc = Quit") self.start() # Create the room. commands = [TDWUtils.create_empty_room(12, 12)] # Create the avatar. commands.extend( TDWUtils.create_avatar(avatar_type="A_Img_Caps", avatar_id=self.avatar_id)) # 1. Set high drag values so it doesn't feel like the avatar is sliding on ice. # 2. Set the room's floor material. # 3. Request keyboard input. commands.extend([{ "$type": "set_avatar_drag", "drag": 10, "angular_drag": 20, "avatar_id": self.avatar_id }, self.get_add_material("parquet_alternating_orange", library="materials_high.json"), { "$type": "set_proc_gen_floor_material", "name": "parquet_alternating_orange" }, { "$type": "set_proc_gen_floor_texture_scale", "scale": { "x": 8, "y": 8 } }]) self.communicate(commands) self.listen(key="W", commands=self.move(direction=1), events=["press", "hold"]) self.listen(key="UpArrow", commands=self.move(direction=1), events=["press", "hold"]) self.listen(key="S", commands=self.move(direction=-1), events=["press", "hold"]) self.listen(key="DownArrow", commands=self.move(direction=1), events=["press", "hold"]) self.listen(key="A", commands=self.turn(direction=-1), events=["press", "hold"]) self.listen(key="LeftArrow", commands=self.turn(direction=-1), events=["press", "hold"]) self.listen(key="S", commands=self.turn(direction=1), events=["press", "hold"]) self.listen(key="RightArrow", commands=self.turn(direction=1), events=["press", "hold"]) self.listen(key="Escape", function=self.stop, events=["press"]) while not self.done: self.communicate([])
def run(self): self.start() init_setup_commands = [{ "$type": "set_screen_size", "width": 600, "height": 480 }, { "$type": "set_render_quality", "render_quality": 5 }] self.communicate(init_setup_commands) # Create an empty room. self.communicate({ "$type": "create_empty_environment", "center": { "x": 0, "y": 0, "z": 0 }, "bounds": { "x": 8, "y": 8, "z": 8 } }) self.communicate({"$type": "set_gravity", "value": False}) cube_record = ModelLibrarian("models_special.json").get_record( "prim_cube") self.communicate({ "$type": "add_object", "name": "prim_cube", "url": cube_record.get_url(), "scale_factor": cube_record.scale_factor, "position": { "x": 0, "y": 0, "z": 0 }, "rotation": { "x": 0, "y": 0, "z": 0 }, "category": cube_record.wcategory, "id": 1 }) self.communicate({ "$type": "scale_object", "scale_factor": { "x": 30, "y": 0.0001, "z": 30 }, "id": 1 }) # Add the avatar. self.communicate( TDWUtils.create_avatar(position={ "x": 0, "y": 0.6, "z": 0 }, look_at=TDWUtils.array_to_vector3( [0.5, 0.5, 0]), avatar_id="avatar")) self.communicate(self.get_add_hdri_skybox("table_mountain_1_4k")) self.communicate({"$type": "rotate_hdri_skybox_by", "angle": 90}) lib = MaterialLibrarian(library="materials_med.json") record = lib.get_record("bricks_chatham_gray_used") self.communicate({ "$type": "add_material", "name": "bricks_chatham_gray_used", "url": record.get_url() }) self.communicate( TDWUtils.set_visual_material(c=self, substructure=cube_record.substructure, object_id=1, material="bricks_chatham_gray_used")) # self.communicate({"$type": "set_field_of_view", # "field_of_view": 68.0, # "avatar_id": "avatar"}) bench = self.add_object(model_name="b04_wood_metal_park_bench", position={ "x": 2, "y": 0, "z": 0.5 }, rotation={ "x": 0, "y": -90, "z": 0 }, library="models_full.json") self.add_object(model_name="b04_wood_metal_park_bench", position={ "x": 5, "y": 0, "z": 0.5 }, rotation={ "x": 0, "y": -90, "z": 0 }, library="models_full.json") bench_bounds = self.get_bounds_data(bench) top = bench_bounds.get_top(0) self.add_object(model_name="cgaxis_models_65_06_vray", position={ "x": 1.8, "y": top[1] - 0.42, "z": 0.35 }, rotation={ "x": 0, "y": 0, "z": 0 }, library="models_full.json") # Enable image capture self.communicate({ "$type": "set_pass_masks", "avatar_id": "avatar", "pass_masks": ["_img", "_id"] }) self.communicate({"$type": "send_images", "frequency": "always"}) scene_data = self.communicate({ "$type": "look_at_position", "avatar_id": "avatar", "position": TDWUtils.array_to_vector3([0.5, 0.5, 0]) }) images = Images(scene_data[0]) TDWUtils.save_images( images, "bench_book", output_directory= "/Users/leonard/Desktop/TDWBase-1.5.0/Python/Leonard/compare_COCO_TDW/replicated_images/exterior" )
def run(self) -> None: robot_id = 0 self.start() # Create the scene. Add a robot. # Request static robot data for this frame only. # Request dynamic robot data per frame. commands = [ TDWUtils.create_empty_room(12, 12), self.get_add_robot(name="ur3", robot_id=robot_id), { "$type": "send_static_robots", "frequency": "once" }, { "$type": "send_robots", "frequency": "always" } ] # Add an avatar to render the scene (just for demo purposes). commands.extend( TDWUtils.create_avatar(look_at=TDWUtils.VECTOR3_ZERO, position={ "x": -0.881, "y": 0.836, "z": -1.396 })) resp = self.communicate(commands) # Parse the output data for static robot data. static_robot: Optional[StaticRobot] = None for i in range(len(resp) - 1): r_id = OutputData.get_data_type_id(resp[i]) if r_id == "srob": r = StaticRobot(resp[i]) if r.get_id() == robot_id: static_robot = r break assert static_robot is not None, f"No static robot data: {resp}" # Get the IDs of the shoulder and the elbow. body_part_ids: Dict[str, int] = dict() for i in range(static_robot.get_num_joints()): b_id = static_robot.get_joint_id(i) b_name = static_robot.get_joint_name(i) body_part_ids[b_name] = b_id assert "Shoulder" in body_part_ids assert "Elbow" in body_part_ids # Rotate the shoulder and the elbow for two motions. # The values in this array are for the angle that the [shoulder, elbow] should rotate to per action. # For more complex actions, you will probably want to organize your commands differently. for angles in [[70, 90], [-30, -25]]: resp = self.communicate([{ "$type": "set_revolute_target", "id": robot_id, "joint_id": body_part_ids["Shoulder"], "target": angles[0] }, { "$type": "set_revolute_target", "id": robot_id, "joint_id": body_part_ids["Elbow"], "target": angles[1] }]) # Get the robot output data. robot = self.get_robot(resp=resp) angles_0 = self.get_joint_angles(robot=robot) # Wait for the joints to stop moving. moving = True while moving: robot = self.get_robot(resp=self.communicate([])) angles_1 = self.get_joint_angles(robot=robot) # Compare the current joint angles to the angles of the previous frame. moving = False for angle_0, angle_1 in zip(angles_0, angles_1): if np.abs(angle_0 - angle_1) > 0.001: moving = True break angles_0 = angles_1
"y": 0, "z": 0 } }, { "$type": "send_static_robots", "ids": [robot_id], "frequency": "once" } ] # Add a camera to the scene. commands.extend( TDWUtils.create_avatar(position={ "x": -2.49, "y": 4, "z": 0 }, look_at={ "x": 0, "y": 0, "z": 0 })) resp = c.communicate(commands) wheel_ids = [] for i in range(len(resp) - 1): r_id = OutputData.get_data_type_id(resp[i]) if r_id == "srob": sr = StaticRobot(resp[i]) for j in range(sr.get_num_joints()): joint_id = sr.get_joint_id(j) joint_name = sr.get_joint_name(j) print(joint_name, joint_id) # Find all of the wheels.
def run(self): self.load_streamed_scene(scene="tdw_room_2018") # Create the container, set up for fluids, and a "hose pipe" stream in particular. self.communicate({ "$type": "create_flex_container", "collision_distance": 0.05, "static_friction": 0.1, "dynamic_friction": 0.1, "particle_friction": 0.1, "solid_rest": 0.1, "fluid_rest": 0.095, "viscocity": 0, "cohesion": 0.02, "surface_tension": 0.01, "radius": 0.25, "damping": 0, "substep_count": 2, "iteration_count": 5, "buoyancy": 1.0, "anisotropy_scale": 1.5, "max_particles": 15000, "max_neighbors": 200 }) self.communicate({"$type": "set_time_step", "time_step": 0.02}) # Create the avatar. self.communicate( TDWUtils.create_avatar(position={ "x": 3.6, "y": 1.8, "z": 1.3 }, look_at={ "x": 0, "y": 1.35, "z": -2 })) # Add the fluid source object, rotated so the fluid hits the wall. self.fluid_id = self.get_unique_id() self.communicate([{ "$type": "load_flex_fluid_source_from_resources", "id": self.fluid_id, "orientation": { "x": 44, "y": 0, "z": 0 }, "position": { "x": 0, "y": 1.0, "z": 0 } }, { "$type": "scale_object", "id": self.fluid_id, "scale_factor": { "x": 0.4, "y": 0.4, "z": 1 } }]) # Create the fluid source actor, assign its container and set the Flex object scale to a reasonable size for a water jet. self.communicate([ { "$type": "create_flex_fluid_source_actor", "id": self.fluid_id, "mass_scale": 1.0, "start_speed": 5.0, "lifetime": 3.0, "mesh_tesselation": 2 }, { "$type": "assign_flex_container", "id": self.fluid_id, "container_id": 0, "fluid_container": True }, ]) # Start an infinite loop to allow the build to simulate physics. while True: self.communicate({"$type": "step_physics", "frames": 1})
color = {"r": 1, "g": 0, "b": 0, "a": 1} commands.append({ "$type": "add_position_marker", "position": { "x": x, "y": 0, "z": z }, "scale": 0.3, "color": color }) # Create an overhead camera and capture an image. commands.extend( TDWUtils.create_avatar(position={ "x": 0, "y": 31, "z": 0 }, look_at=TDWUtils.VECTOR3_ZERO)) commands.extend([{ "$type": "set_pass_masks", "pass_masks": ["_img"] }, { "$type": "send_images" }]) resp = c.communicate(commands) # Save the image. images = get_data(resp=resp, d_type=Images) TDWUtils.save_images(images=images, filename=f"{scene}_{layout}", output_directory=output_dir, append_pass=False)
def run(self): self.start() init_setup_commands = [{ "$type": "set_screen_size", "width": 1280, "height": 962 }, { "$type": "set_render_quality", "render_quality": 5 }] self.communicate(init_setup_commands) # Create an empty room. self.load_streamed_scene(scene="tdw_room_2018") # Disable physics. self.communicate({"$type": "set_gravity", "value": False}) # Add the avatar. self.communicate( TDWUtils.create_avatar(position={ "x": 2.5, "y": 1.2, "z": 0 }, look_at=TDWUtils.array_to_vector3( [0, 0.7, 0]), avatar_id="avatar")) self.communicate({ "$type": "set_field_of_view", "field_of_view": 64.0, "avatar_id": "avatar" }) table_id = self.add_object(model_name="quatre_dining_table", position={ "x": 0, "y": 0, "z": 0 }, library="models_full.json") table_bounds = self.get_bounds_data(table_id) top = table_bounds.get_top(0) self.add_object(model_name="orchid_3_2010", position={ "x": 0, "y": top[1], "z": 0 }, library="models_full.json") self.add_object(model_name="coffeemug", position={ "x": -0.3, "y": top[1], "z": 0.2 }, library="models_full.json") self.add_object(model_name="coffeemug", position={ "x": 0.13, "y": top[1], "z": -0.19 }, library="models_full.json") # Enable image capture self.communicate({ "$type": "set_pass_masks", "avatar_id": "avatar", "pass_masks": ["_img", "_id"] }) self.communicate({"$type": "send_images", "frequency": "always"}) scene_data = self.communicate({ "$type": "look_at_position", "avatar_id": "avatar", "position": TDWUtils.array_to_vector3([0, 0.7, 0]) }) images = Images(scene_data[0]) TDWUtils.save_images(images, "mugs_and_flowers", output_directory="replicated_images")
def run(self): self.start() init_setup_commands = [{"$type": "set_screen_size", "width": 640, "height": 480}, {"$type": "set_render_quality", "render_quality": 5}] self.communicate(init_setup_commands) self.communicate({"$type": "create_empty_environment", "center": {"x": 0, "y": 0, "z": 0}, "bounds": {"x": 15, "y": 15, "z": 15}}) # Disable physics. self.communicate({"$type": "set_gravity", "value": False}) # Add the avatar. self.communicate(TDWUtils.create_avatar(position={"x": -1.5, "y": 1.4, "z": 0.6}, look_at=TDWUtils.array_to_vector3([3, 0.5, 0.5]), avatar_id="avatar")) self.communicate(self.get_add_hdri_skybox("table_mountain_1_4k")) self.communicate({"$type": "set_field_of_view", "field_of_view": 68.0, "avatar_id": "avatar"}) cube_record = ModelLibrarian("models_special.json").get_record("prim_cube") self.communicate({"$type": "add_object", "name": "prim_cube", "url": cube_record.get_url(), "scale_factor": cube_record.scale_factor, "position": {"x": 0, "y": 0, "z": 0}, "rotation": {"x": 0, "y": 0, "z": 0}, "category": cube_record.wcategory, "id": 1}) self.communicate({"$type": "scale_object", "scale_factor": {"x": 30, "y": 0.0001, "z": 30}, "id": 1}) grass_record = MaterialLibrarian("materials_high.json").get_record("grass_countryside") self.communicate({"$type": "add_material", "name": "grass_countryside", "url": grass_record.get_url()}) self.communicate(TDWUtils.set_visual_material(c=self, substructure=cube_record.substructure, object_id=1, material="grass_countryside")) self.add_object(model_name="b03_horse", position={"x": 3, "y": 0.0001, "z": 0.5}, rotation={"x": 0, "y": 0, "z": 0}, library="models_full.json") bird = self.add_object(model_name="polysurface63", position={"x": 1.2, "y": 0.0001, "z": 1}, rotation={"x": 0, "y": 0, "z": 0}, library="models_full.json") self.communicate({"$type": "scale_object", "scale_factor": {"x": 2, "y": 2, "z": 2}, "id": bird}) bird = self.add_object(model_name="b03_realistic_pigeon_max", position={"x": 1.8, "y": 0.0001, "z": 0.45}, rotation={"x": 0, "y": -95, "z": 0}, library="models_full.json") self.communicate({"$type": "scale_object", "scale_factor": {"x": 2, "y": 2, "z": 2}, "id": bird}) bird = self.add_object(model_name="rockdove_polysurface77", position={"x": 1, "y": 0.0001, "z": 0.7}, rotation={"x": 0, "y": -80, "z": 0}, library="models_full.json") self.communicate({"$type": "scale_object", "scale_factor": {"x": 2, "y": 2, "z": 2}, "id": bird}) # Enable image capture self.communicate({"$type": "set_pass_masks", "avatar_id": "avatar", "pass_masks": ["_img", "_id"]}) self.communicate({"$type": "send_images", "frequency": "always"}) scene_data = self.communicate({"$type": "look_at_position", "avatar_id": "avatar", "position": TDWUtils.array_to_vector3([3, 0.5, 0])}) images = Images(scene_data[0]) TDWUtils.save_images(images, "bird2", output_directory="/Users/leonard/Desktop/TDWBase-1.5.0/Python/Leonard/compare_COCO_TDW/replicated_images/exterior")
def run(self, num_trials: int): """ Build a "Rube Goldberg" machine to produce impact sounds. """ # Load the photorealistic "archviz_house" environment. self.load_streamed_scene(scene="archviz_house") # Organize all initialization commands into a single list. # Set global values, including the desired screen size and aspect ratio (720P). # Frame rate is set to 60 fps to facilitate screen / video recording. init_commands = [{"$type": "set_render_quality", "render_quality": 5}, {"$type": "set_screen_size", "width": 1280, "height": 720}, {"$type": "set_target_framerate", "framerate": 60}, {"$type": "set_time_step", "time_step": 0.02}] # Create the avatar. init_commands.extend(TDWUtils.create_avatar(avatar_type="A_Img_Caps_Kinematic", position={"x": -15.57, "y": 1.886, "z": -4.97})) # Aim the avatar camera to frame the desired view. init_commands.extend([{"$type": "rotate_sensor_container_by", "axis": "yaw", "angle": 109.13, "sensor_name": "SensorContainer", "avatar_id": "a"}, {"$type": "rotate_sensor_container_by", "axis": "pitch", "angle": 6.36, "sensor_name": "SensorContainer", "avatar_id": "a"}]) # Add the audio sensor. init_commands.extend([{"$type": "add_audio_sensor", "avatar_id": "a"}]) # Adjust post-processing settings. init_commands.extend([{"$type": "set_post_exposure", "post_exposure": 0.35}, {"$type": "set_screen_space_reflections", "enabled": True}, {"$type": "set_vignette", "enabled": False}, {"$type": "set_ambient_occlusion_intensity", "intensity": 0.175}, {"$type": "set_ambient_occlusion_thickness_modifier", "thickness": 5.0}]) # Set the shadow strength to maximum. init_commands.extend([{"$type": "set_shadow_strength", "strength": 1.0}]) # Send all of the initialization commands. self.communicate(init_commands) for i in range(num_trials): self.do_trial()
def run(self): self.start() init_setup_commands = [{ "$type": "set_screen_size", "width": 1280, "height": 962 }, { "$type": "set_render_quality", "render_quality": 5 }] self.communicate(init_setup_commands) width = 8 length = 8 # Create an empty room. self.communicate(TDWUtils.create_empty_room(width, length)) # Disable physics. self.communicate({"$type": "set_gravity", "value": False}) # Add the avatar. self.communicate( TDWUtils.create_avatar(position={ "x": 3, "y": 1.7, "z": 2 }, look_at={ "x": 0, "y": 0.8, "z": 2 }, avatar_id="avatar")) self.communicate({ "$type": "set_field_of_view", "field_of_view": 68.0, "avatar_id": "avatar" }) self.add_object(model_name="b05_bed_giorgetti_rea", position={ "x": 0.3, "y": 0, "z": 2.2 }, rotation={ "x": 0, "y": 180, "z": 0 }, library="models_full.json") # Create the dining table. # self.dining_table() # Create the sofa. self.sofa(width, length) # Bake the NavMesh. self.communicate({"$type": "bake_nav_mesh"}) print('mesh') # Get a random position on the NavMesh. position = TDWUtils.get_random_position_on_nav_mesh( self, width, length) position = TDWUtils.array_to_vector3(position) # Add a side table. self.side_table(position) position = TDWUtils.get_random_position_on_nav_mesh( self, width, length) position = TDWUtils.array_to_vector3(position) self.add_object(model_name="648972_chair_poliform_harmony", position=position, library="models_full.json") # Enable image capture self.communicate({ "$type": "set_pass_masks", "avatar_id": "avatar", "pass_masks": ["_img", "_id"] }) self.communicate({"$type": "send_images", "frequency": "always"}) scene_data = self.communicate({ "$type": "look_at_position", "avatar_id": "avatar", "position": { "x": 0, "y": 0.8, "z": 2 } }) images = Images(scene_data[0]) TDWUtils.save_images(images, "bedroom1", output_directory="replicated_images")
def run(self): self.start() init_setup_commands = [{ "$type": "set_screen_size", "width": 640, "height": 480 }, { "$type": "set_render_quality", "render_quality": 5 }] self.communicate(init_setup_commands) self.communicate(TDWUtils.create_empty_room(8, 8)) # Disable physics. self.communicate({"$type": "set_gravity", "value": False}) # Add the avatar. self.communicate( TDWUtils.create_avatar(position={ "x": 1.6, "y": 1.6, "z": 0.35 }, look_at=TDWUtils.array_to_vector3( [3, 0.5, 0.35]), avatar_id="avatar")) self.communicate({ "$type": "set_field_of_view", "field_of_view": 68.0, "avatar_id": "avatar" }) table = self.add_object( model_name="boconcept_lugo_dining_table_vray1.5", position={ "x": 3, "y": 0, "z": 0.35 }, rotation={ "x": 0, "y": -90, "z": 0 }, library="models_full.json") # record = ModelLibrarian(library='models_full.json').get_record("b03_simpsons_london_-_round_hudson_mirror") # self.communicate({"$type": "add_object", # "name": "b05_tv1970", # "url": record.get_url(), # "scale_factor": 0.5, # "position": {"x": 3, "y": 0.6, "z": 0.35}, # "rotation": {"x": 0, "y": -90, "z": 0}, # "category": record.wcategory, # "id": self.get_unique_id()}) table_bounds = self.get_bounds_data(table) top = table_bounds.get_top(0) self.add_object(model_name='035_vray', position={ "x": 3, "y": top[1], "z": 0.7 }, rotation={ "x": 0, "y": 0, "z": 0 }, library="models_full.json") backpack = self.add_object(model_name='brown_leather_travel_backpack', position={ "x": 3, "y": top[1], "z": 0.25 }, rotation={ "x": -90, "y": 0, "z": 0 }, library="models_full.json") backpack_bounds = self.get_bounds_data(backpack) backpack_top = backpack_bounds.get_top(0) self.add_object(model_name="cgaxis_models_65_06_vray", position={ "x": 2.7, "y": top[1], "z": 0.5 }, rotation={ "x": 0, "y": 0, "z": 0 }, library="models_full.json") self.add_object(model_name="cup", position={ "x": 3.3, "y": top[1], "z": -0.2 }, rotation={ "x": 0, "y": 0, "z": 0 }, library="models_full.json") # Enable image capture self.communicate({ "$type": "set_pass_masks", "avatar_id": "avatar", "pass_masks": ["_img", "_id"] }) self.communicate({"$type": "send_images", "frequency": "always"}) scene_data = self.communicate({ "$type": "look_at_position", "avatar_id": "avatar", "position": TDWUtils.array_to_vector3([3, 0.5, 0.35]) }) images = Images(scene_data[0]) TDWUtils.save_images(images, "pizza", output_directory="replicated_images")
def run(self): self.start() init_setup_commands = [{ "$type": "set_screen_size", "width": 1280, "height": 962 }, { "$type": "set_render_quality", "render_quality": 5 }] self.communicate(init_setup_commands) # Create an empty room. self.load_streamed_scene(scene="tdw_room_2018") # Disable physics. self.communicate({"$type": "set_gravity", "value": False}) # Add the avatar. self.communicate( TDWUtils.create_avatar(position={ "x": -2, "y": 1.5, "z": 0 }, look_at=TDWUtils.array_to_vector3( [4, 0.8, 0]), avatar_id="avatar")) self.communicate({ "$type": "set_field_of_view", "field_of_view": 68.0, "avatar_id": "avatar" }) self.add_object(model_name="b05_c-5_classic_series_bench-2012", position={ "x": 3, "y": 0, "z": -1.5 }, library="models_full.json") self.add_object(model_name="b05_c-5_classic_series_bench-2012", position={ "x": 3, "y": 0, "z": 1.5 }, library="models_full.json") self.add_object(model_name="baleri_italia_folded_black_table", position={ "x": 2, "y": 0, "z": -1.5 }, rotation=TDWUtils.array_to_vector3([0, 90, 0]), library="models_full.json") self.add_object(model_name="baleri_italia_folded_black_table", position={ "x": 2, "y": 0, "z": 1.5 }, rotation=TDWUtils.array_to_vector3([0, 90, 0]), library="models_full.json") self.add_object(model_name="linen_dining_chair", position={ "x": 1, "y": 0, "z": -1.5 }, rotation=TDWUtils.array_to_vector3([0, 90, 0]), library="models_full.json") self.add_object(model_name="linen_dining_chair", position={ "x": 1, "y": 0, "z": 1.5 }, rotation=TDWUtils.array_to_vector3([0, 90, 0]), library="models_full.json") # self.communicate({"$type": "rotate_object_by", # "angle": 180, # "axis": "yaw", # "id": chair_id, # "is_world": True}) # record = ModelLibrarian(library='models_full.json').get_record("b05_tv1970") # self.communicate({"$type": "add_object", # "name": "b05_tv1970", # "url": record.get_url(), # "scale_factor": 30, # "position": {"x": 0, "y": 0, "z": 3.5}, # "category": record.wcategory, # "id": self.get_unique_id()}) # # # side_table = self.add_object(model_name="side_table_wood", # position={"x": 1.5, "y": 0, "z": 4}, # library="models_full.json") # side_table_bounds = self.get_bounds_data(side_table) # # top = side_table_bounds.get_top(0) # self.add_object(model_name="acacia_table_lamp_jamie_young", # position={"x": 1.5, "y": top[1], "z": 4}, # library="models_full.json") # Enable image capture self.communicate({ "$type": "set_pass_masks", "avatar_id": "avatar", "pass_masks": ["_img", "_id"] }) self.communicate({"$type": "send_images", "frequency": "always"}) scene_data = self.communicate({ "$type": "look_at_position", "avatar_id": "avatar", "position": TDWUtils.array_to_vector3([4, 0.8, 0]) }) images = Images(scene_data[0]) TDWUtils.save_images( images, "two_tables_v2", output_directory= "/Users/leonard/Desktop/TDWBase-1.5.0/Python/Leonard/compare_COCO_TDW/replicated_images" )
def run(self, force=80): """ :param force: The force magnitude used to move the avatar. """ print("W, up-arrow = Move forward") print("S, down-arrow = Move backward") print("A, left-arrow = Turn counterclockwise") print("D, right-arrow = Turn clockwise") print("Esc = Quit") self.start() self.communicate(TDWUtils.create_empty_room(12, 12)) self.communicate(TDWUtils.create_avatar(avatar_type="A_Img_Caps")) # Set high drag values so it doesn't feel like the avatar is sliding on ice. self.communicate({"$type": "set_avatar_drag", "drag": 10, "angular_drag": 20}) # Change the floor material so it's easier to visualize the avatar's movement. self.communicate([self.get_add_material("parquet_alternating_orange", library="materials_high.json"), {"$type": "set_proc_gen_floor_material", "name": "parquet_alternating_orange"}, {"$type": "set_proc_gen_floor_texture_scale", "scale": {"x": 8, "y": 8}}]) done = False while not done: ch = getch() ch_int = ord(ch) forward = 0 torque = 0 # Move forward with w or up-arrow. if ch == b'w' or ch_int == 72: forward = force # Move backward with s or down-arrow. elif ch == b's' or ch_int == 80: forward = -force # Rotate counterclockwise with d or right-arrow. elif ch == b'd' or ch_int == 77: torque = force # Rotate clockwise with a or left-arrow. elif ch == b'a' or ch_int == 75: torque = -force # Quit with esc. elif ch_int == 27: done = True continue commands = [] # If there was any directional input, apply a directional force. if abs(forward) > 0: commands.append({"$type": "move_avatar_forward_by", "magnitude": forward}) # If there was any rotational input, apply a torque. if abs(torque) > 0: commands.append({"$type": "turn_avatar_by", "torque": torque}) # If there was no input, do nothing and advance the simulation. # (This isn't necessary in this example, but you'll want this if anything else is moving in the scene). if len(commands) > 0: commands.append({"$type": "do_nothing"}) self.communicate(commands)
def get_trial_initialization_commands(self) -> List[dict]: commands = [{"$type": "destroy_avatar"}] # Add a grid of cubes. d_y = 0.7 d_x = 2 d_z = 2 o_id = 0 for x in range(5): for y in range(10): for z in range(5): if x == 0 and z == 0: continue commands.extend( self.add_physics_object(position={ "x": x - d_x, "y": y * d_y, "z": z - d_z }, rotation=TDWUtils.VECTOR3_ZERO, dynamic_friction=0.1, static_friction=0.1, bounciness=0.9, o_id=o_id, mass=2, record=self.cube)) commands.extend([{ "$type": "scale_object", "id": o_id, "scale_factor": { "x": 0.7, "y": 0.7, "z": 0.7 } }, { "$type": "set_object_collision_detection_mode", "id": o_id, "mode": detection_mode }]) o_id += 1 commands.extend( TDWUtils.create_avatar(avatar_type="A_StickyMitten_Adult")) # Make the mittens sticky. for is_left in [True, False]: for side in ["palm", "back", "side"]: commands.append({ "$type": "set_stickiness", "sub_mitten": side, "sticky": True, "is_left": is_left, "show": False }) commands.extend([{ "$type": "set_pass_masks", "pass_masks": [] }, { "$type": "send_images", "frequency": "never" }, { "$type": "set_avatar_collision_detection_mode", "mode": detection_mode }]) return commands