Notifications
Clear all

[Closed] determine longest, visible uv egde

Hoi,

I’m currently working on a function where I need to find the verts of the longest ‘visible’ uv edge, not the diagonal, of a tri or quad.

Currently I’m processing like the following:

[color=White]1. – convert uv face selection to uv vert selection
2. – collect all selected uv verts
3. – collect x,y coordinates for all uv verts
4. – calculate the length of uv vert1 -> uv vert2[/color]
this is actually where my problem is…as it presupposes that the verts are always in the right order to determine the longest ‘non diagonal’ edge…

for those who prefer reading the code:


 	oM = $.modifiers[#unwrap_uvw]
 		oM.faceToVertSelect()
 
 		aVList = (oM.getSelectedVertices() as array)
 		iNumV = aVList.count
 		aVPos = #()
 		aVSource = #()
 		aELength = #()
 		
 		aVPos = for nV in aVList collect oM.getVertexPosition 0 nV
 		
 		--// calculate the lenght of all edges
 		for i = 1 to iNumV do (
 			if i < iNumV then (
 				aELength[i] = (length (aVPos[i+1] - aVPos[i]))
 			)
 			else (
 				aELength[i] = (length (aVPos[i] - aVPos[1]))
 			)
 		)
 		
 		--// find the longest uv edge
 		iaVE = findItem aELength (amax aELength)
 		if iaVE < iNumV then (
 			aVSource[1] = aVList[iaVE]
 			aVSource[2] = aVList[iaVE+1]
 		)
 		else (
 			aVSource[1] = aVList[iaVE]
 			aVSource[2] = avList[1]
 		)
 

Any ideas or help would be appreciated.

4 Replies

Hi zortech,
I revised the algorithm to get the index of the longest UV Edge from a UV Face selection.

  1. Convert Face selection to Edges selection and store it
  2. Cycle through each Edge stored and get its extreme Verts and their distance
  3. Test if that distance is the max so far
  4. Return the longest UV Edge index

Warnings

  • There’s not error prevention in this code.
  • If two or more edges share the exact same length, only one will be detected.

(
	-- point to the Unwrap modifier
	local modUnwrap = $.modifiers[#unwrap_uvw]

	-- store the UV SO Selection before manipulation
	local baStoreFaceSel = modUnwrap.getSelectedFaces()
	local baStoreEdgeSel = modUnwrap.getSelectedEdges()
	local baStoreVertSel = modUnwrap.getSelectedVertices()

	-- process start: convert current Face selection to Edges
	modUnwrap.faceToEdgeSelect()

	-- get selected Edges from Faces
	local baSelEdges = modUnwrap.getSelectedEdges()

	-- not really needed, clean current Edges and Verts selection
	modUnwrap.selectVertices #{}
	modUnwrap.selectEdges #{}

	-- init some test variables
	local fEdgeMaxLength = 0
	local iEdgeMax = 0

	-- start to cycle thrugh Edges form selected Faces
	for iEdge in baSelEdges do
	(
		-- select each Edge and get its extreme Verts
		modUnwrap.selectEdges #{iEdge}
		modUnwrap.edgeToVertSelect()

		local aiEdgeVerts = (modUnwrap.getSelectedVertices()) as Array

		-- get extreme Verts position and their distance
		local p3PosVert01 = modUnwrap.getVertexPosition 0 aiEdgeVerts[1]
		local p3PosVert02 = modUnwrap.getVertexPosition 0 aiEdgeVerts[2]
		local fEdgeLength = distance p3PosVert01 p3PosVert02

		-- test actual edge length with maximum edge length achieved so far;
		-- if current length is bigger than current max, then the current edge
		-- is the longest so far
		if (fEdgeLength > fEdgeMaxLength) then
		(
			fEdgeMaxLength = fEdgeLength
			iEdgeMax = iEdge
		)
	)

	-- restore UV SO selection to the one before the process
	modUnwrap.selectFaces baStoreFaceSel
	modUnwrap.selectEdges baStoreEdgeSel
	modUnwrap.selectVertices baStoreVertSel

	-- print the index of longest UV Edge from the starting Face Selection
	-- and its length
	print iEdgeMax
	print fEdgeMaxLength
)

I hope this helps.

  • Enrico

Thanks for the effort Enrico! Your script is much more straightforward than mine.

The problem is that the diagonal, not visible edge, will be determined as the longest edge in at least every 2nd face…I’ve checked the reference but didn’t find anything that might help.

Thought maybe getting the position of the middle of every edge and check it to find out which edge is in center. I’ll try to translate that to ms…and implement it.

Hi Max,
I’m not completely sure to understand your needing. The script I wrote takes in account visible edges only, the diagonal (edge hidden in a nGon in an Editable Poly) is never calculated. This is why starting from Faces I get Edges and not Verts, they indeed store information on Vertices connections. I assume we’re working on an Editable Poly made by nGons.

Not sure about your diagonal filtering idea, since it should work for Quads only, made by two identical Tris, or else the mid point of the diagonal edge wouldn’t be in the Quad center point.

If this doesn’t help, can you post an image to make me understand the diagonal issue? Thanks.

  • Enrico

Ah darn…of course you’re right, sry for the confusion. The code works like a charm, thanks again for the effort!