Notifications
Clear all

[Closed] Insert Edge Loop

I’ve made a fully working maxscript which generates and edgeloop on a cylinder. This code was initially based off of something Haywood wrote several years back, however his version is no longer available and it no longer worked either.

Right now it works on EditablePoly. You just create a mesh and selection some edge loops and run the script. This script in particular is designed to work on cylindrical shapes. It works like the 3ds Max Swift Loop/Flow connect.

However I do have one question…
Right now I’m trying to calculate the amount of offset for each vert so it maintains the curvature of the object…what is the actual correct math behind this? I’m not sure if what is being used is correct.
Thanks


struct edgeData (id, verts=#())

-- returns the vertex normal by averaging the surrounding face normals
fn GetVertexNormal obj theVert = 
(
	local faceArr = polyop.getFacesUsingVert obj theVert as array
	local vertNormal = polyop.getFaceNormal obj faceArr[1]

	if faceArr.count >= 2 do 
	(
		for i = 2 to faceArr.count do 
		(
			vertNormal += polyop.getFaceNormal obj faceArr[i]
		)
	)
	return (normalize vertNormal)
)

fn ConnectRound node:unsupplied =
(
	if classof node != Editable_Poly do return false
		
	-- collect edge selection
	local edgeBitArr = polyop.getEdgeSelection node
	
	if edgeBitArr.numberset == 1 then 
	(
		node.selectEdgeRing()
	)

	-- collect all verts associated with each edge
	local edges = #()

	for i in edgeBitArr do
	(
		vertices = polyOp.getVertsUsingEdge node i as array
		append edges (edgeData id:i verts:vertices)
	)
	
	local newVertArr = #()

	for i = 1 to edges.count do 
	(
		edgeID = edges[i].id

		-- add a new vert in the middle of the edge
		newVert = node.insertVertexInEdge edgeID 0.5
		append newVertArr newVert

		-- define the two end verts of the edge
		v1 = edges[i].verts[1]
		v2 = edges[i].verts[2]

		-- get the position of the two end verts
		vPos1 = polyop.getVert node v1
		vPos2 = polyop.getVert node v2

		-- get the normal vector for each vert (the new one and the two ends)
		vDir1 = GetVertexNormal node v1
		vDir2 = GetVertexNormal node v2
		vDir3 = GetVertexNormal node newVert

		-- get the angle between the two end verts
		vAngle = acos(dot vDir1 vDir2)

		-- get the distance between the new vert and one of the end verts
		d1 = distance (polyop.getVert node newVert) (polyop.getVert node v1)

		-- move the new vert out along it's Z axis
		-- the amount is defined by the angle between the end verts, the distance between the new vert and one of the end verts,
		-- and a number that seems to work good for what I want
		angleCheck = (dot vDir1 (vPos1 - vPos2))
			
		-- if angleCheck is negative, move the vert down along it's local Z axis
		-- if angleCheck is positive, move the vert up along it's local Z axis
		if angleCheck < 0 then polyop.moveVert node newVert -(vDir3 * vAngle * d1 * .005)
		if angleCheck > 0 then polyop.moveVert node newVert (vDir3 * vAngle * d1 * .005)
	)
	subObjectLevel = 1
	polyOp.setVertSelection node newVertArr
	node.buttonOp #connectVertices
)

for o in selection do ConnectRound node:o

1 Reply