WaitForProcess() fails with mayapy

Hey guys,
I’m trying to rework mayabatch plugin to work with mayapy but WaitForProcess() function doesnt work anymore and command CheckForMonitoredManagedProcessPopups() always return empty string. What do you think could cause this kind of problems. Maybe I miss something in terms of #Python.NET implementation.
Thanks

#Python.NET

import re

from System import *
from System.Diagnostics import *
from System.IO import *
from System.Text import *
from System.Text.RegularExpressions import *

from Deadline.Plugins import *
from Deadline.Scripting import *

######################################################################
## This is the function that Deadline calls to get an instance of the
## main DeadlinePlugin class.
######################################################################
def GetDeadlinePlugin():
	return ToonboxMayaSim()

######################################################################
## This is the main DeadlinePlugin class for the Nuke plugin.
######################################################################
class ToonboxMayaSim (DeadlinePlugin):
	"""Note: The BatchMode option here is experimental and isn't exposed in any of the
	components that are shipped out of the box with Deadline. BatchMode starts up Nuke
	in a mode that accepts python code through stdin, and keeps Nuke loaded in memory
	between tasks. Once day, we may use this mode as the default, but for now we find
	regular command line rendering to be far more reliable and flexible with Nuke."""
	
	BatchMode = True
	Process = None
	ProcessName = "MayaPy"
	## Utility functions
	def WritePython( self, statement ):
		FlushMonitoredManagedProcessStdout( self.ProcessName )
		WriteStdinToMonitoredManagedProcess( self.ProcessName, statement )
		FlushMonitoredManagedProcessStdout( self.ProcessName )
		
		self.WaitForProcess()
	
	def WaitForProcess( self ):
		FlushMonitoredManagedProcessStdout( self.ProcessName )
		WriteStdinToMonitoredManagedProcess( self.ProcessName, self.Process.ReadyForInputCommand() )
		

		LogInfo( self.Process.ReadyForInputCommand() )
		blockingDialogMessage = CheckForMonitoredManagedProcessPopups( self.ProcessName )
		LogInfo( "F...K")
		
		LogInfo( blockingDialogMessage)
	
	

			
		while not self.Process.IsReadyForInput():
			VerifyMonitoredManagedProcess( self.ProcessName )
			FlushMonitoredManagedProcessStdout( self.ProcessName )
			blockingDialogMessage = CheckForMonitoredManagedProcessPopups( self.ProcessName )
			LogInfo( blockingDialogMessage )
			
			if( blockingDialogMessage != "" ):
				FailRender( blockingDialogMessage )
			
			if IsCanceled():
				FailRender( "Received cancel task command" )
			
			Sleep( 1 )
		
		self.Process.ResetReadyForInput()	

		
	def __init__( self ):

		# Set up the event handlers because this is a Python.NET plugin
		self.InitializeProcessEvent += self.InitializeProcess
		self.StartJobEvent += self.StartJob
		self.RenderTasksEvent += self.RenderTasks
		self.EndJobEvent += self.EndJob
		

		
	## Called by Deadline to initialize the process.
	def InitializeProcess( self ):
		# Set the plugin specific settings.
		self.SingleFramesOnly = False
		self.PluginType = PluginType.Advanced
	
	def StartJob( self ):
		self.Process = MayaPyProcess( self.BatchMode )
		StartMonitoredManagedProcess( self.ProcessName, self.Process )
	
	def RenderTasks( self ):
		
		LogInfo( "Rendering write node ")
		self.WritePython( "print \"SHIT IM HERE\"\n" )

	
	def EndJob( self ):
		LogInfo( "Ending Maya Job" )
		FlushMonitoredManagedProcessStdoutNoHandling( self.ProcessName )
		LogInfo( "Waiting for Maya to shut down" )
		
		ShutdownMonitoredManagedProcess( self.ProcessName )
		LogInfo( "Maya has shut down" )		
		
		
		
		LogInfo( "Ending MayaPy" )
		FlushMonitoredManagedProcessStdoutNoHandling( self.ProcessName )
		WriteStdinToMonitoredManagedProcess( self.ProcessName, "quit()" )
		FlushMonitoredManagedProcessStdoutNoHandling( self.ProcessName )
		WaitForMonitoredManagedProcessToExit( self.ProcessName, 200 )
		ShutdownMonitoredManagedProcess( self.ProcessName )
	
class MayaPyProcess (ManagedProcess):
	#FinishedFrameCount = 0
	TempSceneFilename = ""
	ReadyForInput = False
	
	
	def __init__( self,  batchMode ):
		self.BatchMode = batchMode
		self.InitializeProcessEvent += self.InitializeProcess	
		self.RenderExecutableEvent += self.RenderExecutable
		self.RenderArgumentEvent += self.RenderArgument		
		self.ReadyForInput = False
	
	def InitializeProcess( self ):
		# Set the process specific settings.
		self.ProcessPriority = ProcessPriorityClass.BelowNormal
		self.UseProcessTree = True
		self.PopupHandling = True
		self.StdoutHandling = True
		
		## Set the stdout handlers.
		self.AddStdoutHandlerEvent( r"555" ).HandleEvent += self.HandleReadyForInput
		self.AddStdoutHandlerEvent( ".*File \"<stdin>\".*" ).HandleEvent += self.HandleError
		self.AddStdoutHandlerEvent( ".*ERROR:.*" ).HandleEvent += self.HandleError
		self.AddStdoutHandlerEvent( ".*Error:.*" ).HandleEvent += self.HandleError
			

	
	def PreRenderTasks( self ):
		sceneFilename = CheckPathMapping( GetPluginInfoEntryWithDefault( "SceneFile", GetDataFilename() ) )

		self.FinishedFrameCount = 0
		SetStatusMessage( "Rendering frame " + str(GetStartFrame()) )
	
	def PostRenderTasks( self ):
		if GetBooleanConfigEntryWithDefault( "EnablePathMapping", True ):
			File.Delete( self.TempSceneFilename )
	
	## Called by Deadline to get the render executable.
	def RenderExecutable( self ):
		#build = GetPluginInfoEntryWithDefault( "Build", "None" ).lower()
		
		return "/usr/autodesk/maya2012-x64/bin/mayapy"
	
	## Called by Deadline to get the render arguments.
	def RenderArgument( self ):
		LogInfo( "No arguments..." )
		return ""
	
	def HandleError( self ):
		FailRender( self.GetRegexMatch( 0 ) )
	
	def HandleProgress( self ):
		self.FinishedFrameCount += 1
		
		startFrame = GetStartFrame()
		endFrame = GetEndFrame()
		totalFrames = endFrame - startFrame + 1
		
		if totalFrames != 0:
			SetProgress( ( float(self.FinishedFrameCount) / float(totalFrames) ) * 100.0 )
		
		currFrame = startFrame + self.FinishedFrameCount
		if currFrame <= endFrame:
			SetStatusMessage( "Rendering frame " + str(currFrame) )
		else:
			SetStatusMessage( "Finished rendering " + str(self.FinishedFrameCount) + " frames" )
		
		currFrame = int( self.GetRegexMatch( 1 ) )
		totalFrames = int( self.GetRegexMatch( 2 ) )
		if totalFrames != 0:
			SetProgress( ( float(currFrame) / float(totalFrames) ) * 100.0 )
		SetStatusMessage( self.GetRegexMatch( 0 ) )
	
	def ResetFinishedFrameCount( self ):
		self.FinishedFrameCount = 0
	
	def HandleReadyForInput( self ):
		self.ReadyForInput = True
		return self.ReadyForInput
	
	
	def IsReadyForInput( self ):
		return self.ReadyForInput
	
	def ResetReadyForInput( self ):
		self.ReadyForInput = False
	
	def ReadyForInputCommand( self ):
		return 'eval("555")\n'
		#return "print( \"READY\"\\n \")"

Is the “555” text being printed to Maya’s stdout? Maybe try adding a LogInfo line to HandleReadyForInput that prints something out when “555” is recognized in stdout.

I tried, it looks like WriteStdinToMonitoredManagedProcess() doesnt do anything. I tried to catch stdout from mayapy verbosity output and it works fine, so it something that not allow me to inject data using WriteStdinToMonitoredManagedProcess() command. Maybe there is something undocumented I should know when working through #Python.NET?
Thanks

I just quickly converted our existing MayaBatch plugin to a Python.NET version, and confirmed that WriteStdinToMonitoredManagedProcess() does work as expected. Of course, this is writing mel to stdin instead of python, but it shouldn’t matter.

Do you see “555” being printed out to Maya’s stdout at all? Maybe it’s an issue with the code you’re passing mayapy to evaluate?

You guys probably want to reflect this in your help, because I almost break my head around this problem. Mayapy executable should be executed with “-i” argument. THIS IS ONLY WAY TO MAKE IT WORK WITH DEADLINE and communicate using ManageProcces. Will get drunk to death today… :smiley:
MayaPY help:
-i : inspect interactively after running script; forces a prompt even
if stdin does not appear to be a terminal; also PYTHONINSPECT=x

Thats end of the problem.
THanks for help rrussell