Notifications
Clear all

[Closed] loop inside loop?

Hi,

I ve done my script that works fine on a single object. Unfortunately once I try to do it to a multiple objects, it doesn’t seem to work.

My goal is have the script align each object selected based on their direction.

  	selorient = $
  	for o in selorient do (
  	timeline = animationrange.end.frame as integer
  	slidertime = 1
  	nthframe = nthkeyframelabel.text as integer
  	animate on
  	
  	for i = 1 to timeline by nthframe do (
  		slidertime = i
  		p1 = at time i selorient.pos
  		p2 = at time (i+1) selorient.pos
  		
  		if rdo8.state == 1 do (
  			x = normalize (p2 - p1)
  			z0 = [0,0,1]
  			y = normalize (cross z0 x)
  			z = normalize (cross x y)
  			mymatrix = (matrix3 x y z p1)
  		)
  		
  		if rdo8.state == 2 do (
  			y = normalize (p2 - p1)
  			z0 = [0,0,1]
  			x = normalize (cross z0 y)
  			z = normalize (cross y x)
  			mymatrix = (matrix3 x y z p1)
  		)
  		
  		if rdo8.state == 3 do (
  			z = normalize (p2 - p1)
  			x0 = [1,0,0]
  			y = normalize (cross x0 z)
  			x = normalize (cross z y)
  			mymatrix = (matrix3 x y z p1)
  		)
  		
  		at time (currenttime.frame) in coordsys (transmatrix selorient.pos) selorient.rotation = inverse(mymatrix.rotation)
  	)
  )

what happen once I start the script with 2 objects selected is that it goes through the timeline twice but both object have the same keys, instead of having each their own.
I would like to find a way for the script maybe to deselect all object not in the instance running, apply the script then automatically select the next one. I think this might be the best way?

Any help would be welcome.

Thanks.

1 Reply

From what I can see from your script, one of the last lines is using ALL the objects for each loop, instead of just the one currently being processed.
Shouldn’t this line:

at time (currenttime.frame) in coordsys (transmatrix selorient.pos) selorient.rotation = inverse(mymatrix.rotation)

be like this, using “o” instead of “selorient”, which I expect to be a collection of objects?

at time (currenttime.frame) in coordsys (transmatrix o.pos) o.rotation = inverse(mymatrix.rotation)

PS. Personally I always indent my code, as it makes it much easier to read.