Creating multiple keyframes

Creating multiple keyframes

leeminardi
Mentor Mentor
554 Views
2 Replies
Message 1 of 3

Creating multiple keyframes

leeminardi
Mentor
Mentor

I created the following code to calculate the location of the end of a boom from a bus (trolley) to an overhead cable.

leeminardi_0-1644275181233.png

-- positions PointCable on line001 a distance LabelControl
-- from PointBus
-- v .01 2/6/2022 L. Minardi
clearlistener()
fn CalculateCablePoint =
(	
select $line001
cable = $
select $PointCable
pCable = $
sM = [0,0,0]
global delta = 0.1
global s = delta
global L= 20.
select $Bus
BusT = $.transform
select $PointBus
pBus = $.position 
sA = 0.0  -- % along value for A
sB = delta  -- initial % along guess for B
flag = true
pA = interpCurve3D cable 1 sA  -- point A 
pB = interpCurve3D cable 1 sB   -- point B
dA = distance pBus pA 
dB = distance pBus pB 
-- get to intervale containing solution
for i = 1 to 10 while flag do
(if dB > L then
  (
	pA = pB 
    sA = sB
    sB = sB + delta
	pB =  interpCurve3D cable 1 sB
	dB = distance pBus pB 
  )	
else
  ( flag = false
  )
  )
-- interval found, now iterate between A and B
flag = true
for i = 1 to 5 while flag do
(
  sM = (sA + sB)/2.
  pM =  interpCurve3D cable 1 sM
  dM = distance pBus pM
  err = abs(dm - L) 
  if (err < (0.01 * L)) then 
  (	flag = false	
   format ("i = %, error = %   ") i err
  )
if dM < L then
(
   pB = pM
   sB = sM 
)	
else
(
   pA = pM 
   sA = sM 
)
) -- end for
pCable.pos = pM
)  
CalculateCablePoint()
  
  

  It works for a single location of the bus.  I am trying to enhance it to generate keyframes as the bus moves.  I have tried adding a For loop for the entire program  and then the following statements to create keyframes at the end of the code. Note, pM is the point location of for the point object PointCable which the boom references with a LookAT constraint. 

	select $PointCable
	pCable = $
	slidertime = j
	with animate on
	pCable.pos = pM

 The keyframes are created but they are all the location and do not track along the cable as the bus moves.  Here's the version with the loop.  Any suggestion as to how to get it to generate keyframes for each calculation of pM?

-- positions PointCable on line001 a distance LabelControl
-- from PointBus
-- v .02 2/7/2022 L. Minardi
clearlistener()
clearSelection
--fn cp =
--(	
--with animate on	
for j = 1 to 15 do
 (	
	select $line001
	cable = $
	select $PointCable
	pCable = $
	sM = [0,0,0]
	global delta = 0.1
	global s = delta
	global L= 20.
	select $Bus
	BusT = $.transform
	select $PointBus
	pBus = $.position 
	sA = 0.0
	sB = delta
	flag = true
	pA = interpCurve3D cable 1 sA
	pB = interpCurve3D cable 1 sB
	dA = distance pBus pA 
	dB = distance pBus pB 
	-- get to intervale containing solution
	for i = 1 to 10 while flag do
		(if dB > L then
		  (
			pA = pB 
			sA = sB
			sB = sB + delta
			pB =  interpCurve3D cable 1 sB
			dB = distance pBus pB 
		  )	
		else
		  ( flag = false
		  )
		)
	-- now iterate between A and B
	flag = true
	for i = 1 to 5 while flag do
	(
		sM = (sA + sB)/2.
		pM =  interpCurve3D cable 1 sM
		dM = distance pBus pM
		err = abs(dm - L) 
		if (err < (0.01 * L)) then 
			(	flag = false	
				format ("i = %, error = %   ") i err
			)
		if dM < L then
			(
			pB = pM
			sB = sM 
			)	
			else
			(
			pA = pM 
			sA = sM 
			)
	) -- end for

	pCable.pos = pM
	clearselection()
	select $PointCable
	pCable = $
	slidertime = j
	with animate on
	pCable.pos = pM

) -- end for j

 

lee.minardi
0 Likes
Accepted solutions (1)
555 Views
2 Replies
Replies (2)
Message 2 of 3

istan
Advisor
Advisor

for what reason you

a) always "select" nodes?

b) do "pCable.pos=pM" right before clearselection() ?

Besides, I'd use a different solution by sphere/line intersection.

0 Likes
Message 3 of 3

leeminardi
Mentor
Mentor
Accepted solution

@istan wrote:

for what reason you

a) always "select" nodes?

b) do "pCable.pos=pM" right before clearselection() ?

Besides, I'd use a different solution by sphere/line intersection.


@istan thank you for your comments.   My post was to help me in solving a bigger problem that is better described in this thread. With help from @denisT.MaxDoctor the need for generating keyframes has been eliminated.

 

As for using a "sphere/line intersection" approach the task is more complex if by "line" you meant straight line.. The path is a spline and there may be multiple solutions or none at all. I think a numerical solution works best in such a situation.   

lee.minardi
0 Likes