'JAnyImg2Map_04 javier@internarte.com (c)
'


'Use: Run script.
'Result: Converts all your standard image files to memory mapped files
'including image sequences--increasing render speed.
'Saves all new .map files to '..ProjectFolder/Pictures/map_x86' 
'keeping existing files intact.
'Creates a log file in the same folder.
'
'Works only for Windows based XSI
'please email me any bugs.
'
'//Script to convert image formats to .map
'~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
	projectpath = ActiveProject.Path 
	Set fso = CreateObject("Scripting.FileSystemObject")
MAIN
'~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
FUNCTION MAIN
dim SourceList,WshShell,newline,newBat

set SourceList = EnumElements("Sources.Image")
set WshShell = CreateObject("WScript.Shell")

IF TypeName(SourceList) <> "Nothing" THEN

	MAKEFOLDER projectpath, newpath, newBat'makes folder 'map_x86' within project 'pictures' folder

	Set fso = CreateObject("Scripting.FileSystemObject")
On Error Resume Next

scenename =ActiveProject.ActiveScene
logname = newpath &"\"& scenename &"_Img2Map.log"

	Set checkfile = fso.GetFile(logname)
	if Err.Number = 0 then 		'append to existing file!!
		Set newBat = fso.OpenTextFile(logname, 8, True)
		newBat.WriteLine "~"
		newBat.WriteLine "new map files created : append to log"
		newBat.WriteLine
	else
		Err.Clear	'create new logfile
		Set newBat = fso.OpenTextFile(logname, 2, True)
		newBat.WriteLine
		newBat.WriteLine ">>> Image FILES conVerted to >'.map' FORMAT >>>"
		newBat.WriteLine ".JanyImg2map javier@internarte.com (c)	--pls report bugs."
		newBat.WriteLine "/Local_Folder/Pictures/image. ...> /Project Folder/Pictures/map_x86/image.map"	
		newBat.WriteLine
		newBat.WriteLine "LOG"
		newBat.WriteLine "~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~"
		newBat.WriteLine	
	end if
	set SourceList = EnumElements("Sources.Image")		'list all sources and loop thru them

On Error Resume Next	

	FOR each elem in SourceList
		imgFullname = Getvalue (elem&".FileName")

		LASTTERM imgFullname,"\",imgName, 1, syllable_slash	
		LASTTERM imgFullname,imgName,oldpath, 2, syllable_path
			'logmessage "oldpath "&oldpath
		
		IF RIGHT(imgName,3) = "map" THEN 	'perform conversion only if format is not 'map'
	logmessage ">Source "& elem.Name&" is already of .map format"
		ELSE
			LASTTERM imgName,".",extension, 1, syllable_dot
				extlength = LEN(extension)
				imgNlength = LEN(imgName)
				mapNlength = imgNlength - extlength	'length of extension string
				baseName = LEFT(imgName, mapNlength) 'Name of file stripped of extension
				
'<< IMAGE SEQUENCE SOURCE: if there are brackets in the name, the Clip is a sequence
				
				LASTTERM imgName,"[",seqClip, 1, syllable_bracket
				
				IF syllable_bracket > 1 THEN

					LASTTERM imgName,"[",seqClipName, 2, syllable_bracket
					'logmessage seqClipName & " name images without frame number"
					seqClipN = Split(seqClip, "]", -1, 1)
					'logmessage seqClipN(0) &" text of image source within brackets: sequence"
										
					LASTTERM seqClipN(0),";",seqComp, 1, syllable_semiC
					IF syllable_semiC > 1 THEN 	'there are padding zeros in the sequence Clip
						digits = seqComp		'number of padding zeros
						FramesAll = SPLIT (seqClipN(0),";",-1,1)
						Frames = FramesAll(0)	'start and end of sequence
					ELSE
						digits = 0		'no padding zeros
						Frames = seqComp	
					END IF

					frameN = SPLIT (Frames,"..",-1,1)	'Frame numbers				
						'logmessage "there are "&frameN(1)&" frames in this clip"
					
					FOR i = frameN(0) to frameN(1)	'loop through frames			
						'logmessage i&": current frame evaluated"


						f = LEN(i)	
						n = digits - f	'number of zeros in image name
						IF n > 0 THEN 
							seqZeros = String( n, "0")   ' strings zeros together
						ELSE 
							seqZeros = ""
						END IF
						baseClipName = seqClipName&seqZeros&i&"."					
						imageFClipname = oldpath&baseClipName&extension
							
						Set checkfile = fso.GetFile(imageFClipname)
'IF fimename.i.ext exists then perform conversion								
						if Err.Number = 0 then
								'logmessage baseClipName
								logmessage "<from: "&imageFClipname
							mapFClipname = newpath&"\"&baseClipName&"map"
								logmessage ">to: "&mapFClipname	
						
							IMG2MAP mapFClipname, baseClipName, imageFClipname,newpath
							WshShell.run "imf_copy "&imageFClipname&" "&mapFClipname,0,true
						else
							Err.clear
						end if
 			
					NEXT
						mapFullname =newpath&"\"& baseName&"map" 'map put back in source syntax
						'logmessage mapFullname
	newBat.WriteLine elem.FileName&" --Sequence Image Source "

				ELSE	'Clip is not a sequence
'<< SINGLE IMAGE SOURCE
					IMG2MAP mapFullname, baseName, imgFullname,newpath
					WshShell.run "imf_copy "&imgFullname&" "&mapFullname,0,true
				
	newBat.WriteLine elem.Name	&" --Image Source"

				END IF
					SetValue elem&".FileName",mapFullname
if Err.Number = 0	then
	newBat.WriteLine "<from:		"&imgFullname		
	newBat.WriteLine ">to: 		"&mapFullname		
	newBat.WriteLine 
else
Err.Clear
	newBat.WriteLine "XXX Failed to convert 	"&imgFullname
	newBat.WriteLine 
end if		
		END IF	
	NEXT
	newbat.Close
END IF
END FUNCTION
'~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
'~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
FUNCTION IMG2MAP (mapFname, bName, imgFname,newp)
	mapName= bName&"map" 
	mapFname = newp&"\"&mapName
	firstterm = Split (imgFname,"\",-1,1)	'is path Relative or Absolute?
	IF firstterm(0) = "Pictures" THEN 	'path is Relative hence it must 
		imgFname = projectpath&"\Pictures\"&imgName 'be in project folder
	END IF
END FUNCTION
'~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
'~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
FUNCTION LASTTERM(MyString,separator,whichterm,termnumber,sylablecount)

	MyArray = Split(MyString, separator, -1, 1)
	sylablecount = 0
	for each term in Myarray
		sylablecount = sylablecount+1
	next
	whichterm =  Myarray(sylablecount-termnumber)
END FUNCTION
'~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
'~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
FUNCTION MAKEFOLDER(path, newpaz, newBat)
Dim fso, f, fc, nf
	Set fso = CreateObject("Scripting.FileSystemObject")
	paz = path&"\Pictures"
	folderName = "map_x86"
	newpaz = paz&"\"&folderName
on error resume next
	Set f = fso.GetFolder(newpaz)
	IF err.Number <> 0 then
		err.clear
		on error goto 0
			Set f = fso.GetFolder(paz)
 			Set fc = f.SubFolders
			Set nf = fc.Add(folderName)
		logmessage ">new folder map_x86 was created"
		
	ELSE
		on error goto 0
		logmessage "<folder map_x86 already exists"

	END IF
END FUNCTION
'~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
