MetaInterval.clearIntervals

When I first saw this function listed in the API on this website, I thought it would clear the MetaInterval of all its intervals. After some code experimentation, I learned that was not the case. MetaInterval.py has the following comments:

This overrides the function defined at the C++ level to

reset the inPython flag. Clearing out the intervals list

allows us to run entirely in C++ again, at least until a new

Python interval gets added.

So this raised a few questions for me:

  1. Should we be calling clearIntervals from python to improve performance?
  2. Is “clearIntervals” the best name for this function? It confused at least one programmer as to what it does.
  3. Why aren’t these comments listed under clearInterval in the API on this website?
  4. What is the best way to remove all the intervals from a MetaInterval? Currently I am doing:
    while (len( self.myParallel ) > 0): self.myParallel.pop()

thanks,
Rob

Actually, I think clearIntervals() does indeed clear all the intervals from the sequence. What isn’t obvious from the comment you quoted is that the C++ version of this method, which the Python version overrides and extends, does clear out all the intervals.

However, you shouldn’t really be calling this method. It’s not designed for public use; the only reason it’s not private is that it needs to be overridden in a derived class, and Python doesn’t provide a concept like C++'s “protected” scoping.

In fact, you shouldn’t be using MetaInterval at all–you should be using one of its derivatives, such as Sequence, Parallel, or Track. These intervals are designed to act like a Python list, and as such they have all of the standard list operations that Python provides. Note that Python does not provide a clear() method on list; rather than emptying a Sequence and filling it up again, you are simply expected to create a new Sequence.

In fact, the intended design is that, once you have played an interval, it should no longer be modified in any way. Some of the caching operations deep within Panda may assume that a Sequence will not be suddenly emptied (or, for that matter, modified at all) after the first time it has been played. Intervals aren’t really intended to be dynamic–if you need to change an interval to something else, it’s intended that you create a new interval instead. However, it’s difficult to force this immutability principle in Python.

What is the application design that requires you to empty an interval before filling it up again? Is it possible to redesign this?

David

I was afraid that recreating a new interval with the same name might leave the old one somewhere in the application as a potential memory leak. The reason I have this handle to an interval that I would like to keep around is that it controls a high level transition between two different game modes. I like having handles to such high level intervals as it makes it easier to clean up or finish() those intervals if necessary.

We’ve been discussing this at the office recently. What is the best way to ensure that all the intervals that your mode might be running are being cleaned up properly on a sudden switch to a different mode. Some of us explicit handles to every interval, while others use a standard naming convention and clean up all intervals with a name matching that convention. Any recommendations?

Those both sound like fine conventions to me. I don’t know if I could recommend one over the other; it really comes down to personal preference.

David

From my experience, this works best BEFORE interval declaration :

  myInterval.pause()
  myInterval = None

It will stop the interval and kill it immediately.
Using clearIntervals seems keeping all intervals run until reach the full duration ! Don’t use that.
To debug it, try to print the interval manager AFTER interval declaration :

  print myInterval.getManager()

using pause and clearIntervals, and see how many intervals remain.
You may want to read this :
https://discourse.panda3d.org/viewtopic.php?t=2038