use f.area where possible over python function and use len(mface) over len(mface.v)
[blender.git] / release / scripts / image_find_paths.py
1 #!BPY
2
3 """
4 Name: 'Find Image Target Paths'
5 Blender: 241
6 Group: 'UV'
7 Tooltip: 'Finds all image paths from this blend and references the new paths'
8 """
9
10 __author__ = "Campbell Barton AKA Ideasman"
11 __url__ = ["http://members.iinet.net.au/~cpbarton/ideasman/", "blender", "elysiun"]
12
13 __bpydoc__ = """\
14 """
15 from Blender import *
16
17 try:
18         import os
19 except:
20         Draw.PupMenu('You need a full python install to use this script')
21         os= None
22
23 #==============================================#
24 # Strips the slashes from the back of a string #
25 #==============================================#
26 def stripPath(path):
27         return path.split('/')[-1].split('\\')[-1]
28
29 # finds the file starting at the root.
30 def findImage(findRoot, imagePath):
31         newImageFile = None
32         
33         imageFile = imagePath.split('/')[-1].split('\\')[-1]
34         
35         # ROOT, DIRS, FILES
36         pathWalk = os.walk(findRoot)
37         pathList = [True]
38         
39         matchList = [] # Store a list of (match, size), choose the biggest.
40         while True:
41                 try:
42                         pathList  = pathWalk.next()
43                 except:
44                         break
45                 
46                 for file in pathList[2]:
47                         # FOUND A MATCH
48                         if file.lower() == imageFile.lower():
49                                 name = pathList[0] + sys.sep + file
50                                 try:
51                                         size = os.path.getsize(name)
52                                 except:
53                                         size = 0
54                                         
55                                 if size:
56                                         print '   found:', name 
57                                         matchList.append( (name, size) )
58                 
59         if matchList == []:
60                 print 'no match for:', imageFile
61                 return None
62         else:
63                 # Sort by file size
64                 matchList.sort(lambda A, B: cmp(B[1], A[1]) )
65                 
66                 print 'using:', matchList[0][0]
67                 # First item is the largest
68                 return matchList[0][0] # 0 - first, 0 - pathname
69                 
70
71 # Makes the pathe relative to the blend file path.
72 def makeRelative(path):
73         blendBasePath = sys.expandpath('//')
74         if path.startswith(blendBasePath):
75                 path = path.replace(blendBasePath, '//')
76                 path = path.replace('//\\', '//')
77         return path
78
79 def find_images(findRoot):
80         print findRoot
81         
82         # findRoot = Draw.PupStrInput ('find in: ', '', 100)
83         
84         if findRoot == '':
85                 Draw.PupMenu('No Directory Selected')
86                 return
87         
88         # Account for //
89         findRoot = sys.expandpath(findRoot)
90         
91         # Strip filename
92         while findRoot[-1] != '/' and findRoot[-1] != '\\':
93                 findRoot = findRoot[:-1]
94         
95         
96         if not findRoot.endswith(sys.sep):
97                 findRoot += sys.sep
98         
99         
100         if findRoot != '/' and not sys.exists(findRoot[:-1]):
101                 Draw.PupMenu('Directory Dosent Exist')
102         
103         
104         Window.WaitCursor(1)
105         # ============ DIR DONE\
106         images = Image.Get()
107         len_images = float(len(images))
108         for idx, i in enumerate(images):
109
110                 progress = idx / len_images
111                 Window.DrawProgressBar(progress, 'searching for images')
112                 
113                 # If files not there?
114                 if not sys.exists(sys.expandpath(i.filename )): 
115                         newImageFile = findImage(findRoot, i.filename)
116                         if newImageFile != None:
117                                 newImageFile = makeRelative(newImageFile)
118                                 print 'newpath:', newImageFile
119                                 i.filename = newImageFile
120                                 i.reload()
121         
122         Window.RedrawAll()
123         Window.DrawProgressBar(1.0, '')
124         Window.WaitCursor(0)
125
126 if __name__ == '__main__' and os != None:
127         Window.FileSelector(find_images, 'SEARCH ROOT DIR', sys.expandpath('//'))
128
129