Psd Layer Splitter Script

Comments

editeiro wrote on 7/23/2024, 3:53 PM

@zzzzzz9125
Thank for the Update, It works very well!

I noticed that in some cases
a rate above 999 makes the first stream (background layer) leave the field of view, just as an Offset above 99 makes the last stream (foreground layer) leave the field of view.

But otherwise, I'll still do more testing, but it seems like a perfect set for working on PSDs in Vegas Pro.

I will make custom icons for my scripts here, and share them here if you want to use them.

I have more Ideas to make 1-Click Scripts to use in Motion Design compositions,
Thank you for your availability and collaboration, I hope to be able to share more ideas here.

editeiro wrote on 7/23/2024, 4:42 PM

@zzzzzz9125
Here is the Icons that I made
PSD Layer Splitter
 PSD Layer Sequence
PSD 3D Layer

zzzzzz9125 wrote on 7/24/2024, 12:50 AM

After some of my tests:
In fact, "depth" does not participate in the actual calculation of the position, so we don't need to change the depth value. It is simply a parameter that allows the user to scale. The formula I mentioned before, depth here is equivalent to the scaling value of width and height, which I think you can understand. That is, instead of assigning a value to depth, we can use it to "restore" the user's scaling value to the current layer.

I noticed that in some cases
a rate above 999 makes the first stream (background layer) leave the field of view, just as an Offset above 99 makes the last stream (foreground layer) leave the field of view.

@editeiro That's because m can't be -1 in the following code:

        public static void SetTrackMotion3D(Vegas vegas, VideoTrack track, int i, double rate = 0, double offset = 0)
        {
            if (track.CompositeMode != CompositeMode.SrcAlpha3D)
            {
                return;
            }

            double unit = vegas.Project.Video.Height / 20, m = (i * rate - offset) / 100;

            foreach (TrackMotionKeyframe kf in track.TrackMotion.MotionKeyframes)
            {
                kf.Depth = unit * (1 + m);
                kf.Width = vegas.Project.Video.Width * (1 + m);
                kf.Height = vegas.Project.Video.Height * (1 + m);
                kf.PositionZ = unit * m * 193 / 9;
            }
        }

It can be mitigated by changing the formula for m slightly, such as:

m = Math.Pow(2.71828, (i * rate - offset) / 100) - 1;

In this way, the larger the offset, m will always approach -1, but large offset values will inevitably cause display problems.

 

To sum up, a more perfect version of this script would look like this:

using System;
using System.Drawing;
using System.Windows.Forms;
using System.Collections.Generic;

using ScriptPortal.Vegas;  // "ScriptPortal.Vegas" for Magix Vegas Pro 14 or above, "Sony.Vegas" for Sony Vegas Pro 13 or below

namespace Test_Script
{

    public class Class
    {
        public Vegas myVegas;
        public const CompositeMode COMPOSITEMODE = CompositeMode.SrcAlpha3D;
        public const double RATE3DDEFAULT = 10, OFFSET3DDEFAULT = 0;
        
        public void Main(Vegas vegas)
        {
            myVegas = vegas;
            myVegas.ResumePlaybackOnScriptExit = true;
            Project project = myVegas.Project;
            bool ctrlMode = ((Control.ModifierKeys & Keys.Control) != 0) ? true : false;

            double rate3D = RATE3DDEFAULT, offset3D = OFFSET3DDEFAULT;
            if (COMPOSITEMODE == CompositeMode.SrcAlpha3D)
            {
                DialogResult result = PopUpWindow(out rate3D, out offset3D);
                if (result != DialogResult.OK)
                {
                    return;
                }
            }

            foreach (VideoEvent vEvent in GetSelectedVideoEvents(project, true))
            {
                if (vEvent.ActiveTake == null || vEvent.ActiveTake.Media == null || vEvent.ActiveTake.MediaStream == null)
                {
                    continue;
                }

                int vStreamCount = vEvent.ActiveTake.Media.StreamCount(MediaType.Video);
                if (vStreamCount < 2)
                {
                    continue;
                }

                // If you hold down Ctrl and click the script icon on the toolbar, the selected event will be converted to Stream 1 (in a programming sense, it's a video stream with Index 0).
                if (ctrlMode)
                {
                    Take newTake = Take.CreateInstance(project, vEvent.ActiveTake.Media.GetVideoStreamByIndex(0));
                    vEvent.Takes.Clear();
                    vEvent.Takes.Add(newTake);
                }

                int vStreamIndex = GetVideoStreamIndex(vEvent);
                if (vStreamIndex == 0)
                {
                    vEvent.Mute = true;
                }

                int vStreamCountAdd = Mod(vStreamIndex - 1, vStreamCount);

                TrackEventGroup grp = vEvent.Group;
                if (grp == null)
                {
                    grp = new TrackEventGroup(project);
                    project.Groups.Add(grp);
                    grp.Add(vEvent);
                }

                VideoTrack myTrack = (VideoTrack)vEvent.Track;

                if (vStreamIndex == 0 && myTrack.Effects.Count > 0 && COMPOSITEMODE == CompositeMode.SrcAlpha3D)
                {
                    foreach (Effect ef in myTrack.Effects)
                    {
                        ef.ApplyAfterComposite = true;
                    }

                    myTrack = AddNewTrack(myVegas, myTrack, 0, true, trackCM: CompositeMode.SrcAlpha);
                    vEvent.Track = myTrack;
                }
                else
                {
                    myTrack.SetCompositeMode(COMPOSITEMODE, false);
                }

                for (int i = 0; i < vStreamCountAdd; i++)
                {
                    VideoTrack newTrack = AddNewTrack(myVegas, myTrack, i, vStreamIndex == 0, rate3D, offset3D);

                    foreach (VideoEvent evnt in newTrack.Events)
                    {
                        if (evnt.Start == vEvent.Start)
                        {
                            if (evnt.ActiveTake == null || evnt.ActiveTake.Media == null || evnt.ActiveTake.MediaStream == null || evnt.ActiveTake.Media == vEvent.ActiveTake.Media)
                            {
                                newTrack.Events.Remove(evnt);
                            }
                        }
                    }

                    VideoEvent newEvent = (VideoEvent) vEvent.Copy(newTrack, vEvent.Start);
                    Take newTake = Take.CreateInstance(project, vEvent.ActiveTake.Media.GetVideoStreamByIndex(vStreamCountAdd - i));
                    newEvent.Takes.Clear();
                    newEvent.Takes.Add(newTake);
                    newEvent.Selected = false;
                    newEvent.Mute = false;
                    grp.Add(newEvent);
                }
            }
        }

        static DialogResult PopUpWindow(out double rate, out double offset)
        {
            rate = RATE3DDEFAULT;
            offset  = OFFSET3DDEFAULT;
            Form form = new Form();
            form.SuspendLayout();
            form.ShowInTaskbar = false;
            form.AutoSize = true;
            form.BackColor = Color.FromArgb(45,45,45);
            form.ForeColor = Color.FromArgb(200,200,200);
            if (System.Diagnostics.FileVersionInfo.GetVersionInfo(Application.ExecutablePath).FileMajorPart < 15)
            {
                form.BackColor = Color.FromArgb(153,153,153);
                form.ForeColor = Color.FromArgb(0,0,0);
            }
            form.Font = new Font("Arial", 9);
            form.Text = "PSD 3D";
            form.FormBorderStyle = FormBorderStyle.FixedToolWindow;
            form.StartPosition = FormStartPosition.CenterScreen;
            form.AutoSizeMode = AutoSizeMode.GrowAndShrink;

            Panel p = new Panel();
            p.AutoSize = true;
            p.AutoSizeMode = AutoSizeMode.GrowAndShrink;
            form.Controls.Add(p);

            TableLayoutPanel l = new TableLayoutPanel();
            l.AutoSize = true;
            l.AutoSizeMode = AutoSizeMode.GrowAndShrink;
            l.GrowStyle = TableLayoutPanelGrowStyle.AddRows;
            l.ColumnCount = 2;
            l.ColumnStyles.Add(new ColumnStyle(SizeType.Absolute, 70));
            l.ColumnStyles.Add(new ColumnStyle(SizeType.Absolute, 70));
            p.Controls.Add(l);

            Label label = new Label();
            label.Margin = new Padding(6, 10, 6, 6);
            label.Text = "Rate";
            label.AutoSize = true;
            l.Controls.Add(label);

            TextBox rateBox = new TextBox();
            rateBox.AutoSize = true;
            rateBox.Margin = new Padding(6, 6, 6, 6);
            rateBox.Text = rate.ToString();
            l.Controls.Add(rateBox);

            label = new Label();
            label.Margin = new Padding(6, 10, 6, 6);
            label.Text = "Offset";
            label.AutoSize = true;
            l.Controls.Add(label);

            TextBox offsetBox = new TextBox();
            offsetBox.AutoSize = true;
            offsetBox.Margin = new Padding(6, 6, 6, 6);
            offsetBox.Text = offset.ToString();
            l.Controls.Add(offsetBox);

            FlowLayoutPanel panel = new FlowLayoutPanel();
            panel.FlowDirection = FlowDirection.RightToLeft;
            panel.AutoSize = true;
            panel.AutoSizeMode = AutoSizeMode.GrowAndShrink;
            panel.Anchor = AnchorStyles.None;
            l.Controls.Add(panel);
            l.SetColumnSpan(panel, 3);
            panel.Font = new Font("Arial", 8);

            Button ok = new Button();
            ok.Text = "OK";
            ok.DialogResult = DialogResult.OK;
            panel.Controls.Add(ok);
            form.AcceptButton = ok;

            form.ResumeLayout();

            DialogResult result = form.ShowDialog();
            double.TryParse(rateBox.Text, out rate);
            double.TryParse(offsetBox.Text, out offset);
            return result;
        }

        public static int Mod(double a, double b)
        {
            return (int)(a - Math.Floor(a / b) * b);
        }

        public static List<VideoEvent> GetSelectedVideoEvents(Project project, bool reverse = false)
        {
            List<VideoEvent> vEvents = new List<VideoEvent>();
            foreach (Track myTrack in project.Tracks)
            {
                if (myTrack.IsVideo())
                {
                    foreach (TrackEvent evnt in myTrack.Events)
                    {
                        if (evnt.Selected)
                        {
                            if (evnt.ActiveTake == null || evnt.ActiveTake.Media == null || evnt.ActiveTake.MediaStream == null)
                            {
                                continue;
                            }

                            vEvents.Add((VideoEvent)evnt);
                        }
                    }
                }
            }
            if (reverse)
            {
                vEvents.Reverse();
            }

            return vEvents;
        }

        public static int GetVideoStreamIndex(VideoEvent vEvent)
        {
            int i = -1;
            foreach (MediaStream ms in vEvent.ActiveTake.Media.Streams)
            {
                if (ms.MediaType == MediaType.Video)
                {
                    i++;
                    if (ms == vEvent.ActiveTake.MediaStream)
                    {
                        return i;
                    }
                }
            }
            return 0;
        }

        public static VideoTrack AddNewTrack(Vegas vegas, VideoTrack track, int i, bool isFirst, double rate = 0, double offset = 0, CompositeMode trackCM = COMPOSITEMODE)
        {
            VideoTrack newTrack = null;
            int j = i + track.Index;
            if (j < vegas.Project.Tracks.Count - 1)
            {
                Track trackBelow = vegas.Project.Tracks[j + 1];
                if (trackBelow.IsVideo())
                {
                    VideoTrack vt = (VideoTrack)trackBelow;
                    if (!vt.IsCompositingParent && vt.CompositeMode == track.CompositeMode && (isFirst ? vt.CompositeNestingLevel == track.CompositeNestingLevel + 1 : vt.CompositeNestingLevel == track.CompositeNestingLevel))
                    {
                        newTrack = (VideoTrack)trackBelow;
                    }
                }
            }

            if (newTrack == null)
            {
                newTrack = new VideoTrack(vegas.Project, j + 1, null);
                vegas.Project.Tracks.Add(newTrack);
                newTrack.CompositeNestingLevel = track.CompositeNestingLevel;
                if (isFirst)
                {
                    newTrack.CompositeNestingLevel += 1;
                }

                if (track.IsCompositingParent)
                {
                    track.SetParentCompositeMode(trackCM, false);
                }
                newTrack.SetCompositeMode(COMPOSITEMODE, false);

                newTrack.Name = track.Name;
                newTrack.Solo = track.Solo;
                newTrack.Mute = track.Mute;
            }

            if (COMPOSITEMODE == CompositeMode.SrcAlpha3D)
            {
                SetTrackMotion3D(vegas, newTrack, i, rate, offset);
            }

            return newTrack;
        }

        public static void SetTrackMotion3D(Vegas vegas, VideoTrack track, int i, double rate = 0, double offset = 0)
        {
            if (track.CompositeMode != CompositeMode.SrcAlpha3D)
            {
                return;
            }

            double unit = vegas.Project.Video.Height / 20, m = Math.Pow(2.71828, (i * rate - offset) / 100) - 1;

            foreach (TrackMotionKeyframe kf in track.TrackMotion.MotionKeyframes)
            {
                kf.Width = vegas.Project.Video.Width * (1 + m) * kf.Depth / unit;
                kf.Height = vegas.Project.Video.Height * (1 + m) * kf.Depth / unit;
                kf.PositionZ = unit * m * 193 / 9;
            }
        }
    }
}


public class EntryPoint
{
    public void FromVegas(Vegas vegas)
    {
        Test_Script.Class test = new Test_Script.Class();
        test.Main(vegas);
    }
}

Please use completely new tracks to test, not previously generated, otherwise there will be logical conflicts.

Using VEGAS Pro 22 build 194 & 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

editeiro wrote on 7/24/2024, 9:45 AM

@zzzzzz9125 Thanks, i made more teste and work very well!
It's a good option to working fast in 3Dcomps, in the most cases, i use the rate until 100 and pffet in until, i'm not overpass this values, and works great.

Only left some adjustments that will need depending of the result wich I want to reach.
So thanks for more that efforts, You Rock dude!
This will save MUCH time in my next projects

editeiro wrote on 7/24/2024, 6:12 PM

@zzzzzz9125 I quote you right here: https://www.vegascreativesoftware.info/us/forum/creating-a-script-to-make-3d-extrusion-in-a-text--146620/

I have other idea to a 1-Click Script envolving 3D Comps, this time to make a 3D extrude Text ;)
Best

ansonmccosh wrote on 2/5/2025, 1:43 PM

@zzzzzz9125 Thanks, i made more teste and work very well!
It's a good option to working fast in 3Dcomps, in the most cases, i use the rate until 100 and pffet in until, i'm not overpass this values, and works great.

Only left some adjustments that will need depending of the result wich I want to reach.
So thanks for more that efforts, You Rock dude!
This will save MUCH time in my next projects

does this script still work? i tried it and i get an error

jetdv wrote on 2/5/2025, 2:01 PM

@ansonmccosh, it ought to still work. I've never tried it.

You might look at this tutorial too:

ansonmccosh wrote on 2/5/2025, 3:52 PM

@ansonmccosh, it ought to still work. I've never tried it.

You might look at this tutorial too:

Thanks il check it out...but any ideas on what that error means?

jetdv wrote on 2/6/2025, 9:01 AM

@ansonmccosh not without testing the script using your VEG file (some media would probably need be required too) in order to test out where/how it is crashing.

ansonmccosh wrote on 2/6/2025, 9:26 AM

@ansonmccosh not without testing the script using your VEG file (some media would probably need be required too) in order to test out where/how it is crashing.

so it works on my v19 after defaulting all settings. so it looks like i have a setting on v22 that is creating a conflict.

zzzzzz9125 wrote on 2/6/2025, 9:34 AM

does this script still work? i tried it and i get an error

@ansonmccosh I can't reproduce your problem in my VEGAS Pro 22 build 239.

However, I can see the error lines in your screenshot. They should be:

if (track.IsCompositingParent)
{
    track.SetParentCompositeMode(trackCM, false);
}
newTrack.SetCompositeMode(COMPOSITEMODE, false);

Perhaps in some ways you can't use VideoTrack.SetCompositeMode() and VideoTrack.SetParentCompositeMode().

You can try replacing all similar lines in the script with something like the following:

if (track.IsCompositingParent)
{
    track.ParentCompositeMode = trackCM;
}
newTrack.CompositeMode = COMPOSITEMODE;

If you have a problem with a particular project, you can send it to me and discuss where the problem is.

Using VEGAS Pro 22 build 194 & 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

ansonmccosh wrote on 2/6/2025, 4:20 PM

does this script still work? i tried it and i get an error

@ansonmccosh I can't reproduce your problem in my VEGAS Pro 22 build 239.

However, I can see the error lines in your screenshot. They should be:

if (track.IsCompositingParent)
{
    track.SetParentCompositeMode(trackCM, false);
}
newTrack.SetCompositeMode(COMPOSITEMODE, false);

Perhaps in some ways you can't use VideoTrack.SetCompositeMode() and VideoTrack.SetParentCompositeMode().

You can try replacing all similar lines in the script with something like the following:

if (track.IsCompositingParent)
{
    track.ParentCompositeMode = trackCM;
}
newTrack.CompositeMode = COMPOSITEMODE;

If you have a problem with a particular project, you can send it to me and discuss where the problem is.

Thanks for the response i did try it out and got the same error but i fixed it by simply restoring vegas 22 to default setting and i could use the original script..all good now...Thanks guys

Steve_Rhoden wrote on 2/7/2025, 1:42 AM

Still, splitting complex psd files on the vegas timeline does not comes out looking anywhere near how they originally look in photoshop...... Not a fault with the script, its just the way it is.

ansonmccosh wrote on 2/13/2025, 2:55 PM

does this script still work? i tried it and i get an error

@ansonmccosh I can't reproduce your problem in my VEGAS Pro 22 build 239.

However, I can see the error lines in your screenshot. They should be:

if (track.IsCompositingParent)
{
    track.SetParentCompositeMode(trackCM, false);
}
newTrack.SetCompositeMode(COMPOSITEMODE, false);

Perhaps in some ways you can't use VideoTrack.SetCompositeMode() and VideoTrack.SetParentCompositeMode().

You can try replacing all similar lines in the script with something like the following:

if (track.IsCompositingParent)
{
    track.ParentCompositeMode = trackCM;
}
newTrack.CompositeMode = COMPOSITEMODE;

If you have a problem with a particular project, you can send it to me and discuss where the problem is.

it's very weird and im not too sure what the issue is but after a while the script stopped working again but resetting vegas to default settings fixes it. its basically a cycle.

so essentially the script works fine > Fails randomly > stops working after repeating attempts > reset Vegas to default setting > starts working again. i'm starting to think it might just be a Vegas issue perhaps?