sun_position: fix warning from deleted prop in User Preferences
[blender-addons.git] / blenderkit / paths.py
blob112e2465b907bca8d9be20a2df86b6838f7852fa
1 # ##### BEGIN GPL LICENSE BLOCK #####
3 # This program is free software; you can redistribute it and/or
4 # modify it under the terms of the GNU General Public License
5 # as published by the Free Software Foundation; either version 2
6 # of the License, or (at your option) any later version.
8 # This program is distributed in the hope that it will be useful,
9 # but WITHOUT ANY WARRANTY; without even the implied warranty of
10 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
11 # GNU General Public License for more details.
13 # You should have received a copy of the GNU General Public License
14 # along with this program; if not, write to the Free Software Foundation,
15 # Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
17 # ##### END GPL LICENSE BLOCK #####
19 import bpy, os, sys
21 _presets = os.path.join(bpy.utils.user_resource('SCRIPTS'), "presets")
22 BLENDERKIT_LOCAL = "http://localhost:8001"
23 BLENDERKIT_MAIN = "https://www.blenderkit.com"
24 BLENDERKIT_DEVEL = "https://devel.blenderkit.com"
25 BLENDERKIT_API = "/api/v1/"
26 BLENDERKIT_REPORT_URL = "usage_report/"
27 BLENDERKIT_USER_ASSETS = "/my-assets"
28 BLENDERKIT_PLANS = "https://www.blenderkit.com/plans/pricing/"
29 BLENDERKIT_MANUAL = "https://youtu.be/1hVgcQhIAo8"
30 BLENDERKIT_MODEL_UPLOAD_INSTRUCTIONS_URL = "https://www.blenderkit.com/docs/upload/"
31 BLENDERKIT_MATERIAL_UPLOAD_INSTRUCTIONS_URL = "https://www.blenderkit.com/docs/uploading-material/"
32 BLENDERKIT_BRUSH_UPLOAD_INSTRUCTIONS_URL = "https://www.blenderkit.com/docs/uploading-brush/"
33 BLENDERKIT_LOGIN_URL = "https://www.blenderkit.com/accounts/login"
34 BLENDERKIT_OAUTH_LANDING_URL = "/oauth-landing/"
35 BLENDERKIT_SIGNUP_URL = "https://www.blenderkit.com/accounts/register"
36 BLENDERKIT_SETTINGS_FILENAME = os.path.join(_presets, "bkit.json")
39 def get_bkit_url():
40 # bpy.app.debug_value = 2
41 d = bpy.app.debug_value
42 # d = 2
43 if d == 1:
44 url = BLENDERKIT_LOCAL
45 elif d == 2:
46 url = BLENDERKIT_DEVEL
47 else:
48 url = BLENDERKIT_MAIN
49 return url
52 def find_in_local(text=''):
53 fs = []
54 for p, d, f in os.walk('.'):
55 for file in f:
56 if text in file:
57 fs.append(file)
58 return fs
61 def get_api_url():
62 return get_bkit_url() + BLENDERKIT_API
65 def get_oauth_landing_url():
66 return get_bkit_url() + BLENDERKIT_OAUTH_LANDING_URL
69 def default_global_dict():
70 from os.path import expanduser
71 home = expanduser("~")
72 return home + os.sep + 'blenderkit_data'
75 def get_categories_filepath():
76 tempdir = get_temp_dir()
77 return os.path.join(tempdir, 'categories.json')
80 def get_temp_dir(subdir=None):
81 user_preferences = bpy.context.preferences.addons['blenderkit'].preferences
83 # tempdir = user_preferences.temp_dir
84 tempdir = os.path.join(user_preferences.global_dir, 'temp')
85 if tempdir.startswith('//'):
86 tempdir = bpy.path.abspath(tempdir)
87 if not os.path.exists(tempdir):
88 os.makedirs(tempdir)
89 if subdir is not None:
90 tempdir = os.path.join(tempdir, subdir)
91 if not os.path.exists(tempdir):
92 os.makedirs(tempdir)
93 return tempdir
96 def get_download_dirs(asset_type):
97 ''' get directories where assets will be downloaded'''
98 subdmapping = {'brush': 'brushes', 'texture': 'textures', 'model': 'models', 'scene': 'scenes',
99 'material': 'materials'}
101 user_preferences = bpy.context.preferences.addons['blenderkit'].preferences
102 dirs = []
103 if user_preferences.directory_behaviour == 'BOTH' or 'GLOBAL':
104 ddir = user_preferences.global_dir
105 if ddir.startswith('//'):
106 ddir = bpy.path.abspath(ddir)
107 if not os.path.exists(ddir):
108 os.makedirs(ddir)
110 subdirs = ['brushes', 'textures', 'models', 'scenes', 'materials']
111 for subd in subdirs:
112 subdir = os.path.join(ddir, subd)
113 if not os.path.exists(subdir):
114 os.makedirs(subdir)
115 if subdmapping[asset_type] == subd:
116 dirs.append(subdir)
117 if (
118 user_preferences.directory_behaviour == 'BOTH' or user_preferences.directory_behaviour == '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.
119 ddir = user_preferences.project_subdir
120 if ddir.startswith('//'):
121 ddir = bpy.path.abspath(ddir)
122 if not os.path.exists(ddir):
123 os.makedirs(ddir)
125 subdirs = ['textures', 'models', 'scenes', 'materials'] # brushes get stored only globally.
126 for subd in subdirs:
127 subdir = os.path.join(ddir, subd)
128 if not os.path.exists(subdir):
129 os.makedirs(subdir)
130 if subdmapping[asset_type] == subd:
131 dirs.append(subdir)
133 return dirs
136 def slugify(slug):
138 Normalizes string, converts to lowercase, removes non-alpha characters,
139 and converts spaces to hyphens.
141 import unicodedata, re
142 slug = slug.lower()
143 slug = slug.replace('.', '_')
144 slug = slug.replace('"', '')
145 slug = slug.replace(' ', '_')
146 # import re
147 # slug = unicodedata.normalize('NFKD', slug)
148 # slug = slug.encode('ascii', 'ignore').lower()
149 slug = re.sub(r'[^a-z0-9]+.- ', '-', slug).strip('-')
150 slug = re.sub(r'[-]+', '-', slug)
151 slug = re.sub(r'/', '_', slug)
152 return slug
155 def extract_filename_from_url(url):
156 if url is not None:
157 imgname = url.split('/')[-1]
158 imgname = imgname.split('?')[0]
159 return imgname
160 return ''
163 def get_download_filenames(asset_data):
164 dirs = get_download_dirs(asset_data['asset_type'])
165 file_names = []
166 # fn = asset_data['file_name'].replace('blend_', '')
167 if asset_data.get('url') is not None:
168 # this means asset is already in scene and we don't need to check
170 fn = extract_filename_from_url(asset_data['url'])
171 fn.replace('_blend', '')
172 n = slugify(asset_data['name']) + '_' + fn
173 # n = 'x.blend'
174 # strs = (n, asset_data['name'], asset_data['file_name'])
175 for d in dirs:
176 file_name = os.path.join(d, n)
177 file_names.append(file_name)
178 return file_names
181 def delete_asset_debug(asset_data):
182 from blenderkit import download
183 user_preferences = bpy.context.preferences.addons['blenderkit'].preferences
184 api_key = user_preferences.api_key
186 download.get_download_url(asset_data, download.get_scene_id(), api_key)
188 file_names = get_download_filenames(asset_data)
189 for f in file_names:
190 if os.path.isfile(f):
191 try:
192 print(f)
193 os.remove(f)
194 except:
195 e = sys.exc_info()[0]
196 print(e)
197 pass;
200 def get_clean_filepath():
201 script_path = os.path.dirname(os.path.realpath(__file__))
202 subpath = "blendfiles" + os.sep + "cleaned.blend"
203 cp = os.path.join(script_path, subpath)
204 return cp
207 def get_thumbnailer_filepath():
208 script_path = os.path.dirname(os.path.realpath(__file__))
209 # fpath = os.path.join(p, subpath)
210 subpath = "blendfiles" + os.sep + "thumbnailer.blend"
211 return os.path.join(script_path, subpath)
214 def get_material_thumbnailer_filepath():
215 script_path = os.path.dirname(os.path.realpath(__file__))
216 # fpath = os.path.join(p, subpath)
217 subpath = "blendfiles" + os.sep + "material_thumbnailer_cycles.blend"
218 return os.path.join(script_path, subpath)
220 for p in bpy.utils.script_paths():
221 testfname= os.path.join(p, subpath)#p + '%saddons%sobject_fracture%sdata.blend' % (s,s,s)
222 if os.path.isfile( testfname):
223 fname=testfname
224 return(fname)
225 return None
229 def get_addon_file(subpath=''):
230 script_path = os.path.dirname(os.path.realpath(__file__))
231 # fpath = os.path.join(p, subpath)
232 return os.path.join(script_path, subpath)
235 def get_addon_thumbnail_path(name):
236 script_path = os.path.dirname(os.path.realpath(__file__))
237 # fpath = os.path.join(p, subpath)
238 ext = name.split('.')[-1]
239 next = ''
240 if not (ext == 'jpg' or ext == 'png'): # already has ext?
241 next = '.jpg'
242 subpath = "thumbnails" + os.sep + name + next
243 return os.path.join(script_path, subpath)