Notifications
Clear all

[Closed] Two Loops simultaneously

Hi,

i have a little problem. I don’t know to “connect” two different loops in one.
For Example, one loop goes through all objects and the other counts from 0 to 10, but the second loop is used in the first command.

Here my little WiP-Code for adding a path constraint with different percent-values to my selected objects:

(
  	--Variables
  	
  	pc = path_Constraint()
  	path1 = $MySpline
  	dist = 1.562
  	
  	-- Script
  	
  	for i in selection do and for o in 0 to 10 do -- Don't know, how to write this line
  	(
  		i.position.controller = pc
  		i.position.controller.path = path1
  		temp = dist*o
  		i.position.controller.percent = temp
  	)
  	   
  )
3 Replies

Is this what you want? This is usually referred to as nested loops. For every iteration of i, a full 0-10 loop of o is run.

 
(
   --Variables
   
   pc = path_Constraint()
   path1 = $MySpline
   dist = 1.562
	  
   -- Script
	  
   for i in selection do
   (
	  for o in 0 to 10 do 
	  (
		 i.position.controller = pc
		 i.position.controller.path = path1
		 temp = dist*o
		 i.position.controller.percent = temp
	  )
   )
)  

1 Reply
(@posativ)
Joined: 11 months ago

Posts: 0

Hi Adam Pletcher,
that’s not this, what I want.

I tried this example but it doesn’t work because the Value Temp is at least 15.62 and this will be assigned to the percent.
I want, that my first selected Object has a path controller with 0.0%, my second 1.562% and so on.

Okay. Sounds like you don’t need two loops at all. By using the selection count as the index, you can multiply that by dist each time through the loop.


for i in 1 to selection.count do
(
   obj = selection[i]
   obj.position.controller = pc
   obj.position.controller.path = path1
   temp = dist * (i - 1)
   obj.position.controller.percent = temp
)

Is that better?
[Edit: fixed small error]