Absolute position of mesh points

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

Moderators: Víctor Paredes, Belgarath, slowtiger

Post Reply
ggoblin
Posts: 266
Joined: Wed Jan 19, 2022 2:09 pm

Absolute position of mesh points

Post by ggoblin »

Hi,

Is there a function in the API that returns the absolute position of a mesh point on the moho viewport?

I know the Mesh Point fPos returns the position relative to the layer the mesh is on. But of course the layer itself could have rotated, translated or resized, or it might be in a group layer that has been rotated, translated or resized.. etc. and this will not be reflected in the value fPos contains. Or is it something left for us to calculate? I'm just wondering this must be a common need so perhaps the API has a utility function that already does this for us? No point reinventing the wheel..

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

Re: Absolute position of mesh points

Post by synthsin75 »

The only ones are more about converting than giving an absolute position:
https://mohoscripting.com/methods/272
https://mohoscripting.com/methods/22
ggoblin
Posts: 266
Joined: Wed Jan 19, 2022 2:09 pm

Re: Absolute position of mesh points

Post by ggoblin »

Sorry I didn't explain clearly. I'm looking for an API function that will return the absolute position of mesh points in the moho vector coordinate system ie y-axis values 1 to -1. By absolute I meant it having taken into account any transformations performed on the layer hosting the mesh, and the transformations performed on the group layers above it. I want to place another shape (another layer) precisely on a mesh point without it having to go through the same transformations as the layer that mesh point is on, if that makes any sense.

I have not test them yet but I think the functions you kindly shared are related to converting between moho coordiantes and screen pixel co-ordinates, I see in the examples they are used to convert between mouse movements in the user interface and moho coordinates?

Thank you.
User avatar
hayasidist
Posts: 3531
Joined: Wed Feb 16, 2011 8:12 pm
Location: Kent, England

Re: Absolute position of mesh points

Post by hayasidist »

to answer your question about an API function - no.

but what you can do is work out where the point will be without all the transforms. Take a look at this (sadly the Kelleytown links are dead) http://www.lostmarble.com/forum/viewtop ... 93#p160993

the "bake vector" script does a whole lot more than just position - so you can skip over all the stuff about shape attributes if all you want is {x,y} - the part of the code you'll probably find useful is in function bakeVector()

The script is from 2017 and has not been updated for new moho features, but it still works sufficiently well for most purposes.
ggoblin
Posts: 266
Joined: Wed Jan 19, 2022 2:09 pm

Re: Absolute position of mesh points

Post by ggoblin »

Thank you Hayasidist,

Its surprising there is no API utility function for that. Thank you for your script, it does a hell of a lot more than I need.

I only needed to take in to account resizng, translation and rotation in 2d, so in the end I didn't need use a matrix and stuck to just simple trigonometry to work out the rotation around the z axis, and resize and translation were straight forward as long as I remembered not to assume the origin was always 0,0. To refresh my trig, search took me to this website: https://www.tutorialandexample.com/2d-rotation shockingly the equations given are wrong - some have wrong angles, and the matrix has the +/- mixed up.. propably just horrendous typos? Oh well its good to know I still have a few grey cells working to notice such mistakes lol.

In case anyone else needs to work out rotation:

if p(x,y) is rotated by angle A around origin(ox,oy) to position p(x1,y1) then

x1 = (x-ox)cosA - (y-oy)sinA + ox
y1 = (x-ox)sinA + (y-oy)cosA +oy


Thank you.
Post Reply