Controller

March 2, 2023 ยท View on GitHub

from tdw.controller import Controller

Base class for all controllers.

Usage:

from tdw.controller import Controller
c = Controller()

Functions

__init__

Controller()

Controller(port=1071, check_version=True, launch_build=True)

Create the network socket and bind the socket to the port.

ParameterTypeDefaultDescription
portint1071The port number.
check_versionboolTrueIf true, the controller will check the version of the build and print the result.
launch_buildboolTrueIf True, automatically launch the build. If one doesn't exist, download and extract the correct version. Set this to False to use your own build, or (if you are a backend developer) to use Unity Editor.

communicate

self.communicate(commands)

Send commands and receive output data in response.

ParameterTypeDefaultDescription
commandsUnion[dict, List[dict]A list of JSON commands.

Returns: The output data from the build.

get_add_object

Controller.get_add_object(model_name, object_id)

Controller.get_add_object(model_name, position=None, rotation=None, library="", object_id)

(Static)

Returns a valid add_object command.

ParameterTypeDefaultDescription
model_namestrThe name of the model.
positionDict[str, float]NoneThe position of the model. If None, defaults to {"x": 0, "y": 0, "z": 0}.
rotationDict[str, float]NoneThe starting rotation of the model, in Euler angles. If None, defaults to {"x": 0, "y": 0, "z": 0}.
librarystr""The path to the records file. If left empty, the default library will be selected. See ModelLibrarian.get_library_filenames() and ModelLibrarian.get_default_library().
object_idintThe ID of the new object.

Returns: An add_object command that the controller can then send via self.communicate(commands).

get_add_physics_object

Controller.get_add_physics_object(model_name, object_id)

Controller.get_add_physics_object(model_name, position=None, rotation=None, library="", object_id, scale_factor=None, kinematic=False, gravity=True, default_physics_values=True, mass=1, dynamic_friction=0.3, static_friction=0.3, bounciness=0.7, scale_mass=True)

(Static)

Add an object to the scene with physics values (mass, friction coefficients, etc.).

ParameterTypeDefaultDescription
model_namestrThe name of the model.
positionDict[str, float]NoneThe position of the model. If None, defaults to {"x": 0, "y": 0, "z": 0}.
rotationDict[str, float]NoneThe starting rotation of the model, in Euler angles. If None, defaults to {"x": 0, "y": 0, "z": 0}.
librarystr""The path to the records file. If left empty, the default library will be selected. See ModelLibrarian.get_library_filenames() and ModelLibrarian.get_default_library().
object_idintThe ID of the new object.
scale_factorDict[str, float]NoneThe scale factor.
kinematicboolFalseIf True, the object will be kinematic.
gravityboolTrueIf True, the object won't respond to gravity.
default_physics_valuesboolTrueIf True, use default physics values. Not all objects have default physics values. To determine if object does: has_default_physics_values = model_name in DEFAULT_OBJECT_AUDIO_STATIC_DATA.
massfloat1The mass of the object. Ignored if default_physics_values == True.
dynamic_frictionfloat0.3The dynamic friction of the object. Ignored if default_physics_values == True.
static_frictionfloat0.3The static friction of the object. Ignored if default_physics_values == True.
bouncinessfloat0.7The bounciness of the object. Ignored if default_physics_values == True.
scale_massboolTrueIf True, the mass of the object will be scaled proportionally to the spatial scale.

Returns: A list of commands to add the object and apply physics values that the controller can then send via self.communicate(commands).

get_add_material

Controller.get_add_material(material_name)

Controller.get_add_material(material_name, library="")

(Static)

Returns a valid add_material command.

ParameterTypeDefaultDescription
material_namestrThe name of the material.
librarystr""The path to the records file. If left empty, the default library will be selected. See MaterialLibrarian.get_library_filenames() and MaterialLibrarian.get_default_library().

Returns: An add_material command that the controller can then send via self.communicate(commands).

get_add_scene

Controller.get_add_scene(scene_name)

Controller.get_add_scene(scene_name, library="")

(Static)

Returns a valid add_scene command.

ParameterTypeDefaultDescription
scene_namestrThe name of the scene.
librarystr""The path to the records file. If left empty, the default library will be selected. See SceneLibrarian.get_library_filenames() and SceneLibrarian.get_default_library().

Returns: An add_scene command that the controller can then send via self.communicate(commands).

get_add_hdri_skybox

Controller.get_add_hdri_skybox(skybox_name)

Controller.get_add_hdri_skybox(skybox_name, library="")

(Static)

Returns a valid add_hdri_skybox command.

ParameterTypeDefaultDescription
skybox_namestrThe name of the skybox.
librarystr""The path to the records file. If left empty, the default library will be selected. See HDRISkyboxLibrarian.get_library_filenames() and HDRISkyboxLibrarian.get_default_library().

Returns: An add_hdri_skybox command that the controller can then send via self.communicate(commands).

get_add_humanoid

Controller.get_add_humanoid(humanoid_name, object_id)

Controller.get_add_humanoid(humanoid_name, position=None, rotation=None, library="", object_id)

(Static)

Returns a valid add_humanoid command.

ParameterTypeDefaultDescription
humanoid_namestrThe name of the humanoid.
positionDict[str, float]NoneThe position of the humanoid. If None, defaults to {"x": 0, "y": 0, "z": 0}.
rotationDict[str, float]NoneThe starting rotation of the humanoid, in Euler angles. If None, defaults to {"x": 0, "y": 0, "z": 0}.
librarystr""The path to the records file. If left empty, the default library will be selected. See HumanoidLibrarian.get_library_filenames() and HumanoidLibrarian.get_default_library().
object_idintThe ID of the new object.

Returns: An add_humanoid command that the controller can then send via self.communicate(commands).

get_add_humanoid_animation

Controller.get_add_humanoid_animation(humanoid_animation_name)

Controller.get_add_humanoid_animation(humanoid_animation_name, library="")

(Static)

Returns a valid add_humanoid_animation command and the record (which you will need to play an animation).

ParameterTypeDefaultDescription
humanoid_animation_namestrThe name of the animation.
librarystr""The path to the records file. If left empty, the default library will be selected. See HumanoidAnimationLibrarian.get_library_filenames() and HumanoidAnimationLibrarian.get_default_library().

Returns: An add_humanoid_animation command that the controller can then send via self.communicate(commands).

get_add_robot

Controller.get_add_robot(name, robot_id)

Controller.get_add_robot(name, robot_id, position=None, rotation=None, library="")

(Static)

Returns a valid add_robot command.

ParameterTypeDefaultDescription
namestrThe name of the robot.
robot_idintA unique ID for the robot.
positionDict[str, float]NoneThe initial position of the robot. If None, defaults to {"x": 0, "y": 0, "z": 0}.
rotationDict[str, float]NoneThe initial rotation of the robot in Euler angles. If None, defaults to {"x": 0, "y": 0, "z": 0}.
librarystr""The path to the records file. If left empty, the default library will be selected. See RobotLibrarian.get_library_filenames() and RobotLibrarian.get_default_library().

Returns: An add_robot command that the controller can then send via self.communicate(commands).

get_add_visual_effect

Controller.get_add_visual_effect(name, effect_id)

Controller.get_add_visual_effect(name, effect_id, position=None, rotation=None, library="")

(Static)

Returns a valid add_effect command.

ParameterTypeDefaultDescription
namestrThe name of the visual effect.
effect_idintA unique ID for the visual effect.
positionDict[str, float]NoneThe initial position of the visual effect. If None, defaults to {"x": 0, "y": 0, "z": 0}.
rotationDict[str, float]NoneThe initial rotation of the visual effect in Euler angles. If None, defaults to {"x": 0, "y": 0, "z": 0}.
librarystr""The path to the records file. If left empty, the default library will be selected. See VisualEffectLibrarian.get_library_filenames() and VisualEffectLibrarian.get_default_library().

Returns: An add_effect command that the controller can then send via self.communicate(commands).

get_version

self.get_version()

Send a send_version command to the build.

Returns: The TDW version and the Unity Engine version.

get_unique_id

Controller.get_unique_id()

(Static)

Generate a unique integer. Useful when creating objects.

Returns: The new unique ID.

get_frame

Controller.get_frame(frame)

(Static)

Converts the frame byte array to an integer.

ParameterTypeDefaultDescription
framebytesThe frame as bytes.

Returns: The frame as an integer.

launch_build

Controller.launch_build()

Controller.launch_build(port=1071)

(Static)

Launch the build. If a build doesn't exist at the expected location, download one to that location.

ParameterTypeDefaultDescription
portint1071The socket port.