[Tutor] Please comment my -first- script

Bernard Lebel python at bernardlebel.com
Wed Aug 18 01:39:43 CEST 2004


Hi everyone,

All right, I finally managed to write my first full-fledge Python script!!
It's not an entirely new idea, because it is actually a rewrite of a JScript
script I did several months. That said, the JScript version was 858 lines
longs, while the Python version is 192 lines. Granted, the JScript version
was not that optimized and the perfect aesthetic example, but it worked. I
must admit that Python has a lot of funcitonalities that JScript doesn't
have, and that are making life such eaiser.

Anyway, what the attaced file does is that it will analyze a directory tree
to find sequences of files, and analyse individual sequences to see if files
are missing or if files are incomplete (under 1k). Since I work in 3D
animation, this is very useful to summarize the results of a rendering for a
given shot. The user gives the sequence information, the first file and the
last file, and the script does the rest.
Sorry about the French strings, but the script is intended for the employees
at my company (wich are French).

I'd like to have some advice if some things could have been done better.
File was renamed to txt for safer internet transfer.


Thanks in advance for your time.

Bernard
-------------- next part --------------
"""
Check_Sequence.py

Par Bernard Lebel
Directeur technique, rendering
Action Synthèse, Marseille (France)
Août 2004

Check_Sequence.py est la version pour l'interpréteur Python.
Il doit être exécuté avec Python IDLE, PythonWin,
ou tout autre interpréteur natif Python.
La version Check_Sequence.pys est la version pour XSI,
et doit être exécutée dans le Script Editor de XSI.



Description:
Check_sequence détecter les frames manquants dans toutes
les passes rendues pour le plan spécifié,
et détecte aussi les drop-frames (frames pesant moins de 1k.

Pour l'instant, tous les dossiers du plan spécifé sont évalués.
Une version future permettra d'être plus sélectif.



Utilisation:
Lancer l'interpréteur Python (en l'occurence, PythonWin).
Faire Ctrl+I (File > Import)
Sélectionner le script, cliquer OK.
Entrer le numéro de séquence avec les zéros qui précèdent,
mais sans le S.
Entrer le numéro de plan avec les zéros qui précèdent,
mais sans le P.
Entrer le premier frame attendu, sans les zéros qui précèdent.
Entrer le dernier frame attendu, sans les zéros qui précèdent.
Lire le rapport.
"""



# -----------------------------------------------------
# Import block

import os
import string

# -----------------------------------------------------
# Instantiation block

s = string

# -----------------------------------------------------

print '                [[ CHECK SEQUENCE ]]                '

print ''
print ">>>>> IMPORTANT:"
print "1. Assurez-vous d'avoir des dossiers propres avant d'executer le script."
print "2. N'entrez pas le S et le P dans les numeros de sequence et de plan."
print ''




sSequence = raw_input( 'Numero de sequence (sans le S): ' )
sPlan = raw_input( 'Numero de plan (sans le P): ' )
iStart = int( raw_input( 'Premier frame: ' ) )
iEnd = int( raw_input( 'Dernier frame: ' ) )
iCount = iEnd - iStart + 1




# Define root search path
sRoot = 'C:\\FRAME2\\SEQUENCES\\' + 'S' + sSequence + '\\' + 'P' + sPlan
#sRoot = '\\\\Sata-NAS1500\\FRAME2\\SEQUENCES\\' + 'S' + sSequence + '\\' + 'P' + sPlan

if os.path.exists( sRoot ) == False:
	print 'Pas de plan pour le dossier specifie.'
else:
	print ''
	print '>>>>> INFORMATIONS SUR LE PLAN:'
	print '1. Dossier du plan: ' + sRoot
	print '2. Premier frame: ' + str( iStart )
	print '3. Dernier frame: ' + str( iEnd )
	print '5. Nombre de frames attendus: ' + str( iCount )
	print ''



	
	print '>>>>> DEBUT DE VERIFICATION DU PLAN'
	print ''
	
	# Iterate through root folder to collected folders
	for sDirPath, oDirNames, oFiles in os.walk( sRoot, True, None ):
	
		# Check if directory contains files
		if len( oFiles ) > 0:
			print '_____ Verification de >>>>>>>>>>>> ' + sDirPath
			
			
			# Create empty list to store sequence names
			aNames = []
		
			# Get name of first file
			oFirstFile = oFiles[0]
		
			# Get first part of the name
			aFirstName = s.split( oFirstFile, '.' )
			
			# Add first name to list
			aNames.append( aFirstName[0] )

			
			# Get file extention
			sExt = ''
			if len( aFirstName ) == 1:
				sExt = ''
			elif len( aFirstName ) == 2:
				sExt = aFirstName[1]
			else:
				sExt = aFirstName[2]

			# DEBUG
			#print '>>>>> First file in aNames: ' + str(aNames[0])
			
			# Iterate through files of current directory
			for oFile in oFiles:
			
				# Split file name into elements
				aName = s.split( oFile, '.' )
				
				# Get first element of file name (sequence name)
				sName = str(aName[0])

				# Check if sequence name is in list of sequence names
				if sName in aNames:
					pass
				else:
					# Sequence name is not in list, add it
					aNames.append( sName )
			
			# Iterate through list of sequence names in current directory
			for sSeqName in aNames:
				# DEBUG
				#print 'sSeqName: ' + sSeqName )
				
				# Loop over virtual full sequence to test if file exists
				for i in range( iCount ):
					iFrame = i + iStart
			
					# Define default file name
					sPad = str( iFrame )
					sFile = sSeqName + '.' + sPad + '.' + sExt
					#sPath = sDirPath + '\\' + sFile
					
					# Loop 5 times with each time an additional leading 0 to the padding to check if the file exists
					for sZero in range(5):
						# Compose file path
						sPath =  sDirPath + '\\' + sSeqName + '.' + sPad + '.' + sExt
						
						#print 'Trying new pad: ' + sPad

						# Check if file path is valid
						if os.path.exists( sPath ) == False:
							# If file path not valid, add a zero to sPad
							sPad = s.zfill( sPad, len(sPad) + 1 )
						else:
							# If file path valid, check file size and break the loop for this file
							
							# Collect file statistics
							oStat = os.stat( sPath )
							
							# Check if file size is below 1024 bytes
							if int( oStat[6] ) < 1024:
								print '      Missed: ' + sPath
							else:
								pass
								
							break

					# The list of possibles paddings came to exhaustion, and a valid file could not be found.
					else:
						print '          Absent: ' + sFile
								
			print ''
				
	# Script completed!
	print ''
	print '>>>>> FIN DE VERIFICATION DU PLAN'
	


More information about the Tutor mailing list