Script for automating Switch layers consecutively?

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

Moderators: Víctor Paredes, Belgarath, slowtiger

User avatar
yearn2animate
Posts: 17
Joined: Mon Jun 21, 2021 7:49 pm
Contact:

Script for automating Switch layers consecutively?

Post by yearn2animate »

Hi all,

I have some effect frame by frame images I've brought into ASP11. I am wondering if there is a script available that will place your switch layers one after the other at certain time increments. The desired effect would lay all the files in order one by one on the timeline and I could tweak it once its on the timeline. Since it is an environmental effect, I have quite a few frames drawn and would love a way to speed up the process before adjusting the final timing.


Is there a script that does this available? If not, is there someone who can help me put it together and possibly break down the steps on how it works? I would love to get into the scripting, and can modify code sometimes to my needs, but I am very much a novice who would like to know more. Thanks for looking, hope there is a easier solution than laying each layer one by one!

Kris
User avatar
Greenlaw
Posts: 9270
Joined: Mon Jun 19, 2006 5:45 pm
Location: Los Angeles
Contact:

Re: Script for automating Switch layers consecutively?

Post by Greenlaw »

I'm guessing you don't want to import as an image sequence because you need to re-time your frames. But go ahead and import the sequence as an Image Sequence layer and then...

1. ...right-click on the Image Sequence layer and select Convert To Switch Layer...

Image

2. The result...

Image

Hope this helps.
User avatar
slowtiger
Posts: 6081
Joined: Thu Feb 16, 2006 6:53 pm
Location: Berlin, Germany
Contact:

Re: Script for automating Switch layers consecutively?

Post by slowtiger »

Scripts > Images > Import Image Sequence does exactly that.
AS 9.5 MacPro Quadcore 3GHz 16GB OS 10.6.8 Quicktime 7.6.6
AS 11 MacPro 12core 3GHz 32GB OS 10.11 Quicktime 10.7.3
Moho 13.5 iMac Quadcore 2,9GHz 16GB OS 10.15

Moho 14.1 Mac Mini Plus OS 13.5
User avatar
Greenlaw
Posts: 9270
Joined: Mon Jun 19, 2006 5:45 pm
Location: Los Angeles
Contact:

Re: Script for automating Switch layers consecutively?

Post by Greenlaw »

Note that you'll need to keyframe every frame to switch. This isn't too awful to do if you use shortcuts or the Switch Selection window, and if the sequence isn't too long.

Depending on how long the sequence is and how often will be re-editing it, you might consider making this into a Smart Bone action so you can flip through frames non-linearly using a Bone Dial.
Last edited by Greenlaw on Sun Jul 04, 2021 10:44 pm, edited 2 times in total.
User avatar
hayasidist
Posts: 3526
Joined: Wed Feb 16, 2011 8:12 pm
Location: Kent, England

Re: Script for automating Switch layers consecutively?

Post by hayasidist »

I think what's wanted is the auto key-framing which, by pure coincidence I did a while back. The following is designed to run inside my test harness, so will need to be modified, but as the OP said they'd like to do that:

Code: Select all

function HS_SequenceStills:Run(moho, view)

	HS_Test_harness:tracePrint (thisUUT, self:Name(), self:Version())
	

	moho.document:PrepUndo(moho.layer)
	moho.document:SetDirty()

	local step = 2
	local layer = moho.layer
	local d, i, j, k, m, n, p, s, v, w

	local sw_layer = moho:LayerAsSwitch(layer)
	if not sw_layer then
		HS_Test_harness:tracePrint (thisUUT, layer:Name(), "not a switch layer")
		return
	end

	local ct = sw_layer:CountLayers()
	local frame = moho.frame

	for i = 0, ct-1 do
--		layer = sw_layer:Layer(i)
		sw_layer:SetValue(frame, sw_layer:Layer(i):Name())
		frame = frame + step
	end
	HS_Test_harness:tracePrint (thisUUT, "done", ct, "layers")


end
You'll need to take out the "test harness" calls and put this function into an "empty" script. This: http://mohoscripting.com/new_script.php will help you set up an "empty" script -- to get strated, you'll need a button/menu with nothing else.

When / if you get stuck, just shout!
User avatar
yearn2animate
Posts: 17
Joined: Mon Jun 21, 2021 7:49 pm
Contact:

Re: Script for automating Switch layers consecutively?

Post by yearn2animate »

Thanks for the quick replies all!

Still learning the ins and outs of the software, so thanks for the heads up on Image sequencer. The sequence I have is just steam rising from a coffee cup, and it was done on ones so re-timing isn't really necessary... (but we will see.)

Hayasidist, thanks for sharing your code. I am pretty good with teaching myself most things, but programming isn't my strongest suit. I will see what I can do your script and reach out if I find it too perplexing. Thanks again!


This community is great, I appreciate all the help.


Kris
User avatar
SimplSam
Posts: 1048
Joined: Thu Mar 13, 2014 5:09 pm
Location: London, UK
Contact:

Re: Script for automating Switch layers consecutively?

Post by SimplSam »

This came up a little while ago (as discussed here: Distribute layers to frames)

If you have your Content Layers in a Switch you can use my Switch 2 FBF script: ss_switch2fbf

To Use: Ensure the target layers are grouped into a Switch layer as uniquely named sub-layers. Select the Switch layer, and run the tool.

You can optionally set:
  • FBF Interval: - The Frame by Frame interval. (default: 1)
  • Start with: - The translation sort order: Top Layer for Top to Bottom (top => 1st frame), Bottom Layer for Bottom to Top (bot => 1st frame). (default: Top Layer)
I think it essentially does the same as hayasidist's scriptlet, but is a fleshed out tool.

Image
Moho 14.1 » Win 11 Pro 64GB » NVIDIA GTX 1080ti 11GB
Moho 14.1 » Mac mini 2012 8GB » macOS 10.15 Catalina
Tube: SimplSam


Sam
User avatar
Greenlaw
Posts: 9270
Joined: Mon Jun 19, 2006 5:45 pm
Location: Los Angeles
Contact:

Re: Script for automating Switch layers consecutively?

Post by Greenlaw »

slowtiger wrote: Sun Jul 04, 2021 6:04 pm Scripts > Images > Import Image Sequence does exactly that.
Interesting...this script doesn't exist for Moho 12.5 or 13.5.1, not by default anyway.

Ok, I just checked, and it needs to be added to the Scripts menu. The script is called js_image_sequence.lua and it's located in the Custom Content folder under Extra Files/Optional Scripts/Menu/Image.

From what I recall, it's fussy about using a specific naming format, which is why I don't use it. The Convert To Switch layer accepts most common file naming conventions.
Last edited by Greenlaw on Sun Jul 04, 2021 7:37 pm, edited 2 times in total.
User avatar
Greenlaw
Posts: 9270
Joined: Mon Jun 19, 2006 5:45 pm
Location: Los Angeles
Contact:

Re: Script for automating Switch layers consecutively?

Post by Greenlaw »

Oh, that's pretty cool simplSam. Will check it out.
User avatar
Greenlaw
Posts: 9270
Joined: Mon Jun 19, 2006 5:45 pm
Location: Los Angeles
Contact:

Re: Script for automating Switch layers consecutively?

Post by Greenlaw »

Greenlaw wrote: Sun Jul 04, 2021 7:33 pm From what I recall, it's fussy about using a specific naming format, which is why I don't use it. The Convert To Switch layer accepts most common file naming conventions.
I looked at the script and according comments in the header, it seems that some of the issues I ran into in the past may have been addressed...

LIMITATIONS:
-must have some valid extension
-must have valid numbering
-don't use numbers in names in the last 10 chars...
DONE:
-support all padding
-support extensions other than 4 char (ex : .ai )
-ImportEPS(path)
TODO:
-visibility off after last frame
-menu to choose frame step etc...
-insert at current time
-any request?


I haven't tried it, so just an FYI for anybody who wants to install it.
Last edited by Greenlaw on Sun Jul 04, 2021 10:42 pm, edited 1 time in total.
User avatar
yearn2animate
Posts: 17
Joined: Mon Jun 21, 2021 7:49 pm
Contact:

Re: Script for automating Switch layers consecutively?

Post by yearn2animate »

SimplSam, This is what I pictured in my head. Thanks for sharing, will be using this in the future.

Import Image Sequence worked well enough, though. Had Sam not shared it would of been my need-it-now solution. I look forward to looking over both Sam and Hayasidist's codes to start my scripting journey...


Kris
User avatar
synthsin75
Posts: 9981
Joined: Mon Jan 14, 2008 11:20 pm
Location: Oklahoma
Contact:

Re: Script for automating Switch layers consecutively?

Post by synthsin75 »

Greenlaw wrote: Sun Jul 04, 2021 7:33 pm Interesting...this script doesn't exist for Moho 12.5 or 13.5.1, not by default anyway.

Ok, I just checked, and it needs to be added to the Scripts menu. The script is called js_image_sequence.lua and it's located in the Custom Content folder under Extra Files/Optional Scripts/Menu/Image.
Since v12 we now have image sequence under the standard File>Import options.
User avatar
SimplSam
Posts: 1048
Joined: Thu Mar 13, 2014 5:09 pm
Location: London, UK
Contact:

Re: Script for automating Switch layers consecutively?

Post by SimplSam »

Just realised that 13.5 does not handle Imported GIF Image Sequences correctly - and the resultant layer only appears to have a single frame.

Whereas importing the same sequence of GIF's to Moho 12, or a sequence of PNG's to Moho 12/13.5 animate as expected.

Reported to LM Support.
Moho 14.1 » Win 11 Pro 64GB » NVIDIA GTX 1080ti 11GB
Moho 14.1 » Mac mini 2012 8GB » macOS 10.15 Catalina
Tube: SimplSam


Sam
User avatar
Greenlaw
Posts: 9270
Joined: Mon Jun 19, 2006 5:45 pm
Location: Los Angeles
Contact:

Re: Script for automating Switch layers consecutively?

Post by Greenlaw »

synthsin75 wrote: Sun Jul 04, 2021 11:34 pm Since v12 we now have image sequence under the standard File>Import options.
Yes, I know but it imports it as a single Sequence Layer, not individual layers for every frame inside a Switch Layer, which is what I thought the OP wanted. I was just pointing out that an Image Sequence layer can be converted to separate image layers using the Convert To Switch Layer command. But it sounds like all he needed was the standard image Sequence layer after all.

slowtiger suggested using the legacy (ASP 11) script that imports separate image layers directly, which is no longer included in the (Moho 12.5/13.5.1) Script menu by default. But that's probably because with the Convert To Switch layer command, it's really not necessary anymore.
Last edited by Greenlaw on Mon Jul 05, 2021 9:40 pm, edited 1 time in total.
User avatar
yearn2animate
Posts: 17
Joined: Mon Jun 21, 2021 7:49 pm
Contact:

Re: Script for automating Switch layers consecutively?

Post by yearn2animate »

In this case, the regular image sequencer would suffice, but SimplSam's solution was what I was looking for in a scripting option.
Post Reply