[Closed] Wait for file in folder?
Hello!
I’m currently working on a script, that at one point, needs to wait for a specific obj to be placed in a specific folder (by another program in this case) in order to import it in the scene and continue with the script.
I’m not sure even where to begin, I’d guess it has something to do with the “if” expression, but I’m kind of a newbie in scripting and I’ve not been able to figure out how to use the “if” expression in this situation, so any help is greatly appreciated! I’ll keep trying anyway!
Some other thoughts:
I’m also not very sure, if this will cause performance issues, since it may take even 60 seconds for the obj to appear in the folder, so I probably don’t want 3ds Max to look for the obj like crazy and instead have it look for the obj in 1 second intervals or something.
Delaying the script with “sleep” is not an option either, since the process outside 3ds Max can take from 2 seconds to even 2 minutes to place the obj in the folder.
So yeah, thanks in advance!
you can use timer and check on its tick a file’s existence.
and there is a fancy way using dotnet and System.IO.FileSystemWatcher and it’s file created event (how I would do it)
Just by chance I was using the FileSystemWatcher in maxscript yesterday. Here’s a bit of code to get you started:
(
function fileWatch_Changed sender evt_args =
(
print sender;
print evt_args;
)
local file = "C:/test.txt"
local dir = getFileNamePath file;
local filename = getFileNameFile file + getFileNameType file;
local fileWatch = dotNetObject "System.IO.FileSystemWatcher" dir;
fileWatch.Filter = filename;
fileWatch.EnableRaisingEvents = true;
fileWatch.NotifyFilter = (dotnetclass "System.IO.Notifyfilters").LastWrite;
dotNet.addEventHandler fileWatch "Changed" fileWatch_Changed;
)
This code watches for changes in a particular file. But it should get you started and with some adjustments, it can be made to do what you’re after.
is theres anything that cannot be done with maxscript, i mean if we can use the windows api from maxscript the control over the operating system is unlimited (or is it my imagination as i havent played with the api’s yet) even the registry can be accessed with maxscript, this leads to serious security issues doesn’t it ? (and theres no warning also before running a maxscript :S, even a microsoft excel macro displays a warning before its run!)
Wow! Thanks a lot! This will help me get started, I’ll have a look tonight
Yes, I was thinking the same thing. I have made some script recently that run bat files, which also use other exe files in order to do all sorts of stuff, through command line of course. And on top of that I have also done some experiments with Sikuli in order to connect the Unreal Editor interface to my scripts. So yeah, I think you can do basically anything with maxscript! Or so it seems…
Pjanssen,
How would you go on stopping this event from happening ? [EDIT: of course the event will keep on happening, but I mean the notification]
And how would you view your current number of open noitification registrations ?
I mean, I ran this function (which is amazing btw, I wasn’t aware of .NET integration in MXS!) a few times, and got a callback to my function for each time I ran it, so I had my function called 7 times.
I wanted to know if there is a way of keeping track of how many registrations I did there, hope I am clear…
Thanks in advance!