def pluginExecute(lArgs, sa):
	logging.info("Executing plugin GoodForEdit.")
	print("Copying versions to clipboard.")

	# First, get the Project from the first version
	if sa.project:
		dSGProject = sa.project
	else:
		lSGVersions = sg.find("Version", sa.ids_filter, ['project'])
		dSGProject = lSGVersions[0]['project']

	iProjectManagerID = int(sg.find_one('Project', [['id', 'is', dSGProject['id']]], ['sg_projectmanager_id'])['sg_projectmanager_id'])
	api = Api()
	oPMRootProject = api.getProjectsBySearch({'uid':iProjectManagerID})[0]

	# Then, find the DAILIES folder and checks if it exists.
	dailiesDir = oPMRootProject.getAbsPathOnline() + '/' + "DAILIES"
	if lArgs[0] == 'Comp' or lArgs[0] == 'CompPlay':
		sTargetPath = dailiesDir + '/' + "EDIT_COMP"
		sSGInEditFieldName = 'sg_in_edit_comp'
	else:
		sTargetPath = dailiesDir + '/' + "EDIT"
		sSGInEditFieldName = 'sg_in_edit_anim'

	if not os.path.exists(sTargetPath):
		os.makedirs(sTargetPath)

	for sId_Filter in sa.selected_ids_filter:
		dSGVersion = sg.find_one("Version", [sId_Filter], ['sg_path_to_movie', 'entity'])
		dSGShot = sg.find_one("Shot", [['id', 'is', dSGVersion['entity']['id']]], ['code','sg_projectmanager_id'])


		sTargetName = dSGShot['code'] + ".%04d.jpg"
		sTargetFilename = sTargetPath + '/' + sTargetName
		cmd = "//online1/software/ffmpeg/ffmpeg.exe -i %s -b 30M %s" % (dSGVersion['sg_path_to_movie'], sTargetFilename)
		print(cmd)
		subprocess.call(cmd)

		# Update version 'In Edit ...' flag in Shotgun. Unset flag on older versions in edit.
		lAllVersionsInEdit = sg.find("Version", [['entity', 'is', dSGShot], [sSGInEditFieldName, 'is', True]])
		for dVersion in lAllVersionsInEdit:
			sg.update("Version", dVersion['id'], {sSGInEditFieldName:False})
		sg.update("Version", dSGVersion['id'], {sSGInEditFieldName:True})

		# TEMP code for SEARCH and rescue. We have to make it generic for all projects some time.
		if lArgs[0] == 'CompPlay':
			oPMShot = api.getObjectByUid('Shot', None, dSGShot['sg_projectmanager_id'])
			sShotPath = oPMShot.getAbsPathOnline()
			sEditPath = sShotPath + '/REFERENCES/OFFLINE/SRPIT_%s_Edit_01a.rv' % (oPMShot.getIdentifier())
			cmd = 'C:/oblique/Software/RV/rvlink.bat %s' % (sEditPath)
			subprocess.call(cmd)




	# Continue code

	print "done."
def pluginExecute(lArgs, sa):
	''' This is the function that gets called when the plugin is fired.
	lArgs is a list of arguments passed from the url defined earlier.
	sa is an instance of ShotgunAction and contains all the info needed to process the call.

	dir(sa):
	 'action',
	 'column_display_names',  # Internal column names currently displayed on the page
	 'columns',               # Human readable names of the columns currently displayed on the page
	 'entity_type',           # entity type that the page was displaying
	 'ids',                   # All ids of the entities returned by the query (not just those visible on the page)
	 'ids_filter',            # All ids of the entities returned by the query in filter format ready to use in a find() query
	 'params',                # contains the dict will all the values. Details following.
	 'project',               # Project info (if the ActionMenuItem was launched from a page not belonging to a Project (Global Page, My Page, etc.), this will be blank
	 'protocol',
	 'selected_ids',          # ids of entities that were currently selected
	 'selected_ids_filter',   # All selected ids of the entities returned by the query in filter format ready to use in a find() query
	 'title',                 # title of the page
	 'url',                   # The original URL passed to the script.
	 'user'                   # user info who launched the ActionMenuItem

	sa.params=
	{'cols': ['code',
			  'image',
			  'sg_qt],
	 'column_display_names': ['Version Name',
							  'Thumbnail',
							  'QT'],
	 'entity_type': 'Version',
	 'grouping_column': 'created_at',
	 'grouping_direction': 'desc',
	 'grouping_method': 'day',
	 'ids': '7754,7738,7739,7740',
	 'project_id': '84',
	 'project_name': 'SOURCE_CODE',
	 'selected_ids': '7754,7738,7739,7740',
	 'sort_column': 'code',
	 'sort_direction': 'asc',
	 'title': 'Versions',
	 'user_id': '39',
	 'user_login': '******'}

	'''
	print("Uploading playlist to INTERDUBS.")

	lSequencesToProcess = []
	for sId_Filter in sa.selected_ids_filter:
		lPlaylists = sg.find("Playlist", [sId_Filter], ['versions', 'code', 'description', 'updated_at'])
		for sgPlaylist in lPlaylists:
			sInterdubsPath = "/" + sa.params['project_name'] + "/" + sgPlaylist['code']
			iInterdubsPathID = idx.chkmkpath(sInterdubsPath)
			if sgPlaylist['description']:
				idx.add_node_note(iInterdubsPathID, sgPlaylist['description'])
			for sgVersion in sgPlaylist['versions']:
				sgVersion = sg.find_one('Version', [['id', 'is', sgVersion['id']]], ['sg_path_to_movie', 'code', 'sg_description_for_client'])
				# check if version exists on Interdubs and skip it.
				if idx.node_exists(os.path.basename(sgVersion['sg_path_to_movie']), iInterdubsPathID):
					print("\n Version %s exists in Interdubs, skipping.\n" % os.path.basename(sgVersion['sg_path_to_movie']))
					continue

				# Recompress
				sFilename = sgVersion['sg_path_to_movie']
				try:
					bzApi = Api(bzhost='projectmanager')
				except:
					raise IOError, "An instance of the ProjectManager API cannot be obtained.\n" \
						  +"This is most probably because there is a problem with the projectmanager machine."\
						  +"(yes there's a machine with that name)"

				oRootProject = bzApi.getProjectFromPath(sFilename).currentRootProject()

				# find the approval directory
				dirnodes = oRootProject.getTemplate().searchNodes(attributes={"path":"CLIENT_APPROVAL"})
				if not dirnodes:
					dirnodes = oRootProject.getTemplate().searchNodes(attributes={"path":"APPROVAL"})
				if len(dirnodes) != 1:
					raise IOError, "Cannot find approval directory in root project"

				# create the approval dir if it doesn't exist
				apprNode = dirnodes[0]
				if not apprNode.exists():
					apprNode.create()
					if not apprNode.exists():
						raise IOError, "Cannot create CLIENT_APPROVAL directory"

				sTargetDir = "%s/%s" % (apprNode.getAbsPathOnline(),datetime.date.today().isoformat())
				if not os.path.exists(sTargetDir):
					os.mkdir(sTargetDir)

				sCompressedFilename = "%s/%s" % (sTargetDir, os.path.basename(sFilename))

				if sFilename.endswith('.mov') or sFilename.endswith('.mp4'):
					# Get info from Project Manager
					dOutRes = oRootProject.getOutputResolution()
					sGoodFrameRate = str(dOutRes['frameRate'])
					iBitRate = 2000 # Kb/s
					sCommand = "//online1/software/ffmpeg/ffmpeg.exe -y -i %s -vcodec libx264 -b %dK -acodec libfaac -ab 128k -threads 0 %s"\
								% (sFilename, iBitRate, sCompressedFilename)

					print("Recompressing movie in H.264 using ffmpeg.")

					oProcess = subprocess.Popen(sCommand,
												shell=True,
												stdin=subprocess.PIPE,
												stdout=subprocess.PIPE,
												stderr=subprocess.PIPE,
												universal_newlines=True)
					sOUT, sERR = oProcess.communicate()
					print(sOUT)
					print(sERR)
					if oProcess.returncode:
						raise IOError, "Error processing with ffmpeg."

					subprocess.call("//online1/software/MP4Box/MP4Box.exe -hint %s" % (sCompressedFilename))

				else:
					print("Copying %s in CLIENT_APPROVAL." %(sFilename))
					shutil.copy(sFilename, sCompressedFilename)


				# Upload Version
				print("\n\nUploading version %s" % (sgVersion['code']))
				note=None
				if sgVersion['sg_description_for_client']:
					idx.uploadfile_into_path(sCompressedFilename , sInterdubsPath, note=sgVersion['sg_description_for_client'])
				else:
					idx.uploadfile_into_path(sCompressedFilename , sInterdubsPath)



	# Continue code

	# In case of error, just raise an exception. It will be trapped in the obqProtocolHandler and the window will stay open for a little while.
	print "done."
def pluginExecute(lArgs, sa):
	print("Sending Versions to CLIENT_APPROVAL")

	lSequencesToProcess = []
	for sId_Filter in sa.selected_ids_filter:
		sgVersion = sg.find_one("Version", [sId_Filter], ['sg_path_to_movie', 'sg_first_frame', 'sg_last_frame'])
		sFilename = sgVersion['sg_path_to_movie']
		try:
			bzApi = Api(bzhost='projectmanager')
		except:
			raise IOError, "An instance of the ProjectManager API cannot be obtained.\n" \
				  +"This is most probably because there is a problem with the projectmanager machine."\
				  +"(yes there's a machine with that name)"

		oRootProject = bzApi.getProjectFromPath(sFilename).currentRootProject()

		# find the approval directory
		dirnodes = oRootProject.getTemplate().searchNodes(attributes={"path":"CLIENT_APPROVAL"})
		if not dirnodes:
			dirnodes = oRootProject.getTemplate().searchNodes(attributes={"path":"APPROVAL"})
		if len(dirnodes) != 1:
			raise IOError, "Cannot find approval directory in root project"

		# create the approval dir if it doesn't exist
		apprNode = dirnodes[0]
		if not apprNode.exists():
			apprNode.create()
			if not apprNode.exists():
				raise IOError, "Cannot create CLIENT_APPROVAL directory"

		sTargetDir = "%s/%s" % (apprNode.getAbsPathOnline(),datetime.date.today().isoformat())
		if not os.path.exists(sTargetDir):
			os.mkdir(sTargetDir)

		sTargetFilename = "%s/%s" % (sTargetDir, os.path.basename(sFilename))

		if lArgs[0] == 'recompress' and (sFilename.endswith('.mov') or sFilename.endswith('.mp4')):
			# Get info from Project Manager
			dOutRes = oRootProject.getOutputResolution()
			sGoodFrameRate = str(dOutRes['frameRate'])
			iBitRate = 2000 # Kb/s
			sCommand = "//online1/software/ffmpeg/ffmpeg.exe -y -i %s -vcodec libx264 -b %dK -acodec libfaac -ab 128k -threads 0 %s"\
						% (sFilename, iBitRate, sTargetFilename)

			print("Recompressing movie in H.264 using ffmpeg.")

			oProcess = subprocess.Popen(sCommand,
										shell=True,
										stdin=subprocess.PIPE,
										stdout=subprocess.PIPE,
										stderr=subprocess.PIPE,
										universal_newlines=True)
			sOUT, sERR = oProcess.communicate()
			print(sOUT)
			print(sERR)
			if oProcess.returncode:
				raise IOError, "Error processing with ffmpeg."

			subprocess.call("//online1/software/MP4Box/MP4Box.exe -hint %s" % (sTargetFilename))

		else:
			print("Copying %s in CLIENT_APPROVAL." %(sFilename))
			shutil.copy(sFilename, sTargetFilename)

	print "done."