PyAPI.
[blender.git] / release / scripts / object_find.py
1 #!BPY
2 """
3 Name: 'Find by Data Use'
4 Blender: 242
5 Group: 'Object'
6 Tooltip: 'Find an object by the data it uses'
7 """
8 __author__= "Campbell Barton"
9 __url__= ["blender.org", "blenderartists.org"]
10 __version__= "1.0"
11
12 __bpydoc__= """
13 """
14
15 # --------------------------------------------------------------------------
16 # Find by Data Use v0.1 by Campbell Barton (AKA Ideasman42)
17 # --------------------------------------------------------------------------
18 # ***** BEGIN GPL LICENSE BLOCK *****
19 #
20 # This program is free software; you can redistribute it and/or
21 # modify it under the terms of the GNU General Public License
22 # as published by the Free Software Foundation; either version 2
23 # of the License, or (at your option) any later version.
24 #
25 # This program is distributed in the hope that it will be useful,
26 # but WITHOUT ANY WARRANTY; without even the implied warranty of
27 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
28 # GNU General Public License for more details.
29 #
30 # You should have received a copy of the GNU General Public License
31 # along with this program; if not, write to the Free Software Foundation,
32 # Inc., 59 Temple Place - Suite 330, Boston, MA  02111-1307, USA.
33 #
34 # ***** END GPL LICENCE BLOCK *****
35 # --------------------------------------------------------------------------
36
37 from Blender import Image, sys, Draw, Window, Scene, Group
38 import bpy
39 import BPyMessages
40
41
42 def get_object_images(ob):
43         # Could optimize this
44         if ob.type == 'Mesh':
45                 unique_images = {}
46                 me = ob.getData(mesh=1)
47                 orig_uvlayer = me.activeUVLayer 
48                 
49                 for uvlayer in me.getUVLayerNames():
50                         me.activeUVLayer = uvlayer
51                         for f in me.faces:
52                                 i = f.image
53                                 if i: unique_images[i.name] = i
54                 
55                 me.activeUVLayer = orig_uvlayer
56                 
57                 return unique_images.values()
58         
59         # Todo, support other object types, materials
60         return []
61         
62         
63
64 def main():
65         
66         NAME_DATA= Draw.Create('')
67         NAME_INGROUP= Draw.Create('')
68         NAME_DUPGROUP= Draw.Create('')
69         NAME_IMAGE= Draw.Create('')
70         NAME_MATERIAL= Draw.Create('')
71         NAME_TEXTURE= Draw.Create('')
72         
73         
74         PREF_CASESENS= Draw.Create(False)
75         PREF_PART_MATCH= Draw.Create(True)
76         
77         
78         # Get USER Options
79         pup_block= [\
80         ('ObData:', NAME_DATA, 0, 32, 'Match with the objects data name'),\
81         ('InGroup:', NAME_INGROUP, 0, 32, 'Match with the group name to find one of its objects'),\
82         ('DupGroup:', NAME_DUPGROUP, 0, 32, 'Match with the group name to find an object that instances this group'),\
83         ('Image:', NAME_IMAGE, 0, 32, 'Match with the image name to find an object that uses this image'),\
84         ('Material:', NAME_MATERIAL, 0, 32, 'Match with the material name to find an object that uses this material'),\
85         ('Texture:', NAME_TEXTURE, 0, 32, 'Match with the texture name to find an object that uses this texture'),\
86         ('Case Sensitive', PREF_CASESENS, 'Do a case sensitive comparison?'),\
87         ('Partial Match', PREF_PART_MATCH, 'Match when only a part of the text is in the data name'),\
88         ]
89         
90         if not Draw.PupBlock('Find object using dataname...', pup_block):
91                 return
92         
93         NAME_DATA = NAME_DATA.val
94         NAME_INGROUP = NAME_INGROUP.val
95         NAME_DUPGROUP = NAME_DUPGROUP.val
96         NAME_IMAGE = NAME_IMAGE.val
97         NAME_MATERIAL = NAME_MATERIAL.val
98         NAME_TEXTURE = NAME_TEXTURE.val
99         
100         PREF_CASESENS = PREF_CASESENS.val
101         PREF_PART_MATCH = PREF_PART_MATCH.val
102         
103         if not PREF_CASESENS:
104                 NAME_DATA = NAME_DATA.lower()
105                 NAME_INGROUP = NAME_INGROUP.lower()
106                 NAME_DUPGROUP = NAME_DUPGROUP.lower()
107                 NAME_IMAGE = NAME_IMAGE.lower()
108                 NAME_MATERIAL = NAME_MATERIAL.lower()
109                 NAME_TEXTURE = NAME_TEXTURE.lower()
110         
111         def activate(ob, scn):
112                 bpy.data.scenes.active = scn
113                 scn.objects.selected = []
114                 scn.Layers = ob.Layers
115                 ob.sel = 1
116         
117         def name_cmp(name_search, name_found):
118                 if name_found == None: return False
119                 if not PREF_CASESENS: name_found = name_found.lower()
120                 if PREF_PART_MATCH:
121                         if name_search in name_found:
122                                 # print name_found, name_search
123                                 return True
124                 else:
125                         if name_found == name_search:
126                                 # print name_found, name_search
127                                 return True
128                 
129                 return False
130         
131         
132         if NAME_INGROUP:
133                 # Best we speed this up.
134                 bpy.data.objects.tag = False
135                 
136                 ok = False
137                 for group in bpy.data.groups:
138                         if name_cmp(NAME_INGROUP, group.name):
139                                 for ob in group.objects:
140                                         ob.tag = True
141                                         ok = True
142                 if not ok:
143                         Draw.PupMenu('No Objects Found')
144                         return
145         
146         for scn in bpy.data.scenes:
147                 for ob in scn.objects:
148                         if NAME_DATA:
149                                 if name_cmp(NAME_DATA, ob.getData(1)):
150                                         activate(ob, scn)
151                                         return
152                         if NAME_INGROUP:
153                                 # Crap and slow but not much we can do about that
154                                 '''
155                                 for group in bpy.data.groups:
156                                         if name_cmp(NAME_INGROUP, group.name):
157                                                 for ob_group in group.objects:
158                                                         if ob == ob_group:
159                                                                 activate(ob, scn)
160                                                                 return
161                                 '''
162                                 # Use speedup, this is in a group whos name matches.
163                                 if ob.tag:
164                                         activate(ob, scn)
165                                         return
166                         
167                         if NAME_DUPGROUP:
168                                 if ob.DupGroup and name_cmp(NAME_DUPGROUP, ob.DupGroup.name):
169                                         activate(ob, scn)
170                                         return
171                         
172                         if NAME_IMAGE:
173                                 for img in get_object_images(ob):
174                                         if name_cmp(NAME_IMAGE, img.name) or name_cmp(NAME_IMAGE, img.filename.split('\\')[-1].split('/')[-1]):
175                                                 activate(ob, scn)
176                                                 return
177                         if NAME_MATERIAL or NAME_TEXTURE:
178                                 try:    materials = ob.getData(mesh=1).materials
179                                 except: materials = []
180                                 
181                                 # Add object materials
182                                 materials.extend(ob.getMaterials())
183                                 
184                                 for mat in materials:
185                                         if mat:
186                                                 if NAME_MATERIAL:
187                                                         if name_cmp(NAME_MATERIAL, mat.name):
188                                                                 activate(ob, scn)
189                                                                 return
190                                                 if NAME_TEXTURE:
191                                                         for tex in mat.getTextures():
192                                                                 if tex:
193                                                                         if name_cmp(NAME_MATERIAL, tex.name):
194                                                                                 activate(ob, scn)
195                                                                                 return
196         
197         
198         Draw.PupMenu('No Objects Found')
199
200 if __name__ == '__main__':
201         main()