Skip to content

Commit e6a333f

Browse files
committed
chore: update listing of scene callbacks examples
1 parent 8cf8bc6 commit e6a333f

18 files changed

+198
-192
lines changed

content/python/basic/parent.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@ There is support to attach a child to an already-existing parent scene objects.
1414
```python
1515
from arena import *
1616

17-
scene = Scene(host="arenaxr.org", scene="earth")
17+
scene = Scene(host="arenaxr.org", scene="example")
1818

1919
earth = GLTF(
2020
object_id="gltf-model_Earth",

content/python/callbacks.md

Lines changed: 0 additions & 169 deletions
This file was deleted.

content/python/simple/chat.md renamed to content/python/callbacks/chat_callbacks.md

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,11 @@
11
---
2-
title: Chat
2+
title: ChatCallbacks
33
layout: default
4-
parent: Simple
4+
parent: Callbacks
55
grand_parent: Python Library
66
---
77

8-
# Chat
8+
# Chat Callback
99

1010
Extremely basic example of setting a chat message handler to echo
1111
chat messages typed in the scene.

content/python/callbacks/index.md

Lines changed: 45 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,45 @@
1+
---
2+
title: Callbacks
3+
nav_order: 1.6
4+
layout: default
5+
has_children: true
6+
parent: Python Library
7+
---
8+
9+
# ARENA Library Scene Callbacks
10+
11+
Library supported callback functions.
12+
13+
### Adding callbacks when you instantiate the class
14+
15+
You can also add callbacks like so:
16+
17+
```python
18+
from arena import *
19+
20+
def on_msg_callback(scene, obj, msg):
21+
pass
22+
23+
def new_obj_callback(scene, obj, msg):
24+
pass
25+
26+
def delete_obj_callback(scene, obj, msg):
27+
pass
28+
29+
scene = Scene(..., on_msg_callback=on_msg_callback, new_obj_callback=new_obj_callback, delete_obj_callback=delete_obj_callback)
30+
```
31+
32+
## Custom Message Callbacks
33+
34+
If you need to use an MQTT client, the `Scene` object exposes a way to use its MQTT client to subscribe to custom topics.
35+
36+
```python
37+
def led_toggle(client, userdata, msg):
38+
# do stuff here
39+
40+
scene.message_callback_add("custom/control/light", led_toggle)
41+
```
42+
43+
## Scene callbacks
44+
45+
There are a number of scene callbacks you can use in the table of contents below.
Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
---
2+
title: MsgCallbacks
3+
layout: default
4+
parent: Callbacks
5+
grand_parent: Python Library
6+
---
7+
8+
# All Messages Callback
9+
10+
`scene.on_msg_callback` is called whenever there is a new message sent to the client. Use this whenever you want to sniff out **all** incoming messages.
11+
12+
```python
13+
from arena import *
14+
15+
scene = Scene(host="arenaxr.org", scene="example")
16+
17+
# [scene] is the Scene that called the callback
18+
# [obj] will be an Object instance
19+
# [msg] is the raw JSON message as a dict
20+
def on_msg_callback(_scene, obj, _msg):
21+
# do stuff with obj here
22+
print("msg", obj, obj.data.position)
23+
# etc.
24+
# could also do obj["object_id"] or msg["object_id"]
25+
26+
scene.on_msg_callback = on_msg_callback
27+
```
Lines changed: 45 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,45 @@
1+
---
2+
title: ObjectCallbacks
3+
layout: default
4+
parent: Callbacks
5+
grand_parent: Python Library
6+
---
7+
8+
# Object Callbacks
9+
10+
`scene.new_obj_callback` is called whenever there is a new object that has been created in the scene,
11+
one that the user does not have a reference to. Use this to make references to any
12+
new objects that may appear during a programs lifetime. Also a good way to find camera ID's.
13+
14+
`scene.delete_obj_callback` is called whenever there is an object has been deleted in the scene.
15+
arena-py will look for all "action" = "delete" messages and call this callback.
16+
Use this to delete references and to be notified when an object is removed by
17+
another user or program.
18+
19+
```python
20+
from arena import *
21+
22+
scene = Scene(host="arenaxr.org", scene="example")
23+
24+
# [scene] is the Scene that called the callback
25+
# [obj] will be an Object instance
26+
# [msg] is the raw JSON message as a dict
27+
def new_obj_callback(scene, obj, msg):
28+
# do stuff with obj here
29+
print("new", obj, obj.object_id, obj.data.position.x)
30+
# etc.
31+
# could also do obj["object_id"] or msg["object_id"]
32+
33+
scene.new_obj_callback = new_obj_callback
34+
35+
# [scene] is the Scene that called the callback
36+
# [obj] will be an Object instance
37+
# [msg] is the raw JSON message as a dict
38+
def delete_obj_callback(scene, obj, msg):
39+
# do stuff with obj here
40+
print("delete", obj, obj.object_id, obj.data.position.x)
41+
# etc.
42+
# could also do obj["object_id"] or msg["object_id"]
43+
44+
scene.delete_obj_callback = delete_obj_callback
45+
```
Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
---
2+
title: ProgramCallbacks
3+
layout: default
4+
parent: Callbacks
5+
grand_parent: Python Library
6+
---
7+
8+
# Program Callbacks
9+
10+
`scene.end_program_callback` is called whenever the program is ending from the SIGINT, Crtl-C, or other kill process.
11+
Use this to cleanup resources you don't want in the scene when the program ends.
12+
This is especially useful for persisted objects use created that you want to be removed.
13+
14+
```python
15+
from arena import *
16+
17+
scene = Scene(host="arenaxr.org", scene="example")
18+
19+
childObject = Box(object_id="child_object")
20+
appParentObject = Object(object_id="parent_object", parent=childObject.object_id)
21+
22+
# [scene] is the Scene that called the callback
23+
def end_program_callback(scene):
24+
# do stuff with scene root objects here
25+
scene.delete_object(appParentObject)
26+
print("App Terminated.")
27+
# etc.
28+
29+
scene.end_program_callback = end_program_callback
30+
```

content/python/simple/addtopic.md renamed to content/python/callbacks/topic_callbacks.md

Lines changed: 4 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,11 @@
11
---
2-
title: Addtopic
2+
title: TopicCallbacks
33
layout: default
4-
parent: Simple
4+
parent: Callbacks
55
grand_parent: Python Library
66
---
77

8-
# Add Topic
8+
# Add/Remove Topic
99

1010
Demonstrate subscribing to a secondary topic on the same broker and monitor
1111
messages from the secondary topic within the same program.
@@ -16,12 +16,10 @@ import json
1616
from arena import *
1717

1818
def objects_callback(_scene, _obj, msg):
19-
print("Object message: "+str(msg))
19+
print("Object message: " + str(msg))
2020

2121
def secondary_callback(_scene, _obj, msg):
22-
print("-----")
2322
print(f"Secondary message:\nTopic: {str(msg.topic)}\nPayload: {json.loads(msg.payload)}")
24-
print("-----")
2523

2624
# subscribe to objects
2725
scene = Scene(host="arenaxr.org", scene="example", on_msg_callback=objects_callback)
@@ -32,16 +30,13 @@ async def test():
3230
# subscribe to secondary
3331
scene.message_callback_add(topic, secondary_callback)
3432
print(f"Subscribed to {topic}")
35-
print()
3633

3734
# sleep for 5 seconds
3835
await scene.sleep(5000)
3936

4037
# unsubscribe to secondary
4138
scene.message_callback_remove(topic)
42-
print()
4339
print(f"Unsubscribed to {topic}")
44-
print()
4540

4641
# our main event loop
4742
scene.run_tasks()

0 commit comments

Comments
 (0)