Trying to get a reliable anytime LayerScript() call counter...

Moho allows users to write new tools and plugins. Discuss scripting ideas and problems here.

Moderators: Víctor Paredes, Belgarath, slowtiger

User avatar
hayasidist
Posts: 3562
Joined: Wed Feb 16, 2011 8:12 pm
Location: Kent, England

Re: Trying to get a reliable anytime LayerScript() call counter...

Post by hayasidist »

I took a sideways look at this... finite state machine... rather than worrying about counting which run it's doing, the script decides which step it needs to take next.


Code: Select all

function LayerScript(moho)
	if state == nil then
		state = 0
	end

	if not count then
		count = 0
	end
	print (count, "State = ", state,  "  Active tool: ", moho:CurrentTool())
	count = count + 1


	local function doState1 (x)
		print ("state 1 " .. x)
		return true, 2
	end

	local function doState2 (x)
		print ("state 2 " .. x)
		return true, 3
	end

	local function doState3 (x)
		print ("state 3 " .. x)
		local v3 = LM.Vector3:new_local()
		v3 = moho.layer.fTranslation:GetValue(moho.frame)
		v3.x = 0
		moho.layer.fTranslation:SetValue(moho.frame, v3)
		return true, 0
	end


	local disp ={doState1, doState2, doState3}

	local rtx


	local name = moho.layer:Name()

	if state == 0 then
		if moho.layer.fTranslation.value.x ~= 0 then
			state = 1
		end

	else
		rtx, state = disp[state](name)
	end
	moho:UpdateUI()
end

what this does:

when "whatever it is that it's looking for" happens (here the layer moves off x=0) it moves out of its idle state (state 0) and starts the (here) 3-step process to put things right.

the critical item here is that the state<n> subroutines MUST return the next state to process. This can be determined algorithmically - e.g. state 1 might need to decide if state 2 or state 3 should be next (here I've kept it simple -- it's always the next one)

I've not tried to use external functions or to have "state" set by external means, although there's no reason why not (e.g. rather than this layerscript deciding to move out of "idle", some external force can nudge it)


I **should** have made an "idle" function that returns "idle" or "begin"... IOW the "If state == 0 etc" should have gone into a function that returns 0 if still nothing or 1 if there's stuff to do ... but I didn't! (mea culpa!)


===

Hope that's helpful.
User avatar
Rai López
Posts: 2250
Joined: Sun Aug 08, 2004 1:41 pm
Location: Spain
Contact:

Re: Trying to get a reliable anytime LayerScript() call counter...

Post by Rai López »

Hi, Paul! Trying to figure out how exactly works, but I easily get a little lost... I think I can grasp the functioning of some parts al least, but not sure what for the "rtx" variable is getting the returned bool for example, or why a moho:UpdateUI() is necessary in this case at the end, or is it there simply just in case other possible actions different than fTranslation (in this case) changes be correctly updated in UI if necessary?

Feel free to correct me, but I understand that the script goes from state 0 (or idle) to state 1 as soon as the condition (in this case "fTranslation.value.x ~= 0") is met after the very first mouse dragging takes place and then pass to state 2 and then to state 3, which seems to correspond to the three calls of the same cycle and seems to work reliably as far as I've tested at least, which would be a very good thing, but... what possible advantages could have this different approach respect to the previous one we've been using being both based on restarting a run counter after the user making a change? I mean, the twist sounds interesting, don't get me wrong! But it's also much more convoluted if it's not carrying some kind of advantages (or it's simply they are there and I can't see them) at the same time.

In any case and of course, thanks for spending the time struggling about it yet! Although at this point I'm almost convinced that nothing different than an official API entry for getting the current LayerScript() cycle call count is going to offer a final solution, or what I was looking for at least (a cycle call counter not based on certain element changes, but always disposable no matter what).

Well, greetings and I hope my deductions make some sense!
...
User avatar
hayasidist
Posts: 3562
Joined: Wed Feb 16, 2011 8:12 pm
Location: Kent, England

Re: Trying to get a reliable anytime LayerScript() call counter...

Post by hayasidist »

all fair questions, Ramon!

the state machine can work across multiple layer scripts - so (e.g.) in layer 1 it has processing in states 1 and 3 only, but layer 2 does state 2 -- IOW to complete a task, if two layers need to do things in a cooordinated manner they only process their own states (that was my - admittedly less than clearly explained - reference to globals and external forces). In the above example, layer 1's idle states are 0 and 2; layer 2's idle states are 0, 1, 3 - when it finishes doing 2, it sets the state to 3 and then layer 1 kicks off again to finish the job. Each moho-run cycle services only one state. So, the script needs to be run by moho "many" times to finish.

the rtx boolean is overkill - it's a safety valve that could be used to do some master reset if things go wrong -- can almost certainly be deleted

the UpdateUI was there to try to nudge moho into running another layerscript cycle as soon as the script had finished processing a state. I don't think it worked as I'd intended... but it would be useful to have the script "do something" to wake moho up when it has done its (non-idle) job.

layer translation x was chosen just as an easy example - I didn't try to address the "this layer is in the wrong place" scenario - it felt "too hard" at the time.


IMO, counting how many times moho kicks off a script is not a complete answer - because in one moho-initiated cycle different scripts seem to run different numbers of times -- and I'm not even sure that there's a consistent pattern (I didn't thoroughly analyse the output from the simple test viewtopic.php?p=206937#p206937)
User avatar
Rai López
Posts: 2250
Joined: Sun Aug 08, 2004 1:41 pm
Location: Spain
Contact:

Re: Trying to get a reliable anytime LayerScript() call counter...

Post by Rai López »

Thanks, Paul! I think between the two explanations I can get a better understanding of some parts that backed me out a little at first.

One thing, tho... I understand that you have to know beforehand how many LayerScript() calls Moho is going to perform depending on the used trigger, isn't? I mean, here the action of translating the layer seems to force a 4 calls cycle (assuming state 0 is also a call of the same cycle, as it seems to be, but not totally sure yet), so... could one of the things to take into account in order to make it work properly the knowing of how many calls the trigger is going to perform? Otherwise, if (e.g.) the trigger only forced 2 calls, the state 3 could never be reached and layer translation wouldn't be reset again to x = 0, thing that would ruin the bucle/logic, isn't? I don't think that should necessarily represent a problem because Moho seems quite consistent on doing the same number of calls per a certain change/interaction, but something to take into account while coding de logic, as I see it (although I could be wrong).

Well, as I said, very interesting... I think I'll continue using the other method for now because currently I see it more under my control/understanding, but it's good to know there is another, with apparently more possibilities, way just in case the situation require it. Greetings!
...
User avatar
hayasidist
Posts: 3562
Joined: Wed Feb 16, 2011 8:12 pm
Location: Kent, England

Re: Trying to get a reliable anytime LayerScript() call counter...

Post by hayasidist »

Ramón López wrote: Thu Sep 22, 2022 9:50 pm ... you have to know beforehand how many LayerScript() calls Moho is going to perform depending on the used trigger ...
that's why the UpdateUI() at the end of the script -- to force another layerscript cycle if needed -- although I'm not convinced that this was actually achieved every time ... something else might be needed instead!? But, unless the need for this approach becomes pressing, I'm parking it for now -- I'm satisfied that the concept is working -- all I now have to do is remember how to find this thread when I need to revisit! :D :wink:
User avatar
Rai López
Posts: 2250
Joined: Sun Aug 08, 2004 1:41 pm
Location: Spain
Contact:

Re: Trying to get a reliable anytime LayerScript() call counter...

Post by Rai López »

Ahhh... Ok, just wanted to be sure about that aspect, so thanks for clarifying.

And yes, I also think the conclusions and workarrounds achived well could be considered enough, taking into account the nature of the issue, for go packing and try to continue working on another things. Of course I still find mysef mulling over it at times, but it's only a matter of time since, in this case and with what we have, it (will) never end up on something really worthy...

In anycase, I learned some things on the way and can turn page thanks to you both, so thanks again for all the time and attention!
...
Post Reply