Problem with array

Hi,

I try to make a little script to change randomly length of 2 vertex spline segments.
(to make sketchy effects like in capture )
It works but I want to have an undo.
So I made a button to store vertex positions when I want.
So I can tweak
If I want to restore vertex position I press "revert" button.
Boom, I can't make it working.
As always with arrays I reach very quickly my scripting limits :-)
Anybody can help ?
Thanks.

(
try destroyDialog rRANGE catch()
 global rRANGE = rollout rRANGE "RANGE"
 (
	button KEEP "Keep"  width:120 height:25 
 spinner spB "range  " width:80 range:[-100,100,0.0] type:#float
 spinner spH "range  " width:80 range:[-100,100,0.0] type:#float
	button GO "GO" width:120 height:25 
	button REVE "Revert"  width:120 height:25 
 
on Go pressed do
(
selec =$
for Aseg=1 to numsplines selec do
(
verts = numKnots selec Aseg
for i=1 to verts by 2 do
(
knt1 = getKnotPoint selec Aseg i
knt2 = getKnotPoint selec Aseg (i+1)
vec= knt2 - knt1
Nvec= normalize (vec)
setKnotPoint SELEC Aseg i ([knt1.x,knt1.y,knt1.z]- Nvec * ( random spB.value spH.value) )
setKnotPoint SELEC Aseg (i+1) ([knt2.x,knt2.y,knt2.z]+ Nvec * ( random spB.value spH.value) )
)
)
updateShape selec
)
 
on KEEP pressed do
(
selec =$
ZZ=0
knt = #()
for Aseg=1 to numsplines selec do
(
verts = numKnots selec Aseg
for i=1 to verts do
(
ZZ=ZZ+1
knt[ZZ] = getKnotPoint selec Aseg i
)
)
)
 
 
on REVE pressed do
(
selec =$
ZZ=0
for Aseg=1 to numsplines selec do
(
verts = numKnots selec Aseg
for i=1 to verts do
(
ZZ=ZZ+1
setKnotPoint selec Aseg i ( knt[ZZ] )
)
)
updateShape selec
)
 
 
 )
 createDialog rRANGE 160 250
 setFocus rRANGE.spB
)
AttachmentSize
capture.jpg61.85 KB

Comments

Comment viewing options

Select your preferred way to display the comments and click "Save settings" to activate your changes.
titane357's picture

Hi, thanks Miauu !! I’m very

Hi, thanks Miauu !! I’m very busy so i can’t try now.
What i don’t understand is why my script don’t work when i click revert....

miauu's picture

.

Your script not works when you press the Revert button because the array that you use to store the position of the knots are local to the KEEP button scope. This array is not accessible via the rest of the code. To make it to work you have to define the array as local for the whole rollout, so all part of the code to have access to it. Like this:

(
	try destroyDialog rRANGE catch()
	global rRANGE = rollout rRANGE "RANGE"
	(
		local knotsPosArr = #()
 
		button KEEP "Keep"  width:120 height:25 
		spinner spB "range  " width:80 range:[-100,100,0.0] type:#float
		spinner spH "range  " width:80 range:[-100,100,0.0] type:#float
		button GO "GO" width:120 height:25 
		button REVE "Revert"  width:120 height:25 
 
		on Go pressed do
		(
			selec = $
			for Aseg=1 to numsplines selec do
			(
				verts = numKnots selec Aseg
				for i=1 to verts by 2 do
				(
					knt1 = getKnotPoint selec Aseg i
					knt2 = getKnotPoint selec Aseg (i+1)
					vec= knt2 - knt1
					Nvec= normalize (vec)
					setKnotPoint SELEC Aseg i ([knt1.x,knt1.y,knt1.z]- Nvec * ( random spB.value spH.value) )
					setKnotPoint SELEC Aseg (i+1) ([knt2.x,knt2.y,knt2.z]+ Nvec * ( random spB.value spH.value) )
				)
			)
			updateShape selec
		)
 
		on KEEP pressed do
		(
			selec = $
			knotsPosArr = #()
 
			for Aseg = 1 to numsplines selec collect
			(
				verts = numKnots selec Aseg
				for i=1 to verts do
				(
					append knotsPosArr (getKnotPoint selec Aseg i)
				)
			)
		)
 
 
		on REVE pressed do
		(
			selec = $
			if knotsPosArr.count != 0 do
			(
				cnt = 1
				for Aseg=1 to numsplines selec do
				(
					verts = numKnots selec Aseg
					for i=1 to verts do
					(
						setKnotPoint selec Aseg i ( knotsPosArr[cnt] )
						cnt += 1
					)
				)
				updateShape selec
			)
		)
 
	)
	createDialog rRANGE 160 250
	setFocus rRANGE.spB
)

But this is also not the solution, because your code stores only the position of the vertices but it not stores the tangents of those vertices. If you test your code on a segments that are not straight you will see where is the problem.

titane357's picture

Okeeyyy !!! Thank you very

Okeeyyy !!! Thank you very much !!! I understand now
some problems in other scripts !!!
For the tangents, no problemo as i just use corners !!!
:-)

miauu's picture

.

Usually it should works without "with undo on". Run the script, press the GO button and then use the Ctrl+Z to see the result.

(
	try destroyDialog rRANGE catch()
	global rRANGE = rollout rRANGE "RANGE"
	(
		spinner spB "range  " width:80 range:[-100,100,0.0] type:#float
		spinner spH "range  " width:80 range:[-100,100,0.0] type:#float
		button GO "GO" width:120 height:25 	
 
		on Go pressed do
		(
			with undo on (convertToSplineShape $)
			selec =$
			for Aseg=1 to numsplines selec do
			(
				verts = numKnots selec Aseg
				for i=1 to verts - 1 by 2 do
				(
					knt1 = getKnotPoint selec Aseg i
					knt2 = getKnotPoint selec Aseg (i+1)
					vec= knt2 - knt1
					Nvec= normalize (vec)
					setKnotPoint SELEC Aseg i ([knt1.x,knt1.y,knt1.z]- Nvec * ( random spB.value spH.value) )
					setKnotPoint SELEC Aseg (i+1) ([knt2.x,knt2.y,knt2.z]+ Nvec * ( random spB.value spH.value) )
				)
			)
			updateShape selec
		)
 
	)
	createDialog rRANGE 160 250
	setFocus rRANGE.spB
)

Comment viewing options

Select your preferred way to display the comments and click "Save settings" to activate your changes.