Home Technical Talk

Braid Generator (MaxScript)

polycounter lvl 10
Offline / Send Message
trebor777 polycounter lvl 10
Started a small generator for braids :)
I simply converted an old tutorial (KevinJohn's) into maxscript and made it parametrable :)
-- BRAID TOOL
try(destroyDialog BraidGeneratorRollout)catch()
rollout BraidGeneratorRollout "Braid Generator" width:176 height:152
(
	spinner spnNB "Nb of Strands" pos:[16,56] width:152 height:16 range:[0,100,3] type:#integer scale:1
	spinner spnLg "Braid Length" pos:[24,8] width:144 height:16 range:[10,1000,400] scale:10
	spinner spnWd "Braid Width" pos:[24,32] width:144 height:16 range:[1,500,40] scale:1
	button btnGenerate "Generate" pos:[8,104] width:160 height:40
	checkbox chkConvert "Convert to Editable Poly" pos:[8,80] width:160 height:16
	
	on btnGenerate pressed do
	(
		nbLines = spnNB.Value
		braidlength = spnLG.value
		braidWidth = spnWd.value
		
		braidSpline = line pos:[-braidWidth/2, 0, 0]
		addNewSpline braidSpline
		addKnot braidSpline 1 #corner #line [-braidWidth/2, 0, 0]
		addKnot braidSpline 1 #corner #line [braidWidth/2, 0, braidWidth]
		addKnot braidSpline 1 #corner #line [-braidWidth/2, 0, braidWidth*2]
		updateShape braidSpline
		undo off (
			numSegs = (numSegments braidSpline 1) 
			for i = numSegs to 1 by -1 do (
				subdivideSegment braidSpline 1 i (nbLines-1)
			)
			updateShape braidSpline
			Corners = #()
			point0 = getKnotPoint braidSpline 1 1
			point1 = getKnotPoint braidSpline 1 2
			halfDistance = (point1.x - point0.x) / 2				
			for i = 1 to (numKnots braidSpline 1) by nbLines do (
				setKnotType braidSpline 1 i #corner
				append Corners i
			)
			
			offsets = #(halfDistance, -halfDistance)
			offset_index = 1
			for i = 1 to (numKnots braidSpline 1) do (
				knotType = getKnotType braidSpline 1 i
				if knotType != #corner then (
					offset = offsets[offset_index]
					currentPoint = getKnotPoint braidSpline 1 i
					setKnotPoint braidSpline 1 i [currentPoint.x, currentPoint.y + offset, currentPoint.z]			
					offset_index += 1
					if offset_index > 2 then offset_index = 1
				)
			)
			
			for id in Corners do (
				knotPoint = getKnotPoint braidSpline 1 id
				factor = if knotPoint.x < 0 then 1 else -1
				knotPoint.x += factor * halfDistance
				setKnotPoint braidSpline 1 id knotPoint
			)
			
			nbCopies = (braidLength / (braidWidth * 2)) as Integer
			copies = #()
			for i = 1 to nbCopies-1 do (
				lName = ("braidTemp_" + (i as String))
				copy braidSpline pos:[braidSpline.position.x, braidSpline.position.y, braidSpline.position.z + i * braidWidth*2] name:lName
				newLine = getNodeByName lName	
				append copies newLine
			)
			
			for i = 1 to copies.count do (
				nLine = copies[i]
				addAndWeld braidSpline nLine 0.1
				
			)
			updateShape braidSpline
			
			for i = 1 to (numKnots braidSpline 1) do (
				setKnotType braidSpline 1 i #smooth
			)
			
			updateShape braidSpline
			braidSpline.render_displayRenderMesh = true
			braidSpline.render_thickness = 0.75 * braidWidth/(nbLines-1)
			braidSpline.render_viewport_sides = 8
			braidSpline.steps = 3
			braidSpline.name = UniqueName "Braid"
			updateShape braidSpline
			
		) -- END UNDO OFF
		
		splines = #()
		for i = 1 to nbLines-1 do (
			maxOps.CloneNodes braidSpline offset:[0,0,i * (braidWidth*2/nbLines)] actualNodeList:&c newNodes:&d			
			append splines d[1]
		)
		for spline in splines do (		
			addAndWeld braidSpline spline 0.1			
		)
		updateShape braidSpline
		if chkConvert.Checked then (				
			convertToPoly(braidSpline)
			braidSpline.autoSmooth()		
		)
	)
)
CreateDialog BraidGeneratorRollout

I set the sides to 8 cause, with 12 it goes crazy high in the poly count :) and can take ages to generate.

It's far from perfect but does most of the job :)
BraidGen.png

Replies

Sign In or Register to comment.