Skip to content

Instantly share code, notes, and snippets.

@danbradham
Created June 4, 2015 18:10
Show Gist options
  • Save danbradham/c5dabb0920f7726d375f to your computer and use it in GitHub Desktop.
Save danbradham/c5dabb0920f7726d375f to your computer and use it in GitHub Desktop.
Imageplane utilities for maya
import pymel.core as pmc
from PySide import QtGui, QtCore
import imgutils
import itertools
import glob
import re
import os
def get_maya_window():
'''Grabs Maya's MainWindow QWidget instance. I feel enough software
has sufficiently switched over to PySide to simplify this function and
stop supporting both PyQt and PySide. Little lazy with the maya import,
allowing this module to be imported to a standard python interpreter.'''
import shiboken
import maya.OpenMayaUI as mui
ptr = long(mui.MQtUtil.mainWindow())
return shiboken.wrapInstance(ptr, QtGui.QWidget)
def selected_imageplanes():
'''Return the selected imageplanes'''
return pmc.ls(sl=True, type='imagePlane')
def is_newer(file_a, file_b):
'''Returns True if file_a is newer than file_b.'''
return os.path.getmtime(file_a) > os.path.getmtime(file_b)
def create_proxy(imageplane):
'''Create proxy sequence for an imageplane.
:param imageplane: PyMEL.PyNode()
:return: First frame in image sequence'''
imagename = imageplane.imageName.get()
progress = QtGui.QProgressDialog(
'Detecting Image Sequence...',
'Cancel',
0,
1,
parent=get_maya_window())
progress.setWindowModality(QtCore.Qt.WindowModal)
progress.setAutoClose(False)
progress.setAutoReset(False)
seq_matcher = re.compile('\.\d+(?=\D*$)')
match = seq_matcher.search(imagename)
if match:
start, end = match.span()
start += 1
glob_pattern = imagename[:start] + '*' + imagename[end:]
files = sorted([os.path.normpath(f) for f in glob.glob(glob_pattern)])
else:
files = [imagename]
progress.setValue(1)
progress.reset()
progress.setLabelText('Creating Proxy Paths...')
progress.setMaximum(len(files) - 1)
proxy_files = []
for i, f in enumerate(files):
progress.setValue(i)
root, filename = os.path.split(f)
basename, ext = os.path.splitext(filename)
proxy_files.append(os.path.join(root, 'proxy', basename + '.jpg'))
try:
os.makedirs(os.path.join(root, 'proxy'))
except OSError:
pass
progress.reset()
progress.setLabelText('Creating Proxy Images...')
progress.setMaximum(len(proxy_files) - 1)
progress.setAutoClose(True)
progress.setAutoReset(True)
for i, (f, pf) in enumerate(itertools.izip(files, proxy_files)):
progress.setValue(i)
if progress.wasCanceled():
return
if os.path.exists(pf) and is_newer(pf, f):
continue
imgutils.copy(f, pf, size=(960, 540), quality=10)
return proxy_files[0]
def get_fullres(proxy_file):
'''Get the filepath of the full resolution image from a proxy_file'''
proxy_root, imagename = os.path.split(proxy_file)
root = os.path.dirname(proxy_root)
imgname = os.path.splitext(imagename)[0] + '.*'
glob_pattern = os.path.join(root, imgname)
fullres_files = glob.glob(glob_pattern)
if fullres_files:
return fullres_files[0]
raise OSError('Can not find full res sequence.')
def use_proxy(imageplane, value):
imgpath = imageplane.imageName.get()
root, imagename = os.path.split(imgpath)
is_proxy = root.endswith('proxy')
if not value and is_proxy:
fullres = get_fullres(imgpath)
imageplane.imageName.set(fullres)
if value and not is_proxy:
proxyname = create_proxy(imageplane)
if proxyname:
imageplane.imageName.set(proxyname)
def toggle_proxy():
imageplanes = selected_imageplanes()
for imageplane in imageplanes:
imgpath = imageplane.imageName.get()
root, imagename = os.path.split(imgpath)
value = not root.endswith('proxy')
use_proxy(imageplane, value)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment