Multi layer Magnet Script?

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

Moderators: Víctor Paredes, Belgarath, slowtiger

User avatar
x-vishon
Posts: 74
Joined: Sat Feb 18, 2006 5:52 pm

Multi layer Magnet Script?

Post 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. :?:
User avatar
GCharb
Posts: 2202
Joined: Mon Oct 10, 2005 2:31 am
Location: Saint-Donat, Quebec, Canada
Contact:

Post 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!
Rudiger
Posts: 786
Joined: Sun Dec 18, 2005 2:25 am

Post 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
User avatar
GCharb
Posts: 2202
Joined: Mon Oct 10, 2005 2:31 am
Location: Saint-Donat, Quebec, Canada
Contact:

Post by GCharb »

Thanks rudiger, will look into that when time permits!
Rudiger
Posts: 786
Joined: Sun Dec 18, 2005 2:25 am

Post 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.
User avatar
GCharb
Posts: 2202
Joined: Mon Oct 10, 2005 2:31 am
Location: Saint-Donat, Quebec, Canada
Contact:

Post 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.
Rudiger
Posts: 786
Joined: Sun Dec 18, 2005 2:25 am

Post 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.
User avatar
Rai López
Posts: 2228
Joined: Sun Aug 08, 2004 1:41 pm
Location: Spain
Contact:

Post 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...
...
sbtamu
Posts: 1915
Joined: Tue Dec 15, 2009 5:05 am
Location: Texas
Contact:

Post 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.
Sorry for bad animation

http://www.youtube.com/user/sbtamu
User avatar
GCharb
Posts: 2202
Joined: Mon Oct 10, 2005 2:31 am
Location: Saint-Donat, Quebec, Canada
Contact:

Post 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! :)
User avatar
GCharb
Posts: 2202
Joined: Mon Oct 10, 2005 2:31 am
Location: Saint-Donat, Quebec, Canada
Contact:

Post 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
Rudiger
Posts: 786
Joined: Sun Dec 18, 2005 2:25 am

Post 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.
sbtamu
Posts: 1915
Joined: Tue Dec 15, 2009 5:05 am
Location: Texas
Contact:

Post by sbtamu »

I'm watching...
Sorry for bad animation

http://www.youtube.com/user/sbtamu
Rudiger
Posts: 786
Joined: Sun Dec 18, 2005 2:25 am

Post 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.
User avatar
Rai López
Posts: 2228
Joined: Sun Aug 08, 2004 1:41 pm
Location: Spain
Contact:

Post 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 :)
...
Post Reply