Hello!
This is Mocha Product Manager Martin Brennand and today we are going to take a more technical
dive into using the Python features in Mocha Pro standalone.
Now to make it absolutely clear this feature is only currently available in the standalone
version.
If you’re in the plugin version, you don’t get all these fun scripting tools.
I’m aiming this tutorial at beginners, but it does help if you already know a little
bit of Python.
So, we’re looking at this scenario: We have a bunch of layers but someone has made the
severe misdemeanour of not categorising their layers into a cohesive naming convention.
So what we're going to do is write a script to take a bunch of these selected layers and
add a new word to the start of each one.
So let's start by opening up the Python script editor.
We come up to the View menu and the Python script editor is in the selections down here
with all our other panels.
So let's choose that one and let's drag it out.
As a side note, I also like to keep this setup in its own custom layout.
So that we don’t have to do this every single time.
So we can see that we have our Python script editor and our standard windows over here
as a Python custom layout.
So here’s our code window.
We have: The open button at the top here to open previous
scripts.
We have our save button to save what we’ve been working on in the window.
And we have the all-important run button to run our scripts.
Below, we have the code editor window where we’ll do our coding and an output window
for general errors printing and so on.
So now we can start coding!
Let’s start by letting Mocha know the details of the project that is open right now.
We can do this by importing a get_current_project function from the mocha Python library: So
we’ll type from mocha.project import get_current_project with underscores.
And this is just pulling it in from the Mocha library for this function.
Next, we’re going to define that variable as a project string.
So I’m going to say “proj” equals get_current_project().
So this is now setting the current project into this variable called “proj”.
Now I’m going to tell Mocha the word I want to add to the front of my selected layers.
In this case, I want to define all the layers that are specifically rotoscoping and not
for tracking data.
I’ll call this variable prepend and I’m going to tell it to use the string “roto”.
And we define a string by putting it in inverted commas like so.
Now importantly, because we want to add it to a layer later, I’m going to put a space
here so that we get a space in the front of our layer name.
Now we have our project defined we can have a look inside it to find all the selected
layers and add the word to.
We’re going to do this by telling Mocha for every layer in the proj.layers: if the
layer is selected, layer.name is going to equal the prepend value plus the layer.name.
So what this is doing is going through every single layer in the layers, checking if that
layer is selceted, and then if it’s selected, setting the layer name to the prepend value
plus the original layer name.
And that’s it.
That’s all the coding we have to do for this!
And now we can go ahead and run it.
I’m going to select the layers I need, choosing shift to select them all and then I’m going
to press Run.
And you can see down here our first error.
We’ve got an error here that says it can’t import get current poject because I’ve mistyped.
So I’m going to come back up to my import here and make sure I put that “r” into
my import.
And now it should run correctly.
So let’s clear the output with our little clear output button over here.
And now when we press Run, you can see that Roto has been added to the front of every
single layer.
If we want to do the same for the tracking layers what you have to do is select our track
layers, change the word to “track”, press run, and then the same thing occurs.
Now we could extend this in many different ways, such as having an actual GUI for this
interface to change the words on the fly.
You could even have it load as a tool up in our script menu here, or as a button in the
interface.
Or we could even have it run through the layer and have it change other properties such as
the motion blur categories or things like the visibility.
If you want to explore further and delve into the Python API reference, it is available
online from the documentation section of the Boris FX website, or you can look at the general
Python guide up here in the Online Python Docs or the Offline Python Guide, in the Help
menu of Mocha Pro standalone.
If you’d like to see more examples for Python in Mocha standalone, let us know in the comments,
or post in the forums at borsifx.com