版本

menu_open
Wwise SDK 2021.1.14
Python(底层)– WAMP 订阅器/发布器

初始化工程

备注: 如果您使用的是 Python 2.7,那么您需要安装 Python 2.7 的 Microsoft Visual C++ Compiler。

从任意目录运行以下命令来安装依赖:

pip install autobahn

如果您使用的是 Python 2.7,请运行以下命令来安装额外的包:

pip install trollius futures

工程代码

备注: 命令行 from waapi_uri import WAAPI_URI 会导入 API 路径声明。

它位于 <Wwise 安装路径>/SDK/include/AK/WwiseAuthoringAPI/py。通过在该示例中扩展 sys.path,该文件的位置被动态添加到 Python 的路径中,但你也可以将 waapi.py 文件通过复制粘贴放到示例文件的相同目录下。

请注意 ak_authobahn.py 这个额外文件提供了一个特殊组件类型,该类型支持将自定义选项发送到 WAAPI。我们在示例目录中提供了该文件。

Python 2.7

找到示例文件 <Wwise installation path>/SDK/samples/WwiseAuthoringAPI/python/low-level/wwise-pubsub-wamp/get_ancestors_py2.py 的位置。

该文件包含以下代码,让您能连接到 Wwise Authoring API。

import sys
import os
import trollius as asyncio
from trollius import From
from autobahn.asyncio.wamp import ApplicationRunner
from ak_autobahn import AkComponent
# You may also copy-paste the waapi.py file alongside this sample
sys.path.append(os.path.join(os.path.dirname(__file__), '../../../../../include/AK/WwiseAuthoringAPI/py'))
from waapi_uri import WAAPI_URI
done = False
class MyComponent(AkComponent):
def onJoin(self, details):
subscription = None
def on_object_created(**kwargs):
yield From(subscription.unsubscribe())
result = kwargs[u"object"]
print("The object was created in the category: {}".format(result.get(u"category", "unknown")))
arguments = {
u"from": {u"id": [result.get(u"id")]},
u"transform": [{u"select": [u"ancestors"]}],
u"options": {
u"return": [u"name"]
}
}
res = yield From(self.call(WAAPI_URI.ak_wwise_core_object_get, **arguments))
ancestors = res.kwresults[u"return"]
print(u"Ancestor of {}:".format(result[u"id"]))
for ancestor in ancestors:
print("\t{}".format(ancestor[u"name"]))
global done
if not done:
self.leave() # Disconnect
done = True
subscribe_args = {
u"options": {
u"return": [u"id", u"name", u"category"]
}
}
# Subscribe to ak.wwise.core.object.created
# Calls on_object_created whenever the event is received
subscription = yield From(self.subscribe(on_object_created,
WAAPI_URI.ak_wwise_core_object_created,
**subscribe_args))
print("Create an object in the Project Explorer")
def onDisconnect(self):
print("The client was disconnected.")
asyncio.get_event_loop().stop()
if __name__ == '__main__':
runner = ApplicationRunner(url=u"ws://127.0.0.1:8080/waapi", realm=u"get_ancestors_demo")
try:
runner.run(MyComponent)
except Exception as e:
print(type(e).__name__ + ": Is Wwise running and Wwise Authoring API enabled?")

Python 3.6

找到示例文件 <Wwise installation path>/SDK/samples/WwiseAuthoringAPI/python/low-level/wwise-pubsub-wamp/get_ancestors_py3.py 的位置。

该文件包含以下代码,让您能连接到 Wwise Authoring API。

import asyncio
import sys
import os
from autobahn.asyncio.wamp import ApplicationRunner
from ak_autobahn import AkComponent
# You may also copy-paste the waapi.py file alongside this sample
sys.path.append(os.path.join(os.path.dirname(__file__), '../../../../../include/AK/WwiseAuthoringAPI/py'))
from waapi_uri import WAAPI_URI
done = False
class MyComponent(AkComponent):
def onJoin(self, details):
subscription = None
def on_object_created(**kwargs):
yield from subscription.unsubscribe()
result = kwargs[u"object"]
print("The object was created in the category: {}".format(result.get(u"category", "unknown")))
arguments = {
"from": {"id": [result.get(u"id")]},
"transform": [{"select": ["ancestors"]}],
"options": {
"return": ["name"]
}
}
res = yield from self.call(WAAPI_URI.ak_wwise_core_object_get, **arguments)
ancestors = res.kwresults[u"return"]
print(u"Ancestor of {}:".format(result[u"id"]))
for ancestor in ancestors:
print("\t{}".format(ancestor[u"name"]))
global done
if not done:
self.leave() # Disconnect
done = True
subscribe_args = {
"options": {
"return": ["id", "name", "category"]
}
}
# Subscribe to ak.wwise.core.object.created
# Calls on_object_created whenever the event is received
subscription = yield from self.subscribe(on_object_created,
WAAPI_URI.ak_wwise_core_object_created,
**subscribe_args)
print("Create an object in the Project Explorer")
def onDisconnect(self):
print("The client was disconnected.")
asyncio.get_event_loop().stop()
if __name__ == '__main__':
runner = ApplicationRunner(url=u"ws://127.0.0.1:8080/waapi", realm=u"get_ancestors_demo")
try:
runner.run(MyComponent)
except Exception as e:
print(type(e).__name__ + ": Is Wwise running and Wwise Authoring API enabled?")

运行工程

使用以下命令从示例文件的目录中运行该示例文件:

Python 2.7:

python get_ancestors_py2.py

Python 3.6:

python get_ancestors_py3.py

在 Project Explorer 中创建对象,它的祖辈列表就会显示出来。 示例会在返回各祖辈对象后终止连接。


此页面对您是否有帮助?

需要技术支持?

仍有疑问?或者问题?需要更多信息?欢迎联系我们,我们可以提供帮助!

查看我们的“技术支持”页面

介绍一下自己的项目。我们会竭力为您提供帮助。

来注册自己的项目,我们帮您快速入门,不带任何附加条件!

开始 Wwise 之旅