Page 1 of 1

Obtain the closest point ?

Posted: Fri Jan 13, 2023 7:31 pm
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

Re: Obtain the closest point ?

Posted: Fri Jan 13, 2023 10:36 pm
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

Re: Obtain the closest point ?

Posted: Sat Jan 14, 2023 12:58 am
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

Re: Obtain the closest point ?

Posted: Sat Jan 14, 2023 2:24 am
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!

Re: Obtain the closest point ?

Posted: Sat Jan 14, 2023 3:13 am
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...

Re: Obtain the closest point ?

Posted: Sat Jan 14, 2023 11:43 am
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.