Moi + Affinity : Moi for 2D workflows

 From:  mkdm
8629.48 
Hi Michael.

I'm pretty sure that you already answered me a long time ago, about this question, but I don't remember where is your answer.
I've searched trough the forum by I didn't find what I'm looking for.

The question is this :

If we simply take into account the "speed of execution" factor, can we state that the code executed inside a the "hmtl" page of a command
or inside a script placed into the "scripts" folder, is faster (and many times faster) the the execution of "js code" present in the "js" counterpart of command's html ?

I ask you this because I have in mind to turn the next version of my "script for 2D workflow" from "script" into "command" just in order to
handle more easily the various working mode of that scripts.

For what I've experienced and learned I've seen that the "js" code executed inside the Hml file of command is much more faster that the counterpart executed into the "js" file of the command :

For example consider the "ScaleIndividual" commands that Max wrote times ago :

1) the original version of Max Smirnov :
get it at https://drive.google.com/open?id=1waO3TgOQSUl2ZIBJ8adixPz5Qes_ja21

2) A version that I have modified and that do the main task into the "js" code inside the Html file of the command :
get it at https://drive.google.com/open?id=1N6qWe_lzyJA-aTOrmESLRPhzky_XHhoB


Why the 2nd version, is much more faster than the first (to make a good test you have to select many objects, I tested with hundreds) ?


This is the code of the original version of ScaleIndividual.js and ScaleIndividual.htm :

code:
#include "GetObjects.js"
#include "WaitForDialogDone.js"

function ScaleObject( obj, factor )
{
	var center = obj.getBoundingBox().center;
	var list = moi.geometryDatabase.createObjectList();
	list.addObject( obj );

	var factory = moi.command.createFactory( 'scale' );
	factory.setInput( 0, list );
	factory.setInput( 1, center );
	factory.setInput( 2, factor );
	factory.commit();
}

function DoIndividualScale()
{
	var objectpicker = moi.ui.createObjectPicker();
	if ( !GetObjects( objectpicker ) )
		return;
		
	var objects = objectpicker.objects;
		
	moi.ui.beginUIUpdate();
	moi.ui.hideUI( 'SelectPrompt' );
	moi.ui.showUI( 'OptionsPrompt' );
	moi.ui.showUI( 'options' );
	moi.ui.endUIUpdate();
	
	if ( !WaitForDialogDone() )
		return;
		
	var scalefactor = moi.ui.commandUI.factor.value;
		
	for ( var i = 0; i < objects.length; ++i )
	{
		var obj = objects.item(i);
		ScaleObject( obj, scalefactor );
	}
}

DoIndividualScale();


code:
<html>
	<body class="commandbody">
		<div class="commandheader">
			<div id="SelectPrompt" class="commandprompt">Select objects to scale</div>
			<div id="OptionsPrompt" class="hiddencommandprompt">Scale options</div>
		</div>
		
		<div id="options" class="hiddencommandoptions">
			<table>
				<tr>
					<td>Scale factor:</td>
					<td><moi:NumericInput id="factor"/></td>
				</tr>		
			</table>
		</div>

		<moi:CommandDoneCancel />
	</body>
</html>



And this is the code of my modified version of ScaleIndividualMainThread.js and ScaleIndividualMainThread.htm :

code:
#include "GetObjects.js"
#include "WaitForDialogDone.js"

function DoIndividualScaleInit() {
	var objectpicker = moi.ui.createObjectPicker();
	if ( !GetObjects( objectpicker ) )
		return;
		
	var objects = objectpicker.objects;
		
	moi.ui.beginUIUpdate();
	moi.ui.hideUI( 'SelectPrompt' );
	moi.ui.showUI( 'OptionsPrompt' );
	moi.ui.showUI( 'options' );
	moi.ui.endUIUpdate();
	
	if ( !WaitForDialogDone() )
		return;
	
	moi.ui.commandUI.DoIndividualScale(objects);
}

DoIndividualScaleInit();


code:
<html>
	<head>
		<script>		
			function ScaleObject( obj, factor ) {
				var center = obj.getBoundingBox().center;
				var list = moi.geometryDatabase.createObjectList();
				list.addObject( obj );

				var factory = moi.command.createFactory( 'scale' );
				factory.setInput( 0, list );
				factory.setInput( 1, center );
				factory.setInput( 2, factor );
				factory.commit();
			}

			function DoIndividualScale(objects) {
				var scalefactor = moi.ui.commandUI.factor.value;
					
				for ( var i = 0; i < objects.length; ++i ) {
					var obj = objects.item(i);
					ScaleObject( obj, scalefactor );
				}
			}
		</script>
	</head>
	<body class="commandbody">
		<div class="commandheader">
			<div id="SelectPrompt" class="commandprompt">Select objects to scale</div>
			<div id="OptionsPrompt" class="hiddencommandprompt">Scale options</div>
		</div>
		
		<div id="options" class="hiddencommandoptions">
			<table>
				<tr>
					<td>Scale factor:</td>
					<td><moi:NumericInput id="factor"/></td>
				</tr>		
			</table>
		</div>

		<moi:CommandDoneCancel />
	</body>
</html>



Thanks a lot for the support :)

Have a nice day.

Ciao!

Marco (mkdm)

EDITED: 4 Nov 2017 by MKDM