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