monitoring file name change

50 views
Skip to first unread message

Chris Gardner

unread,
Aug 4, 2014, 3:15:39 AM8/4/14
to python_in...@googlegroups.com
Hi all,

I'm trying to write a simple lockfile system for maya (hey, user X has
that file open!) and I'm trying to catch when the current filename
changes (ie, saveAs, etc).

I'm monitoring the OpenMaya.MSceneMessage.kBeforeSave event, but
querying cmds.file(query=True, sn=True) gives me the *new* name, as
does OpenMaya.MFileIO.beforeSaveFilename(). i need both the old and
new filenames to compare.

Am i barking up the wrong tree, or does anyone have any insights?

Many Thanks,
chrisg

Marcus Ottosson

unread,
Aug 4, 2014, 3:30:34 AM8/4/14
to python_in...@googlegroups.com
Are you familiar with how Softimage does it?

In a nutshell, whenever a user opens a file, a file with the same name and a "LOCK" extension is looked up. If none is found, a new one is created and the users workstation name is written into it, and if one is found, the user is alerted with the contained workstation name.

You should be able to accomplish the same using the method you described.

Monitoring changes however is probably a different topic and may not be what you're after, but there are generally two method of accomplishing it; polling or grabbing a file handle. For polling, you could have a look at Watchdog, a library for Python. It should help guide you through what it means to monitor file-system changes.



--
You received this message because you are subscribed to the Google Groups "Python Programming for Autodesk Maya" group.
To unsubscribe from this group and stop receiving emails from it, send an email to python_inside_m...@googlegroups.com.
To view this discussion on the web visit https://groups.google.com/d/msgid/python_inside_maya/CAPoKtNek2mMAERcMtNAt_MkdOUaVUg%2BqpES_RCbqZ2KHp7RtoA%40mail.gmail.com.
For more options, visit https://groups.google.com/d/optout.



--
Marcus Ottosson
konstr...@gmail.com

Chris Gardner

unread,
Aug 4, 2014, 5:25:19 AM8/4/14
to python_in...@googlegroups.com
Hi Marcus,

Yeah, i'm an old softie, so i'm basically replicating that behavior inside maya.

Thanks for your suggestions - i'll have a look and see what's
applicable. I've used something similar to watchdog in another
context.

I'm working around it by storing a global var, as i have a callback on
scene open as well, and that seems to do the trick. However, i'd
prefer a more elegant way if possible.

cheers,
chrisg

Fredrik Averpil

unread,
Aug 4, 2014, 6:54:22 AM8/4/14
to python_in...@googlegroups.com
Forgive me for perhaps missing a step here... but how do you detect the fact that you close the scene, maya exists or maya crashes?
--- meaning, when to know to remove the lock file?


Fredrik



--
You received this message because you are subscribed to the Google Groups "Python Programming for Autodesk Maya" group.
To unsubscribe from this group and stop receiving emails from it, send an email to python_inside_m...@googlegroups.com.

Chris Gardner

unread,
Aug 4, 2014, 7:01:14 AM8/4/14
to python_in...@googlegroups.com
Hey Fredrik,

I am also watching events on newScene and mayaExit. Maya crashing is
the tricky one.

However, i'm not stopping the user from doing anything if there's a
lock on the scene. It's for information purposes only. It's not my job
to prevent people from doing things if it won't kill 'em :)

cheers,
chrisg

Marcus Ottosson

unread,
Aug 4, 2014, 9:43:22 AM8/4/14
to python_in...@googlegroups.com
--- meaning, when to know to remove the lock file?

That's a good question. Softimage does this transparently, but I'm not sure how. If I had to take a guess, I'd say that the Softimage process has an active file-handle on the LOCK file, and as soon as the process dies so does the handle, leaving it to the next process, either by the same user or not, to clean up the, now invalid, lock.

There's some fascinating information on the topic in the Wiki


--
You received this message because you are subscribed to the Google Groups "Python Programming for Autodesk Maya" group.
To unsubscribe from this group and stop receiving emails from it, send an email to python_inside_m...@googlegroups.com.

For more options, visit https://groups.google.com/d/optout.



--
Marcus Ottosson
konstr...@gmail.com

Justin Israel

unread,
Aug 4, 2014, 4:54:09 PM8/4/14
to python_in...@googlegroups.com
Is that Windows-specific behaviour, related to how it can actively prevent any other process from accessing the file, and can also delete on close? I don't think *nix has that same behaviour. Also does that windows behaviour work the same on NFS? 
You can do file locking on *nix, but I don't think it can cover deleting the physical file on an application crash (only releasing the lock), if that makes any different. Probably not, since the next application would just acquire the lock and update the file anyways. But also, from what I have read, file locking flock/fcntl isn't recommended over network mounts. I think it can depend on what type of mount and how it was mounted. 



Chris Gardner

unread,
Aug 4, 2014, 7:00:34 PM8/4/14
to python_in...@googlegroups.com
It could very well be windows specific, given how soft is tied to the
windows api (even on linux - mainwin).

It's kinda clever - i just opened a file in soft, saw the lock file,
killed the xsi process and the lock file disappeared. Hmmm....

cheers,
chrisg

Justin Israel

unread,
Aug 4, 2014, 8:17:44 PM8/4/14
to python_in...@googlegroups.com
And that works the same if the lockfile is located on an NFS mount? Again, I guess it would depend on the exact mount options of an NFS mount, or whether there is an external daemon that cleans up the lockfile outside of the application crashing, or something like that. 


--
You received this message because you are subscribed to the Google Groups "Python Programming for Autodesk Maya" group.
To unsubscribe from this group and stop receiving emails from it, send an email to python_inside_m...@googlegroups.com.

Chris Gardner

unread,
Aug 4, 2014, 8:21:27 PM8/4/14
to python_in...@googlegroups.com
I'm only on win / smb, so can't test that, unfortunately. I can't see
any other processes floating around, but that's just lack of smarts on
my part.

cheers,
chrisg
Reply all
Reply to author
Forward
0 new messages