Skip to content

Commit

Permalink
Tweak custom message listener script and test
Browse files Browse the repository at this point in the history
  • Loading branch information
thisjustin123 committed Dec 9, 2024
1 parent 9077bc2 commit ca8ef47
Show file tree
Hide file tree
Showing 2 changed files with 150 additions and 36 deletions.
80 changes: 44 additions & 36 deletions pyrcareworld/pyrcareworld/demo/examples/example_custom_message.py
Original file line number Diff line number Diff line change
Expand Up @@ -19,51 +19,59 @@
import os
import sys
import pyrcareworld.attributes as attr

sys.path.append(os.path.abspath(os.path.join(os.path.dirname(__file__), "..", "..")))

import argparse
from pyrcareworld.demo import executable_path
from pyrcareworld.envs.base_env import RCareWorld

# Initialize the environment with the specified scene file
player_path = os.path.join(executable_path, "../executable/Player/Player.x86_64")

# Initialize the environment with a custom asset
env = RCareWorld(assets=["CustomAttr"], executable_file=player_path)
def _main(dev):
sys.path.append(os.path.abspath(os.path.join(os.path.dirname(__file__), "..", "..")))

# Initialize the environment with the specified scene file
player_path = os.path.join(executable_path, "../executable/Player/Player.x86_64")

# Initialize the environment with a custom asset
env = RCareWorld(executable_file="@editor" if dev else player_path, assets=["CustomAttr"])

# Create an instance of a custom attribute and send a custom message
custom = env.InstanceObject(name="CustomAttr", id=1, attr_type=attr.CustomAttr)
custom.CustomMessage(message="this is instance channel custom message")

# Create an instance of a custom attribute and send a custom message
custom = env.InstanceObject(name="CustomAttr", id=123456, attr_type=attr.CustomAttr)
custom.CustomMessage(message="this is instance channel custom message")
# Perform a simulation step to process the custom message
env.step()

# Perform a simulation step to process the custom message
env.step()
# Print the custom message data
print(custom.data["custom_message"])

# Print the custom message data
print(custom.data["custom_message"])
# Callback function to handle dynamic object messages
def dynamic_object_callback(args):
for i, arg in enumerate(args):
print(f"Arg {i}: {arg}")

# Callback function to handle dynamic object messages
def dynamic_object_callback(args):
for i, arg in enumerate(args):
print(f"Arg {i}: {arg}")
# Add a listener for dynamic object messages
env.AddListenerObject("DynamicObject", dynamic_object_callback)

# Add a listener for dynamic object messages
env.AddListenerObject("DynamicObject", dynamic_object_callback)
# Send a dynamic object message with various data types
env.SendObject(
"DynamicObject",
"string:", "this is dynamic object",
"int:", 1,
"bool:", True,
"float:", 4849.6564,
"list:", [616445.085, 9489984.0, 65419596.0, 9849849.0],
"dict:", {"1": 1, "2": 2, "3": 3},
"tuple:", ("1", 1, 0.562)
)

# Send a dynamic object message with various data types
env.SendObject(
"DynamicObject",
"string:", "this is dynamic object",
"int:", 123456,
"bool:", True,
"float:", 4849.6564,
"list:", [616445.085, 9489984.0, 65419596.0, 9849849.0],
"dict:", {"1": 1, "2": 2, "3": 3},
"tuple:", ("1", 1, 0.562)
)
# Perform a simulation step to process the dynamic object message
env.step()

# Perform a simulation step to process the dynamic object message
env.step()
# Close the environment
env.Pend()
env.close()

# Close the environment
env.Pend()
env.close()
if __name__ == "__main__":
parser = argparse.ArgumentParser(description='Run RCareWorld bathing environment simulation for sponge forces.')
parser.add_argument('-d', '--dev', action='store_true', help='Run in developer mode')
args = parser.parse_args()
_main(args.dev)
106 changes: 106 additions & 0 deletions tests/test_base_env.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,106 @@
import pytest
from pyrcareworld.envs.base_env import RCareWorld
import pyrcareworld.attributes as attr
from pyrcareworld.envs.bathing_env import BathingEnv
from pyrcareworld.envs.dressing_env import DressingEnv

import os
import sys

def test_save_and_load():
player_path = "pyrcareworld/pyrcareworld/demo/executable/Player/Player.x86_64"

env = RCareWorld(assets=["Collider_Box", "Rigidbody_Sphere"], executable_file=player_path, log_level=1, graphics=False)

box1 = env.InstanceObject(name="Collider_Box", attr_type=attr.ColliderAttr, id=1)
box1.SetTransform(position=[-0.5, 0.5, 0], scale=[0.1, 1, 1])
box2 = env.InstanceObject(name="Collider_Box", attr_type=attr.ColliderAttr, id=2)
box2.SetTransform(position=[0.5, 0.5, 0], scale=[0.1, 1, 1])
box3 = env.InstanceObject(name="Collider_Box", attr_type=attr.ColliderAttr, id=3)
box3.SetTransform(position=[0, 0.5, 0.5], scale=[1, 1, 0.1])
box4 = env.InstanceObject(name="Collider_Box", attr_type=attr.ColliderAttr, id=4)
box4.SetTransform(position=[0, 0.5, -0.5], scale=[1, 1, 0.1])
sphere = env.InstanceObject(name="Rigidbody_Sphere", attr_type=attr.RigidbodyAttr, id=5)
sphere.SetTransform(position=[0, 0.5, 0], scale=[0.5, 0.5, 0.5])

env.SaveScene("test_scene.json")

# Give some time to save...
for _ in range(10):
env.step()

assert env.GetAttr(1).data["name"] == "Collider_Box"
assert env.GetAttr(2).data["name"] == "Collider_Box"
assert env.GetAttr(3).data["name"] == "Collider_Box"
assert env.GetAttr(4).data["name"] == "Collider_Box"
assert env.GetAttr(5).data["name"] == "Rigidbody_Sphere"

assert os.path.exists("pyrcareworld/pyrcareworld/demo/executable/Player/Player_Data/StreamingAssets/SceneData/test_scene.json")

env.ClearScene()

with pytest.raises(AssertionError):
env.GetAttr(1)

with pytest.raises(AssertionError):
env.GetAttr(2)

with pytest.raises(AssertionError):
env.GetAttr(3)

with pytest.raises(AssertionError):
env.GetAttr(4)

with pytest.raises(AssertionError):
env.GetAttr(5)

env.LoadSceneAsync("test_scene.json")

# Give some time to load...
for _ in range(10):
env.step()

assert env.GetAttr(1).data["name"] == "Collider_Box"
assert env.GetAttr(2).data["name"] == "Collider_Box"
assert env.GetAttr(3).data["name"] == "Collider_Box"
assert env.GetAttr(4).data["name"] == "Collider_Box"
assert env.GetAttr(5).data["name"] == "Rigidbody_Sphere"

env.close()

def test_object_listener():
env = RCareWorld(assets=["Collider_Box", "Rigidbody_Sphere"], executable_file="pyrcareworld/pyrcareworld/demo/executable/Player/Player.x86_64")

called = False

def object_listener(args):
called = True
dict = {}
for i, arg in enumerate(args):
dict[i] = arg

assert dict[0] == "string:"
assert dict[1] == "This is dynamic object"
assert dict[2] == "int:"
assert dict[3] == "123"
assert dict[4] == "float:"
assert dict[5] == "456.0"
assert dict[6] == "bool:"
assert dict[7] == "False"
assert dict[8] == "list:"
assert dict[9] == "[7.889999866485596, 1.1100000143051147]"
assert dict[10] == "dict:"
assert dict[11] == "{'a': 1, 'b': 2}"
assert dict[12] == "tuple:"
assert dict[13] == "('a', 1, 0.5619999766349792)"

env.AddListenerObject("Collider_Box", object_listener)

# Get data back...
env.step(10)

assert called

env.close()

# TODO: Test more functions of the environment

0 comments on commit ca8ef47

Please sign in to comment.