from tdw.add_ons.object_manager import ObjectManager
A simple manager class for objects in the scene. This add-on can cache static object data (name, ID, etc.) and record dynamic data (position, velocity, etc.) per frame.
- This add-on assumes that this is a PhysX simulation, as opposed to a simulation with physics disabled or a Flex simulation.
- This add-on will record data for all objects in the scene. If you only need data for specific objects, you should use low-level TDW commands.
- By default, this add-on will record transform data but not rigidbody data or bounds data. You can set which data the add-on will record in the constructor, but be aware that this can slow down the simulation.
import numpy as np
from tdw.controller import Controller
from tdw.tdw_utils import TDWUtils
from tdw.librarian import ModelLibrarian
from tdw.add_ons.object_manager import ObjectManager
c = Controller()
c.model_librarian = ModelLibrarian("models_special.json")
# Create the object manager.
om = ObjectManager()
c.add_ons.append(om)
commands = [TDWUtils.create_empty_room(100, 100)]
# The starting height of the objects.
y = 10
# The radius of the circle of objects.
r = 7.0
# Get all points within the circle defined by the radius.
p0 = np.array((0, 0))
o_id = 0
for x in np.arange(-r, r, 1):
for z in np.arange(-r, r, 1):
p1 = np.array((x, z))
dist = np.linalg.norm(p0 - p1)
if dist < r:
commands.extend([c.get_add_object("prim_cone",
object_id=o_id,
position={"x": x, "y": y, "z": z},
rotation={"x": 0, "y": 0, "z": 180})])
o_id += 1
pass
c.communicate(commands)
for i in range(1000):
for object_id in om.transforms:
print(object_id, om.transforms[object_id].position)
c.communicate([])
c.communicate({"$type": "terminate"})
-
objects_static
The static object data. Key = The ID of the object. -
categories
The segmentation color per category as use in the _category image pass. Key = The category. Value = The color as an[r, g, b]
numpy array. -
transforms
The transform data for each object on the scene on this frame. Key = The object ID. Iftransforms=False
in the constructor, this dictionary will be empty. -
rigidbodies
The rigidbody data for each rigidbody object on the scene on this frame. Key = The object ID. Ifrigidbodies=False
in the constructor, this dictionary will be empty. -
bounds
The bounds data for each object on the scene on this frame. Key = The object ID. Ifbounds=False
in the constructor, this dictionary will be empty. -
commands
These commands will be appended to the commands of the nextcommunicate()
call. -
initialized
If True, this module has been initialized.
ObjectManager()
ObjectManager(transforms=True, rigidbodies=False, bounds=False)
Parameter | Type | Default | Description |
---|---|---|---|
transforms | bool | True | If True, record the transform data of each object in the scene. |
rigidbodies | bool | False | If True, record the rigidbody data of each rigidbody object in the scene. |
bounds | bool | False | If True, record the bounds data of each object in the scene. |
self.get_initialization_commands()
This function gets called exactly once per add-on. To re-initialize, set self.initialized = False
.
Returns: A list of commands that will initialize this add-on.
self.on_send(resp)
This is called within Controller.communicate(commands)
after commands are sent to the build and a response is received.
Use this function to send commands to the build on the next Controller.communicate(commands)
call, given the resp
response.
Any commands in the self.commands
list will be sent on the next Controller.communicate(commands)
call.
Parameter | Type | Default | Description |
---|---|---|---|
resp | List[bytes] | The response from the build. |
self.before_send(commands)
This is called within Controller.communicate(commands)
before sending commands to the build. By default, this function doesn't do anything.
Parameter | Type | Default | Description |
---|---|---|---|
commands | List[dict] | The commands that are about to be sent to the build. |
self.get_early_initialization_commands()
This function gets called exactly once per add-on. To re-initialize, set self.initialized = False
.
These commands are added to the list being sent on communicate()
before any other commands, including those added by the user and by other add-ons.
Usually, you shouldn't override this function. It is useful for a small number of add-ons, such as loading screens, which should initialize before anything else.
Returns: A list of commands that will initialize this add-on.
self.reset()
Reset the cached static data. Call this when resetting the scene.