Looping through selected splines to modify pivot

Looping through selected splines to modify pivot

leeminardi
Mentor Mentor
889 Views
2 Replies
Message 1 of 3

Looping through selected splines to modify pivot

leeminardi
Mentor
Mentor

With very limited scripting experience I have cobbled together the following Maxscript to modify the pivot of a selected spline such that the Z axis aligns with the first to second vertex of the spline.  The program works for a selected spline.    I have been trying to add a for loop to cycle through all selected splines with no success.

 

How can looping be added to the following code to cycle through and modify all selected splines?  I have tried several for-loop formats with no success. 

 

spl = selection[1]
s = 1	
e1 = getKnotPoint spl s 1 
e2 = getKnotPoint spl s 2
mid12 = (e1+e2)/2
v = e2-e1
vlen = length(v)
vu = v/vlen
m = spl.transform
ftm = translate (matrixfromnormal vu) mid12
itm = ftm*(inverse m)
spl.transform = ftm
spl.objectOffsetPos *= inverse itm
spl.objectOffsetRot *= inverse itm.rotation
spl.transform
lee.minardi
0 Likes
Accepted solutions (1)
890 Views
2 Replies
Replies (2)
Message 2 of 3

drew_avis
Autodesk
Autodesk
Accepted solution

Hi there, the "selection" object contains all the selected nodes, and you can use that.  Try this:

 

for spl in selection do (
	s = 1	
	e1 = getKnotPoint spl s 1 
	e2 = getKnotPoint spl s 2
	mid12 = (e1+e2)/2
	v = e2-e1
	vlen = length(v)
	vu = v/vlen
	m = spl.transform
	ftm = translate (matrixfromnormal vu) mid12
	itm = ftm*(inverse m)
	spl.transform = ftm
	spl.objectOffsetPos *= inverse itm
	spl.objectOffsetRot *= inverse itm.rotation
	spl.transform
)

You might want to check that spl is actually a spline before you use it, in case something else is selected as well.

 

Hope that helps,

Drew



Drew Avis
Content Experience Designer
Message 3 of 3

leeminardi
Mentor
Mentor

Thank you Drew.  That does the trick!  I had done a search on the Maxscript "selection" keyword and got nowhere.

lee.minardi
0 Likes