Page 1 of 1

Binary file export and/or system call from Rubescript

Posted: Wed Jun 19, 2019 6:12 pm
by bogwog
Hi! Just want to start by saying that RUBE is awesome! I bought it a while back but didn't really start using it until recently, and am just now realizing how polished this is.

I'm currently trying to write a script hook for dropped images which will automatically copy the dropped file(s) into a different directory relative to the rube file. The problem is that readFile and writeFile don't seem to support reading/writing binary files (like images). I also can't seem to find a way to do a system call so that I could copy the files that way instead.

Here's my script to illustrate what I'm trying to do:

Code: Select all

image[] img = getDroppedImages();

string imgDest = "assets/images/";

const uint numImg = img.length();

for(uint i=0; i<numImg; i++){
	string src = img[i].getFile();
	print("processing: " + src);
	
	int idx = src.findLast("/");
	string filename = src.substr(idx + 1);

	string dest = imgDest + filename;

	print("\tSaving to: \""+dest+"\"");

	if(fileExists(dest)){
		if(!queryYesNo("The file \"" + dest + "\" already exists. Overwrite destination?")){
			continue;
		}
	}

	writeFile(dest, readFile(src)); //this doesn't work
}
I could work around this by doing the file move another way, but it would be nice if I could let RUBE handle this part of the process for me, especially since things like naming conflicts are easier to resolve at the instant I actually drag the file into the program rather than later.

Re: Binary file export and/or system call from Rubescript

Posted: Sun Jun 30, 2019 2:11 pm
by iforce2d
Perhaps you could make a script that prints out the necessary command line, and then paste that into the terminal to execute it?
Eg. on Linux I would want something like this:

cp A.png B.png C.png destinationFolder

But to be honest it sounds much simpler to just drag and drop in the regular file explorer, I mean you're already doing the first part (drag...) :)

Re: Binary file export and/or system call from Rubescript

Posted: Wed Jul 03, 2019 12:29 am
by bogwog
That's kind of what I'm doing now. I have a tool that processes a rube file and converts it into my custom format. Before conversion, I copy all the files on the system into the local directory and update the references in the scene.

This works, but it'd be more convenient to have the editor handle this part because it makes things much more sane. For example, when my tool copies the external images into the local directory, it can't possibly know which one to keep without prompting the user, yet the user probably doesn't even know which is the correct file either because either someone else created it, or they did it a long time ago and don't remember.

But to be honest it sounds much simpler to just drag and drop in the regular file explorer, I mean you're already doing the first part (drag...) :)
Yes, but my reasoning for this system is to catch mistakes. If I'm working on a very large scene with hundreds or thousands of objects, but then accidentally drag in an image to Rube that isn't in the correct place, then the scene will look correct in Rube and I won't know it's broken until I try to import it into my engine. Also, if I copy the rube file and local asset directory to another computer, then those missing files will not load and I'll have to 1) hunt them down from the original computer and 2) update all the references.


Alternatively, I could just show an error with rubescript if it detects a file was dropped from outside the expected directory, but that's annoying... My current setup (with forced overwrite) has been working okay for me so far, so this isn't a critical feature for me at all. This is just an "it'd be nice" type of request :)

Re: Binary file export and/or system call from Rubescript

Posted: Wed Jul 03, 2019 7:18 pm
by iforce2d
If you're mainly intending to do this at the time of exporting, you might be able to use a combination of event hook and system hook.

The "before export" event hook could run a rubescript that figures out the command line invocations needed to copy the files, and uses writeFile to create a text file containing those commands.

The "after export" system hook could then run the text file that was created. I think if you use all absolute paths in the commands it would help to avoid possible problems with paths, and make it easier to check before running that the script will do what you expected.
Some info on system hooks (there's only one so far, "after export") https://youtu.be/7GcgF7-Xutk?t=953