Select only Single Line Text Events

joelsonforte.br wrote on 6/25/2024, 4:46 PM

I'm trying to create a script that finds and selects in timeline only text events that have a single line of text. But I can't get the script to work. Can anyone help me with this?

using System;
using ScriptPortal.Vegas;

public class EntryPoint
{
    private static Vegas myVegas;

    // Main method called by Vegas Pro
    public void FromVegas(Vegas vegas)
    {
        try
        {
            myVegas = vegas;

            // Perform the search and selection operation
            SelectSingleLineTextEvents();

            // Update the Vegas Pro user interface
            myVegas.UpdateUI();
        }
        catch (Exception ex)
        {
            Console.WriteLine("Error: " + ex.Message);
            // Here you can handle the error appropriately if needed
        }
    }

    // Selects single-line text events
    private static void SelectSingleLineTextEvents()
    {
        if (myVegas == null)
        {
            throw new InvalidOperationException("Vegas was not initialized correctly.");
        }

        foreach (Track track in myVegas.Project.Tracks)
        {
            foreach (TrackEvent trackEvent in track.Events)
            {
                // Check if it is a video text event and has a single line of text
                if (IsTextEvent(trackEvent) && HasSingleLineText(trackEvent))
                {
                    trackEvent.Selected = true; // Select the event
                }
            }
        }
    }

    // Checks if the event is a video text event
    private static bool IsTextEvent(TrackEvent trackEvent)
    {
        return trackEvent.MediaType == MediaType.Video &&
               trackEvent.ActiveTake != null &&
               trackEvent.ActiveTake.Media != null &&
               trackEvent.ActiveTake.Media.IsGenerated();
    }

    // Checks if the text event contains only one line of text
    private static bool HasSingleLineText(TrackEvent trackEvent)
    {
        string text = GetEventText(trackEvent);
        return !string.IsNullOrEmpty(text) && !text.Contains(Environment.NewLine);
    }

    // Gets the text from the video event using OFX effects
    private static string GetEventText(TrackEvent trackEvent)
    {
        Media media = trackEvent.ActiveTake.Media;
        foreach (Effect effect in media.Effects)
        {
            OFXEffect ofxEffect = effect.OFXEffect;
            if (ofxEffect != null)
            {
                // Find the text parameter by the name "Text"
                OFXStringParameter textParam = ofxEffect.FindParameterByName("Text") as OFXStringParameter;
                if (textParam != null)
                {
                    return textParam.Value;
                }
            }
        }
        return string.Empty;
    }
}

 

Comments

zzzzzz9125 wrote on 6/25/2024, 6:48 PM

You've made 2 mistakes:

1. You were trying to find a Generator in Media FX.

Titles & Text belongs to Media Generators. To find it, you should use:

        Effect effect = trackEvent.ActiveTake.Media.Generator;
        if (effect != null)
        {
            ...
        }

 

2. You were trying to find newlines directly in the rich text string.

The text of Titles & Text is a rich text string. To handle it, I suggest you use the following code:

using System.Windows.Forms;

...

    // Checks if the text event contains only one line of text
    private static bool HasSingleLineText(TrackEvent trackEvent)
    {
        RichTextBox rtb = new RichTextBox();
        rtb.Rtf = GetEventText(trackEvent);
        string text = rtb.Text;
        return !string.IsNullOrEmpty(text) && !text.Contains("\n") && !text.Contains("\r\n");
    }

Last changed by zzzzzz9125 on 6/25/2024, 6:56 PM, changed a total of 1 times.

Using VEGAS Pro 22 build 248 & VEGAS Pro 21 build 208.

Information about my PC:
Brand Name: HP VICTUS Laptop
System: Windows 11.0 (64-bit) 10.00.22631
CPU: 12th Gen Intel(R) Core(TM) i7-12700H
GPU: NVIDIA GeForce RTX 3050 Laptop GPU
GPU Driver: NVIDIA Studio Driver 560.70

jetdv wrote on 6/25/2024, 8:05 PM

@joelsonforte.br, this tutorial might also help you...