Removed bundled brush pack
[blender-addons.git] / blenderkit / autothumb_material_bg.py
blobc8d3497922935bf6337c2c490d5c48874c557919
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 #####
20 if "bpy" in locals():
21 from importlib import reload
23 utils = reload(utils)
24 append_link = reload(append_link)
25 bg_blender = reload(bg_blender)
26 else:
27 from blenderkit import utils, append_link, bg_blender
29 import sys, json, math
30 import bpy
31 from pathlib import Path
33 BLENDERKIT_EXPORT_TEMP_DIR = sys.argv[-1]
34 BLENDERKIT_THUMBNAIL_PATH = sys.argv[-2]
35 BLENDERKIT_EXPORT_FILE_INPUT = sys.argv[-3]
36 BLENDERKIT_EXPORT_DATA = sys.argv[-4]
39 def render_thumbnails():
40 bpy.ops.render.render(write_still=True, animation=False)
43 def unhide_collection(cname):
44 collection = bpy.context.scene.collection.children[cname]
45 collection.hide_viewport = False
46 collection.hide_render = False
47 collection.hide_select = False
50 if __name__ == "__main__":
51 try:
52 bg_blender.progress('preparing thumbnail scene')
53 with open(BLENDERKIT_EXPORT_DATA, 'r') as s:
54 data = json.load(s)
55 # append_material(file_name, matname = None, link = False, fake_user = True)
56 mat = append_link.append_material(file_name=BLENDERKIT_EXPORT_FILE_INPUT, matname=data["material"], link=True,
57 fake_user=False)
59 user_preferences = bpy.context.preferences.addons['blenderkit'].preferences
61 s = bpy.context.scene
63 colmapdict = {
64 'BALL': 'Ball',
65 'BALL_COMPLEX': 'Ball complex',
66 'FLUID': 'Fluid',
67 'CLOTH': 'Cloth',
68 'HAIR': 'Hair'
71 unhide_collection(colmapdict[data["thumbnail_type"]])
72 if data['thumbnail_background']:
73 unhide_collection('Background')
74 bpy.data.materials["bg checker colorable"].node_tree.nodes['input_level'].outputs['Value'].default_value \
75 = data['thumbnail_background_lightness']
76 tscale = data["thumbnail_scale"]
77 bpy.context.view_layer.objects['scaler'].scale = (tscale, tscale, tscale)
78 bpy.context.view_layer.update()
79 for ob in bpy.context.visible_objects:
80 if ob.name[:15] == 'MaterialPreview':
81 ob.material_slots[0].material = mat
82 ob.data.use_auto_texspace = False
83 ob.data.texspace_size.x = 1 / tscale
84 ob.data.texspace_size.y = 1 / tscale
85 ob.data.texspace_size.z = 1 / tscale
86 if data["adaptive_subdivision"] == True:
87 ob.cycles.use_adaptive_subdivision = True
89 else:
90 ob.cycles.use_adaptive_subdivision = False
91 ts = data['texture_size_meters']
92 if data["thumbnail_type"] in ['BALL', 'BALL_COMPLEX', 'CLOTH']:
93 utils.automap(ob.name, tex_size = ts / tscale, just_scale = True, bg_exception=True)
94 bpy.context.view_layer.update()
96 s.cycles.volume_step_size = tscale * .1
98 if user_preferences.thumbnail_use_gpu:
99 bpy.context.scene.cycles.device = 'GPU'
101 s.cycles.samples = data['thumbnail_samples']
102 bpy.context.view_layer.cycles.use_denoising = data['thumbnail_denoising']
104 # import blender's HDR here
105 hdr_path = Path('datafiles/studiolights/world/interior.exr')
106 bpath = Path(bpy.utils.resource_path('LOCAL'))
107 ipath = bpath / hdr_path
108 ipath = str(ipath)
110 # this stuff is for mac and possibly linux. For blender // means relative path.
111 # for Mac, // means start of absolute path
112 if ipath.startswith('//'):
113 ipath = ipath[1:]
115 img = bpy.data.images['interior.exr']
116 img.filepath = ipath
117 img.reload()
119 bpy.context.scene.render.resolution_x = int(data['thumbnail_resolution'])
120 bpy.context.scene.render.resolution_y = int(data['thumbnail_resolution'])
122 bpy.context.scene.render.filepath = BLENDERKIT_THUMBNAIL_PATH
123 bg_blender.progress('rendering thumbnail')
124 render_thumbnails()
125 bg_blender.progress('background autothumbnailer finished successfully')
128 except Exception as e:
129 print(e)
130 import traceback
132 traceback.print_exc()
134 sys.exit(1)