Page 1 of 1
Import relative python file
Posted: Thu Jul 11, 2019 10:49 am
by bryanfordney
Is there a way to import other python files into a python script?
My method for this when the relative imports don't work is usually to manually add the current directory of the current file to the PATH:
import sys
this_path = os.path.realpath(os.path.dirname(__file__))
if not this_path in sys.path:
sys.path.append(this_path)
But, in Fusion ,`__file__` doesn't exist.
Re: Import relative python file
Posted: Fri Jul 12, 2019 3:23 pm
by Movalex
your module path will not preserve in sys.path, so you have to append it on each script run.
Try this:
import sys
import os
sys.path.append(os.path.expanduser("~/Downloads/import_file/pymodules"))
try:
from fu9import import remote_task as rts
except ImportError:
print('no module imported')
test = rts(os.path.expanduser("~/Downloads/import_file/test1.comp"))
if test:
print("\nLoaded comp: {}".format(test.GetAttrs()["COMPS_FileName"]))
import sys
from pathlib import Path
def remote_task(comp_path):
comp_path = Path(comp_path)
if not comp_path.is_file() or comp_path.suffix != ".comp":
print("wrong comp path")
return None
fu = bmd.scriptapp("Fusion")
current_comp = fu.GetCurrentComp()
if not current_comp.ActiveTool:
tool = current_comp.Blur
print("created {}".format(tool.Name))
temp_comp = fu.LoadComp(str(comp_path))
return temp_comp
Re: Import relative python file
Posted: Wed Jul 17, 2019 1:41 pm
by bryanfordney
This works for my purposes (had to reorganize all my reusable code into modules):
LIB_PATH = comp.MapPath("UserData:/Scripts/Lib")
if LIB_PATH not in sys.path:
sys.path.append(LIB_PATH)