import os, sys, subprocess
import maya.cmds as cmds
import maya.mel as mel

# Output functions
def __errorMsg(message):
	mel.eval((u'error("%s")') % message)

def __warningMsg(message):
	mel.eval((u'warning("%s")') % message)

def invokeImfDisp(fileName, channel):
	"""
	Display given image file using imf_disp that is packaged with Maya.

	If the image is an OpenEXR and a channel is defined then that channel
	will be displayed, otherwise the default channels (R,G,B,A) will be used.
	"""
	if os.path.exists(fileName):
		# create imf_copy command
		imfDispBin = ('%s/bin/imf_disp')
		if cmds.about(mac=True):
			imfDispBin += ('.app/Contents/MacOS/imf_disp')
		cmdAndArgs = [ imfDispBin % mel.eval(('getenv("MENTALRAY_LOCATION")')) ]
		if channel:
			cmdAndArgs.append(('--layer=%s') % channel)

		# modify file name on Windows as imf_disp is picky
		finalFileName = fileName
		if cmds.about(win=True):
			finalFileName = os.path.normpath(fileName)
		cmdAndArgs.append(finalFileName)

		# encode params to subprocess in the system encoding because
		# the subprocess module does not play nice with unicode strings
		fse = sys.getfilesystemencoding()
		try:
			cmdAndArgs = [ arg.encode(fse) if isinstance(arg, unicode) else arg for arg in cmdAndArgs ]
		except:
			message = mel.eval('getPluginResource("Mayatomr", "kEncodingError")')
			message %= ' '.join(cmdAndArgs)
			__warningMsg(message)

		# run command in non-blocking manner, user will be responsible
		# for closing new window
		try:
			subprocess.Popen(cmdAndArgs)
		except:
			message = mel.eval('getPluginResource("Mayatomr", "kRunProcessFailed")')
			message %= ' '.join(cmdAndArgs)
			__errorMsg(message)
	else:
		message = mel.eval('getPluginResource("Mayatomr", "kImageFileMissing")')
		message %= fileName
		__errorMsg(message)

# Copyright (C) 1997-2014 Autodesk, Inc., and/or its licensors.
# All rights reserved.
#
# The coded instructions, statements, computer programs, and/or related
# material (collectively the "Data") in these files contain unpublished
# information proprietary to Autodesk, Inc. ("Autodesk") and/or its licensors,
# which is protected by U.S. and Canadian federal copyright law and by
# international treaties.
#
# The Data is provided for use exclusively by You. You have the right to use,
# modify, and incorporate this Data into other products for purposes authorized 
# by the Autodesk software license agreement, without fee.
# THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND. AUTODESK
# DOES NOT MAKE AND HEREBY DISCLAIMS ANY EXPRESS OR IMPLIED WARRANTIES
# INCLUDING, BUT NOT LIMITED TO, THE WARRANTIES OF NON-INFRINGEMENT,
# MERCHANTABILITY OR FITNESS FOR A PARTICULAR PURPOSE, OR ARISING FROM A COURSE 
# OF DEALING, USAGE, OR TRADE PRACTICE. IN NO EVENT WILL AUTODESK AND/OR ITS
# LICENSORS BE LIABLE FOR ANY LOST REVENUES, DATA, OR PROFITS, OR SPECIAL,
# DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES, EVEN IF AUTODESK AND/OR ITS
# LICENSORS HAS BEEN ADVISED OF THE POSSIBILITY OR PROBABILITY OF SUCH DAMAGES.

