How to open a project file with the Python API

Hi there,

I’m just getting started with the MochaPro 2020 Python API and am trying to write a custom project manager. As a start I want to be able to open existing mocha project files.

I’ve run this in the Python Script Editor in an active Mocha Pro 2020 session (with no project loaded):

import mocha
project = mocha.project.Project("/path/to/mochaProjectFile.mocha")
print(project.clips)

This creates a valid Project instance and I can access project.clips and other properties.
However the Mocha UI does not change nor does it load the specified project file.

How can I load a project file just like a user would do manually through File > Open Project…?

Thanks in advance for any help :v:

If Mocha is already open, you can just use a standard subprocess call:

import subprocess
filename = "/_test_footage/results/your_mochafile.mocha"
subprocess.check_call(['open', filename])

This will automatically open the file if it is registered to your current application, and will also ask you if you want to save any current work.
Then use get_current_project() to read the currently open file into your project variable.

Hi @martinb,

thanks for the suggestion. Unfortunately I can’t get it to work. When I run the above code in the Python Script Editor in MochaPro 2020 it throws this error:

Traceback (most recent call last):
  File "<string>", line 3, in <module>
  File "/opt/MochaPro2020/python/lib/python3.7/subprocess.py", line 347, in check_call
    raise CalledProcessError(retcode, cmd)
subprocess.CalledProcessError: Command '['open', '/tmp/test.mocha']' returned non-zero exit status 1

Edit 1:

The Terminal that opens with Mocha prints this when python throws the above error:

Couldn't get a file descriptor referring to the console

It might be worth mentioning that I’m on Linux, where the open command might not act as on OSX or Windows. I’ve tried xdg-open, but that just opens a new MochaPro session instead of opening in the current one.

Hey, what specific version number of Mocha are you using? You can check this in the about box.

The specific version is:

Mocha Pro 2020
Version 7.0.4 build 9.g7a500571e508
Build Date Jan 21 2020

Okay, you might want to update to the latest update (v7.5.1) as there’s been a number of improvements, including fixes to the Python API.

Alright, I’ll try that. Thanks :v: