[Closed] Script the check if file is already open elsewhere
Basically I want to write a script that would perform the same function as AutoCAD. When you open a file that is already opened elsewhere a window pops up telling you that the file is open elsewhere. (something max should have already). How this is done in AutoCAD is there is a temp file that’s created in the same directory when the file is opened and I guess the presence of that file is what triggers the message. When the file is closed the temp file is deleted.
I’m just wondering if anyone has already done this or had experience with it, or any tips on how to accomplish it.
try(destroyDialog fRol) catch()
rollout fRol "Open Max File" width:140 height:30
(
button open_max "Open" width:120 tooltip:"Open Max File"
on open_max pressed do
(
if (file = getMAXOpenFileName()) != undefined and doesfileexist file do
(
if (toLower file) == toLower (maxfilepath + maxfilename) then case (yesNoCancelBox "File is already opened.
Do you want to reload it?" title:"Warning!") of
(
#yes: loadMaxFile file
#no: open_max.pressed()
#cancel: return ok
)
else loadMaxFile file
)
)
)
createdialog fRol
Enjoy…
You could try to rename the file within a try/catch function. If the file is open elsewhere, it should generate an error while attempting to rename, no? If it does, then you’d just throw your messagebox code within the catch brackets.
I don’t see how this code does what ILS was asking for?
It doesn’t seem to check if ANYONE else has the file open, just the current Max session?
As I understood the question is “Is the file already opened in the current scene?” This is not about getting “check off” state of the file using Perforce for example…
denisT, he is asking for a way to make sure no one else has the file open, not the current max session, because you could simply look at the title bar for that.
There is no easy way to do this. Since Max doesn’t lock .max files, then the rename suggestion won’t work. You would need to create a maxscript callback that would create the file you want, and another that would check if the file exists. This can get messy though with left over files caused by crashes, etc. You would be better off using an asset tracking system to find out if the file has been checked out or not.
-Eric
Lets wait ILS for details. If he wants to now is any file currently opened on someone elses machine he has to check documentation for assets management system which is used by his company (e.g., Alienbrain, Perforce, etc.).
@ILS
Worst case scenario (if you are not using an assets management system), you could always try to replicate what you said about Autocad. Have a filePreOpen callback generate a file or a registry entry or something every time a file is open. Of course the same script would have to be able to also remove the file/entry if a new file is opened or if the max session ends.
It’s not entirely practical however, as for instance if MAX crashes you would still have the entry lying around…
About 7 years ago last time I worked in some small company and we didn’t use any client/server system. The easiest way to know that any file is currently opened for edit by someone else was to stand up and yell: “Hey guys, anyone working on …?” Pretty quick and secure, isnt it?
Unless you have more than one floor in the building, in which case you are gonna need some plastic cups and a lot of thread.
By the way, let’s look on some scenario…
-
Every max (we are talking about only max files) file has to have some flag stored with file and accessible from MAXScript without opening this file. Simplest solution is to use READ ONLY flag. MAX 2010 provides new Asset Metadata system (check documentation). So, let call this flag FLAG.
-
On #filePreOpenProces callback the script has to check the FLAG
a) FLAG in ON means FILE is free for use. SCRIPT sets FLAG to OFF (busy) and sets some internal flag that FILE can be saved by this user.
b) FLAG is OFF means FILE opened for edit by someone else. SCRIPTS shows the message “FILE checked out. And can’t to be save.” (there is no way to interrupt max file loading), sets some internal flag that FILE cant be saved by this user.
-
On #filePreSaveProcess SCRIPT checks for “normal” save and:
a) FILE is opened for edit by this user. SCRIPT allows saving it.
b) FILE is opened for edit by other user. SCRIPTS sets FILE’s ReadOnly flag to ON. MAX doesnt allow saving FILE. -
On #filePostSaveProcess SCRIPT sets FILE’s ReadOnly flag to previous state if necessary.
-
On #systemPreReset (it calls every time when new file created, system reset) SCRIPT checks what type of FILE is currently loaded:
a) Free FILE. SCRIPT sets FLAG to ON (free)
b) Busy FILE. SCRIPT keeps FLAG as OFF (busy)
6,7,8
SCRIPT has to check if FILE was saving with same name as was opened. SCRIPT doesnt protect from saving, renaming, or deleting the FILE by any other then MAX external program. SCRIPT doesnt protect FILE from renaming and deleting from any other script or plug-in
Ooh
sounds crazy but doable with some limitations.
Usually that works pretty well. However as a project comes to that critical point close to the deadline and there are last minute “fixes” and several groups are working on them at the same time comminication tends to break down and in an already stressed out environment yelling back and forth might not be the best plan.
your flag idea sounds interesting, I’m pretty new at this so I will have to take some time to digest that tidbit. and just to throw a wrench in the works how would this affect network rendering.
easiest way in your case:
- all people have to follow mandatory rule: DON’T EDIT NOT READ ONLY FILES
- set READ ONLY flag for all files to OFF
- make startup scripts to check file READ ONLY flag on #filePreOpenProcess callback event
- if FLAG is ON it’s OK to edit this file; if FLAG is OFF this file open someoneelse for edit – TO GIVE A WARNING;
- if file free for edit on #filePostOpenProcess callback event set file’s READ ONLY flag to OFF
- Restore FLAG on any #reset event
callbacks.removescripts id:#readonly_rule
global readonly_rule = #()
fn readonly_pre_open =
(
if (file = callbacks.notificationParam()) != undefined and file[1] == 1 and doesfileexist file[2] then
(
readonly_rule = #(file[2], getFileAttribute file[2] #readonly)
)
else readonly_rule = undefined
)
fn readonly_post_open =
(
if readonly_rule != undefined do case readonly_rule[2] of
(
(on): case (yesNoCancelBox "File is OK for Editing.
Do you want to Edit it?" title:"Warning!") beep:off of
(
#yes: setFileAttribute readonly_rule[1] #readonly off
#no: ok
#cancel: resetMaxFile #noprompt
)
(off): case (queryBox "File opened for edit! You can't edit this file!.
Do you want to open it any way?" title:"Warning!" beep:off) of
(
(off): resetMaxFile #noprompt
)
)
)
fn readonly_pre_reset =
(
if readonly_rule != undefined and (file = maxfilepath + maxfilename) != "" and file == readonly_rule[1] and not (getFileAttribute file #readonly) do
(
-- messageBox (maxfilepath + maxfilename) beep:off
setFileAttribute file #readonly on
)
readonly_rule = undefined
)
callbacks.addscript #systemPreReset "readonly_pre_reset()" id:#readonly_rule
callbacks.addscript #systemPreNew "readonly_pre_reset()" id:#readonly_rule
callbacks.addscript #preSystemShutdown "readonly_pre_reset()" id:#readonly_rule
callbacks.addscript #filePreOpenProcess "readonly_pre_open()" id:#readonly_rule
callbacks.addscript #filePostOpenProcess "readonly_post_open()" id:#readonly_rule
here is a code that you have to put in scripts/startup
i haven’t time to double check but you got an idea…
good luck!