Trying to move audiofiles in FMOD via script, what's the best way?

Hey All, wondering if anyone’s had success using the FMOD Studio scripting api to import and move assets?

using the built in javascript, project.importAudioFile() works fine, but it only imports to root. I want to move the files to correct asset path via script as well. However using the function “setAssetPath” results in a garbled folder structure, it seems to truncate the folder name and even renames the folder on disc so it messes up the metadata and folderpaths for the whole project. Direct setting the value of “assetPath” results in the metadata not matching the file path and the file doesnt get moved.

The documentation says to set it via “AudioFile.container” but that function or property doesn’t exist and its unclear which relationship needs to change in order to move the file correctly.

Is this something that is better done outside of FMOD?

EDIT UPDATE: Okay so I think the moving files is partly my bad, didn’t realize assetPath needed to include the full file name so that was part of the problem.

Also wanted to update asking if it was possible to just specify a folder path to import to using “project.importAudioFile()”, would save a step. Also unclear if “project.importAudioFile()” will overwrite or modify exisiting files if iterating on an asset

We have an example script for sorting audio files on import that combines the usage of project.audioFileImported and AudioFile.setAssetPath in a manner similar to what you’re describing - just make sure you’re viewing the correct version of the documentation in the “version” dropdown at the top of the page.

If you want to specify a folder path, you can do something like the following in your script, which will create a dialog window to allow you to select a path using the UI to be stored in a variable for use elsewhere in the script:

// Create path variable to store selected path in
var path = "";
// Display modal dialog to allow for path selection
studio.ui.showModalDialog({
	widgetType: studio.ui.widgetType.Layout,
	layout: studio.ui.layoutType.VBoxLayout,
	spacing: 12,
	items: [
		// PathLineEdit to select path
		{
			widgetType: studio.ui.widgetType.PathLineEdit,
			windowTitle: "audioFileImported Path Selection",
			label: "Select a path to move imported audio file to:",
			caption: "test",
			pathType: studio.ui.pathType.Directory,
			onEditingFinished: function() {
				// Assign selected path to variable for use outside of modal dialog
				path = this.text();
			},
		},
		// PushButton for confirming selection
		{
			widgetType: studio.ui.widgetType.PushButton,
			text: "Confirm",
			onClicked: function() {
				this.closeDialog();
			},
		},
	],
});
// For demonstration purposes, use an alert to display selected path
alert("Path selected = " + path);

Thanks Leah, didn’t see that example thanks for flagging.

I did get asset.setAssetPath() to work, however it seems like it does not overwrite existing files so for iterating on sounds doesn’t quite do what I want. Is there a way with the FMOD scripting API to just update an asset?

Can I get you to elaborate on what exactly you mean? Are you overwriting a file on disk with a new file that has the same name, or importing a new file and wanting to use it to replace an existing asset?

I’m trying to overwrite an existing file on disk with a new file of the same name that has already been imported and moved to where it should be in my asset organization structure

If the old file has already been replaced on disk with the new file, and the new file has the same name and location, then FMOD Studio should automatically be using the new asset in place of the old one, but the assets will be marked as “#modified” in the Asset Browser.

To resolve the #modified tag, normally you could use File → Refresh Modified Assets, or right click on the asset and do “Refresh Selected Assets”, but these aren’t available from the Scripting API. Instead, you can run this code to trigger a refresh of all assets:

studio.project.workspace.masterAssetFolder.assets.forEach(function(asset){
	if (asset.isOfExactType("AudioFile")){
		asset.refreshAudio();
	}
});

The menu-based functionality should definitely be triggerable from the Scripting API though, so I’ve added that to our feature/improvement tracker.

I did get setAsset to work eventually, and it does work in the same way as rightclick menu “move” and will ask to replace existing files. However if I have 20 files say then it will prompt for every single one. If its possible to suppress this dialogue when using script that would be great.

I worked around this by outputing a command straight to “cmd.exe” and then using the asset.refreshAudio() function you posted.

var copyProcess = studio.system.start("cmd.exe", {args: "/c copy /y /v " + SOURCE_FILE, workingDir: targetAssetFolder.getAbsoluteAssetPath(),  timeout: 20000});

this can be looped for each file and will overwrite existing files silently

I don’t think there is a way to handle that particular message box via the scripting API- it is a blocking modal, so you won’t be able to execute anything while it is open, and it doesn’t have any identifiers for us to search for on the scripting side.
Your workaround using the OS file management capabilities sounds like a better alternative.