Loop to increment variable from 0 to 1 and back again in steps of 0.05

Hi there,

In Revit I’m aiming to “animate” a mass, for this I need to increment a variable (NCP) between 0 and 1 in steps of 0.05 over a period of time or indefinite loop, i am not sure if this is possible in the bounds of revit so any help will be greatly appreciated :slight_smile:

Animation is doable, but there are some steps. Usually I do mine in Dynamo sandbox as it is faster and more reliable in finishing graphics. This post should help you work out the details in either platform.

First up you will need to know how long the graph takes to run in the WORST possible run. 5 second? 2? 3? Rhis isn’t just the tile for Dynamo or Revit to become responsive, but for the graphics to show up. The hope is that you can get under one second, as it will make the process much smoother. If at frame 0 it would take less than a second but at frame 40 it will take 400 seconds then you’re going to need to use the 400 seconds. Utilize the TuneUp view extension and check around the range of slider values. Once you know that number multiply it by at least 1.5 and round up to an integer. For now I am going to assume the graph runs in 1.4 seconds at the worst case, multiply that by two and round up means we need 3 seconds to generate each frame. This is the ‘time between runs’ for your graph.

Next we need to work out how many frames do you want? 1/0.05 = 20, and a return trip is another 20 so 40. But do you really need to animate the return trip? If it is exactly the same why not duplicate the frames in your animation software? I’ll assume that the return trip will need to be run, we have 40 total frames. This means that the animation will need to take 40*3 seconds to complete, or 120.

The good news is that 120 seconds is a multiple of 60 - if not often round up to a value which adds time. Now we know how many frames you need to run your graph every three seconds for 40 consecutive times, which will take 120 seconds. How many minutes is that? Easy enough divide by 60 and get 2. So we will take the modulo (the % symbol stands for this mathematical function - it returns the remainder of a value when divided by another value) of the current time’s minutes component to get a value of 0 or 1. And we could multiply that number times 60 to get either 0 or 60 if it is an odd or even minute respectively. Add that to the number of seconds divided by the time between frames and we will have a number from 0 to 119.

The full formula for pulling a frame is:
((minuteNow % minutesToAnimate)*secondsPerMinute + secondsToAnimate)/secondsBetweenSamples = frameNumber

0 minutes and 15 seconds = ((0%2)*60 + 15)/3= frame five.
0:33 = ((0%2)*60 +33)/3 = frame 11
0:57 = ((0%2)*60 + 57)/3 = frame 19
1:00 = ((1%2)*60 + 0)/3 = frame 20
1:27 = ((1%2)*60 + 27)/3 = frame 29
1:57 = ((1%2)*60 + 57)/3 = frame 39

Now that we have the math worked out to generate the frame numbers based on the time, we can build it in Dynamo. Place a DateTime.Now node on canvas, and feed it into a DateTime.Components node. This node has an output port for minutes, seconds, hours, etc. you’ll need the minutes and seconds only (with hours the VM would likely go sour before it finishes).

In a code block type the following:
frame = ((m%2)*60+ s)/3;
Wire the minutes output of the DateTime.Components node into the m input and the seconds output into the s. Then set periodic run mode, adjust the time between runs to 3000, and watch the value March along from 0 to 39. Whoooooo! Almost there.

Now we need to change those numbers from 0 to 1 and replace your current input with them.

0 is straight forward, but after that we aren’t getting fractions of our values…
Frame 1 would want to be 0.05 and frame 2 would be 0.10… divide the value by 20!
Frame 0 / 20 = 0
1/20 = 0.05
2/20 = 0.1

19/20 = 0.95
20/20 = 1
21/20 = 1.05… DOH!
If the ‘limit’ on your slider is net we have to turn around… hey an if statement! if the value is over 20 (?) return the 1 less remaining value after dividing by 20 divided by 20 (:slight_smile: otherwise return the value divided by 20.

19/20 = 0.95
20/20 = 1
1-(21%20)/20 = 0.95
1-(22%20)/20 = 0.9

1-(39%20)/20 = 0.05

So in a code block we write out our formula:
value = f>20? 1-(f%20)/20 : f/20

Wire the results of value into the spot you had your number slider at, and presto! it’s ready to animate.

Some tips:

  • Make sure you start your periodic runs at a multiple of your sample rate.
  • Remember you can add and subtract frames in post production.
  • Faster graphs animate better, so address speed stuff first.
  • Start the image sampling after you confirm a few loops.
  • Don’t rely on Revit to share your animation. It isn’t built for this. Screen2Gif is a great application for image sampling and editing for the final animation.
  • When possible reuse frames (ie: use the yo-yo feature of screen2gif to make the animation run backwards and restart; tell the GIF to loop, etc.).
  • This is a hacked work-around to make animations. The best quality animation would be best coming from an actual animation software, (ie: 3DS Max, Maya, etc.) rather than the hacked workaround.
5 Likes