Page 1 of 4

Multi layer Magnet Script?

Posted: Wed Jun 15, 2011 1:43 am
by x-vishon
Is there a way to script a tool that lets you move points on multiple layers with the magnet tool. I want to use more then one layer for Line work and shading and be able to reposition the content of all the selected layers using the magnet tool. :?:

Posted: Fri Sep 02, 2011 1:15 pm
by GCharb
Been asking for this for ages now, and since I am just starting to script in Anime Studio I do not have the answer, hopefully someone more knowledgeable will answer!

Posted: Wed Sep 07, 2011 10:45 pm
by Rudiger
It should be possible simply by wrapping up the relevant code for each function in the Magnet Tool with the following...

Code: Select all

local document = moho.document
for i = 0, document:CountSelectedLayers()-1 do
    local layer = document:GetSelectedLayer(i)
    local mesh = layer:Mesh()

   .
   .
   [code for magnet, but with any "mesh =" lines  removed]
   .
   .

end

Posted: Wed Sep 07, 2011 10:52 pm
by GCharb
Thanks rudiger, will look into that when time permits!

Posted: Thu Sep 08, 2011 3:10 am
by Rudiger
No probs. BTW, you may want to make it check that the mesh is not nil before the Magnet Tool code, just in case you have any non vector layers selected.

Let me know how you get on. If it works on the Magnet Tool, it should be possible to make all of AS's point transform tools work on multiple layers at once, which would be huge.

Posted: Thu Sep 08, 2011 3:26 am
by GCharb

Code: Select all

local mesh = moho:Mesh()
	if (mesh == nil) then
		return
	end
I will, but need to finish my next tutorial on scripting first.

The script for the tutorial is done, just needs cleaning.

The script creates gears with various settings, fun stuff for a second script.

Here are 3 gears generated by the script.

Image

Will try to be done by Friday and should work on this next weekend.

Posted: Thu Sep 08, 2011 3:35 am
by Rudiger
The gears look cool. I can imagine all sorts of contraptions that could be built with them using AS's Physics engine.

With regards to the code you posted, that is the sort of thing I meant, only you don't want to return if the mesh is nil as you still want to keep looping through the other layers. Other languages have a "continue" of "next" statement that lets you jump straight to the next iteration of the loop, but Lua doesn't have an equivalent, so you will have to wrap the entire block of Magnet code in a "if mesh ~= nil then" branch.

Posted: Thu Sep 08, 2011 4:37 am
by Rai López
Hi! About the Multi-layer Magnet, that is one of the things that will be possible to do with my new tool; tool that, if no were for certain complications, it still would be available BTW... But! I think I can write this now cause, fortunately, I have just (re)found a legendary thread that seems to through some light precisely over the areas about I was totally blocked (maths! :x) and I hope it accelerates the process from now on :); although, even though, something tells me that you'll get it work long before I can even think to finish it...

Posted: Thu Sep 08, 2011 5:00 am
by sbtamu
Are you trying to get the teeth on the gears to work with physics? If so, I have tried that and failed so may times. That is why I ended up using the profile tool and just simulate the rotation with the physics motor. If you get this to work please let me know.

Posted: Thu Sep 08, 2011 5:26 am
by GCharb
sbtamu wrote:Are you trying to get the teeth on the gears to work with physics? If so, I have tried that and failed so may times. That is why I ended up using the profile tool and just simulate the rotation with the physics motor. If you get this to work please let me know.
Nah, just a script i wrote for my next scripting tutorial, a more advanced one then the first, which was the basics really.

Still, I will try and see if it works! :)

Posted: Thu Sep 08, 2011 5:44 am
by GCharb
Worked well here!

First gear has motor on, the others are just following the first.

Fun to actually see some use to that script! :)

File

http://www.mediafire.com/?uylwn89224cc32d

Image

Posted: Thu Sep 08, 2011 6:14 am
by Rudiger
So does your gear generation script let you change the diameter while keeping the size of the teeth the same? If so, it would be interesting to see large gears driving small gears and vice-versa.

Posted: Thu Sep 08, 2011 6:59 am
by sbtamu
I'm watching...

Posted: Thu Sep 08, 2011 10:38 am
by Rudiger
I forgot to mention that if you would prefer the Magnet Tool to work on all child layers of the currently selected Group Layer, instead of having to explicitly select the layers yourself, you would need to take a slightly different approach.

Code: Select all

function process_layer(moho, layer)

    if layer:IsGroupType() then

        local group_layer = moho:LayerAsGroup(layer)

        for i = 0, group_layer:CountLayers()-1 do
            process_layer(moho, group_layer:Layer(i))
        end

    else
        
        local mesh = layer:Mesh()

        if mesh ~= nil then
            .
            .
            .
            [code for magnet, but with any "mesh =" lines  removed] 
            .
            .
        end
    end

end
This is called recursion (because the process_layer function calls a copy of itself), and would make a great scripting tutorial one day. It gives a really straight forward way to traverse hierarchical tree type structures as is often required when writing scripts that deal with multiple layers in AS.

Posted: Thu Sep 08, 2011 11:06 am
by Rai López
Rudiger wrote:It gives a really straight forward way to traverse hierarchical tree type structures as is often required when writing scripts that deal with multiple layers in AS.

Hmmm... Incredibly interesting, as usually :)