Possible solution for multiple layer scripts

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

Moderators: Víctor Paredes, Belgarath, slowtiger

Post Reply
User avatar
heyvern
Posts: 7035
Joined: Fri Sep 02, 2005 4:49 am

Possible solution for multiple layer scripts

Post by heyvern »

I love layer scripts, they are awesome. One of the problems is how to have multiple layer scripts effect one layer. I could rewrite the code to include multiple effects in custom scripts, but that is a pain and not flexible. I need a flexible solution that could work for any and all combinations of multiple layer scripts.

This came about because I FINALLY created a "Translation Constraint" bone layer script that works great. I can draw vector shapes to confine the translation of a bone. I can create "sliders" so bones can only travel in the x or y axes. The code is really small and efficient. The trouble is I would want to use this functionality with other scripts without having to add the same code over and over. So I need a way to have this script work WITH other layer scripts.

My first idea is to use nested layers to hold multiple layer scripts that "cascade" down the layer hierarchy. This of course would involve some sort of mechanism to determine if the layer a script is on wil run on the layer it's embedded in or a sub layer, and which sub layer. This will add a bit of "bulk" to the projects requiring an additional parent layer for each script and could get quite messy.

My second idea be a completely separate group layer as a "holder" for multiple layer scripts. SInce layer scripts can access any layer in the entire document AND you can store layer script data in the layer itself, you could target layers from anywhere. If you need a new layer script, add a layer to your "Script Group" and embed the script. A pop up would allow you to choose the layer or LAYERS to apply the layer script to. Or maybe I create a custom tool to work with these script layers so you could add or remove layers from the script.

Any thoughts and suggestions are welcome.
Rudiger
Posts: 786
Joined: Sun Dec 18, 2005 2:25 am

Re: Possible solution for multiple layer scripts

Post by Rudiger »

Now that we can store any information we want in an anime file, do you really need to use additional layers to store the embedded scripts?

The way I would do it is have a Menu script to manage the embedded scripts for a layer. When you ran the script it would look up all of the embedded scripts that have been applied to the current layer as this list would be stored in the file. You would then be able to add or remove scripts from the list. When you clicked OK, it would merge all of the scripts into one that is actually used by AS as the layer's only actual embedded script.

Merging the embedded scripts is actually the easy part. Say you had 2 scripts, trans_constraint.lua and mesh_instance.lua, the merged script would look like the following:

Code: Select all

function trans_constraint_LayerScript(moho)
.
.
.
end
function mesh_instance_LayerScript(moho)
.
.
.
end
function LayerScript(moho)
    trans_constraint_LayerScript(moho)
    mesh_instance_LayerScript(moho)
end

User avatar
funksmaname
Posts: 3174
Joined: Tue May 29, 2007 11:31 am
Location: New Zealand

Re: Possible solution for multiple layer scripts

Post by funksmaname »

ooh - would it be possible to have an 'installed scripts' list so you can just pick them from a list (move from left box to right or something) - that would make adding, removing and combining scripts a dream!
User avatar
heyvern
Posts: 7035
Joined: Fri Sep 02, 2005 4:49 am

Re: Possible solution for multiple layer scripts

Post by heyvern »

I've been thinking about this a bit.

Based on Rudiger's idea, my only concern is the "merging" of the scripts. Would this be a saved out file as a new script? Would it be loaded into ram? Or could it simply be references (paths) to other script files?

Another thought is how much can be done with the metadata storage in the layer. Could you save an entire script in the metadata or would this break the file format because of all of the funky code stuff? I have to test this.

What I like is the idea of a "master" script file in one "script layer". This layer would contain a list (metadata) of layers and paths to scripts. You can only actually have one embedded script per layer. Running multiples would require targeting each layer in the list. This could be done from the master script layer. This layer would have a single layer script that runs the mechanism. It would create value pairs in the layer metadata. Two ways to store the info; all layer/script pairs could be stored in one layer, or the info could be stored in each layer.

This system would need either a button or menu script to choose layers and script files (or functions from a master script).

So here's my process:

1 master script "holder" layer
1 embedded layer script in the master script layer
Master script button/menu
(I suppose special tool should work only on the master layer
or it could work on any layer but that would be confusing and complicated)


Steps for usage:
Select master script layer
Select tool or run button or menu script

Pop up displays layer list and a file chooser for scripts
Select a layer choose multiple scripts to effect that layer
or
Choose a script and add layers to it
(either way would be almost the same I think
Maybe the list of scripts could be "bulk" loaded from a folder)


Once selections are made click the "okay" button on popup.
Script paths and layer ID combos are saved to metadata in the ASP document

The master script in the master script layer then runs the system. it will read in the path to the script file and run the functions...

uh... er... crap... this is the part that always gets me. How to read in the functions as actual lua code from an external file? I am concerned about performance. Often simply embedding scripts can bog down. With this system there would have to be some sort of "emulation" of the code read in to ram. Like, read in a chunk of an external file and use lua to "execute" it. This could get... icky.

That is why I keep falling back to having a layer with each script embedded in it. The master script layer could be a group layer. Each script would be embedded in a separate layer inside the group. You would have a layer for each script to run on multiple layers.

This would be WAY easier to accomplish. The embedded script is right there and ready to do it's stuff. You just need a bit of "don't do anything unless this is true" crud in the front of it. Each layer script would only run on the layer pulled from the metadata list. The problem with these script files is that they will run if you don't stop them. If they are in a layer and things aren't correct you will just get errors. So, the code to stop the script from running unless "told" to run must be in each script on each layer. This would also be much better when using the "Gather Media" feature. Since all the scripts are embedded they will be copied automatically. I don't know if this could work with script path storage.

I know this sounds a bit complicated but it should work pretty well and not actually be that complicated in action.

Just brainstorming... nothing locked down yet.
User avatar
heyvern
Posts: 7035
Joined: Fri Sep 02, 2005 4:49 am

Re: Possible solution for multiple layer scripts

Post by heyvern »

Ooooh! Brainstorm!!!

The button/menu script pop up could CREATE THE LAYERS AND EMBED THE SCRIPTS!!!!

This would make the process almost invisible to the user. The group layer with the script layer holders would never have to be accessed directly. The special button script could be run on any layer. You could simply run the special tool and if there is no "Master Script Layer" it would create it for you. You would also have a load file function to import scripts. Load the scripts and it would create the layers and embed that script without having to do anything.

If you delete a script using the special button/menu script, it would delete that layer for you. All of this would happen without having to manage the layers.

I think having a layer for each script is the only way to make this work.
User avatar
heyvern
Posts: 7035
Joined: Fri Sep 02, 2005 4:49 am

Re: Possible solution for multiple layer scripts

Post by heyvern »

Woohoooo!!! Testing is going great!

My popup script test does the following successfully:
- Loads a list of all layers in a group or document in a list box
- Load and save a list of external script files with path and file names in a list box

Doesn't sound like much but this is the main starting point. The script can access all the layers in the document before anything happens. This is step 1. With the list of layers and layer IDs the list of external script files can be ASSIGNED to the list of layers and this data can be stored. Since this script can access the layer/s it can read in the metadata.
I was worried that the limitations of scripting in a popup wouldn't be able to do this.

Now comes the hard part of storing the lists in the metadata and then running scripts from layers on the layer list. This is just tedious detail work.

I think this will work!!! This is freaking awesome!

EDIT:
I think the metadata of applied scripts to layers will be stored in the individual layer metadata instead of all of the data stored in just the master script layer. That way if you add new layers or change layers etc, the layer metadata "keys" will stay with each assigned layer. Once a layer is assigned multiple script lists to run on it, this won't change when modifying the layer order or structure. Deleting and adding layers won't muck up the list.
If this info was stored in a master layer list the layer IDs would change if layers are changed and the master metadata wouldn't match the actual layers.

So running the script again will simply read in the layers custom metadata and build the lists based on that info. It should work great.
User avatar
heyvern
Posts: 7035
Joined: Fri Sep 02, 2005 4:49 am

Re: Possible solution for multiple layer scripts

Post by heyvern »

Bummer,

So a textlist gui element in ASP doesn't allow for multiple selections. You can only choose one item from a text list box.

The only way to do this would be to use the textlist for layers, and checkboxes for loaded scripts. This would be much better since there is going to be a lot less loaded scripts than layers. The checkboxes take up a lot more space so using them for the smaller list of items makes more sense.

Other than that small glitch everything works so far. I have successfully stored a script file path from a pop up file selection in a pop up script window in the metadata of a layer.

I was just thinking... if they added the ability to have more than one embedded script JUST for scripting this could be easier to do. I still think a scrolling list inside the layer palette for embedded scripts would be an awesome feature.
Post Reply