Skip to content

Instantly share code, notes, and snippets.

@ak-brodrigue
Last active April 24, 2024 12:44
Show Gist options
  • Save ak-brodrigue/389ec46d796403952513a176bb3ef216 to your computer and use it in GitHub Desktop.
Save ak-brodrigue/389ec46d796403952513a176bb3ef216 to your computer and use it in GitHub Desktop.
Automatically Assign the children of a switch container to the switches
{
"version":2,
"commands":[
{
"id": "ak.auto-assign.switch",
"displayName": "Auto Assign Switch",
"contextMenu":{
"basePath":"WAAPI"
},
"startMode":"MultipleSelectionSingleProcessSpaceSeparated",
"program": "py",
"args": "-3 C:\\CHANGE_ME\\AutoAssignSwitchContainer_levenshtein.py ${id}"
}
]
}
import argparse
from pprint import pprint
from waapi import WaapiClient # pip install waapi-client
import numpy as np # pip install numpy
import Levenshtein # pip install levenshtein
parser = argparse.ArgumentParser(description='Auto-Assign Switch Containers: using WAAPI, for the selected containers, automatically assign each child object to a switch or state from the group.')
parser.add_argument("objects", nargs="*", help="GUIDs of target switch containers")
args = parser.parse_args()
if len(args.objects) < 1:
exit(1)
with WaapiClient() as client: # Connect (default URL)
# Group actions together in a single Undo in Wwise
result = client.call("ak.wwise.core.undo.beginGroup")
for container in args.objects:
# Get the children list from the switch container
children = client.call( "ak.wwise.core.object.get", {
"waql": '$ "' + container + '" select children ',
"options": {"return": ["name", "id"]} })["return"]
# Get the switch group used in the container
switchContainer = client.call( "ak.wwise.core.object.get", {
"waql": '$ "' + container + '"',
"options": {"return": ["name", "id", "@SwitchGroupOrStateGroup"]} })["return"][0]
# Get the children of the switch group (switches)
switches = client.call("ak.wwise.core.object.get", {
"waql": '$ "' + switchContainer["@SwitchGroupOrStateGroup"]["id"] + '" select children ',
"options": {"return": ["name", "id"]}})["return"]
# Find the best match for each children
child_to_switch = []
for i in range(0,len(children)):
child_name = children[i]['name'].lower()
# use the Levenshtein distance as a metric to calculate how much the strings differ
distances = np.array(list(map(lambda switch: Levenshtein.distance(switch['name'].lower(), child_name), switches)))
switch_index = np.argmin(distances)
child_to_switch.append([children[i]["id"], switches[switch_index]["id"]])
pprint( child_to_switch )
# Do associate children with switches
for child_id, switch_id in child_to_switch:
client.call("ak.wwise.core.switchContainer.addAssignment", {"child":child_id,"stateOrSwitch":switch_id})
# Close the Undo in Wwise
result = client.call("ak.wwise.core.undo.endGroup", {"displayName":"Auto Assign"})
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment