-
Notifications
You must be signed in to change notification settings - Fork 75
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #55 from threedworld-mit/floorplan_controller
Floorplan controller
- Loading branch information
Showing
10 changed files
with
2,216 additions
and
3 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,36 @@ | ||
# `floorplan_controller.py` | ||
|
||
## `FloorplanController(Controller)` | ||
|
||
`from tdw.floorplan_controller import FloorplanController` | ||
|
||
A controller that can create an interior scene populated with objects. | ||
|
||
```python | ||
from tdw.floorplan_controller import FloorplanController | ||
|
||
c = FloorplanController() | ||
init_commands = c.get_scene_init_commands(scene="2a", layout=0, audio=True) | ||
c.communicate(init_commands) | ||
``` | ||
|
||
*** | ||
|
||
#### `get_scene_init_commands(self, scene: str, layout: int, audio: bool) -> List[dict]` | ||
|
||
Get commands to create a scene and populate it with objects. | ||
Valid scenes and layouts: | ||
| `scene` | `layout` | | ||
| --- | --- | | ||
| `"2a"`, `"2b"`, or `"2b"` | 0 | | ||
|
||
| Parameter | Description | | ||
| --- | --- | | ||
| scene | The name of the scene. Corresponds to a record named: `floorplan_[scene]`. | | ||
| layout | The layout index. | | ||
| audio | If True, instantiate physics values per object from audio properties. | | ||
|
||
_Returns:_ A list of commands to initialize the scene and populate it with objects. | ||
|
||
*** | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,95 @@ | ||
# `object_init_data.py` | ||
|
||
## `TransformInitData` | ||
|
||
`from tdw.object_init_data import TransformInitData` | ||
|
||
Basic initialization parameters for an object. Can be converted to and from a list of commands. | ||
|
||
This is similar to [`Controller.get_add_object()`](controller.md) except that it includes more parameters. | ||
|
||
*** | ||
|
||
#### `__init__(self, name: str, library: str = "models_core.json", scale_factor: Dict[str, float] = None, position: Dict[str, float] = None, rotation: Dict[str, float] = None, kinematic: bool = False, gravity: bool = True)` | ||
|
||
|
||
| Parameter | Description | | ||
| --- | --- | | ||
| name | The name of the model. | | ||
| library | The filename of the library containing the model's record. | | ||
| scale_factor | The [scale factor](../api/command_api.md#scale_object). | | ||
| position | The initial position. If None, defaults to: `{"x": 0, "y": 0, "z": 0`}. | | ||
| rotation | The initial rotation as a quaternion. If None, defaults to: `{"w": 1, "x": 0, "y": 0, "z": 0}` | | ||
| kinematic | If True, the object will be [kinematic](../api/command_api.md#set_kinematic_state). | | ||
| gravity | If True, the object won't respond to [gravity](../api/command_api.md#set_kinematic_state). | | ||
|
||
*** | ||
|
||
#### `get_commands(self) -> Tuple[int, List[dict]]` | ||
|
||
_Returns:_ Tuple: The ID of the object; a list of commands to create the object: `[add_object, rotate_object_to, scale_object, set_kinematic_state, set_object_collision_detection_mode]` | ||
|
||
*** | ||
|
||
## `RigidbodyInitData(TransformInitData)` | ||
|
||
`from tdw.object_init_data import RigidbodyInitData` | ||
|
||
A subclass of `TransformInitData`. Includes data and commands to set the mass and physic material of the object. | ||
|
||
*** | ||
|
||
#### `__init__(self, name: str, mass: float, dynamic_friction: float, static_friction: float, bounciness: float, library: str = "models_core.json", scale_factor: Dict[str, float] = None, position: Dict[str, float] = None, rotation: Dict[str, float] = None, kinematic: bool = False, gravity: bool = True)` | ||
|
||
|
||
| Parameter | Description | | ||
| --- | --- | | ||
| name | The name of the model. | | ||
| library | The filename of the library containing the model's record. | | ||
| scale_factor | The [scale factor](../api/command_api.md#scale_object). | | ||
| position | The initial position. If None, defaults to: `{"x": 0, "y": 0, "z": 0`}. | | ||
| rotation | The initial rotation as a quaternion. If None, defaults to: `{"w": 1, "x": 0, "y": 0, "z": 0}` | | ||
| kinematic | If True, the object will be [kinematic](../api/command_api.md#set_kinematic_state). | | ||
| gravity | If True, the object won't respond to [gravity](../api/command_api.md#set_kinematic_state). | | ||
| mass | The mass of the object. | | ||
| dynamic_friction | The [dynamic friction](../api/command_api.md#set_physic_material) of the object. | | ||
|
||
*** | ||
|
||
#### `get_commands(self) -> Tuple[int, List[dict]]` | ||
|
||
_Returns:_ Tuple: The ID of the object; a list of commands to create the object: `[add_object, rotate_object_to, scale_object, set_kinematic_state, set_object_collision_detection_mode, set_mass, set_physic_material]` | ||
|
||
*** | ||
|
||
## `AudioInitData(RigidbodyInitData)` | ||
|
||
`from tdw.object_init_data import AudioInitData` | ||
|
||
A subclass of `RigidbodyInitData` that includes [audio values](py_impact.md#objectinfo). | ||
Physics values are derived from these audio values. | ||
|
||
*** | ||
|
||
#### `__init__(self, name: str, library: str = "models_core.json", scale_factor: Dict[str, float] = None, position: Dict[str, float] = None, rotation: Dict[str, float] = None, kinematic: bool = False, gravity: bool = True, audio: ObjectInfo = None)` | ||
|
||
|
||
| Parameter | Description | | ||
| --- | --- | | ||
| name | The name of the model. | | ||
| library | The filename of the library containing the model's record. | | ||
| scale_factor | The [scale factor](../api/command_api.md#scale_object). | | ||
| position | The initial position. If None, defaults to: `{"x": 0, "y": 0, "z": 0`}. | | ||
| rotation | The initial rotation as a quaternion. If None, defaults to: `{"w": 1, "x": 0, "y": 0, "z": 0}` | | ||
| kinematic | If True, the object will be [kinematic](../api/command_api.md#set_kinematic_state). | | ||
| gravity | If True, the object won't respond to [gravity](../api/command_api.md#set_kinematic_state). | | ||
| audio | If None, derive physics data from the audio data in `PyImpact.get_object_info()` (if the object isn't in this dictionary, this constructor will throw an error). If not None, use these values instead of the default audio values. | | ||
|
||
*** | ||
|
||
#### `get_commands(self) -> Tuple[int, List[dict]]` | ||
|
||
_Returns:_ Tuple: The ID of the object; a list of commands to create the object: `[add_object, rotate_object_to, scale_object, set_kinematic_state, set_object_collision_detection_mode, set_mass, set_physic_material]` | ||
|
||
*** | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,73 @@ | ||
from pathlib import Path | ||
from json import loads | ||
from pkg_resources import resource_filename | ||
from typing import List | ||
from tdw.controller import Controller | ||
from tdw.object_init_data import TransformInitData, AudioInitData | ||
|
||
|
||
class FloorplanController(Controller): | ||
""" | ||
A controller that can create an interior scene populated with objects. | ||
```python | ||
from tdw.floorplan_controller import FloorplanController | ||
c = FloorplanController() | ||
init_commands = c.get_scene_init_commands(scene="2a", layout=0, audio=True) | ||
c.communicate(init_commands) | ||
``` | ||
""" | ||
|
||
def get_scene_init_commands(self, scene: str, layout: int, audio: bool) -> List[dict]: | ||
""" | ||
Get commands to create a scene and populate it with objects. | ||
Valid scenes and layouts: | ||
| `scene` | `layout` | | ||
| --- | --- | | ||
| `"2a"`, `"2b"`, or `"2b"` | 0 | | ||
:param scene: The name of the scene. Corresponds to a record named: `floorplan_[scene]`. | ||
:param layout: The layout index. | ||
:param audio: If True, instantiate physics values per object from audio properties. | ||
:return: A list of commands to initialize the scene and populate it with objects. | ||
""" | ||
|
||
scene = scene | ||
scene_index = scene[0] | ||
layout = str(layout) | ||
|
||
floorplans = loads(Path(resource_filename(__name__, "floorplan_layouts.json")). | ||
read_text(encoding="utf-8")) | ||
if scene_index not in floorplans: | ||
raise Exception(f"Floorplan not found: {scene_index}") | ||
if layout not in floorplans[scene_index]: | ||
raise Exception(f"Layout not found: {layout}") | ||
|
||
objects = floorplans[scene_index][layout] | ||
|
||
commands = [self.get_add_scene(scene_name=f"floorplan_{scene}"), | ||
{"$type": "set_aperture", | ||
"aperture": 8.0}, | ||
{"$type": "set_focus_distance", | ||
"focus_distance": 2.25}, | ||
{"$type": "set_post_exposure", | ||
"post_exposure": 0.4}, | ||
{"$type": "set_ambient_occlusion_intensity", | ||
"intensity": 0.175}, | ||
{"$type": "set_ambient_occlusion_thickness_modifier", | ||
"thickness": 3.5}] | ||
# Deserialize the JSON data. | ||
if audio: | ||
objects = [AudioInitData(**o) for o in objects] | ||
else: | ||
objects = [TransformInitData(**o) for o in objects] | ||
# Get the commands to initialize each object. | ||
for o in objects: | ||
object_id, object_commands = o.get_commands() | ||
commands.extend(object_commands) | ||
return commands |
Oops, something went wrong.