Repository navigation
Expand file tree
/
Copy pathpaths.py
More file actions
790 lines (657 loc) · 27.4 KB
/
Copy pathpaths.py
File metadata and controls
790 lines (657 loc) · 27.4 KB
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
# ##### 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 #####
from __future__ import annotations
import getpass
import logging
import os
import re
import shutil
import subprocess
import sys
import tempfile
import uuid
from functools import lru_cache
import bpy
from . import client_lib, global_vars, reports, utils
bk_logger = logging.getLogger(__name__)
BLENDKIT_API = f"{global_vars.SERVER}/api/v1"
BLENDKIT_OAUTH_LANDING_URL = f"{global_vars.SERVER}/oauth-landing"
BLENDKIT_PLANS_URL = f"{global_vars.SERVER}/plans/pricing"
BLENDKIT_REPORT_URL = f"{global_vars.SERVER}/usage_report"
BLENDKIT_USER_ASSETS_URL = f"{global_vars.SERVER}/my-assets"
BLENDKIT_ASSETS_EDIT_URL = f"{global_vars.SERVER}/asset-edit"
BLENDKIT_MANUAL_URL = "https://youtu.be/0P8ZjfbUjeA"
BLENDKIT_MODEL_UPLOAD_INSTRUCTIONS_URL = f"{global_vars.SERVER}/docs/upload/"
BLENDKIT_PRINTABLE_UPLOAD_INSTRUCTIONS_URL = (
f"{global_vars.SERVER}/docs/upload-printables/"
)
BLENDKIT_MATERIAL_UPLOAD_INSTRUCTIONS_URL = (
f"{global_vars.SERVER}/docs/uploading-material/"
)
BLENDKIT_BRUSH_UPLOAD_INSTRUCTIONS_URL = f"{global_vars.SERVER}/docs/uploading-brush/"
BLENDKIT_HDR_UPLOAD_INSTRUCTIONS_URL = f"{global_vars.SERVER}/docs/uploading-hdr/"
BLENDKIT_SCENE_UPLOAD_INSTRUCTIONS_URL = f"{global_vars.SERVER}/docs/uploading-scene/"
BLENDKIT_ADDON_UPLOAD_INSTRUCTIONS_URL = f"{global_vars.SERVER}/add-ons-upload-beta/"
BLENDKIT_LOGIN_URL = f"{global_vars.SERVER}/accounts/login"
BLENDKIT_SIGNUP_URL = f"{global_vars.SERVER}/accounts/register"
WINDOWS_PATH_LIMIT = 250
ASSET_LIBRARY_NAME = "Blendkit"
def url_with_utm(url: str, placement: str) -> str:
"""Tag an outbound web link so analytics can attribute it to the add-on surface that opened it."""
base, hash_sign, fragment = url.partition("#")
separator = "&" if "?" in base else "?"
return (
f"{base}{separator}utm_source=blender_addon&utm_medium=app"
f"&utm_content={placement}{hash_sign}{fragment}"
)
_stable_system_id: str | None = None
def get_system_id_filepath() -> str:
"""Where Blendkit-Client keeps the machine ID; the add-on only reads it."""
preferences = bpy.context.preferences.addons[__package__].preferences
return os.path.join(bpy.path.abspath(preferences.global_dir), "client", "system_id")
def get_stable_system_id() -> str:
"""Machine ID for telemetry: the 15 digits Blendkit-Client persisted, else ``uuid.getnode()``.
The Client owns the ID. On its first start it writes its own MAC-derived
value to ``global_dir/client/system_id`` and reports that from then on, so a
machine keeps the ID the server already knows while MAC randomization, VPN
adapters and Python's random fallback stop splitting one machine into many.
The add-on must not seed the file itself: measured on production,
``uuid.getnode()`` picks a different adapter than the Client on 79% of
Windows machines, so an add-on-written seed would rename most of them.
Previous root-level and default-directory files are also read until the
Client copies the ID into the client directory, without removing old files.
A value read from the file is cached; the ``uuid.getnode()`` fallback is
not, so a login before the Client's first write still picks the file up
later. Deleting ``blenderkit_data`` only makes the Client re-seed from its
MAC; a copied data directory makes two machines share an ID (measured at
~0.02% of machines, accepted).
"""
global _stable_system_id
if _stable_system_id is not None:
return _stable_system_id
current_filepath = get_system_id_filepath()
for filepath in (
current_filepath,
os.path.join(os.path.dirname(os.path.dirname(current_filepath)), "system_id"),
os.path.join(default_global_dict(), "client", "system_id"),
os.path.join(default_global_dict(), "system_id"),
):
try:
with open(filepath) as f:
stored = f.read().strip()
except (OSError, UnicodeDecodeError):
continue
if re.fullmatch(r"\d{15}", stored):
_stable_system_id = stored
return stored
return f"{uuid.getnode():015d}"
def _normalize_path(path_value: str | None) -> str:
"""Return an absolute, normalized path for comparisons; blank string if invalid."""
if not path_value:
return ""
try:
resolved = bpy.path.abspath(path_value)
except Exception:
resolved = path_value
return os.path.normpath(os.path.abspath(resolved))
def cleanup_old_directories():
"""function to clean up any historical directories for Blendkit. By now removes the temp directory."""
orig_temp = os.path.join(os.path.expanduser("~"), "blenderkit_data", "temp")
if os.path.isdir(orig_temp):
try:
shutil.rmtree(orig_temp)
except Exception as e:
bk_logger.error("could not delete old temp directory: %s", e)
def find_in_local(text=""):
fs = []
for p, d, f in os.walk("."):
for file in f:
if text in file:
fs.append(file)
return fs
def get_unlock_asset_url(
asset_id, placement: str, variant_id: str | None = None
) -> str:
"""Purchase/unlock page for a locked asset.
Keeps ``from_addon=True`` - the server page renders add-on-specific content
from it (see get_blenderkit/views.py) - and adds UTM tags for attribution.
``variant_id`` tags the link with the copy option the user saw.
"""
url = f"{global_vars.SERVER}/get-blenderkit/{asset_id}/?from_addon=True"
if variant_id:
url += f"&ab_variant={variant_id}"
return url_with_utm(url, placement)
def get_author_gallery_url(author_id: int, placement: str = "author_gallery"):
return url_with_utm(
f"{global_vars.SERVER}/asset-gallery?query=author_id:{author_id}", placement
)
def get_asset_gallery_url(asset_id, placement: str = "asset_web_view"):
return url_with_utm(
f"{global_vars.SERVER}/asset-gallery-detail/{asset_id}/", placement
)
def default_global_dict():
home = os.path.expanduser("~")
data_home = os.environ.get("XDG_DATA_HOME")
if data_home != None:
home = data_home
return home + os.sep + "blenderkit_data"
def get_categories_filepath():
tempdir = get_temp_dir()
return os.path.join(tempdir, "categories.json")
dirs_exist_dict = {} # cache these results since this is used very often
# this causes the function to fail if user deletes the directory while blender is running,
# but comes back when blender is restarted.
def get_temp_dir(subdir=None):
user_preferences = bpy.context.preferences.addons[__package__].preferences
# first try cached results
if subdir is not None:
d = dirs_exist_dict.get(subdir)
if d is not None:
return d
else:
d = dirs_exist_dict.get("top")
if d is not None:
return d
try: # if USERNAME envvar is unset on Win, getuser() fallbacks to pwd module which is not available on Windows
username = getpass.getuser()
except ModuleNotFoundError as e:
username = "bkuser"
safe_username = "".join(c for c in username if c.isalnum())
tempdir = os.path.join(tempfile.gettempdir(), f"bktemp_{safe_username}")
if tempdir.startswith("//"):
tempdir = bpy.path.abspath(tempdir)
try:
if not os.path.exists(tempdir):
os.makedirs(tempdir)
dirs_exist_dict["top"] = tempdir
if subdir is not None:
tempdir = os.path.join(tempdir, subdir)
if not os.path.exists(tempdir):
os.makedirs(tempdir)
dirs_exist_dict[subdir] = tempdir
cleanup_old_directories()
except Exception as e:
reports.add_report("Cache directory not found. Resetting Cache directory path.")
bk_logger.warning("due to exception: %s", e)
p = default_global_dict()
if p == user_preferences.global_dir:
message = "Global dir was already default, please set a global directory in addon preferences to a dir where you have write permissions."
reports.add_report(message)
return None
user_preferences.global_dir = p
tempdir = get_temp_dir(subdir=subdir)
return tempdir
def _try_makedirs(dirpath, label="download"):
"""Try to create directory, report error on failure. Returns True on success."""
if os.path.exists(dirpath):
return True
try:
os.makedirs(dirpath)
return True
except OSError as e:
bk_logger.error("Cannot create download directory: %s - %s", dirpath, e)
reports.add_report(
f"Cannot create {label} folder: {dirpath}\n{e}",
type="ERROR",
)
return False
def get_download_dirs(asset_type):
"""get directories where assets will be downloaded"""
plurals_mapping = {
"brush": "brushes",
"texture": "textures",
"model": "models",
"scene": "scenes",
"material": "materials",
"hdr": "hdrs",
"nodegroup": "nodegroups",
"printable": "printables",
"addon": "addons",
"author": "authors",
}
dirs = []
if global_vars.PREFS.get("directory_behaviour") is None:
global_vars.PREFS = utils.get_preferences_as_dict()
if global_vars.PREFS["directory_behaviour"] in ("BOTH", "GLOBAL"):
ddir = global_vars.PREFS["global_dir"]
if ddir.startswith("//"):
ddir = bpy.path.abspath(ddir)
subdir = os.path.join(ddir, plurals_mapping[asset_type])
if not _try_makedirs(subdir, "download"):
return dirs
dirs.append(subdir)
if (
global_vars.PREFS["directory_behaviour"] in ("BOTH", "LOCAL")
and bpy.data.is_saved
): # it's important local get's solved as second, since for the linking process only last filename will be taken. For download process first name will be taken and if 2 filenames were returned, file will be copied to the 2nd path.
ddir = global_vars.PREFS["project_subdir"]
ddir = bpy.path.abspath(ddir)
subdir = os.path.join(ddir, plurals_mapping[asset_type])
if sys.platform == "win32" and len(subdir) > WINDOWS_PATH_LIMIT:
bk_logger.warning(
f"Skipping LOCAL download directory. Over 250 characters: {ddir}"
)
return (
dirs # project subdir is over 250, no space for adding filenames later
)
if not _try_makedirs(subdir, "project download"):
return dirs
dirs.append(subdir)
return dirs
def ensure_asset_library_path(
global_dir: str | None = None, previous_global_dir: str | None = None
):
"""Ensure Blender's asset library list contains the Blendkit library entry.
Safety contract: this function only ever creates, reads, or updates the
single asset library entry that Blendkit owns (the one named
``ASSET_LIBRARY_NAME``). It never renames, repaths, or otherwise mutates any
other asset library. This guarantees it cannot interfere with Blender's
built-in "Essentials" library (default brushes, "Smooth by Angle", etc.) or
with libraries the user configured themselves.
- Creates the Blendkit entry when missing.
- Updates only the Blendkit entry's path if the global directory changes.
"""
if bpy.app.background:
return
prefs = bpy.context.preferences.addons[__package__].preferences # type: ignore
if not getattr(prefs, "create_asset_library", False):
return
filepaths = getattr(bpy.context.preferences, "filepaths", None)
asset_libraries = getattr(filepaths, "asset_libraries", None) if filepaths else None
if asset_libraries is None:
return
target_path = _normalize_path(
global_dir
or bpy.context.preferences.addons[__package__].preferences.global_dir # type: ignore
)
if not target_path:
return
# Diagnostic snapshot: log the whole asset-library list before we touch it.
# This makes it easy to confirm from a user's log that Blendkit only ever
# adds/updates its own entry and never removes Essentials or user libraries.
bk_logger.info(
"ensure_asset_library_path: create_asset_library=ON, target=%s, "
"libraries before=%s",
target_path,
[(lib.name, lib.path) for lib in asset_libraries],
)
try:
os.makedirs(target_path, exist_ok=True)
except OSError as e:
bk_logger.warning(
"Cannot create asset library directory %s: %s", target_path, e
)
return
# Only ever manage the entry we own (matched strictly by our name). We never
# touch any other library, even one that happens to point at target_path, so
# we cannot affect Essentials or user-configured libraries.
existing = (
asset_libraries.get(ASSET_LIBRARY_NAME)
if hasattr(asset_libraries, "get")
else None
)
if existing is not None:
if _normalize_path(existing.path) != target_path:
bk_logger.info(
"ensure_asset_library_path: repointing existing '%s' entry %s -> %s",
ASSET_LIBRARY_NAME,
existing.path,
target_path,
)
existing.path = target_path
return existing
bk_logger.info(
"ensure_asset_library_path: adding new '%s' entry at %s",
ASSET_LIBRARY_NAME,
target_path,
)
# No Blendkit entry yet. Prefer the direct API which lets us set our name on
# creation without touching anything else.
if hasattr(asset_libraries, "new"):
try:
return asset_libraries.new(name=ASSET_LIBRARY_NAME, directory=target_path)
except Exception as e:
bk_logger.warning(
"Failed to add asset library: %s. "
"Please add it manually in Preferences > File Paths",
e,
)
return None
# Fallback for Blender builds without asset_libraries.new(): use the
# operator, but rename ONLY the entry it just created (identified by being
# absent before the call), never a pre-existing library.
names_before = {lib.name for lib in asset_libraries}
try:
bpy.ops.preferences.asset_library_add(directory=target_path)
except Exception as e:
logging.getLogger(__name__).warning(
"Failed to add asset library: %s. "
"Please add it manually in Preferences > File Paths",
e,
)
return None
for lib in asset_libraries:
if lib.name not in names_before and _normalize_path(lib.path) == target_path:
try:
lib.name = ASSET_LIBRARY_NAME
except Exception:
pass
return lib
return None
def remove_asset_library_path():
"""Remove the Blendkit asset library entry from Blender's asset library list.
Safety contract: only removes the entry Blendkit owns, identified by BOTH
its name (``ASSET_LIBRARY_NAME``) AND its path matching the configured
Blendkit global directory. This avoids removing a user-created library that
merely happens to share the name. Never touches any other library, so it
cannot affect Blender's built-in "Essentials" library. Does nothing in
background mode or when no matching entry exists.
"""
if bpy.app.background:
return
filepaths = getattr(bpy.context.preferences, "filepaths", None)
asset_libraries = getattr(filepaths, "asset_libraries", None) if filepaths else None
if asset_libraries is None:
return
existing = (
asset_libraries.get(ASSET_LIBRARY_NAME)
if hasattr(asset_libraries, "get")
else None
)
if existing is None:
return
# Only remove it if it points at our global directory, so we never delete a
# user library that coincidentally uses the same name.
prefs = bpy.context.preferences.addons[__package__].preferences # type: ignore
target_path = _normalize_path(getattr(prefs, "global_dir", ""))
if target_path and _normalize_path(existing.path) != target_path:
bk_logger.info(
"remove_asset_library_path: keeping '%s' entry at %s (does not match "
"Blendkit global dir %s), assuming it is user-owned",
ASSET_LIBRARY_NAME,
existing.path,
target_path,
)
return
bk_logger.info(
"remove_asset_library_path: removing '%s' entry at %s",
ASSET_LIBRARY_NAME,
existing.path,
)
try:
asset_libraries.remove(existing)
except Exception as e:
bk_logger.warning(
"Failed to remove asset library %s: %s", ASSET_LIBRARY_NAME, e
)
def slugify(input: str) -> str:
"""
Slugify converts a string to a URL-friendly slug.
Converts to lowercase, replaces non-alphanumeric characters with hyphens.
Ensures only one hyphen between words and that string starts and ends with a letter or number.
It also ensures that the slug does not exceed 50 characters.
Same as: utils.go/Slugify()
"""
# Normalize string: convert to lowercase
slug = input.lower()
# Remove non-alpha characters, and convert spaces to hyphens.
slug = re.sub(r"[^a-z0-9]+", "-", slug)
# Replace multiple hyphens with a single one
slug = re.sub(r"[-]+", "-", slug)
# Ensure the slug does not exceed 50 characters
if len(slug) > 50:
slug = slug[:50]
# Ensure slug starts and ends with alphanum character
slug = slug.strip("-")
return slug
def extract_filename_from_url(url):
"""Mirrors utils.go/ExtractFilenameFromURL()"""
if url is None:
return ""
filename = url.split("/")[-1]
filename = filename.split("?")[0]
return filename
resolution_suffix = {
"blend": "",
"resolution_0_5K": "_05k",
"resolution_1K": "_1k",
"resolution_2K": "_2k",
"resolution_4K": "_4k",
"resolution_8K": "_8k",
}
resolutions = {
"resolution_0_5K": 512,
"resolution_1K": 1024,
"resolution_2K": 2048,
"resolution_4K": 4096,
"resolution_8K": 8192,
}
def round_to_closest_resolution(res):
rdist = 1000000
# while res/2>1:
# p2res*=2
# res = res/2
for rkey in resolutions:
d = abs(res - resolutions[rkey])
if d < rdist:
rdist = d
p2res = rkey
return p2res
def get_res_file(asset_data, resolution, find_closest_with_url=False):
"""
Returns closest resolution that current asset can offer.
If there are no resolutions, return orig file.
If orig file is requested, return it.
params
asset_data
resolution - ideal resolution
find_closest_with_url:
returns only resolutions that already contains url in the asset data, used in scenes where asset is/was already present.
Returns:
resolution file
resolution, so that other processes can pass correctly which resolution is downloaded.
"""
orig = None
zipf = None
res = None
closest = None
target_resolution = resolutions.get(resolution)
mindist = 100000000
for f in asset_data["files"]:
if f["fileType"] == "blend":
orig = f
if resolution == "blend":
# orig file found, return.
return orig, "blend"
if f.get("fileType") == "zip_file":
zipf = f
if f["fileType"] == resolution:
# exact match found, return.
return f, resolution
# find closest resolution if the exact match won't be found.
rval = resolutions.get(f["fileType"])
if rval and target_resolution:
rdiff = abs(target_resolution - rval)
if rdiff < mindist:
closest = f
mindist = rdiff
if not res and not closest:
if orig is not None:
return orig, "blend"
if zipf is not None:
return zipf, "zip_file"
return closest, closest["fileType"]
def server_to_local_filename(server_filename: str, asset_name: str) -> str:
"""
Convert server format filename to human readable local filename. Function mirrors: utils.go/ServerToLocalFilename()
"resolution_2K_d5368c9d-092e-4319-afe1-dd765de6da01.blend" > "asset-name_2K_d5368c9d-092e-4319-afe1-dd765de6da01.blend"
"blend_d5368c9d-092e-4319-afe1-dd765de6da01.blend" > "asset-name_d5368c9d-092e-4319-afe1-dd765de6da01.blend"
"""
fn = server_filename.replace("blend_", "")
fn = fn.replace("resolution_", "")
local_filename = slugify(asset_name) + "_" + fn
return local_filename
def get_texture_directory(asset_data, resolution="blend"):
tex_dir_path = f"//textures{resolution_suffix[resolution]}{os.sep}"
return tex_dir_path
def get_asset_directory_name(asset_name: str, asset_id: str) -> str:
"""Get name of the directory for the asset."""
name_slug = slugify(asset_name)
if len(name_slug) > 16:
name_slug = name_slug[:16]
return f"{name_slug}_{asset_id}"
def get_asset_directories(asset_data):
"""Only get path where all asset files are stored."""
asset_dir_name = get_asset_directory_name(asset_data["name"], asset_data["id"])
dirs = get_download_dirs(asset_data["assetType"])
asset_dirs = []
for d in dirs:
asset_dir_path = os.path.join(d, asset_dir_name)
asset_dirs.append(asset_dir_path)
return asset_dirs
def get_download_filepaths(asset_data, resolution="blend", can_return_others=False):
"""Get all possible paths of the asset and resolution. Usually global and local directory."""
dirs = get_download_dirs(asset_data["assetType"])
res_file, resolution = get_res_file(
asset_data, resolution, find_closest_with_url=can_return_others
)
asset_dir_name = get_asset_directory_name(asset_data["name"], asset_data["id"])
file_names = []
if not res_file:
return file_names
# fn = asset_data['file_name'].replace('blend_', '')
if res_file.get("url") is not None:
# Tweak the names a bit:
# remove resolution and blend words in names
#
serverFilename = extract_filename_from_url(res_file["url"])
localFilename = server_to_local_filename(serverFilename, asset_data["name"])
for dir in dirs:
asset_dir_path = os.path.join(dir, asset_dir_name)
if sys.platform == "win32" and len(asset_dir_path) > WINDOWS_PATH_LIMIT:
reports.add_report(
"The path to assets is too long, "
"only Global directory can be used. "
"Move your .blend file to another "
"directory with shorter path to "
"store assets in a subdirectory of your project.",
timeout=60,
type="ERROR",
)
continue
if not os.path.exists(asset_dir_path):
os.makedirs(asset_dir_path)
file_name = os.path.join(asset_dir_path, localFilename)
file_names.append(file_name)
utils.p("file paths", file_names)
for f in file_names:
if sys.platform == "win32" and len(f) > WINDOWS_PATH_LIMIT:
reports.add_report(
"The path to assets is too long, "
"only Global directory can be used. "
"Move your .blend file to another "
"directory with shorter path to "
"store assets in a subdirectory of your project.",
timeout=60,
type="ERROR",
)
file_names.remove(f)
return file_names
def delete_asset_debug(asset_data):
"""TODO fix this for resolutions - should get ALL files from ALL resolutions."""
user_preferences = bpy.context.preferences.addons[__package__].preferences
api_key = user_preferences.api_key
_, download_url, file_name = client_lib.get_download_url(
asset_data, utils.get_scene_id(), api_key
)
asset_data["files"][0]["url"] = download_url
asset_data["files"][0]["file_name"] = file_name
filepaths = get_download_filepaths(asset_data)
for file in filepaths:
asset_dir = os.path.dirname(file)
if os.path.isdir(asset_dir) is False:
continue
try:
shutil.rmtree(asset_dir)
bk_logger.info("deleted asset dir: %s", asset_dir)
except Exception as err:
e = sys.exc_info()[0]
bk_logger.error("%s - %s", e, err)
def get_clean_filepath():
script_path = os.path.dirname(os.path.realpath(__file__))
subpath = "blendfiles" + os.sep + "cleaned.blend"
cp = os.path.join(script_path, subpath)
return cp
def get_thumbnailer_filepath():
script_path = os.path.dirname(os.path.realpath(__file__))
# fpath = os.path.join(p, subpath)
subpath = "blendfiles" + os.sep + "thumbnailer.blend"
return os.path.join(script_path, subpath)
def get_material_thumbnailer_filepath():
script_path = os.path.dirname(os.path.realpath(__file__))
# fpath = os.path.join(p, subpath)
subpath = "blendfiles" + os.sep + "material_thumbnailer_cycles.blend"
return os.path.join(script_path, subpath)
"""
for p in bpy.utils.script_paths():
testfname= os.path.join(p, subpath)#p + '%saddons%sobject_fracture%sdata.blend' % (s,s,s)
if os.path.isfile( testfname):
fname=testfname
return(fname)
return None
"""
def get_addon_file(subpath=""):
script_path = os.path.dirname(os.path.realpath(__file__))
# fpath = os.path.join(p, subpath)
return os.path.join(script_path, subpath)
script_path = os.path.dirname(os.path.realpath(__file__))
# cache this for minor performance boost
@lru_cache(maxsize=128)
def get_addon_thumbnail_path(name):
global script_path
# fpath = os.path.join(p, subpath)
ext = name.split(".")[-1]
next = ""
if not (ext == "jpg" or ext == "png"): # already has ext?
next = ".jpg"
subpath = f"thumbnails{os.sep}{name}{next}"
return os.path.join(script_path, subpath)
# cache this for minor performance boost
@lru_cache(maxsize=128)
def icon_path_exists(path: str) -> bool:
"""Cached version of os.path.exists"""
return os.path.exists(path)
def get_config_dir_path() -> str:
"""Get the path to the config directory in global_dir."""
global_dir = bpy.context.preferences.addons[__package__].preferences.global_dir # type: ignore
directory = os.path.join(global_dir, "config")
return os.path.abspath(directory)
def ensure_config_dir_exists():
"""Ensure that the config directory exists."""
config_dir = get_config_dir_path()
if not os.path.exists(config_dir):
os.makedirs(config_dir)
return config_dir
def open_path_in_file_browser(dir_path):
"""Open the path in the file browser."""
if sys.platform == "win32":
os.startfile(dir_path)
elif sys.platform == "darwin":
subprocess.Popen(["open", dir_path])
else:
subprocess.Popen(["xdg-open", dir_path])