C++ Learning to understand Envelopes class

Thomas wrote on 8/9/2008, 2:28 AM
I am trying to resolve some major roadblock I am for sure just having in MY mind with the code snippet enclosed. Obviously, it compiles but doesn't run.


using namespace System::Windows::Forms;
using namespace Sony::Vegas;

public ref class EntryPoint
{
public:
void FromVegas(Vegas vegas) {
Project ^project = vegas.Project;
// create objects...
AudioTrack ^audioTrack = gcnew AudioTrack();
Envelope ^volEnvelope = gcnew Envelope(EnvelopeType::Volume);
// link them
/* --> */ audioTrack->Envelopes->Add(volEnvelope);
project->Tracks->Add(audioTrack);
return;
}


};



Looking in the debugger, I see that audioTrack->Envelopes is indicating a NULL reference. I understand that Add(..) will then fail of course. On the other hand I am missing a constructor for Envelopes, there is only an Add(...) method. And an AudioTrack permits only get() for Envelopes, so, even if I would be able to create an Envelopes-object, I would not be able assign it.

Can someone give me a hint, what I missed? Is there a comprehensive description of the vegas scripting dll? I downloaded the API items for vegas 7 + 8 with the API description and FAQ, but maybe my question is something anyone usually knows.

Any hint appreciated.
Thanks,
Thomas

Comments

Teetow wrote on 8/9/2008, 3:28 AM
I haven't yet made the jump to C++, but I assume there isn't much difference in how the Vegas API works.

This works fine for me (only showing the relevant lines, the rest is just a standard template)

AudioTrack NewTrack = new AudioTrack();
MyVegas.Project.Tracks.Add(NewTrack);
Envelope NewEnv = new Envelope(EnvelopeType.Volume);
NewTrack.Envelopes.Add(NewEnv);
NewEnv.Points.Add(new EnvelopePoint(Timecode.FromSeconds(1), double.MinValue));
NewEnv.Points.Add(new EnvelopePoint(Timecode.FromSeconds(2), double.MaxValue));
NewEnv.Points.Add(new EnvelopePoint(Timecode.FromSeconds(3), double.MinValue));


So I think what you want to do is add the track to the project before trying to add a volume envelope to it.

jetdv wrote on 8/9/2008, 4:56 AM
The difference between the two is that Teetow added the new track to the project before attempting to add the envelope to the track. That is the correct procedure. Thomas, you must add the track to the project before you can add things like envelopes to that track.
JohnnyRoy wrote on 8/9/2008, 6:08 AM
Yea, this is something you need to get use to. When you create most objects with the Script API, they are invalid until you attach them to a parent. Most developers will fully setup an object and then add it but that doesn't work here. So as in this example, once you create a new track the very next thing you must do is add it to the project before doing anything to it. Likewise when you create a new event, you must first add it to a track before manipulating it in any way. You get use to it after a while but I still get bit by this every so often.

~jr
Thomas wrote on 8/9/2008, 10:18 AM
Teetow, jetdv, JohnnyRoy: Thanks for that quick and easy explanation! It works well, now. Adding the audioTrack to the projects tracks indeed initiates subsequent creation of all the objects not providing a set method. Not too bad if you're aware of instead of creating all the container objects manually and assigning them.

Thomas
Teetow wrote on 8/9/2008, 12:26 PM
Well, this is something we've all been through, especially those of us who had relatively little genuine coding experience prior to writing Vegas add-ons. You always end up throwing micro-fits over "well, how was I supposed to know that?!"

The truth is, the info is usually out there, just not always consolidated to one spot. My usual check-list is:

* the API docs (AND the release notes!)
* the example code
* the forums (this and jetdv's)

Good news are, the Vegas scripting API is rather intuitive. There's a rhythm to doing most things, which becomes familiar after you've mucked about for a while, and you soon learn to predict roughly where you can find the info or the method you're after.

By the way, you've inspired me to try out writing some managed C++ Vegas stuff just as a segue into writing "proper" stand-alone C++ apps, which I've been meaning to do for a long time.
TissoShark wrote on 9/11/2008, 7:24 AM
New around here. Just wated to salute you...

Any one can recomend a good begginers book or website to get some tutorials?

Good to be here!
Tisso
jetdv wrote on 9/11/2008, 7:58 AM
Tisso, my website has quite a bit of scripting information - mainly with JScript and C# but the techniques would be similar in your language of choice.