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 #####
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")
40 # bpy.app.debug_value = 2
41 d
= bpy
.app
.debug_value
44 url
= BLENDERKIT_LOCAL
46 url
= BLENDERKIT_DEVEL
52 def find_in_local(text
=''):
54 for p
, d
, f
in os
.walk('.'):
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
):
89 if subdir
is not None:
90 tempdir
= os
.path
.join(tempdir
, subdir
)
91 if not os
.path
.exists(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
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
):
110 subdirs
= ['brushes', 'textures', 'models', 'scenes', 'materials']
112 subdir
= os
.path
.join(ddir
, subd
)
113 if not os
.path
.exists(subdir
):
115 if subdmapping
[asset_type
] == subd
:
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
):
125 subdirs
= ['textures', 'models', 'scenes', 'materials'] # brushes get stored only globally.
127 subdir
= os
.path
.join(ddir
, subd
)
128 if not os
.path
.exists(subdir
):
130 if subdmapping
[asset_type
] == subd
:
138 Normalizes string, converts to lowercase, removes non-alpha characters,
139 and converts spaces to hyphens.
141 import unicodedata
, re
143 slug
= slug
.replace('.', '_')
144 slug
= slug
.replace('"', '')
145 slug
= slug
.replace(' ', '_')
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
)
155 def extract_filename_from_url(url
):
157 imgname
= url
.split('/')[-1]
158 imgname
= imgname
.split('?')[0]
163 def get_download_filenames(asset_data
):
164 dirs
= get_download_dirs(asset_data
['asset_type'])
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
174 # strs = (n, asset_data['name'], asset_data['file_name'])
176 file_name
= os
.path
.join(d
, n
)
177 file_names
.append(file_name
)
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
)
190 if os
.path
.isfile(f
):
195 e
= sys
.exc_info()[0]
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
)
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):
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]
240 if not (ext
== 'jpg' or ext
== 'png'): # already has ext?
242 subpath
= "thumbnails" + os
.sep
+ name
+ next
243 return os
.path
.join(script_path
, subpath
)