Obtain the closest point ?

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

Moderators: Víctor Paredes, Belgarath, slowtiger

Post Reply
bbrraayyaann
Posts: 68
Joined: Thu Jan 24, 2019 3:52 am

Obtain the closest point ?

Post by bbrraayyaann »

The first point created is ID 0 , the first point created is ID 1
Image
Then I add points randomly.
But now the added points have different IDs
Image
For example, if I select point zero.
How could I get the next point by following the curve until I reach point 1 ?
in the order 0,5,3,4,2,1
Image
I tried the code : M_Mesh:ClosestPoint() but it didn't work, is there any way to do that?

What I want to achieve is that if for example there are many shapes; and the user selects a group of points, to get the next ones in order following the curve.

Code: Select all

	local count = 0
	local PuntosSel = {}
	for i=0, mesh:CountPoints()-1 do
		local point = mesh:Point(i)
		if point.fSelected then
			Pos = point.fPos
			break
		end
	end
	mesh:SelectConnected()
	for i=0,mesh:CountPoints()-1 do
		local point = mesh:Point(i)
		if point.fSelected then
			count = count+1
		end
	end
	local ignore = -1
	for i=1,count do
		local puntoID = mesh:ClosestPoint(Pos,ignore)
		ignore = puntoID
		table.insert(PuntosSel,puntoID)
	end
	for k,v in ipairs(PuntosSel) do
		print(v)
	end
User avatar
Rai López
Posts: 2243
Joined: Sun Aug 08, 2004 1:41 pm
Location: Spain
Contact:

Re: Obtain the closest point ?

Post by Rai López »

Hi. I think in this case you need to follow a different approach than go parsing points directly from mesh and do it from curves instead. If you attach this simple/useless example...

Code: Select all

function LayerScript(moho)
	local frame = moho.frame
	local mesh = moho:Mesh()
	local PuntosSel = {}

	if mesh ~= nil and frame > 0 then
		for i = 0, mesh:CountCurves() - 1 do
			local curve = mesh:Curve(i)

			for j = 0, curve:CountPoints() - 1 do
				local pt = curve:Point(j)
				--if pt.fSelected then
					--table.insert(PuntosSel, pt)
					if j == frame then
						pt.fPos.y = pt.fAnimPos:GetValue(0).y + (j / 10)

					end
				--end
			end
		end
	end
end
...to some vector layer containing curves with messed points ID like those in your pictures, as frame number advances you'll see how the points movement flows accordingly to its position in curve independently of the order they were added. It's a very quick and simple example and I've not dealt with points lately... but I think it's from where I'd start, so hope it helps.

EDIT: Directly as Lua file :arrow: rl_traverse_points.lua
...
bbrraayyaann
Posts: 68
Joined: Thu Jan 24, 2019 3:52 am

Re: Obtain the closest point ?

Post by bbrraayyaann »

Ramón López wrote: Fri Jan 13, 2023 10:36 pm Hi. I think in this case you need to follow a different approach than go parsing points directly from mesh and do it from curves instead. If you attach this simple/useless example...

EDIT: Directly as Lua file :arrow: rl_traverse_points.lua
Hi Ramon, Thank you very much, it worked correctly.
The only thing I wanted to get was the IDs of the points in order following the curve and it worked perfect.

Code: Select all

local mesh = moho:Mesh()
local PuntosSel = {}
for i = 0, mesh:CountCurves() - 1 do
	local curve = mesh:Curve(i)
	for j = 0, curve:CountPoints() - 1 do
		local pt = curve:Point(j)
		if pt.fSelected then
			table.insert(PuntosSel,mesh:PointID(pt))
		end
	end
end
for k,v in ipairs(PuntosSel) do
	print(v)
end
User avatar
Rai López
Posts: 2243
Joined: Sun Aug 08, 2004 1:41 pm
Location: Spain
Contact:

Re: Obtain the closest point ?

Post by Rai López »

Cool! It was like a fun/quick little training task to do and I'm glad it also helped... Good luck with your scripting!
...
User avatar
SimplSam
Posts: 1048
Joined: Thu Mar 13, 2014 5:09 pm
Location: London, UK
Contact:

Re: Obtain the closest point ?

Post by SimplSam »

The real fun begins when you have Shapes composed from multiple Curves (or parts of multiple Curves) - and you have to deal with Edges and Segments...
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
hayasidist
Posts: 3525
Joined: Wed Feb 16, 2011 8:12 pm
Location: Kent, England

Re: Obtain the closest point ?

Post by hayasidist »

I've updated mohoscripting.com's description of M_Curve:Point(id) to clarify (hopefully!) the difference between mesh point ids and the sequence number of a point in a curve.
Please let me know if it's not clear.
Post Reply