Working autosave, but need help to finalize it

Next
 From:  Moi3dFan
11150.1 
I wrote a function in SidePane that executes a Save command every 3 minutes.

It really works. You have to specify the save path first the first time, and then everything is saved automatically.

Now I want to prescribe the conditions to be saved in the GeneralOptions menu. To take the time interval parameter and incremental save option from the moi.ini file.


That's what it looks like now. And it certainly doesn't work.




Below in the figure, how it looks in GeneralOptions




And here are the parameters that I would like the autosafe to take from moi.ini



Autosave=18000
AutosaveIncremental=y

Please help me figure out how to put it all together

  Reply Reply More Options
Post Options
Reply as PM Reply as PM
Print Print
Mark as unread Mark as unread
Relationship Relationship
IP Logged

Previous
Next
 From:  Michael Gibson
11150.2 In reply to 11150.1 
Hi Moi3dFan,

re:
> And here are the parameters that I would like the autosafe to take from moi.ini

When a script calls moi.command.getOption( 'optionName', true /* fromIni */ ), the option will be stored in the [commands] section of the moi.ini file.

To initialize your controls you could add some code to the Initialize() function that is inside GeneralOptions.htm. Give the controls an id="" value so they can be addressed by the script code.

Something like this:

var Autosave = 18000;

try {
// Be prepared for moi.command.getOption() to throw an exception if the setting is not present.
Autosave = moi.command.getOption( 'Autosave', true /* fromIni */ );
} catch(e) {}

control_id.value = Autosave;


And to persist it, add this to the Shutdown() function:


moi.command.setOption( 'Autosave', control_id.value, true /* true = save to ini */ );


- Michael
  Reply Reply More Options
Post Options
Reply as PM Reply as PM
Print Print
Mark as unread Mark as unread
Relationship Relationship
IP Logged

Previous
Next
 From:  Moi3dFan
11150.3 In reply to 11150.2 
Unfortunately, I don't have the knowledge to make it all work.

I'll attach the files for you to study.

  Reply Reply More Options
Post Options
Reply as PM Reply as PM
Print Print
Mark as unread Mark as unread
Relationship Relationship
IP Logged

Previous
Next
 From:  Moi3dFan
11150.4 In reply to 11150.2 
Michael, do you have time to help me make a working version of autosave?
  Reply Reply More Options
Post Options
Reply as PM Reply as PM
Print Print
Mark as unread Mark as unread
Relationship Relationship
IP Logged

Previous
Next
 From:  Michael Gibson
11150.5 In reply to 11150.4 
Hi Moi3dFan, so for the UI part inside GeneralOptions.htm, in order for the script code to interact with
the UI controls you need to set an id="" attribute on the controls.

Also the <moi:Text> control is used to look up a text value from the current language string table.
Since there aren't any translatable strings set up for these you should just have your text directly and not any <moi:Text> elements.


So the UI part inside GeneralOptions.htm should look more like this where the <moi:NumericInput> and <moi:CheckButton> have id attributes set on them:

code:
		<fieldset>
			<legend>Autosave</legend>
		
			<table>
				<tr>
					<td>Autosave Time:</td>
					<td><moi:NumericInput id="AutosaveTimeControl" /></td>
					<td><moi:CheckButton id="AutosaveIncrementalControl">Incremental</moi:CheckButton></td>
					<td><moi:Spacer/></td>
					<td style="font-size:75%; color:gray; margin:5px;">For correct work, the first time you 
						need to <br> specify a location for automatic saving</td>
				</tr>
			</table>

		</fieldset>


So now that script code can access the numeric input and check button controls to get or set
the values they contain. This is done by setting the .value property on the control. In script
code every element on the page that has an id="" attribute set on it can be accessed through
a global variable of that name. So if you have this for the HTML part:

<moi:NumericInput id="AutosaveTimeControl" />

Then script code can use it like:
var value = AutosaveTimeControl.value;


So inside Initialize() the script code should go something like this:

code:
				var Autosave = 18000;
				var Autosaveincremental = false;

				try {
					Autosave = moi.command.getOption( 'Autosave', true);
				} catch(e) {}

				try {
					AutosaveIncremental = moi.command.getOption( 'AutosaveIncremental', true);
				} catch(e) {}

				// Set UI controls to have the values that were just read in from moi.ini

				AutosaveTimeControl.value = Autosave;
				AutosaveIncrementalControl.value = AutosaveIncremental;



Then inside the Shutdown() function you can retrieve the values from the controls like this:
code:
				moi.command.setOption( 'Autosave', AutosaveTimeControl.value, true);
				moi.command.setOption( 'AutosaveIncremental', AutosaveIncremetnalControl.value, true);


I hope that will get you unblocked, I haven't tested the above code so there could be typos.

- Michael

EDITED: 20 Jul 2023 by MICHAEL GIBSON

  Reply Reply More Options
Post Options
Reply as PM Reply as PM
Print Print
Mark as unread Mark as unread
Relationship Relationship
IP Logged

Previous
Next
 From:  Moi3dFan
11150.6 In reply to 11150.5 
Thanks a lot! Everything works now.

One last question, the autosave options only apply when the entire interface is reloaded or if reopen moi3d

I call reload by method onchange="moi.ui.theme = value;"

This is fundamentally wrong, because it changes the theme of the interface, but reloads moi3d and applies the autosave settings.
I tried everything I could, and only this helped.

How to properly reload the interface without changing the theme.
Image Attachments:
Size: 24 KB, Downloaded: 12 times, Dimensions: 1372x280px
  Reply Reply More Options
Post Options
Reply as PM Reply as PM
Print Print
Mark as unread Mark as unread
Relationship Relationship
IP Logged

Previous
Next
 From:  Michael Gibson
11150.7 In reply to 11150.6 
Hi Moi3dFan,

re:
> One last question, the autosave options only apply when the entire interface is reloaded or if reopen moi3d

I think this is because you need the script code inside of SidePane.htm to run again and currently the code in there is only set to run when the SidePane.htm document is loading.

You'll need to package up the code in SidePane.htm so the timer can be cleared and reset when the settings change.

So for example change the script code in SidePane.htm to something like this (untested):
code:
			<script>
				var myTimer = false;

				function SetAutosaveTimer()
				{
					if ( myTimer )
						clearInterval( myTimer );

					... get any value from moi.ini and set up timer
				}

				// Call SetAutosaveTimer() on SidePane.htm load to get it running.
				SetAutosaveTimer();
			</script>



Then in some other location like in GeneralOptions.htm when you want to clear and reset the timer, you can call that function inside SidePane.htm like this:

moi.ui.sidePane.SetAutosaveTimer();

- Michael
  Reply Reply More Options
Post Options
Reply as PM Reply as PM
Print Print
Mark as unread Mark as unread
Relationship Relationship
IP Logged

Previous
Next
 From:  Moi3dFan
11150.8 In reply to 11150.7 
I tried this method, it passes without error.
But unfortunately it didn't help.
Only reloading the interface leads to results
  Reply Reply More Options
Post Options
Reply as PM Reply as PM
Print Print
Mark as unread Mark as unread
Relationship Relationship
IP Logged

Previous
Next
 From:  Michael Gibson
11150.9 In reply to 11150.8 
Hi Moi3dFan, can you please show your updated SidePane.htm file?

- Michael
  Reply Reply More Options
Post Options
Reply as PM Reply as PM
Print Print
Mark as unread Mark as unread
Relationship Relationship
IP Logged

Previous
Next
 From:  Moi3dFan
11150.10 In reply to 11150.9 
Yep!

p.s. I have textID configured


  Reply Reply More Options
Post Options
Reply as PM Reply as PM
Print Print
Mark as unread Mark as unread
Relationship Relationship
IP Logged

Previous
Next
 From:  Michael Gibson
11150.11 In reply to 11150.10 
Hi Moi3dFan, the problem is your function SetAutosaveTimer() inside SidePane.htm only consists of this:

code:
					function SetAutosaveTimer()
					{
						if ( Autosave == 0 ) clearInterval(AutosaveTimer);
					}


That's only clearing the timer, not setting it up.

The point of making the function is for it to contain the setup code as well. So you need to move the code for setting up the timer into the function.

Something like this (untested, may have typos):

code:
					var AutosaveTimer = false;
					var Autosave = 0;
					var AutosaveIncremental = false;
	
					function SetAutosaveTimer()
					{
						if ( AutosaveTimer ) clearInterval(AutosaveTimer);

						Autosave = moi.command.getOption( 'Autosave', true); // return autosave time (ms) from ini (180000 = 3 min)
						AutosaveIncremental = moi.command.getOption( 'AutosaveIncremental', true); //return AS mode (false)

						AutosaveTimer = setInterval(function() {
							if (AutosaveIncremental) { 
								moi.command.execCommand('IncrementalSave');
							} else { 
								moi.command.execCommand('Save');
							}
						}, Autosave)

					}
					SetAutosaveTimer();


Also since the setup is retrieving the values from moi.ini you need to alter the code in GeneralOptions.htm so it will store the values in moi.ini before it calls moi.ui.sidePane.SetAutosaveTimer(); in addition to doing it in the Shutdown() function.

- Michael
  Reply Reply More Options
Post Options
Reply as PM Reply as PM
Print Print
Mark as unread Mark as unread
Relationship Relationship
IP Logged

Previous
Next
 From:  Moi3dFan
11150.12 In reply to 11150.11 
Hi Michael! I'm back to you.
I've set everything up as you indicated and it seems to be working fine.
Later it turned out that the Shutdown() function does not run moi.ui.sidePane.SetAutosaveTimer(); in SidePane

This came to light when I changed the theme and my reloaded all the functions. Moi3d gives this error.

What could it be?



  Reply Reply More Options
Post Options
Reply as PM Reply as PM
Print Print
Mark as unread Mark as unread
Relationship Relationship
IP Logged

Previous
Next
 From:  Michael Gibson
11150.13 In reply to 11150.12 
Hi Moi3dFan,

re:
> This came to light when I changed the theme and my reloaded all the functions. Moi3d gives this error.
>
> What could it be?

Can you post your GeneralOptions.htm and SidePane.htm files ?

- Michael
  Reply Reply More Options
Post Options
Reply as PM Reply as PM
Print Print
Mark as unread Mark as unread
Relationship Relationship
IP Logged

Previous
Next
 From:  Moi3dFan
11150.14 In reply to 11150.13 
Here you go!

  Reply Reply More Options
Post Options
Reply as PM Reply as PM
Print Print
Mark as unread Mark as unread
Relationship Relationship
IP Logged

Previous
Next
 From:  Michael Gibson
11150.15 In reply to 11150.14 
Hi Moi3dFan, I guess during a UI reload (when changing the theme) you can get in the situation that SidePane.htm has not finished loading yet while the Shutdown function is running.

So to guard against an error there try changing the last like of function Shutdown() to this:

code:
if ( moi.ui.sidePane && moi.ui.sidePane.SetAutosaveTimer )
    moi.ui.sidePane.SetAutosaveTimer();


- Michael
  Reply Reply More Options
Post Options
Reply as PM Reply as PM
Print Print
Mark as unread Mark as unread
Relationship Relationship
IP Logged

Previous
Next
 From:  Moi3dFan
11150.16 In reply to 11150.15 
The error no longer occurs. But there is a new one.

The setting only works once. The video shows that at first it was 0, then I changed it to 0.1 minute, everything worked as it should. But when I returned 0 minutes, autosave didn't turn off.

EDITED: 14 Nov 2023 by MOI3DFAN

  Reply Reply More Options
Post Options
Reply as PM Reply as PM
Print Print
Mark as unread Mark as unread
Relationship Relationship
IP Logged

Previous
Next
 From:  Michael Gibson
11150.17 In reply to 11150.16 
Hii Moi3dFan, I think maybe you're getting multiple timers going.

Probably the very first thing that should be done inside function SetAutosaveTimer() is to clear any previous timer, by doing:

code:
if ( AutosaveTimer )
{
    clearInterval( AutosaveTimer );
    AutosaveTimer = false;
}


If Autosave is 0 then it's probably better to exit out right after that, currently it sets up a timer and then will clear it out at the end.

So maybe more like this:

code:
				function SetAutosaveTimer()
				{
					// Clear any existing timer.
					if ( AutosaveTimer )
					{
						clearInterval( AutosaveTimer );
						AutosaveTimer = false;
					}

					Autosave = moi.command.getOption( 'Autosave', true); // return autosave time (ms) from ini (180000 = 3 min)
					AutosaveIncremental = moi.command.getOption( 'AutosaveIncremental', true); //return AS mode (false)

					// If Autosave is turned off, exit without setting any timer up.
					if ( Autosave == 0 )
						return;

					AutosaveTimer = setInterval(function() {
						if (AutosaveIncremental) { 
							moi.command.execCommand('IncrementalSave');
						} else { 
							moi.command.execCommand('Save');
						}
					}, Autosave);
				}
  Reply Reply More Options
Post Options
Reply as PM Reply as PM
Print Print
Mark as unread Mark as unread
Relationship Relationship
IP Logged

Previous
Next
 From:  Moi3dFan
11150.18 In reply to 11150.17 
It's working! Thanks, how likely is it that autosave will appear in the native version of moi3d v5 beta?
  Reply Reply More Options
Post Options
Reply as PM Reply as PM
Print Print
Mark as unread Mark as unread
Relationship Relationship
IP Logged

Previous
 From:  Michael Gibson
11150.19 In reply to 11150.18 
Hi Moi3dFan,

re:
> It's working! Thanks, how likely is it that autosave will appear in the native version of moi3d v5 beta?

Not too likely, there are a lot of details to work out for a real version of it.

- Michael
  Reply Reply More Options
Post Options
Reply as PM Reply as PM
Print Print
Mark as unread Mark as unread
Relationship Relationship
IP Logged
 

Reply to All Reply to All