forked from julienduroure/gltf2-blender-importer
-
Notifications
You must be signed in to change notification settings - Fork 5
/
Copy path__init__.py
1279 lines (1004 loc) · 41.3 KB
/
__init__.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
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
"""
* ***** BEGIN GPL LICENSE BLOCK *****
*
* This program is free software; you can redistribute it and/or
* modify it under the terms of the GNU General Public License
* as published by the Free Software Foundation; either version 2
* of the License, or (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program; if not, write to the Free Software Foundation,
* Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
*
*
* ***** END GPL LICENSE BLOCK *****
"""
import os
import urllib
import requests
import threading
import time
from collections import OrderedDict
import bpy
import bpy.utils.previews
from bpy.props import (StringProperty,
EnumProperty,
BoolProperty,
IntProperty,
PointerProperty)
from .gltf import *
from .blender import *
from .sketchfab import Config, Utils, Cache
bl_info = {
'name': 'Sketchfab Plugin',
'description': 'Browse and download free Sketchfab downloadable models',
'author': 'Sketchfab',
'license': 'GPL',
'deps': '',
'version': (0, 0, 90),
'blender': (2, 7, 9),
'location': 'View3D > Tools > Sketchfab',
'warning': '',
'wiki_url': 'https://github.com/sketchfab/gltf2-blender-importer/releases',
'tracker_url': 'https://github.com/sketchfab/gltf2-blender-importer/issues',
'link': 'https://github.com/sketchfab/gltf2-blender-importer',
'support': 'COMMUNITY',
'category': 'Import-Export'
}
PLUGIN_VERSION = str(bl_info['version']).strip('() ').replace(',', '.')
preview_collection = {}
is_plugin_enabled = False
# helpers
def get_sketchfab_login_props():
return bpy.context.window_manager.sketchfab_api
def get_sketchfab_props():
return bpy.context.window_manager.sketchfab_browser
def get_sketchfab_props_proxy():
return bpy.context.window_manager.sketchfab_browser_proxy
def get_sketchfab_model(uid):
skfb = get_sketchfab_props()
return skfb.search_results['current'][uid]
def run_default_search():
searchthr = GetRequestThread(Config.DEFAULT_SEARCH, parse_results)
searchthr.start()
def get_plugin_enabled():
props = get_sketchfab_props()
return props.is_plugin_enabled
def refresh_search(self, context):
pprops = get_sketchfab_props_proxy()
props = get_sketchfab_props()
props.query = pprops.query
props.animated = pprops.animated
props.pbr = pprops.pbr
props.staffpick = pprops.staffpick
props.categories = pprops.categories
props.face_count = pprops.face_count
props.sort_by = pprops.sort_by
bpy.ops.wm.sketchfab_search('EXEC_DEFAULT')
def set_login_status(status_type, status):
login_props = get_sketchfab_login_props()
login_props.status = status
login_props.status_type = status_type
def set_import_status(status):
props = get_sketchfab_props()
props.import_status = status
class SketchfabApi:
def __init__(self):
self.access_token = ''
self.headers = {}
self.display_name = ''
self.plan_type = ''
self.next_results_url = None
self.prev_results_url = None
def build_headers(self):
self.headers = {'Authorization': 'Bearer ' + self.access_token}
def login(self, email, password):
bpy.ops.wm.login_modal('INVOKE_DEFAULT')
def is_user_logged(self):
if self.access_token and self.headers:
return True
return False
def logout(self):
self.access_token = ''
self.headers = {}
Cache.delete_key('username')
Cache.delete_key('access_token')
Cache.delete_key('Test')
Cache.delete_key('key')
def request_user_info(self):
requests.get(Config.SKETCHFAB_ME, headers=self.headers, hooks={'response': self.parse_user_info})
def get_user_info(self):
if self.display_name and self.plan_type:
return 'as {} ({})'.format(self.display_name, self.plan_type)
else:
return ('', '')
def parse_user_info(self, r, *args, **kargs):
if r.status_code == 200:
user_data = r.json()
self.display_name = user_data['displayName']
self.plan_type = user_data['account']
else:
print('Invalid access token')
self.access_token = ''
self.headers = {}
def parse_login(self, r, *args, **kwargs):
if r.status_code == 200 and 'access_token' in r.json():
self.access_token = r.json()['access_token']
self.build_headers()
self.request_user_info()
else:
if 'error_description' in r.json():
print("Failed to login: {}".format(r.json()['error_description']))
else:
print('Login failed.\n {}'.format(r.json()))
def request_thumbnail(self, thumbnails_json):
url = Utils.get_thumbnail_url(thumbnails_json)
thread = ThumbnailCollector(url)
thread.start()
def request_model_info(self, uid):
url = Config.SKETCHFAB_MODEL + '/' + uid
model_infothr = GetRequestThread(url, self.handle_model_info)
model_infothr.start()
def handle_model_info(self, r, *args, **kwargs):
uid = get_uid_from_model_url(r.url)
model = get_sketchfab_props().search_results['current'][uid]
json_data = r.json()
model.license = json_data['license']['fullName']
model.animated = int(json_data['animationCount']) > 0
get_sketchfab_props().search_results['current'][uid] = model
def search(self, query, search_cb):
search_query = '{}{}'.format(Config.BASE_SEARCH, query)
searchthr = GetRequestThread(search_query, search_cb)
searchthr.start()
def search_cursor(self, url, search_cb):
requests.get(url, hooks={'response': search_cb})
def download_model(self, uid):
skfb_model = get_sketchfab_model(uid)
if skfb_model.download_url:
# Check url sanity
if time.time() - skfb_model.time_url_requested < skfb_model.url_expires:
self.get_archive(skfb_model.download_url)
else:
print("Download url is outdated, requesting a new one")
skfb_model.download_url = None
skfb_model.url_expires = None
skfb_model.time_url_requested = None
requests.get(Utils.build_download_url(uid), headers=self.headers, hooks={'response': self.handle_download})
else:
requests.get(Utils.build_download_url(uid), headers=self.headers, hooks={'response': self.handle_download})
def handle_download(self, r, *args, **kwargs):
if r.status_code != 200 or 'gltf' not in r.json():
print('Download not available for this model')
return
skfb = get_sketchfab_props()
uid = get_uid_from_model_url(r.url)
gltf = r.json()['gltf']
skfb_model = get_sketchfab_model(uid)
skfb_model.download_url = gltf['url']
skfb_model.time_url_requested = time.time()
skfb_model.url_expires = gltf['expires']
self.get_archive(gltf['url'])
def get_archive(self, url):
if url is None:
print('Url is None')
return
r = requests.get(url, stream=True)
uid = get_uid_from_model_url(url)
temp_dir = os.path.join(Config.SKETCHFAB_MODEL_DIR, uid)
if not os.path.exists(temp_dir):
os.makedirs(temp_dir)
archive_path = os.path.join(temp_dir, '{}.zip'.format(uid))
if not os.path.exists(archive_path):
wm = bpy.context.window_manager
wm.progress_begin(0, 100)
set_log("Downloading model..")
with open(archive_path, "wb") as f:
total_length = r.headers.get('content-length')
if total_length is None: # no content length header
f.write(r.content)
else:
dl = 0
total_length = int(total_length)
for data in r.iter_content(chunk_size=4096):
dl += len(data)
f.write(data)
done = int(100 * dl / total_length)
wm.progress_update(done)
set_log("Downloading model..{}%".format(done))
wm.progress_end()
else:
print('Model already downloaded')
gltf_path, gltf_zip = unzip_archive(archive_path)
if gltf_path:
try:
import_model(gltf_path, uid)
except Exception as e:
import traceback
print(traceback.format_exc())
else:
print("Failed to download model (url might be invalid)")
model = get_sketchfab_model(uid)
set_import_status("Import model ({})".format(model.download_size if model.download_size else 'fetching data'))
class SketchfabLoginProps(bpy.types.PropertyGroup):
def update_tr(self, context):
self.status = ''
if self.email != self.last_username or self.password != self.last_password:
self.last_username = self.email
self.last_password = self.password
if not self.password:
set_login_status('ERROR', 'Password is empty')
bpy.ops.wm.sketchfab_login('EXEC_DEFAULT')
email = StringProperty(
name="email",
description="User email",
default="")
password = StringProperty(
name="password",
description="User password",
subtype='PASSWORD',
default="",
update=update_tr
)
access_token = StringProperty(
name="access_token",
description="oauth access token",
subtype='PASSWORD',
default=""
)
status = StringProperty(name='', default='')
status_type = EnumProperty(
name="Face Count",
items=(('ERROR', "Error", ""),
('INFO', "Information", ""),
('FILE_REFRESH', "Progress", "")),
description="Determines which icon to use",
default='FILE_REFRESH'
)
last_username = StringProperty(default="default")
last_password = StringProperty(default="default")
skfb_api = SketchfabApi()
class SketchfabBrowserPropsProxy(bpy.types.PropertyGroup):
# Search
query = StringProperty(
name="",
update=refresh_search,
description="Query to search",
default="",
options={'SKIP_SAVE'}
)
pbr = BoolProperty(
name="PBR",
description="Search for PBR model only",
default=False,
update=refresh_search,
)
categories = EnumProperty(
name="Categories",
items=Config.SKETCHFAB_CATEGORIES,
description="Show only models of category",
default='ALL',
update=refresh_search
)
face_count = EnumProperty(
name="Face Count",
items=Config.SKETCHFAB_FACECOUNT,
description="Determines which meshes are exported",
default='ANY',
update=refresh_search
)
sort_by = EnumProperty(
name="Sort by",
items=Config.SKETCHFAB_SORT_BY,
description="Sort ",
default='LIKES',
update=refresh_search
)
animated = BoolProperty(
name="Animated",
description="Show only models with animation",
default=False,
update=refresh_search
)
staffpick = BoolProperty(
name="Staffpick",
description="Show only staffpick models",
default=False,
update=refresh_search
)
class SketchfabBrowserProps(bpy.types.PropertyGroup):
# Search
query = StringProperty(
name="Search",
description="Query to search",
default=""
)
pbr = BoolProperty(
name="PBR",
description="Search for PBR model only",
default=False
)
categories = EnumProperty(
name="Categories",
items=Config.SKETCHFAB_CATEGORIES,
description="Show only models of category",
default='ALL',
)
face_count = EnumProperty(
name="Face Count",
items=Config.SKETCHFAB_FACECOUNT,
description="Determines which meshes are exported",
default='ANY',
)
sort_by = EnumProperty(
name="Sort by",
items=Config.SKETCHFAB_SORT_BY,
description="Sort ",
default='LIKES',
)
animated = BoolProperty(
name="Animated",
description="Show only models with animation",
default=False,
)
staffpick = BoolProperty(
name="Staffpick",
description="Show only staffpick models",
default=False,
)
status = StringProperty(name='status', default='idle')
use_preview = BoolProperty(
name="Use Preview",
description="Show results using preview widget instead of regular buttons with thumbnails as icons",
default=True
)
search_results = {}
current_key = StringProperty(name='current', default='current')
has_searched_next = BoolProperty(name='next', default=False)
has_searched_prev = BoolProperty(name='prev', default=False)
skfb_api = SketchfabLoginProps.skfb_api
custom_icons = bpy.utils.previews.new()
has_loaded_thumbnails = BoolProperty(default=False)
is_latest_version = IntProperty(default=-1)
import_status = StringProperty(name='import', default='')
is_plugin_enabled = BoolProperty(default=False)
def list_current_results(self, context):
skfb = get_sketchfab_props()
# No results:
if 'current' not in skfb.search_results:
return preview_collection['default']
if skfb.has_loaded_thumbnails and 'thumbnails' in preview_collection:
return preview_collection['thumbnails']
res = []
missing_thumbnail = False
if 'current' in skfb.search_results and len(skfb.search_results['current']):
skfb_results = skfb.search_results['current']
for i, result in enumerate(skfb_results):
if result in skfb_results:
model = skfb_results[result]
if model.uid in skfb.custom_icons:
res.append((model.uid, model.title, "", skfb.custom_icons[model.uid].icon_id, i))
else:
res.append((model.uid, model.title, "", preview_collection['skfb']['0'].icon_id, i))
missing_thumbnail = True
else:
print('Result issue')
# Default element to avoid having an empty preview collection
if not res:
res.append(('NORESULTS', 'empty', "", preview_collection['skfb']['0'].icon_id, 0))
preview_collection['thumbnails'] = tuple(res)
skfb.has_loaded_thumbnails = not missing_thumbnail
return preview_collection['thumbnails']
def draw_search(layout, context):
layout.row()
props = get_sketchfab_props_proxy()
col = layout.box().column(align=True)
col.prop(props, "query")
col.operator("wm.sketchfab_search", text="Search", icon='VIEWZOOM')
pprops = get_sketchfab_props()
def draw_model_info(layout, model, context):
ui_model_props = layout.box().column(align=True)
ui_model_props.operator("wm.sketchfab_view", text="View on Sketchfab", icon='WORLD').model_uid = model.uid
ui_model_props.label('{}'.format(model.title), icon='OBJECT_DATA')
ui_model_props.label('{}'.format(model.author), icon='ARMATURE_DATA')
if model.license:
ui_model_props.label('{}'.format(model.license), icon='TEXT')
else:
ui_model_props.label('Fetching..')
if model.vertex_count and model.face_count:
ui_model_stats = ui_model_props.row()
ui_model_stats.label('Verts: {} | Faces: {}'.format(Utils.humanify_number(model.vertex_count), Utils.humanify_number(model.face_count)), icon='MESH_DATA')
if(model.animated):
ui_model_props.label('Animation is not supported (possible issues)', icon='ERROR')
import_ops = ui_model_props.column()
skfb = get_sketchfab_props()
import_ops.enabled = skfb.skfb_api.is_user_logged() and bpy.context.mode == 'OBJECT'
if not skfb.skfb_api.is_user_logged():
downloadlabel = 'You need to be logged in to download a model'
elif bpy.context.mode != 'OBJECT':
downloadlabel = "Import is available only in object mode"
else:
downloadlabel = "Import model ({})".format(model.download_size if model.download_size else 'fetching data')
if skfb.import_status:
downloadlabel = skfb.import_status
download_icon = 'EXPORT' if import_ops.enabled else 'INFO'
import_ops.label('')
import_ops.operator("wm.sketchfab_download", icon=download_icon, text=downloadlabel, translate=False, emboss=True).model_uid = model.uid
def set_log(log):
get_sketchfab_props().status = log
def get_uid_from_thumbnail_url(thumbnail_url):
return thumbnail_url.split('/')[4]
def get_uid_from_model_url(model_url):
return model_url.split('/')[5]
def unzip_archive(archive_path):
if os.path.exists(archive_path):
set_import_status('Unzipping model')
import zipfile
try:
zip_ref = zipfile.ZipFile(archive_path, 'r')
extract_dir = os.path.dirname(archive_path)
zip_ref.extractall(extract_dir)
zip_ref.close()
except zipfile.BadZipFile:
print('Error when dezipping file')
os.remove(archive_path)
print('Invaild zip. Try again')
set_import_status('')
return None, None
gltf_file = os.path.join(extract_dir, 'scene.gltf')
return gltf_file, archive_path
else:
print('ERROR: archive doesn\'t exist')
def run_async(func):
from threading import Thread
from functools import wraps
@wraps(func)
def async_func(*args, **kwargs):
func_hl = Thread(target=func, args=args, kwargs=kwargs)
func_hl.start()
return func_hl
return async_func
def import_model(gltf_path, uid):
bpy.ops.wm.import_modal('INVOKE_DEFAULT', gltf_path=gltf_path, uid=uid)
def build_search_request(query, pbr, animated, staffpick, face_count, category, sort_by):
final_query = '&q={}'.format(query)
# Disabled until animation import is implemented
if animated:
final_query = final_query + '&animated=true'
if staffpick:
final_query = final_query + '&staffpicked=true'
if sort_by == 'LIKES':
final_query = final_query + '&sort_by=-likeCount'
elif sort_by == 'RECENT':
final_query = final_query + '&sort_by=-publishedAt'
elif sort_by == 'VIEWS':
final_query = final_query + '&sort_by=-viewCount'
if face_count == '10K':
final_query = final_query + '&max_face_count=10000'
elif face_count == '50K':
final_query = final_query + '&min_face_count=10000&max_face_count=50000'
elif face_count == '100K':
final_query = final_query + '&min_face_count=50000&max_face_count=100000'
elif face_count == '250K':
final_query = final_query + "&min_face_count=100000&max_face_count=250000"
elif face_count == '250KP':
final_query = final_query + "&min_face_count=250000"
if category != 'ALL':
final_query = final_query + '&categories={}'.format(category)
if pbr:
final_query = final_query + '&pbr_type=metalness'
return final_query
def parse_results(r, *args, **kwargs):
skfb = get_sketchfab_props()
json_data = r.json()
if 'current' in skfb.search_results:
skfb.search_results['current'].clear()
del skfb.search_results['current']
skfb.search_results['current'] = OrderedDict()
for result in list(json_data['results']):
uid = result['uid']
skfb.search_results['current'][result['uid']] = SketchfabModel(result)
if not os.path.exists(os.path.join(Config.SKETCHFAB_THUMB_DIR, uid) + '.jpeg'):
skfb.skfb_api.request_thumbnail(result['thumbnails'])
elif uid not in skfb.custom_icons:
skfb.custom_icons.load(uid, os.path.join(Config.SKETCHFAB_THUMB_DIR, "{}.jpeg".format(uid)), 'IMAGE')
if json_data['next']:
skfb.skfb_api.next_results_url = json_data['next']
else:
skfb.skfb_api.next_results_url = None
if json_data['previous']:
skfb.skfb_api.prev_results_url = json_data['previous']
else:
skfb.skfb_api.prev_results_url = None
class ThumbnailCollector(threading.Thread):
def __init__(self, url):
self.url = url
threading.Thread.__init__(self)
def set_url(self, url):
self.url = url
def run(self):
if not self.url:
return
requests.get(self.url, stream=True, hooks={'response': self.handle_thumbnail})
def handle_thumbnail(self, r, *args, **kwargs):
uid = r.url.split('/')[4]
if not os.path.exists(Config.SKETCHFAB_THUMB_DIR):
os.makedirs(Config.SKETCHFAB_THUMB_DIR)
thumbnail_path = os.path.join(Config.SKETCHFAB_THUMB_DIR, uid) + '.jpeg'
with open(thumbnail_path, "wb") as f:
total_length = r.headers.get('content-length')
if total_length is None and r.content:
f.write(r.content)
else:
dl = 0
total_length = int(total_length)
for data in r.iter_content(chunk_size=4096):
dl += len(data)
f.write(data)
props = get_sketchfab_props()
if uid not in props.custom_icons:
props.custom_icons.load(uid, os.path.join(Config.SKETCHFAB_THUMB_DIR, "{}.jpeg".format(uid)), 'IMAGE')
class LoginModal(bpy.types.Operator):
bl_idname = "wm.login_modal"
bl_label = "Import glTF model into Sketchfab"
bl_options = {'INTERNAL'}
is_logging = BoolProperty(default=False)
error = BoolProperty(default=False)
error_message = StringProperty(default='')
def exectue(self, context):
return {'FINISHED'}
def handle_login(self, r, *args, **kwargs):
browser_props = get_sketchfab_props()
if r.status_code == 200 and 'access_token' in r.json():
browser_props.skfb_api.access_token = r.json()['access_token']
login_props = get_sketchfab_login_props()
Cache.save_key('username', login_props.email)
Cache.save_key('access_token', browser_props.skfb_api.access_token)
browser_props.skfb_api.build_headers()
set_login_status('INFO', '')
browser_props.skfb_api.request_user_info()
else:
if 'error_description' in r.json():
set_login_status('ERROR', 'Failed to authenticate: bad login/password')
else:
set_login_status('ERROR', 'Failed to authenticate: bad login/password')
print('Cannot login.\n {}'.format(r.json()))
self.is_logging = False
def modal(self, context, event):
if self.error:
self.error = False
set_login_status('ERROR', '{}'.format(self.error_message))
return {"FINISHED"}
if self.is_logging:
set_login_status('FILE_REFRESH', 'Loging in to your Sketchfab account...')
return {'RUNNING_MODAL'}
else:
return {'FINISHED'}
def invoke(self, context, event):
self.is_logging = True
try:
context.window_manager.modal_handler_add(self)
login_props = get_sketchfab_login_props()
url = '{}&username={}&password={}'.format(Config.SKETCHFAB_OAUTH, urllib.parse.quote_plus(login_props.email), urllib.parse.quote_plus(login_props.password))
requests.post(url, hooks={'response': self.handle_login})
except Exception as e:
self.error = True
self.error_message = str(e)
return {'RUNNING_MODAL'}
class ImportModalOperator(bpy.types.Operator):
bl_idname = "wm.import_modal"
bl_label = "Import glTF model into Sketchfab"
bl_options = {'INTERNAL'}
gltf_path = StringProperty()
uid = StringProperty()
def exectue(self, context):
print('IMPORT')
return {'FINISHED'}
def modal(self, context, event):
bpy.context.scene.render.engine = 'CYCLES'
gltf_data = glTFImporter(self.gltf_path, Log.default())
success, txt = gltf_data.read()
if not success:
print('Failed to read GLTF')
try:
model_name = 'GLTFModel'
if 'extras' in gltf_data.scene.gltf.json['asset'] and 'title' in gltf_data.scene.gltf.json['asset']['extras']:
model_name = gltf_data.scene.gltf.json['asset']['extras']['title']
blender_scene(gltf_data.scene, root_name=model_name)
set_import_status('')
Utils.clean_downloaded_model_dir(self.uid)
return {'FINISHED'}
except Exception:
import traceback
print(traceback.format_exc())
set_import_status('')
return {'FINISHED'}
return {'RUNNING_MODAL'}
def invoke(self, context, event):
context.window_manager.modal_handler_add(self)
set_import_status('Importing...')
return {'RUNNING_MODAL'}
class GetRequestThread(threading.Thread):
def __init__(self, url, callback):
self.url = url
self.callback = callback
threading.Thread.__init__(self)
def run(self):
requests.get(self.url, hooks={'response': self.callback})
class View3DPanel:
bl_space_type = 'VIEW_3D'
bl_region_type = 'TOOLS'
bl_label = "Sketchfab Assets Browser"
bl_category = "Sketchfab"
bl_context = 'objectmode'
@classmethod
def poll(cls, context):
return (context.scene is not None)
class LoginPanel(View3DPanel, bpy.types.Panel):
bl_idname = "VIEW3D_PT_test_1"
bl_label = "Log in to your Sketchfab account"
is_logged = BoolProperty()
def draw(self, context):
skfb = get_sketchfab_props()
if not skfb.is_plugin_enabled:
self.layout.operator('wm.skfb_enable', text='Connect to Sketchfab', icon='LINKED').enable = True
else:
# LOGIN
skfb_login = get_sketchfab_login_props()
layout = self.layout.box().column(align=True)
layout.enabled = get_plugin_enabled()
if skfb_login.skfb_api.is_user_logged():
login_row = layout.row()
login_row.label('Logged in as {}'.format(skfb_login.skfb_api.get_user_info()))
login_row.operator('wm.sketchfab_login', text='Logout', icon='GO_LEFT').authenticate = False
if skfb_login.status:
layout.prop(skfb_login, 'status', icon=skfb_login.status_type)
else:
layout.label("Login to your Sketchfab account", icon='INFO')
layout.prop(skfb_login, "email")
layout.prop(skfb_login, "password")
ops_row = layout.row()
ops_row.operator('wm.sketchfab_signup', text='Create an account', icon='PLUS')
ops_row.operator('wm.sketchfab_login', text='Log in', icon='WORLD').authenticate = True
if skfb_login.status:
layout.prop(skfb_login, 'status', icon=skfb_login.status_type)
# Version info
self.layout.separator()
if skfb.is_latest_version == 1:
self.bl_label = "Sketchfab plugin v{} (up-to-date)".format(PLUGIN_VERSION)
elif skfb.is_latest_version == 0:
self.bl_label = "Sketchfab plugin v{} (outdated)".format(PLUGIN_VERSION)
self.layout.operator('wm.skfb_new_version', text='New version available', icon='NEW')
elif skfb.is_latest_version == -2:
self.bl_label = "Sketchfab plugin v{}".format(PLUGIN_VERSION)
# External links
doc_ui = self.layout.row()
doc_ui.operator('wm.skfb_help', text='Documentation', icon='QUESTION')
doc_ui.operator('wm.skfb_report_issue', text='Report an issue', icon='ERROR')
layout = self.layout
class FiltersPanel(View3DPanel, bpy.types.Panel):
bl_idname = "VIEW3D_PT_sketchfab_filters"
bl_label = "Search filters"
bl_options = {'DEFAULT_CLOSED'}
def draw(self, context):
layout = self.layout
layout.enabled = get_plugin_enabled()
props = get_sketchfab_props_proxy()
col = layout.box().column(align=True)
col.prop(props, "pbr")
col.prop(props, "staffpick")
col.separator()
col.prop(props, "categories")
col.label('Sort by')
sb = col.row()
sb.prop(props, "sort_by", expand=True)
col.label('Face count')
col.prop(props, "face_count", expand=True)
def draw_results_icons(results, props, nbcol=4):
props = get_sketchfab_props()
current = props.search_results['current']
dimx = nbcol if current else 0
dimy = int(24 / nbcol) if current else 0
if dimx is not 0 and dimy is not 0:
for r in range(dimy):
ro = results.row()
for col in range(dimx):
col2 = ro.column(align=True)
index = r * dimx + col
if index >= len(current.keys()):
return
model = current[list(current.keys())[index]]
if model.uid in props.custom_icons:
col2.operator("wm.sketchfab_modelview", icon_value=props.custom_icons[model.uid].icon_id, text="{}".format(model.title + ' by ' + model.author)).uid = list(current.keys())[index]
else:
col2.operator("wm.sketchfab_modelview", text="{}".format(model.title + ' by ' + model.author)).uid = list(current.keys())[index]
else:
results.row()
results.row().label('No results')
results.row()
class ResultsPanel(View3DPanel, bpy.types.Panel):
bl_idname = "VIEW3D_PT_sketchfab_results"
bl_label = "Results"
uid = ''
def draw(self, context):
self.layout.enabled = get_plugin_enabled()
layout = self.layout
col = layout.column(align=True)
props = get_sketchfab_props()
results = layout.column(align=True)
model = None
result_pages_ops = col.row()
if props.skfb_api.prev_results_url:
result_pages_ops.operator("wm.sketchfab_search_prev", text="Previous page", icon='FRAME_PREV')
if props.skfb_api.next_results_url:
result_pages_ops.operator("wm.sketchfab_search_next", text="Next page", icon='FRAME_NEXT')
result_label = 'Click below to see more results'
col.label(result_label, icon='INFO')
try:
col.template_icon_view(bpy.context.window_manager, 'result_previews', show_labels=True, scale=5.0)
except Exception:
print('ResultsPanel: Failed to display results')
pass
if 'current' not in props.search_results or not len(props.search_results['current']):
self.bl_label = 'No results'
return
else:
self.bl_label = "Results"
if bpy.context.window_manager.result_previews not in props.search_results['current']:
return
model = props.search_results['current'][bpy.context.window_manager.result_previews]
if not model:
return
if self.uid != model.uid:
self.uid = model.uid
if not model.info_requested:
props.skfb_api.request_model_info(model.uid)
model.info_requested = True
draw_model_info(col, model, context)
class SketchfabLogger(bpy.types.Operator):
bl_idname = 'wm.sketchfab_login'
bl_label = 'Sketchfab Login'
bl_options = {'INTERNAL'}
authenticate = BoolProperty(default=True)
def execute(self, context):
set_login_status('FILE_REFRESH', 'Login to your Sketchfab account...')
wm = context.window_manager
if self.authenticate:
wm.sketchfab_browser.skfb_api.login(wm.sketchfab_api.email, wm.sketchfab_api.password)
else:
wm.sketchfab_browser.skfb_api.logout()
wm.sketchfab_api.password = ''
wm.sketchfab_api.last_password = "default"
set_login_status('FILE_REFRESH', '')
return {'FINISHED'}
# Operator to perform search on Sketchfab
class SketchfabBrowse(View3DPanel, bpy.types.Panel):
bl_idname = "wm.sketchfab_browse"
bl_label = "Search"
def draw(self, context):
self.layout.enabled = get_plugin_enabled()
draw_search(self.layout, context)
def invoke(self, context, event):
wm = context.window_manager
return wm.invoke_props_dialog(self, width=900, height=850)
class SketchfabModel:
def __init__(self, json_data):
self.title = str(json_data['name'])
self.author = json_data['user']['displayName']
self.uid = json_data['uid']