Media ReplaceWith works in script but not in extension

Niels-Meijer wrote on 8/3/2018, 6:45 AM

I'm trying to replace a Media object. I managed to get this working in a script, but the exact same code doesn't seem to work for an extension.

This is my code for the script:

using ScriptPortal.Vegas;

namespace VegasProMediaReplacer
{
    public class EntryPoint
    {
        private const string VideosLocation = "C:/Users/Niels Meijer/Videos/";

        public void FromVegas(Vegas vegas)
        {
            Media oldMedia = null;
            foreach(Media m in vegas.Project.MediaPool)
            {
                oldMedia = m;
            }

            Media newMedia = new Media(VideosLocation + "newVideo.mp4");
            oldMedia.ReplaceWith(newMedia);
        }
    }
}

And my code for the extension:

using ScriptPortal.Vegas;
using System;
using System.Collections;
using System.Windows.Forms;

namespace VegasProMediaReplacer
{
    public class Class1 : ICustomCommandModule
    {
        private const string PicturesLocation = "C:/Users/Niels Meijer/Pictures/";
        private const string VideosLocation = "C:/Users/Niels Meijer/Videos/";

        private Vegas _vegas;

        public ICollection GetCustomCommands()
        {
            CustomCommand command = new CustomCommand(CommandCategory.Tools, "Test");
            command.DisplayName = "Test";
            command.Invoked += OnCommand;
            return new CustomCommand[] { command };
        }

        public void InitializeModule(Vegas vegas)
        {
            _vegas = vegas;
        }

        private void OnCommand(object sender, EventArgs args)
        {
            Media oldMedia = null;
            foreach(Media m in _vegas.Project.MediaPool)
            {
                oldMedia = m;
            }

            Media newMedia = new Media(VideosLocation + "newVideo.mp4");

            try
            {
                oldMedia.ReplaceWith(newMedia);
            }
            catch(Exception e)
            {
                string text = e.Message + "\n" + e.StackTrace;
                MessageBox.Show(text);
                Clipboard.SetText(text);
            }
        }
    }
}

I know this only grabs the last Media item, but this was just a test to see if I could get it to work. Media is also never null, I've made sure it actually grabs a Media object.
When running the extension I get the following log from the try/catch:

Catastrophic failure (Exception from HRESULT: 0x8000FFFF (E_UNEXPECTED))
   at ScriptPortal.Vegas.IMediaCOM.Replace(UInt32 mediaID, UInt32 otherID)
   at ScriptPortal.Vegas.Media.ReplaceWith(Media other)
   at VegasProMediaReplacer.Class1.OnCommand(Object sender, EventArgs args) in D:\Projects\VegasProMediaReplacer\VegasProMediaReplacer\Class1.cs:line 40

Is there anything I'm missing here/doing wrong, or does this just not work for extensions?

Thanks,

Niels

Comments

jetdv wrote on 8/3/2018, 10:03 AM

Did you use an "UndoBlock"? UndoBlock is required to add the activity to the "undo" list.

 

using (UndoBlock undo = new UndoBlock("Undo List Name To Use"))
            {

//Make the changes in Vegas here

            }

 

Harold-Linke wrote on 9/23/2018, 8:04 AM

@jetdv

Thank you for providing this trick. I ran into the same issue. Now it works perfect.