Version 1.02 - October 6th, 2019
Author: Harold Linke

History of Change
1.00 - 28.07.2019 - First release with PythonNet
1.01 - 18.08.2019 - Font and Font Size of Interactive VEGASPython Window can be configured in VEGASPython_CONFIG.JSON
1.02 - 06.10.2019 - Support for non-ascii characters as script names for translations of the scripts

For more details check: https://www.hlinke.de/dokuwiki/doku.php?id=en:vegas_python

This is a Vegas pro Custom Command Extension adding a Python Scripting Interpreter and some features to VEGAS 

Installation:

All files and directories in the zip file must be copied to one of these folders :

C:\Users\<username>\Documents\Vegas Application Extensions\  
C:\Users\<username>\AppData\Local\Vegas Pro\Application Extensions\
C:\Users\<username>\AppData\Local\Vegas Pro\Application Extensions\
C:\ProgramData\Vegas Pro\Application Extensions\

I'm using on a Win10 system : 
C:\Users\Harold\Documents\Vegas Application Extensions

or 

C:\ProgramData\Vegas Pro\Application Extensions\

If the 'Application Extensions' folder does not jet exist, it must be allocated with exact the name as specified in the list above.

---------------------------------------------------------------------------
For VEGAS Users:

VEGASPython extends VEGAS with following functions:

Simple functions to demonstrate the principle:
- Add2SecondGap
- AddMarkersAtInterval
- AddMarkersToEvents
- AddRegionsToEvent
- AddTransitionToSelectedEvents
- Audit_Event_Levels
- Chopoff_BeginandEndofEvent_PN
- DeleteEmptySpaceBetweenEvents
- LimitSelectedEventLength
- LimitSelectedEventLengthto4Seconds

New functions only available with VEGASPython
- VegasSceneDetect

VEGASSceneDetect creates subclips of a mediaclip from the Mediapool or on the Timeline based on scene changes.

Usage: Select clips in the mediapool or select clips on the timeline
Call the VEGAS Extension: VEGASScenedetect

A window with the preview of the clip will open. The white bar at the top of the screen shows the progress of the scene detection.
Pressing ESC when the preview window is active aborts the scenedetection process.

VEGASScenedetect is based on PyScenedetect by Brandon Castellano

The commands can be found in VEGAS under the menu point: Extra / Extensions

----------------------------------------------------------------------
For VEGAS Script Developers:

VEGASPython Interactive Windows

VEGASPython includes an interactive window for Python developers.
VEGASPython interactive window is activated via the 'View' / 'Extensions' tab. 

A VEGAS dockable window opens. The window can float over VEGAS or can be integrated into the VEGAS layout as any other VEGAS window.

The VEGASPython window shows two textboxes:

- Input PYTHON Comands
- Output
 
and a menu with two items "File" and "Execute VEGASPython Script".

Python comands are entered in the textbox "Input". Several lines can be entered one after each other. It is possible to copy a complete PYTHON Script from another editor via CTRL+C and CTRL+V.
The PYTHON commands will be executed by clicking on the menu "Execute VEGASPython Script".

All output from PYTHON print statements and all error messages will be shown in the "Output" textbox.


VEGASPython scripts:

VEGASPython scripts are located in the subfolder VEGASPYTHON_PN.You can edit them or create your own with any Python Editor available.

VEGASPython is based on Python 3.7.3 and PythonNet.
Python and PythonNet are included in the download file. There is no need to install anything else.
The Python directory is:  "Application Extension"-Folder\Python37

The Python implementation includes follwing additional modules:
- openCV
- NumPy
- Scenedetect

If you have already another Python installation it will be ignored.

As of version 0.92 a configuration file is provided that allows you to change the Python directory.

For more details see https://www.hlinke.de/dokuwiki/doku.php?id=en:vegas_python_dev

---------------------------------------------
How to access VEGAS from Python:

The VEGAS API can be accessed directly by using the build-in variable "pyVEGAS".
All VEGAS API items are available.

Example for a simple script that can be used in the interactive window:

print("pyVEGAS.Version)

This command prints the current VEGAS version into the output window.

A more complex script:

import clr
clr.AddReference('ScriptPortal.Vegas')
import ScriptPortal.Vegas
from ScriptPortal.Vegas import *

# adapt this value to your needs
maxLength = Timecode.FromString("00:00:04:00")
 
# Go through the list of Tracks
for track in pyVEGAS.Project.Tracks:
    for evnt in track.Events:
        if (evnt.Selected):
            dLength = evnt.Length
            if dLength > maxLength:
                dLength = maxLength
            dStart = evnt.Start
            evnt.AdjustStartLength(dStart, dLength, True)


A script that is used as an extension needs to be included in a function FromVegas. See example below.

import clr
clr.AddReference('ScriptPortal.Vegas')
import ScriptPortal.Vegas
from ScriptPortal.Vegas import *

def FromVegas(pyVEGAS):
   # adapt this value to your needs
    maxLength = Timecode.FromString("00:00:04:00")
 
  # Go through the list of Tracks
    for track in pyVEGAS.Project.Tracks:
        for evnt in track.Events:
            if (evnt.Selected):
                dLength = evnt.Length
                if dLength > maxLength:
                    dLength = maxLength
                dStart = evnt.Start
                evnt.AdjustStartLength(dStart, dLength, True)

The script needs to be saved in the VEGASPYTHON_PN subdirectory. 

-------------------------------------------------------------------------------------
New in Version 1.01:
For user:

VEGASScenedetect:
New function: 
- script names can now contain non-ascii characters - needed if you want to trabslate the scripts into your language


For Developers:
- limitation of the non-ascii character support: 
  - scripts that use non-ascii-characters in their name cannot be debugged using Visual Studio or WING-IDE
  - scripts that use non-ascii-characters in their name must have the following lines at the end of the code:
if __name__ == "__main__":
    FromVegas(pyVEGAS)                 

Justification: Scripts that use non-ascii-characters in their name cannot be imported like other scripts. They are read into a string and the commands in the string are executed directly. The two additinal lines make sure that the function "FromVegas(pyVEGAS)" is called at the end of the script execution correctly.
All VEGASPython Scripts have been adapted to be compatible with this new feature.

VEGASPython 1.02 includes now the newest version of PythonNet 2.4.0. 

PythonNet 2.4.0 changelog:

Added

    Added support for embedding python into dotnet core 2.0 (NetStandard 2.0)
    Added new build system (pythonnet.15.sln) based on dotnetcore-sdk/xplat(crossplatform msbuild). Currently there two side-by-side build systems that produces the same output (net40) from the same sources. After a some transition time, current (mono/ msbuild 14.0) build system will be removed.
    NUnit upgraded to 3.7 (eliminates travis-ci random bug)
    Added C# PythonEngine.AddShutdownHandler to help client code clean up on shutdown.
    Added clr.GetClrType (#432)(#433)
    Allowed passing None for nullable args (#460)
    Added keyword arguments based on C# syntax for calling CPython methods (#461)
    Catches exceptions thrown in C# iterators (yield returns) and rethrows them in python (#475)(#693)
    Implemented GetDynamicMemberNames() for PyObject to allow dynamic object members to be visible in the debugger (#443)(#690)
    Incorporated reference-style links to issues and pull requests in the CHANGELOG (#608)
    Added PyObject finalizer support, Python objects referred by C# can be auto collect now ([#692][p692]).
    Added detailed comments about aproaches and dangers to handle multi-app-domains (#625)
    Python 3.7 support, builds and testing added. Defaults changed from Python 3.6 to 3.7 ([#698][p698])

Changed

    PythonException included C# call stack
    Reattach python exception traceback information (#545)
    PythonEngine.Intialize will now call Py_InitializeEx with a default value of 0, so signals will not be configured by default on embedding. This is different from the previous behaviour, where Py_Initialize was called instead, which sets initSigs to 1. (#449)
    Refactored MethodBinder.Bind in preparation to make it extensible (#829)
    Look for installed Windows 10 sdk's during installation instead of relying on specific versions.

Fixed

    Fixed secondary PythonEngine.Initialize call, all sensitive static variables now reseted. This is a hidden bug. Once python cleaning up enough memory, objects from previous engine run becomes corrupted. (#534)
    Fixed Visual Studio 2017 compat (#434) for setup.py
    Fixed crashes when integrating pythonnet in Unity3d (#714), related to unloading the Application Domain
    Fixed interop methods with Py_ssize_t. NetCoreApp 2.0 is more sensitive than net40 and requires this fix. (#531)
    Fixed crash on exit of the Python interpreter if a python class derived from a .NET class has a __namespace__ or __assembly__ attribute (#481)
    Fixed conversion of 'float' and 'double' values (#486)
    Fixed 'clrmethod' for python 2 (#492)
    Fixed double calling of constructor when deriving from .NET class (#495)
    Fixed clr.GetClrType when iterating over System members (#607)
    Fixed LockRecursionException when loading assemblies (#627)
    Fixed errors breaking .NET Remoting on method invoke (#276)
    Fixed PyObject.GetHashCode (#676)
    Fix memory leaks due to spurious handle incrementation ([#691][i691])
    Fix spurious assembly loading exceptions from private types ([#703][i703])
    Fix inheritance of non-abstract base methods (#755)

    

-------------------------------------------------------------------------------------
New in Version 1.01:
For user:

VEGASScenedetect:
Bug correction: 
- wrong behavior when clip on timeline was trimmed at the start of the clip


For Developers:
- Font and Font Size of Interactive VEGASPython Window can be configured in VEGASPython_CONFIG.JSON

-------------------------------------------------------------------------------------
New in Version 1.00:
For user:

VEGASScenedetect:
New functions: 
- PreviewWindow shows first frame of each scene (marked with red line below the frame) 
- PreviewWindow ProgressBar shows cuts
- Grouping of clips is maintained - e.g. video and sound clip

Bug corretion:
- wrong behavior when project framerate and clip media framerate were different

For Developers:
- VEGASPython Scripts can be called as commandline argument
- VEGASPython Scripts now behave like other VEGAS scripts - the interaktive window is not popping up anymore, but can stil be used for error messages
- improved stability

------------------------------------------------------------------------------------
New in Version 0.92:

For user:

VEGASScenedetect:
New functions: 
- split clips on timeline based on scene changes


For Developers:
- enhanced debugging support, configurable via config file
- support for debugging with WING Python IDE
- Configure CPython directory

------------------------------------------------------------------------------------

New in Version 0.90:

For user:

VEGASScenedetect:
New functions: 
- split mediapool clips into sub clips based on scene changes


For Developers:
- VEGASPython is now based on PYTHONNet and CPython - Full compatibility with standard CPython packages and modules

