Welcome to WSL!

New to the forum? Please read this and this.

Getting scripting to work with Python 3 on Windows?

User avatar
lateralfilms
Posts: 13
Joined: Fri Apr 09, 2021 2:32 pm
Real name: Robert Arnold

Getting scripting to work with Python 3 on Windows?

#1

Post by lateralfilms »

Hi All,

I've got an application working great in Python 3.6.8 on Mac which I'm trying to port to Windows.

On Win, it seems like imp is deprecated (it's Python 3.9) and causes a fatal error when it hits this line:

script_module = imp.load_dynamic("fusionscript", path + "fusionscript" + ext)

How to rewrite this line so it works in python 3.9 on Windows? I'm stuck....

User avatar
lateralfilms
Posts: 13
Joined: Fri Apr 09, 2021 2:32 pm
Real name: Robert Arnold

Re: Getting scripting to work with Python 3 on Windows?

#2

Post by lateralfilms »

I downgraded to 3.6.8 and just used the existing code. Would still love to now what the updated version of this code would be...

User avatar
Igor Ridanovic
Fusioneer
Posts: 56
Joined: Mon Apr 16, 2018 11:17 pm
Answers: 1
Location: Los Angeles
Been thanked: 1 time
Contact:

Re: Getting scripting to work with Python 3 on Windows?

#3

Post by Igor Ridanovic »

The BMD API does not support Python versions above 3.6.

User avatar
Movalex
Sir Requestalot
Posts: 316
Joined: Fri Nov 03, 2017 5:36 am
Answers: 4
Real name: Alexey Bogomolov
Been thanked: 17 times
Contact:

Re: Getting scripting to work with Python 3 on Windows?

#4

Post by Movalex »

@latenitefilms
In the latest Python version you should use importlib. So to import dll to python 3.9, the BlackmagicFusion.py file would look like this:
  1. import sys
  2. import importlib
  3. import os
  4.  
  5. fu_mod = None
  6.  
  7. <...>
  8.  
  9.     for path in paths:
  10.         module = "fusionscript"
  11.         full_path = path + "fusionscript" + ext
  12.         if not os.path.exists(full_path):
  13.             continue
  14.         loader = importlib.machinery.SourceFileLoader(module, full_path)
  15.         spec = importlib.util.spec_from_loader(module, loader)
  16.         fu_mod = importlib.util.module_from_spec(spec)
  17.         try:
  18.             loader.exec_module(fu_mod)
  19.         except ValueError as e:
  20.             print("unable to execute module\n", e)
  21.  
  22. if fu_mod:
  23.     sys.modules[__name__] = fu_mod
  24. else:
  25.     raise ImportError("could not locate module dependencies")
But as Igor said, bmd does not support the latest python version, and fusionscript.dll would not load with the error "source code string cannot contain null bytes". Probably BMD will soon update this file to support latest Python versions (say in the next 10 years).

User avatar
lateralfilms
Posts: 13
Joined: Fri Apr 09, 2021 2:32 pm
Real name: Robert Arnold

Re: Getting scripting to work with Python 3 on Windows?

#5

Post by lateralfilms »

Damn, I'm stuck trying to get a Windows 10 executable to work. I am using pyinstaller, and my exe works fine - on a system that has Python 3.6.8 installed. On a test system, the exe chokes on this line:

script_module = imp.load_dynamic("fusionscript", path + "fusionscript" + ext)

(I know that both "path" and "ext" are the right values, thanks to a print statement.)

The weird thing is, I've determined that there's just one file that is necessary for the exe to run on the test system, and that's the os.py file in the Lib folder of the Python install. Without that file, the exe just quits (without any error message) when it hits the above line of code. If os.py is present, it runs fine. Any ideas on how to fix this? I tried to force pyinstaller to include the os.py file (by listing it in the datas of my .spec), but to no avail....

User avatar
Movalex
Sir Requestalot
Posts: 316
Joined: Fri Nov 03, 2017 5:36 am
Answers: 4
Real name: Alexey Bogomolov
Been thanked: 17 times
Contact:

Re: Getting scripting to work with Python 3 on Windows?

#6

Post by Movalex »

Maybe something with the test machine setup? Check if you can make import BlackmagicFusion as bmd on a test system.
Also this should return current comp properties in command line:
  1. import imp
  2. bmd = imp.load_dynamic("fusionscript", "C:\\Program Files\\Blackmagic Design\\Fusion 17\\fusionscript.dll")
  3. fu = bmd.scriptapp("Fusion")
  4. print(fu.GetCurrentComp().GetAttrs())