summaryrefslogtreecommitdiffstats
path: root/tools/MayaTools/Maya4.0/scripts/SimpsonsArt/ajrAutoSave.mel
blob: d3f7f0d5ef0e71de3fd591bf25cde353021d6bfb (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
/*  This file downloaded from Highend3d.com
''  
''  Highend3d.com File Information:
''  
''    Script Name: ajrAutoSave v1.3
''    Author: Andy Rawling
''    Last Updated: July 17, 2001
''    Update/Change this file at:
''    http://www.highend3d.com/maya/mel/?section=utilities#198
''  
''  Please do not alter any information above this line
''  it is generated dynamically by Highend3d.com and will
''  be changed automatically on any updates.
*/

//	ajrAutoSave.mel
//	v 1.3	July 16 2001
//	
//	Tested on Maya2.5Irix Maya2.5.1NT Maya3.0Irix
//	
//	Provides automatic backup of scene files.
//
//	When this script is first called, it creates two MEL scriptJobs to run
//	short procedures whenever the user selects or de-selects an object, and
//	when they change the current time. These jobs trigger backups in two 
//	ways, either based on the number of times the user selects objects, or
//	on the amount of time since the last save.
//	The script also modifies your main File menu, adding File->Autosave options...
//	to the end. Choosing this menu command brings up the configuration GUI
//	(by running the same script again, but that's not important right now).
//
//	In selection mode, the script keeps a count of how many times the selection
//	has changed, and will trigger an automatic scene backup when the number of 
//	selection changes passes the limit set by the user.
//
//	In time mode, the script checks the time whenever the selection or the 
//	current time is changed by the user. The scene is backed up when the amount
//	of time since the last save passes the limit
//
//	This script is designed to be called from your 'userSetup.mel' script.
//
//	Configuration
//	-------------
//	If the script is run again, it will bring up a GUI which allows you to 
//	modify the way that AutoSave works. The options are:
//		o Enable AutoSave
//		o Save After XX minutes/selects
//		o Keep up to XX backups
//		o Save in XX
//		o notify when saving
//
//	
//	Enable AutoSave:	Check this option off to disable AutoSave (default=ON)
//	Save After XX (things):	Set the number of things which happen before the 
//				autosave is triggered - either the user changing which items are
//				selected, or the number of minutes since the last save (default=15)
//	(minutes/selects):	What sort of things are we using to spot changes? Either
//				number of times the selection changes, or number of minutes
//				passed since the last save (default=minutes , but see the caveats
//				below)
//	Keep up to XX backups:	AutoSave keeps a rolling sequence of backups. The 
//				number of separate backups can be changed here 
//				(default=5)
//	Save in XXXX:		Choose the folder where you want the backups to be kept.
//				This can be relative to your current project (default="scenes")
//	notify  when saving:	Bring up a small window to tell you when a backup is being 
//				made (default=OFF)
//	
//
//	Caveats
//	-------
//	Maya has no clock event, so when the save is time-based the clock is only
//	checked when the selection or the current time (as in time slider) changes.
//
//	Changes (* is a highend3d release)
//	-------
//	v1.3*	Autosaving no longer clears the current tool - cheers to Roger Klado
//			Feedback line is now more informative as the GUI settings are changed.
//	v1.2	I've got M3.0, so here there are annotations all over the GUI. Removed
//			the 'Include in File menu` option from the GUI - it's always included
//			now. Notify defaults to off now (too many hardware render mess-ups)
//	v1.1	Layout is more Irix/Motif-friendly now (was a bit squashed)
//	v1.0*	Fixed minor bug when the scene wasn't saved, choosing not to save
//			would switch the autosave off. New behavior is to nag the user again
//			after the trigger period. Now uses a timer as well as selection counter
//			to trigger backups.
//
//	Disclaimer
//	----------
//	This script is unsupported; neither me nor (especially) my employer accept 
//	liability for damage or loss resulting from its use or misuse. Use it at 
//	your own risk. That said - if you like it, spot a bug in it or think it
//	could be improved please let me know.
//	
//	Andy Rawling	andrew.rawling@_CUT_THIS_BIT_OUT_bigfoot.com
//	Animator
//	

global proc ajrAS_SelectionHasChanged() {
// This is called even when AutoSave is disabled, it just doesn't perform the save in this case
// We're checking for time passing as well as selection changes here!

	if((`optionVar -exists "ajrAS_iSelectTrigger"`) && (`optionVar -exists "ajrAS_iEnabled"`) && (`optionVar -exists "ajrAS_iUseTime"`)) {
	
		if((`optionVar -query "ajrAS_iUseTime"`)) {
		// We're using time to trigger saves
			global float $ajrAS_fLastSave; //Absolute time of the last save (or script startup)
			float $fElapsed;
			
			$fElapsed = `timerX -startTime $ajrAS_fLastSave`;
			if(($fElapsed / 60.0) > `optionVar -query "ajrAS_iSelectTrigger"`) {
				if(`optionVar -query "ajrAS_iEnabled"`)
					if (`ajrAS_SaveBackupScene`)
						$ajrAS_fLastSave = `timerX`;
			}			
		} else {
		// Using the number of selection changes to trigger
			global int $ajrAS_iSelectCount;	// The selection change counter
		
			$ajrAS_iSelectCount ++;
			if ($ajrAS_iSelectCount >= `optionVar -query "ajrAS_iSelectTrigger"`) {
				if(`optionVar -query "ajrAS_iEnabled"`)
					if (`ajrAS_SaveBackupScene`)
						$ajrAS_iSelectCount = 0;
			}
		}
	}
}

global proc ajrAS_CurrentTimeHasChanged() {
// This is called even when AutoSave is disabled, it just doesn't perform the save in this case
// We're only interested in time passing here
	
	if((`optionVar -exists "ajrAS_iSelectTrigger"`) && (`optionVar -exists "ajrAS_iEnabled"`)) {
	
		if((`optionVar -query "ajrAS_iUseTime"`)) {
			global float $ajrAS_fLastSave; //Absolute time of the last save (or script startup)
			float $fElapsed;
			
			$fElapsed = `timerX -startTime $ajrAS_fLastSave`;
			if(($fElapsed / 60.0) > `optionVar -query "ajrAS_iSelectTrigger"`) {
				if(`optionVar -query "ajrAS_iEnabled"`)
					if (`ajrAS_SaveBackupScene`)
						$ajrAS_fLastSave = `timerX`;
			}			
		}
	}
}

global proc int ajrAS_SaveBackupScene() {
// Save the backup file
// This routine is called when the number of events reaches the level set by the user
// to trigger a backup save.
// The save routine appends "_AutoSave#1" to the scene name of the first backup, then
// #2, #3 etc, until the number of saves reaches the maximum limit, when the number
// resets to 1 again.
// If the notifier is switched on, a message window is created for the duration of the\
// save. Either way, a message is shown on the command feedback line
//
	global int $ajrAS_iBackupNumber;	// Number of the current backup file
	string $sSceneName;			// Full name (including directory path) of the scene file
	string $sBackupName;			// Name (not inc. path or extension) of the backup scene
	string $sBackupDir;
	string $sWorkingTool;
	int $iReturn = 0;

	// If these values are not in the prefs, then this function should never have been called. Check anyway.
	if((`optionVar -exists "ajrAS_iSaveCopies"`) && (`optionVar -exists "ajrAS_iNotify"`) && (`optionVar -exists "ajrAS_sBackupDir"`)) {

		$sSceneName = `file -query -sceneName`;
		// If the scene has no name (hasn't been saved yet) then prompt to do a "Save As"
		if($sSceneName == "") {
			string $sReturn = `confirmDialog -title "AutoSave"
				-message "This scene has not been saved.\nAutoSave will not back up an unsaved scene\n\nDo you want to save your scene now?" 
				-button "Yes" 
				-button "No"
				-button "Configure"
    				-defaultButton "Yes" 
    				-cancelButton "No"
    				-dismissString "No"`;
			switch($sReturn) {
	    			
	    			case "Configure":
    					ajrAS_GUI;
    					break;
    			
    				case "Yes":
					projectViewer("SaveAs");
					break;
					
				case "No":
					// Reset the event counter
					// The user will be nagged again shortly
					$iReturn = 1;
					break;
			}
		} else {
			$sBackupDir = `optionVar -query "ajrAS_sBackupDir"`;
			if (`file -query -exists $sBackupDir`) {
				$sWorkingTool = `currentCtx`;
				$sBackupName = (`optionVar -query "ajrAS_sBackupDir"` + "/" + `file -query -namespace` + "_AutoSave#" + $ajrAS_iBackupNumber);
				if(`optionVar -query "ajrAS_iNotify"`) 
					ajrAS_MakeNotifyWindow($sBackupName);
				print("Auto saving file: " + $sBackupName + ".\n");
				file -rename $sBackupName;
				file -save -type mayaBinary -defaultExtensions true;
				$iReturn = 1;
				file -rename $sSceneName;
				$ajrAS_iBackupNumber ++;
				if ($ajrAS_iBackupNumber > `optionVar -query "ajrAS_iSaveCopies"`)
					$ajrAS_iBackupNumber = 1;
				if(`optionVar -query "ajrAS_iNotify"`) { 
					deleteUI ui_ajrAS_NotifyWindow;
				}
				setToolTo $sWorkingTool;
			} else {
				ajrAS_CheckBackupDir;
			}
		}
	}
	return ($iReturn);
}

global proc ajrAS_MakeNotifyWindow(string $sFileName) {

	if (`window -exists ui_ajrAS_NotifyWindow`)
		deleteUI ui_ajrAS_NotifyWindow;

	window 	-title ("Saving backup: " + $sFileName + ", please wait...")
		-iconName "Saving"
		-widthHeight 500 25
		-menuBar false
		ui_ajrAS_NotifyWindow;
		
	showWindow ui_ajrAS_NotifyWindow;
}

global proc ajrAS_Reset() {
// Reset GUI fields & prefs to defaults. 
	checkBox -edit -value 		on		ui_ajrAS_iEnabled;
	intField -edit -value 		15		ui_ajrAS_iSelectTrigger;
	optionMenu -edit -select	2		ui_ajrAS_type;
	intField -edit -value 		5		ui_ajrAS_iSaveCopies;
	checkBox -edit -value 		on		ui_ajrAS_iNotify;
	textField -edit -fileName 	"scenes"	ui_ajrAS_sBackupDir;
	ajrAS_Update;
	$ajrAS_iSelectCount = 0;
}

global proc ajrAS_Update() {
// Update the prefs settings
	optionVar -intValue "ajrAS_iEnabled"		`checkBox -query -value ui_ajrAS_iEnabled`;
	optionVar -intValue "ajrAS_iSelectTrigger" 	`intField -query -value ui_ajrAS_iSelectTrigger`;
	optionVar -intValue "ajrAS_iUseTime"  		(`optionMenu -query -value ui_ajrAS_type` == "minutes");
	optionVar -intValue "ajrAS_iSaveCopies"  	`intField -query -value ui_ajrAS_iSaveCopies`;
	optionVar -intValue "ajrAS_iNotify"  		`checkBox -query -value ui_ajrAS_iNotify`;
	optionVar -stringValue "ajrAS_sBackupDir"  	`textField -query -fileName ui_ajrAS_sBackupDir`;
	string $sReportStr = "AutoSave: ";
	if(! `optionVar -query "ajrAS_iEnabled"`)
		$sReportStr += ("disabled.\n");
	else {
		ajrAS_SelectionHasChanged;
		ajrAS_CheckBackupDir;
		$sReportStr += ("every " + `optionVar -query "ajrAS_iSelectTrigger"` + " ");
		if((`optionVar -query "ajrAS_iUseTime"`)) {
			global float $ajrAS_fLastSave; //Absolute time of the last save (or script startup)
			$sReportStr += ("minutes (next in ");
			$sReportStr += ((int)(`optionVar -query "ajrAS_iSelectTrigger"` - (`timerX -startTime $ajrAS_fLastSave` / 60.0)) + "), ");
		} else {
			global int $ajrAS_iSelectCount;	// The selection change counter
			$sReportStr += ("selects (next in ");
			$sReportStr += ((`optionVar -query "ajrAS_iSelectTrigger"` - ($ajrAS_iSelectCount)) + "), ");
		}
		$sReportStr += ("keep " + `optionVar -query "ajrAS_iSaveCopies"` + " copies in " + `optionVar -query "ajrAS_sBackupDir"` + ".\n");
	}
	print $sReportStr;
}

global proc int ajrAS_SetBackupDir(string $sDirName, string $sDirType) {
// Callback function for 'fileBrowser' call on backup directory chooser
	textField -edit -fileName `workspace -projectPath $sDirName` ui_ajrAS_sBackupDir;
	ajrAS_Update;
	return true;
}

global proc int ajrAS_CheckBackupDir() {
// Check that the backup location is valid
// If not, give the option of creating the backup directory, changing the location
// or just carrying on (in which cast the save doesn't happen)
	string $sBackupDir;
	int $iReturn;

	$sBackupDir = `optionVar -query "ajrAS_sBackupDir"`;
	if (! `file -query -exists $sBackupDir`) {
		string $sQuestion;
		string $sAnswer;
		$sQuestion =	"Your backup folder, (" + $sBackupDir + ") doesn't exist.\n" + 
				"Would you like to create this folder, or use select a new location?\n" + 
				"\n" + 
				"(If you choose to do neither, saving will be disabled)";

		$sAnswer = `confirmDialog -title "AutoSave"
			-message $sQuestion 
			-button "Create" 
			-button "Re-Select"
			-button "Forget it"
    			-defaultButton "Re-Select" 
    			-cancelButton "Forget It"
    			-dismissString "Forget It"`;

    		switch($sAnswer) {
    			case "Create":
    				// Try to create the folder (no return value to check success)
    				workspace -create `workspace -expandName $sBackupDir`;
    				// Call this procedure again to see if it worked
    				ajrAS_CheckBackupDir;
    				break;
    			case "Re-Select":
    				optionVar -stringValue "ajrAS_sBackupDir" "scenes";
    				if (`window -exists ui_ajrAS`) 
    					textField -edit -fileName "scenes" ui_ajrAS_sBackupDir;
    				else
					ajrAS_GUI;
    				break;
    			default:
    				if (`window -exists ui_ajrAS`) 
    					checkBox -edit -value off ui_ajrAS_iEnabled;
    				optionVar -intValue "ajrAS_iEnabled" off;
    				$iReturn = 0;
    				break;
    		}

	} else
		$iReturn = 1;
	
	return $iReturn;
}

global proc ajrAS_FileMenu() {
// Add or remove the AutoSave item from the main File menu
	global string $gMainFileMenu;		// Name of Maya 'File' menu

	if(`menuItem -exists ui_ajrAS_FileAutoSaveSeperator`)
		deleteUI ui_ajrAS_FileAutoSaveSeperator; 
	if(`menuItem -exists ui_ajrAS_FileAutoSaveMenu`)
		deleteUI ui_ajrAS_FileAutoSaveMenu;
		
	menuItem	-parent $gMainFileMenu
			-divider true
			ui_ajrAS_FileAutoSaveSeperator;
	menuItem	-parent $gMainFileMenu 
			-label "Auto Save options..."
			-annotation "Auto Save: Configure automatic backups"
			-command "ajrAutoSave"
			ui_ajrAS_FileAutoSaveMenu;
}

global proc ajrAS_GUI() {
// Call up the GUI
	if (`window -exists ui_ajrAS`)
		deleteUI ui_ajrAS;
	
	window	-title "AutoSave v1.3"
		-iconName "AutoSave"
		-widthHeight 280 300
		-menuBar false
		ui_ajrAS;
			
		formLayout ui_ajrAS_mainForm;
		
			frameLayout	-borderStyle "in"
					-labelVisible false
					ui_ajrAS_mainFrame;
						
				scrollLayout	-childResizable true
						-verticalScrollBarThickness 10
						-horizontalScrollBarThickness 0;
				
					columnLayout	-adjustableColumn true
							-cal "left";
									
						frameLayout	-labelVisible false
								-borderStyle "etchedIn" 
								-manage true;
							
							columnLayout	-adjustableColumn true
									-columnOffset "left" 30
									-columnAlign "left";
								
								checkBox	-label "Enable AutoSave"
										-value `optionVar -query ajrAS_iEnabled`
										-onCommand  "ajrAS_Update"
										-offCommand "ajrAS_Update; $ajrAS_iSelectCount = 0;"
										-annotation "Enable: Switch the autosave feature on/off"
										ui_ajrAS_iEnabled;

							setParent ..;
						setParent ..;

						frameLayout	-label "Options"
								-borderStyle "etchedIn" 
								 ui_ajrAS_optionsFrame;

							columnLayout	-adjustableColumn true
									-columnOffset "left" 30
									-columnAlign "left";
									
								rowColumnLayout	-numberOfColumns 3
										-columnWidth 1 70
										-columnWidth 2 40
										-columnWidth 3 100;
										
									text	"Save after";
									intField	-value `optionVar -query ajrAS_iSelectTrigger`
											-width 35
											-changeCommand "ajrAS_Update"
											-annotation "Save after: Good values are 200 for selects, 30 for minutes"
											ui_ajrAS_iSelectTrigger;
									optionMenu	ui_ajrAS_type;
										menuItem -label "selects";
										menuItem -label "minutes";
									optionMenu	-edit
											-changeCommand "ajrAS_Update"
											-annotation "Save after: Will you make backups as the minutes pass, or as you pick objects"
											-select (`optionVar -query ajrAS_iUseTime` + 1)
											ui_ajrAS_type;
									
									text	"Keep up to";
									intField	-value `optionVar -query ajrAS_iSaveCopies`
											-annotation "Keep up to: How many rolling copies do you want to keep"
											-width 35
											-changeCommand "ajrAS_Update"
										ui_ajrAS_iSaveCopies;
									text	" backups";
									
								setParent ..;
								
								rowLayout	-numberOfColumns 3
										-columnWidth3 70 100 50
										-columnAttach3 "left" "left" "left"
										-columnOffset3 0 0 5
										-adjustableColumn 2;
										
									text 		-label "Save in";
									textField	-fileName `optionVar -query ajrAS_sBackupDir`
											-width 90
											-changeCommand "ajrAS_Update"
											-annotation "Save in: Path to your backup folder (can be relative to your project)"
											ui_ajrAS_sBackupDir;
									symbolButton	-image "navButtonBrowse.xpm"
											-annotation "Save in: Browse for the backup folder"
											-command "fileBrowser(\"ajrAS_SetBackupDir\",\"AutoSave\", \"folder\", 4);"
											browser;
								setParent ..;

								checkBox	-label "notify when saving"
										-annotation "Notify: Pop up an window when saving (may interfere with hardware rendering)"
										-value `optionVar -query ajrAS_iNotify`
										-onCommand  "ajrAS_Update"
										-offCommand "ajrAS_Update"
										ui_ajrAS_iNotify;

							setParent ..;
							
						setParent ..;

 					setParent ..;
   				setParent ..;
   			setParent ..;

		button	-label "Close"
				-command "ajrAS_Update; deleteUI ui_ajrAS"
				ui_ajrAS_close;
		
		button	-label "Reset"
				-command "ajrAS_Reset"
				ui_ajrAS_reset;
		
		formLayout -edit
			-attachForm ui_ajrAS_mainFrame "left" 5
			-attachForm ui_ajrAS_mainFrame "right" 5
			-attachForm ui_ajrAS_mainFrame "top" 5
			-attachForm ui_ajrAS_mainFrame "bottom" 35
			
			-attachPosition	ui_ajrAS_reset "left" 2 50
			-attachForm	ui_ajrAS_reset "right" 5
			-attachNone	ui_ajrAS_reset "top"
			-attachForm	ui_ajrAS_reset "bottom" 5

			-attachForm	ui_ajrAS_close "left" 5
			-attachPosition	ui_ajrAS_close "right" 2 50
			-attachNone	ui_ajrAS_close "top"
			-attachForm	ui_ajrAS_close "bottom" 5

		ui_ajrAS_mainForm;
 
 	showWindow ui_ajrAS;
}

global proc ajrAutoSave() {
// Called when Maya starts up, and when the user wants to change settings.
// If the selection script job can't be found, then assume it's startup time & just
// silently go into the preferred state; otherwise bring up the GUI
	global int $ajrAS_iTimeJobNo;		// The number of the time change script job
	global int $ajrAS_iSelJobNo;		// The number of the select change script job
	global int $ajrAS_iBackupNumber;
	global float $ajrAS_fLastSave;		// Time of the last save, or script launch

	// If these values are not in the prefs, then set them to the defaults
	if(! `optionVar -exists "ajrAS_iEnabled"`)
		optionVar -intValue "ajrAS_iEnabled" 		off;
	if(! `optionVar -exists "ajrAS_iSelectTrigger"`)
		optionVar -intValue "ajrAS_iSelectTrigger" 	15;
	if(! `optionVar -exists "ajrAS_iUseTime"`)
		optionVar -intValue "ajrAS_iUseTime" 		on;
	if(! `optionVar -exists "ajrAS_iSaveCopies"`)
		optionVar -intValue "ajrAS_iSaveCopies" 	5;
	if(! `optionVar -exists "ajrAS_iNotify"`)
		optionVar -intValue "ajrAS_iNotify" 		off;
	if(! `optionVar -exists "ajrAS_sBackupDir"`)
		optionVar -stringValue "ajrAS_sBackupDir" 	"scenes";

	if ($ajrAS_iSelJobNo != 0)
		ajrAS_GUI;
	else {
		$ajrAS_iBackupNumber = 1;
		$ajrAS_fLastSave = `timerX`;
		ajrAS_FileMenu;
		$ajrAS_iTimeJobNo = `scriptJob -event timeChanged ajrAS_CurrentTimeHasChanged`;
		$ajrAS_iSelJobNo = `scriptJob -event SelectionChanged ajrAS_SelectionHasChanged`;
	}
}