-
Notifications
You must be signed in to change notification settings - Fork 0
/
definitions.py
351 lines (283 loc) · 10.9 KB
/
definitions.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
import urllib2
import urllib
import webbrowser as wb
import simplejson
import socket
import os
import pymel.core as py
from PIL import Image
def google_image_search(**kwargs):
"""
Queries google using JSON API using the parsed search results.
Kwargs -
search_term = no default
page = 1*
result_count = 7*
Returns resulting dictionary from google image search
Returns 1 if URL error
Returns 2 if JSON error
"""
search_term = kwargs.setdefault('search_term')
page = kwargs.setdefault('page', 1)
number_of_results = kwargs.setdefault('result_count', 7)
ip_address = socket.gethostbyname(socket.gethostname())
url = ('https://ajax.googleapis.com/ajax/services/search/images?' +
'v=1.0&q=' + search_term +
'&userip=' + ip_address +
'&start=' + str(page) +
'&rsz=' + str(number_of_results))
try:
request = urllib2.Request(url, None, {'Referer': 'http://www.evancox.net/'})
response = urllib2.urlopen(request)
except:
py.warning("Could not complete request: Please check your internet connection.")
#py.deleteUI(cls.search_frame)
return 1
try:
results = simplejson.load(response)
return results
except:
error = "Error loading json results into dictionary"
py.warning(error)
#py.deleteUI(cls.search_frame)
return 2
def download_image_results(results, term):
"""
Downloads image results from the google image search results.
Saves in a temporary reference image directory located in the user's home directory.
(If directory doesn't already exists, it is created)
Dictionaries are created with attributes of the images, then all put in a list.
Returns a list of dictionaries with the following keys:
width (str)
height (str)
url (str)
thumb_width (str)
thumb_height (str)
path (str)
thumb_path (str)
im_id (unique image ID provided by Google) (str)
Downloaded (bool)
search_term (str)
"""
num = 0
return_list = []
search_directory = create_directories(term=term)
for result in results['responseData']['results']:
result_count = len(results['responseData']['results'])
thumb_path = os.path.join(search_directory, result['imageId']+'_thumbnail.png')
if not os.path.exists(thumb_path):
try:
urllib.urlretrieve(result['unescapedUrl'], search_directory + result['imageId'] + '.png')
except IOError:
py.warning('Cannot download some images. Check your internet connection!')
attr_dict = {'width': result['width'],
'height': result['height'],
'url': result['unescapedUrl'],
'thumb_width': result['tbWidth'],
'thumb_height': result['tbHeight'],
'path': os.path.join(search_directory, result['imageId']+'.png'),
'thumb_path': thumb_path,
'im_id': result['imageId'],
'downloaded': True,
'search_term': term}
make_thumbnail(attr_dict['path'], thumbPath=thumb_path)
num += 1
print "Processed & Downloaded %i out of %i for %s" % (num, result_count, term)
else:
path = os.path.join(search_directory, result['imageId']+'.png')
im = Image.open(path)
width, height = im.size
attr_dict = downloaded_image_dictionary(path=path,
thumb_path=thumb_path,
imageId=result['imageId'],
term=term,
width=width,
height=height)
return_list.append(attr_dict)
return return_list
def make_thumbnail(path, **kwargs):
"""
Uses PIL to create thumbnails.
Kwargs -
thumbSize = Tuple, (200,200)*
thumbPath = str
returnWidthHeight = False*
Returns widthHeight tuple of new thumbnail if returnWidthHeight is True
Returns 1 if exception raised
Else Returns 0
"""
thumb_size = kwargs.setdefault('thumbSize', (200, 200))
thumb_path = kwargs.setdefault('thumbPath')
width_height_bool = kwargs.setdefault('returnWidthHeight', False)
try:
im = Image.open(path)
im.thumbnail(thumb_size, Image.ANTIALIAS)
im.save(thumb_path, "PNG")
if width_height_bool is True:
return im.size
except:
os.remove(path)
return 1
return 0
def downloaded_image_dictionary(**kwargs):
"""
Dictionary creation for images that were previously downloaded/custom images.
Returns created dictionary
"""
path = kwargs.setdefault('path')
thumb_path = kwargs.setdefault('thumb_path')
im_id = kwargs.setdefault('imageId')
search_term = kwargs.setdefault('term')
width = kwargs.setdefault('width')
height = kwargs.setdefault('height')
dictionary = {'path': path,
'thumb_path': thumb_path,
'im_id': im_id,
'downloaded': False,
'search_term': search_term,
'width': width,
'height': height}
return dictionary
def reference_plane(flip, **kwargs):
"""
Creates reference planes based on kwargs and boolean "flip"
If height of image is greater than or equal to 700, scaleFactor of 0.25 is applied.
Kwargs -
axis - (1, 2, 3) for (X, Y, Z)
dict - image dictionary
term - search term
width - image width
height - image height
scaleFactor - 1*
Returns reference plane
"""
fallback_dict = {'path': "", "width": 0, "height": 0}
radio = kwargs.setdefault('axis')
image_dict = kwargs.setdefault('dict', fallback_dict)
name = kwargs.setdefault('term')+'_reference'
path = kwargs.setdefault('path', image_dict['path'])
material = create_material(path)
scale_factor = kwargs.setdefault('scaleFactor', 1)
dict_width = float(image_dict['width'])
dict_height = float(image_dict['height'])
width = kwargs.setdefault('width', dict_width)
height = kwargs.setdefault('height', dict_height)
print height
if height >= 700:
scale_factor = 0.25
width *= scale_factor
height *= scale_factor
if radio is 3:
plane = py.polyPlane(n=name, ax=(0, 0, 1), w=width, h=height, sx=1, sy=1)[0]
py.move(0, (0.5*height), 0, plane)
py.xform(plane, piv=(0, -0.5*height, 0))
elif radio is 2:
plane = py.polyPlane(n=name, ax=(0, 1, 0), w=width, h=height, sx=1, sy=1)[0]
else:
plane = py.polyPlane(n=name, ax=(1, 0, 0), w=width, h=height, sx=1, sy=1)[0]
py.move(0, (0.5*height), 0, plane)
py.xform(plane, piv=(0, -0.5*height, 0))
if flip is True:
py.setAttr(plane+'.scaleZ', -1)
apply_material(plane, material, freeze=True)
reference_layer(plane)
return plane
def apply_material(objects, material, **kwargs):
"""
Apply a material to an object
Returns 0
"""
freeze_bool = kwargs.setdefault('freeze', False)
py.select(objects)
py.hyperShade(assign=material)
if freeze_bool is True:
py.delete(ch=True)
py.makeIdentity(apply=True, t=1, r=1, s=1, n=0)
py.select(cl=True)
return 0
def create_material(filepath, **kwargs):
"""
Creates material to be applied later.
Kwargs -
name = 'refMaterial'
material_type = 'lambert'
Returns new material
"""
name = kwargs.setdefault('name', 'refMaterial')
material_type = kwargs.setdefault('material_type', 'lambert')
material = py.shadingNode(material_type, asShader=True, n=name)
alpha_value = kwargs.setdefault('alpha', 0.5)
shader_group = py.sets(material, renderable=True, noSurfaceShader=True, empty=True)
py.connectAttr((material + ".outColor"), (shader_group + ".surfaceShader"), f=True)
py.setAttr((material+".transparency"), alpha_value, alpha_value, alpha_value, type="double3")
img = py.shadingNode('file', asTexture=True)
py.setAttr(img+'.fileTextureName', filepath, type='string')
py.connectAttr(img+'.outColor', material+'.color', force=True)
return material
def image_plane(path, **kwargs):
"""
Creates an image plane from the path provided. Width and height optional but highly recommended.
Returns new image plane
"""
width_height = kwargs.setdefault('widthHeight', (100, 100))
image_plane_xform = py.imagePlane(width=width_height[0], height=width_height[1])[0]
py.setAttr("%s.imageName" % image_plane_xform, path, type="string")
reference_layer(image_plane_xform)
return image_plane_xform
def apply_as_texture(**kwargs):
"""
Creates & applies material with image as texture to an object
Returns 0
"""
fallback_dict = {"search_term": " ", "path": " "}
image_dict = kwargs.setdefault('dict', fallback_dict)
selected = py.ls(sl=True)
name = kwargs.setdefault('name', image_dict['search_term'])
path = kwargs.setdefault('path', image_dict['path'])
alpha_value = kwargs.setdefault('alpha', 0)
material_name = name+'_material'
material = create_material(path, name=material_name, alpha=alpha_value)
apply_material(selected, material)
return 0
def reference_layer(object, **kwargs):
"""
Creates reference layer "Ref_Layer" if not already made or no new name is provided
Adds object to new layer
Returns new reference layer
"""
layer = kwargs.setdefault('name', 'Ref_Layer')
if py.objExists(layer) is False:
py.createDisplayLayer(empty=True, n=layer)
py.setAttr(layer+".dt", 2)
py.editDisplayLayerMembers(layer, object)
return layer
def create_directories(**kwargs):
"""
Checks and creates directories needed for image and thumbnail storage.
Returns 0
"""
search_bool = kwargs.setdefault('search', True)
temp_directory = os.path.expanduser('~/') + 'temp_reference_images/'
custom_directory = os.path.join(temp_directory, "custom_images"+'/')
if not os.path.exists(temp_directory):
os.makedirs(temp_directory)
if not os.path.exists(custom_directory):
os.makedirs(custom_directory)
if search_bool is True:
term = kwargs.setdefault('term')
search_directory = os.path.join(temp_directory, term+'/')
if not os.path.exists(search_directory):
os.makedirs(search_directory)
return search_directory
if search_bool is False:
image_name = kwargs.setdefault('image_name')
thumb_path = os.path.join(custom_directory, image_name)
return thumb_path
return 0
def open_url(url):
"""
Uses urllib2 to open URL's for extenal libs
Returns 0
"""
wb.open(url)
return 0