Run Python plugins in Hiero - python

I am facing a small problem in executing custom made python code from Hiero. As everybody knows, the first place to look for details is the offical documentation "Nukepedia", the Python Dev Guide and the Hiero User Guide. Well according to the Python Dev Guide, Hiero creates a ".hiero" directory by default which lets people add plugin paths to use them in the software. Well, I can't find that directory and I deduced after several tests that Hiero is using the init.py saved in the ".nuke" directory.
So I thought that maybe I could add the plugin paths there but kept getting the famous Import Error for the _fnpython module (before creating Python/Startup directory).
I added Python/Startup folders in .nuke and added the plugins in Startup, I got the same error, I even tried it by adding the path to the plugins in init.py and got the same error too.
Then I created a ".hiero" folder and did the same thing as before but Hiero never took that folder into consideration, I deduced that by printing some strings in the console, Hiero always took the init.py saved in the ".nuke" folder and kept showing the same error.
Finally, I tried to look into the installation process and try to seperate Nuke and Hiero's folders maybe that would create the ".hiero" directory but everything was automated.
The code that I want to run is given by Nuke and Hiero (in the examples directory), I just can't figure out what to do in order to run it from the program.
What should I do in order to fix this problem ?
Thank you in advance.

The setup for The Foundry HIERO is a little different than for NUKE.
HIERO has a core module. You'll see it in __init__.py file:
import FnRedirect
import FnPythonFixes
import core
import ui
try:
if hasattr(core, "taskRegistry"):
import importers
import exporters
except ImportError as e:
pass
I'm running HIERO and NUKE STUDIO on a Mac, so there's a full path to HIERO's __init__.py file inside package contents:
/Applications/Nuke10.5v5/Contents/MacOs/pythonextensions/site-packages/hiero/__init__.py
You need to import this module using:
import hiero.core
or using a reference to the core package:
from core import *
To find HIERO's current paths you have to run this line in its Script Editor:
print hiero.core.pluginPath()
Click this link for further details: Hiero's Environment Setup
All these instructions are suitable for macOS 10.9 and above. Here are two blocks of steps: first for Terminal Mode and second for UI Mode.
BLOCK 1: setup for Terminal Sessions
You need to manually create .hiero directory in your Home area.
The recommended default location for running Python on startup is:
~/.hiero/Python/Startup
~/.hiero/Python/StartupUI
Type in your bash Terminal (when you're inside your Home user directory) the following line:
mkdir .hiero/
then:
mkdir .hiero/Python/
and then:
mkdir .hiero/Python/StartupUI/
then navigate to Home directory with:
cd ~
and check it with:
ls -a
Also you can specify any number of user-defined paths using the environment variable HIERO_PLUGIN_PATH, just like the standard Unix PATH environment variable.
For that you need to set up an environment variable in .bash_profile. To run in Terminal PICO editor just type (remember you need an administrator's password for sudo command):
sudo pico .bash_profile
and paste these three lines in it (change swift for <yourName> and save this file):
echo HIERO environment var is set...
export HIERO_PLUGIN_PATH=/Users/swift/.hiero/Python/StartupUI/
export PATH=$PATH:$HIERO_PLUGIN_PATH
Write out a file with ctrl o
Exit pico editor with ctrl x
Restart Terminal
In Terminal you could print this environment variable typing:
printenv HIERO_PLUGIN_PATH
You should put inside that StartupUI directory menu.py, any set of xxxx.py or xxxx.pyc files, as well as __init__.py file.
Now you can use /Users/swift/.hiero/Python/StartupUI/ path in Terminal Mode.
BLOCK 2: setup for UI Sessions
To assign the current paths that HIERO searches when loading plug-ins, you need to create __init__.py file with the following lines:
import hiero.core
path='/Users/swift/.hiero/Python/Startup/'
hiero.core.addPluginPath(path)
After that make Python/Startup/ subdirectories under ~/.nuke/ folder.
It's not a mistake: I typed .nuke.
Then place this __init__.py file into /Users/swift/.nuke/Python/Startup/ directory.
Restart HIERO (or NUKE STUDIO) if it works.
After that launch HIERO or NUKE STUDIO and run
print hiero.core.pluginPath()
command in the HIERO's Script Editor or in NUKE STUDIO's Script Editor and you'll see this result:
After that you'll find new __init__.pyc file in /Users/swift/.nuke/Python/Startup/ directory.
Each package or module discovered when you launch HIERO is imported and added to the built-in package hiero.plugins.
TEST 1: custom_guides.py
I do not have a commercial version of HIERO so I tested custom_guides.py script ( found here ) using NUKE STUDIO NC.
I placed custom_guides.py in ~/.nuke/Python/Startup directory and then added two lines to NUKE's init.py file located in ~/.nuke directory.
import nuke
nuke.pluginAddPath("./Python/Startup")
The only thing I could say: "it works" Do the same actions as I did and it'll work for HIERO.
Look at safe_zone and masking_ratio dropdown menus. They are different: before and after.
Before uploading custom_guides.py script:
After uploading custom_guides.py script:
# file custom_guides.py contains these lines:
viewer_masks = [
hiero.ui.guides.MaskGuide("NTSC", 0.91),
hiero.ui.guides.MaskGuide("PAL", 1.09),
hiero.ui.guides.MaskGuide("NTSC_16:9", 1.21),
hiero.ui.guides.MaskGuide("PAL_16:9", 1.46),
hiero.ui.guides.MaskGuide("Cinemascope 2:1", 2.0)
]
TEST 2: web_browser.py
I placed web_browser.py file in ~/.nuke/Python/Startup directory. This Python script creates dockable panel with web browser written with PySide Qt.
I do not have a commercial version of HIERO so I tested web_browser.py script ( found here ) using NUKE STUDIO NC.

Related

Python - How Do I Make Relative Imports Compatible with Nose

I am working on a package which has a folder structure like:
Root
|---Source
|---Testing
|---Test_Utils
|---test_fixtures.py
|---test_integration.py
|---test_unit.py
There are a fair amount of relative references flying around (e.g. in test_integration.py I need to import classes from the files in the Source folder as well as test harnesses and data from the Test_Utils folder.
So far I've managed this by using complete references e.g.:
from Root.Testing.Test_Utils.test_fixtures import *
Which seemed to work fine until actually trying to run nosetests. This is because nose seems only to find test files in the active directory (not the root working directory), so I have to cd Testing before running nosetests.. at which point the relative references break with:
ModuleNotFoundError: No module named 'Root'
How can I get round this seeming incompatibility (without using pytest, since I am using test generators (i.e. using yield) which I believe are deprecated in pytest)?
PYTHONPATH sets the search path for importing python modules.
If you are using (Mac or GNU/Linux distro), add this to your ~/.bashrc.
# add this line to ~/.bashrc
export PYTHONPATH=PYTHONPATH:/path/to/folder # path where is your Root folder
# after this we need to reload ~/.bashrc
$ source ~/.bashrc
# if source ~/.bashrc don't work simple restart your terminal
# after this we can echo out our PYTHONPATH environment variable to check if it was added successfully
$ echo PYTHONPATH
# it need to output your folder

Run python script in package

I am struggling with running python script in shell. I use PyCharm where is everything ok, but I want to run script without running PyCharm.
So my project folder is like:
data/
file.txt
main/
__init__.py
script.py
tools/
__init__.py
my_strings.py
I want to run main/script.py, which start with from tools import my_strings and working directory should be data/.
My PyCharm config is:
Script path: <PROJECT>/main/script.py
Working directory: <PROJECT>/data
Add content roots to PYTHONPATH: YES
Add source roots to PYTHONPATH: YES
So I want to run main/script.py in shell on Ubuntu. I tried:
PYTHONPATH=<PROJECT>
cd <PROJECT>/data
python3 ../main/script.py
But I just got: ImportError: No module named 'tools'
Check out this post, it's explains the PYTHONPATH variable.
How to use PYTHONPATH and the documentation the answer points to https://docs.python.org/2/using/cmdline.html#envvar-PYTHONPATH
When you run from the data directory by default python can't find your tools directory.
Also regarding your comment about needing to run from the data directory, you could just use open('../data/file.txt') if you decided to run from the main directory instead.
Ideally, you should be able to run your script from anywhere though. I find this snippet very useful os.path.dirname(sys.argv[0]). It returns the directory in which the script exists.
I simply forgot to export $PYTHONPATH as suggested by Steve.

Importing a module outside of Jenkins home directory

I am trying to run a custom Python module located at '/home/modules/module.py'
The python script is run by Jenkins and located at '/home/user_name/scripts/script.py'
Since Jenkins home directory is '/var/lib/jenkins' how can I import my module?
I have tried adding the following to the python script:
import sys
sys.path.insert(0,'/home/modules') """also /home/modules/"""
import module
but I am still getting the error:
ImportError: No module named
I am running everything in Ubuntu 14.04 and Python 2.7
EDIT:
I changed Jenkins user directory like this:
root#dwh-01:~$ usermod -m -d /home/jenkins jenkins
and modified JENKINS_HOME=/home/jenkins in /etc/default/jenkins
I think I can work with this, but now the issue is, if I log into jenkins shell and do:
jenkins#dwh-01:/$ cd
bash: cd: jenkins/: No such file or directory
Should the behaviour of the cd command direct me to /home/jenkins/ ?
If I repeat the same when in /home/ It works though.
If I try and start jenkins, it gives me:
Starting Jenkins Continuous Integration Server jenkins
No directory, logging in with HOME=/
EDIT-2:
User jenkins home dir error fixed, I just made sure the home directory of the user was /home/jenkins using usermod -d /home/jenkins jenkins
I'm a step closer to importing the module, but am still having issues going one step outside of the jenkins home directory.
You should have two options that would allow you to import module into your code.
Option 1
import sys
sys.path.append('/home/modules')
import module
Option 2
import importlib.machinery
importlib.machinery.SourceFileLoader('module', '/home/modules').load_module()
If neither of these work, hopefully they will at least point you and others in the right direction.
Ok finally figured it out.
What I did was change in the /etc/default/jenkins file the variable JENKINS_USER and JENKINS_GROUP to the user i needed to outside of the jenkins user folder (outside of /var/lib/jenkins).
This way the scripts ran by Jenkins will be ran as if it was the selected user I specified.
After that, I realized that in Jenkins, even if the working directory is /var/lib/jenkins/jobs/adjust_data_parser/workspace, scripts and files can be called from '/'
So the trick was:
-Accessing the script outside of the jenkins home directory
-Importing the module from its absolute path.

django import local settings from the server

In my home directory, I have a folder called local. In it, there are files called __init__.py and local_settings.py. My django app is in a completely different directory. When the app is NOT running in DEBUG mode, I want it to load the local_settings.py file. How can this be acheived? I read the below:
Import a module from a relative path
Importing files from different folder in Python
http://docs.python.org/tutorial/modules.html
Basically, those tutorials are allowing to import from another directory, but what about a completely different working tree? I don't want to keep doing .., .., .. etc. Is there a way to goto the home directory?
I tried the following code:
import os, sys
os.chdir(os.path.join(os.getenv("HOME"), 'local'))
from local_settings import *
But i keep seeing errors in my apache error.log for it...
os.chdir just affects the current working directory, which has nothing whatsoever to do with where Python imports modules from.
What you need to do is to add the the local directory to the Pythonpath. You can either do this from the shell by modifying PYTHONPATH, or from inside Python by modifying sys.path:
import sys
import os
sys.path.append(os.path.expanduser("~/local"))
import local_settings
In response to your concerns about source control, you can just set the source control to ignore that file, and/or have a symlink installed as part of your deploy script to link the file on the os into another. I do both , though not in django. but it's a trivial task.
your deploy layout could look like this:
/current ( symlink to /releases/v3 )
/settings/local_settings.py
/releases/v1
/releases/v2
/releases/v3
and a task runs as part of your deploy:
cd /current
ln -s /settings/local_settings.py local_settings.py
if you're deploying with fab or capistrano, it's a few lines of configuration. i'm sure you could do this with puppet/chef simply too.

What do I specify for "Rope project root folder: . " with python files and RopeVim plugin?

I've installed the plugin RopeVim (using Pathogen) and it seems to be working.
Now when I call :RopeGoToDefinition with my vim cursor (in command mode) on a function I'd like to see the definition of...I get:
Rope project root folder: .
displayed in the status line of my vim (fwiw, I'm using MacVim).
What is the proper folder to specify here?
My project folder structure has a root folder, and various subdirs. I can't even tell if I should be specifying a system filepath or a python-style module.
See https://github.com/python-rope/rope/blob/master/docs/overview.rst#ropeproject-folder and the Getting Started section of https://github.com/python-rope/rope/blob/master/README.rst
You can stick it anywhere, but you probably want to use the root directory of your project. Note how the prompt defaults to ., the current directory. If your file is in the root directory, you can just hit Enter to accept that default. Otherwise, try something like ../.. or /path/to/root.

Categories