Skip to content

Instantly share code, notes, and snippets.

@qbit
Last active August 29, 2015 14:05
Show Gist options
  • Select an option

  • Save qbit/5975a07c0ca27e26a2d5 to your computer and use it in GitHub Desktop.

Select an option

Save qbit/5975a07c0ca27e26a2d5 to your computer and use it in GitHub Desktop.
Script to monitor a directory for new STL files and run slic3r when new ones are added.
var fs = require('fs'),
spawn = require('child_process').spawn,
dir = process.argv[2],
cmd = process.argv[3],
timer = process.argv[4] || 10000,
running_file = "";
function run_update(file, rcmd) {
return function() {
if (running_file === "") {
running_file = file;
rcmd = rcmd.replace("%f", file);
var s = spawn('slic3r', rcmd.split(' '));
s.stdout.on('data', function(d) {
console.log(d.toString());
});
s.stderr.on('data', function(d) {
console.log(d.toString());
});
s.on('close', function(code) {
console.log("finished '%s' with code '%d'", rcmd, code);
running_file = "";
});
} else {
return;
}
};
}
fs.exists(dir, function(exists) {
if (exists) {
console.log("Watching '%s'", dir);
fs.watch(dir, function(e, fname) {
if (fname.match(/\.stl$/i)) {
setTimeout(run_update(dir + '/' + fname, cmd), timer);
}
});
} else {
console.log("'%s' does not exist.", dir);
process.exit();
}
});
@qbit

qbit commented Aug 13, 2014

Copy link
Copy Markdown
Author

Use in the following way:

node octoslic3r.js /home/octo/.octoprint/uploads '--load /home/octo/printer_settings.ini %f'

%f will be replaced with the file that is being added.

First argument is the directory you want to monitor.
Second argument is the command line options to be passed to slic3r.
A third and optional argument can be supplied as the time to wait prior to running Slic3r (default is 10 seconds)

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment