Fmod Studio import CSV file to create Events

I want to write a Js script which can import csv file. But i can not find a function to open a file explorer or browser to select a csv file to import.

i could use studio.system.getFile(filepath); to open a file, but how to get this filepath?

Could any one help me? Thank you so much!

Hi,

You can use the filesystem “Open” window with the following code:

var path = "";

studio.ui.showModalDialog({
            windowTitle: "Open a file",
            widgetType: studio.ui.widgetType.PathLineEdit,
	text: "",
	label: "Please select a file to open and close the window when done",
	onEditingFinished: function() {
		path = this.text();
	}
});

On selecting a file and closing the Modal Dialog Window in FMOD Studio, the filepath will be assigned to the path variable.

Also, I happen to have a script on hand which opens a text file containing a list of events in folder delimited like so:

folder/subfolder1/event1
folder/subfolder1/event2
folder/subfolder2/event3
folder/subfolder2/event4

and generates the folder and events in the FMOD Studio project based on that. It’s not exactly what you need, since you’re using a CSV that is likely delimited differently, but it may serve as a helpful reference:

studio.menu.addMenuItem({
	name: "Events From File",
	execute: function() {
		createEventsFromFile();
	}
});

function createEventsFromFile(){
	var filePath = studio.project.filePath;
	filePath = filePath.substr(0, filePath.lastIndexOf("/") + 1) + "/eventList.txt";

    // Open txt file at project path, returning early if file cannot be opened
    var textFile = studio.system.getFile(filePath);
    if (!textFile.open(studio.system.openMode.ReadOnly)) {    
        alert("Failed to open file {0}".format(filePath));
        console.error("Failed to open file {0}.".format(filePath));
        return;
    }

	// Read entire text file
	var text = textFile.readText(textFile.size());
	// Split text to lines
	var lines = text.split('\n');

	// For each line, split event paths into individual elements
	lines.forEach(function(line){
		var lineElements = line.split('/');	

		// For each folder/event
		for(var i = 0; i < lineElements.length; i++) {

			// Generate paths of folder/event and parent folder
			var folderPath = lineElements.slice(0, i + 1).join('/');
			var parentFolderPath = lineElements.slice(0, i).join('/');

			// Try to retrieve existing folder/event and parent folder from project 
			var folder = studio.project.workspace.masterEventFolder.getItem(folderPath);
			var parentFolder = studio.project.workspace.masterEventFolder.getItem(parentFolderPath);

			// If existing folder/event doesn't exist, create new, and assign appropriately
			if (typeof folder == 'undefined') {
				if (i < lineElements.length - 1){
					var folder = studio.project.create('EventFolder');
					folder.name = lineElements[i];
					if (parentFolder != 'undefined'){
						folder.folder = parentFolder;
					}
				} else {
					var folder = studio.project.create('Event');
					folder.name = lineElements[i];
					if (parentFolder != 'undefined'){
						folder.folder = parentFolder;
					}
				}
			}
		}
	});

	textFile.close();
}

Let me know if you run into any issues.

1 Like

This Would be Great help! May ask another question?
If i know the file path right now, may i open the file folder? such as “reveal in file explorer”

The following should open Windows Explorer and select your file given a filepath:

var path = "C:\\Your\\File\\Path\\Here.csv";
studio.system.startAsync("explorer", {workingdir: "", args: ["/select,", path]});
1 Like

Thank you so much!

1 Like