Problem with Markers.Delete

casikmeier wrote on 8/16/2005, 4:30 AM
Am I wrong with the code to delete all markers?

while (!markerEnum.atEnd()) {
Vegas.Project.Markers.Remove(Marker(markerEnum.item()));
markerEnum.moveNext();
}

I have about 600 Markers to delete. Every time I run the script there are only a few deletet. So I have to run the script several times to get all deleted.

Can someone give me a hint?

Thank you in advance,
Wolfgang

Comments

jetdv wrote on 8/16/2005, 6:35 AM
Actually, it should be deleting HALF of them.

Think of it this way: you have markers 12345678

Now delete marker 1 and you have 2345678

Now delete marker 2 (i.e. you in the second position now) and you have: 245678

Now delete the marker in the THIRD position and you have:
24678

Now delete in the 4th position and you have
2468

And now there is none in the 5th position so you're done.


There's a couple ways around this issue. The first is to process the markers BACKWARDS.


for (i=Vegas.Project.Markers.Count - 1; i >= 0; i--) {
Vegas.Project.Markers.Remove(Vegas.Project.Markers[i]);
}


Now... if you want to get rid of EVERY marker, here's a much easier way:

Vegas.Project.Markers.Clear();

casikmeier wrote on 8/16/2005, 6:47 AM
Thanx a lot. Now I understand the way to step through the timeline for all markers ...

But at that moment I did not see the simple way (clear()) ... sometimes I'm going to walk the hard way ;-)

Greetings
Wolfgang
jetdv wrote on 8/16/2005, 7:42 AM
Wolfgang, if you don't mind, send me an e-mail.
JohnnyRoy wrote on 8/16/2005, 10:48 AM
Modifying the contents of a collection that you are iterating over will always yield unpredictable results. As a safe programming practice, you should make a by value copy of the collection and iterate over that instead.

Let’s say you only wanted to remove the markers that have no label. Lots of times I add markers just to line things up and then other markers for Chapter points which have labels. This script will clearn up the project by removing all non-labeled markers.
/** 
* Program: Remove Empty Markers
* Author: John Rofrano (jrofrano at vasst dot com)
* Description: This script will remove any markers that do not have a label
* Last Updated: August 16, 2005 - JJR - Initial Release
**/
import System.Collections;
import System.Windows.Forms;
import Sony.Vegas;

try
{
var markerList : ArrayList = new ArrayList(Vegas.Project.Markers);
for (var marker : Marker in markerList)
{
if (marker.Label == "")
{
Vegas.Project.Markers.Remove(marker);
}
}
}
catch (errorMsg)
{
MessageBox.Show(errorMsg, "Error", MessageBoxButtons.OK, MessageBoxIcon.Error);
}
/*** END SCRIPT ***/
It works because I am not modifying the list I am iterating over. Instead I’m iterating over a copy of that list and modifying the real one. Hope this helps,

~jr
casikmeier wrote on 8/16/2005, 10:21 PM
Hello you both,

thank you for the instant help.

Maybe I was misunderstood. jetdv's first reply helped me and I was happy with the solution.
What I meant was, that I tried to solve it (before jetdv's reply) on the hard way without the simple method "clear()".
But John's hint to delete markers with a special label was another good idea for me.

Thanx a lot,
best regards from Germany,
Wolfgang