def start_material_thumbnailer(self=None, json_args=None, props=None, wait=False, add_bg_process=True): ''' Parameters ---------- self json_args - all arguments: props - blenderkit upload props with thumbnail settings, to communicate back, if not present, not used. wait - wait for the rendering to finish Returns ------- ''' if props: props.is_generating_thumbnail = True props.thumbnail_generating_state = 'starting blender instance' binary_path = bpy.app.binary_path script_path = os.path.dirname(os.path.realpath(__file__)) tfpath = paths.get_material_thumbnailer_filepath() datafile = os.path.join(json_args['tempdir'], BLENDERKIT_EXPORT_DATA_FILE) try: with open(datafile, 'w', encoding='utf-8') as s: json.dump(json_args, s, ensure_ascii=False, indent=4) proc = subprocess.Popen([ binary_path, "--background", "-noaudio", tfpath, "--python", os.path.join(script_path, "autothumb_material_bg.py"), "--", datafile, ], bufsize=1, stdout=subprocess.PIPE, stdin=subprocess.PIPE, creationflags=utils.get_process_flags()) eval_path_computing = "bpy.data.materials['%s'].blenderkit.is_generating_thumbnail" % json_args['asset_name'] eval_path_state = "bpy.data.materials['%s'].blenderkit.thumbnail_generating_state" % json_args['asset_name'] eval_path = "bpy.data.materials['%s']" % json_args['asset_name'] bg_blender.add_bg_process(name=f"{json_args['asset_name']} thumbnailer", eval_path_computing=eval_path_computing, eval_path_state=eval_path_state, eval_path=eval_path, process_type='THUMBNAILER', process=proc) if props: props.thumbnail_generating_state = 'Saving .blend file' if wait: while proc.poll() is None: stdout_data, stderr_data = proc.communicate() print(stdout_data) except Exception as e: if self: self.report({'WARNING'}, "Error while packing file: %s" % str(e)) else: print(e) return {'FINISHED'}
def start_material_thumbnailer(self, context): # Prepare to save the file mat = bpy.context.active_object.active_material mat.blenderkit.is_generating_thumbnail = True mat.blenderkit.thumbnail_generating_state = 'starting blender instance' binary_path = bpy.app.binary_path script_path = os.path.dirname(os.path.realpath(__file__)) basename, ext = os.path.splitext(bpy.data.filepath) if not basename: basename = os.path.join(basename, "temp") if not ext: ext = ".blend" asset_name = mat.name tempdir = tempfile.mkdtemp() file_dir = os.path.dirname(bpy.data.filepath) thumb_path = os.path.join(file_dir, asset_name) rel_thumb_path = os.path.join('//', mat.name) i = 0 while os.path.isfile(thumb_path + '.png'): thumb_path = os.path.join(file_dir, mat.name + '_' + str(i).zfill(4)) rel_thumb_path = os.path.join('//', mat.name + '_' + str(i).zfill(4)) i += 1 filepath = os.path.join(tempdir, "material_thumbnailer_cycles" + ext) tfpath = paths.get_material_thumbnailer_filepath() datafile = os.path.join(tempdir, BLENDERKIT_EXPORT_DATA_FILE) try: # save a copy of actual scene but don't interfere with the users models bpy.ops.wm.save_as_mainfile(filepath=filepath, compress=False, copy=True) with open(datafile, 'w') as s: bkit = mat.blenderkit json.dump( { "type": "material", "material": mat.name, "thumbnail_type": bkit.thumbnail_generator_type, "thumbnail_scale": bkit.thumbnail_scale, "thumbnail_background": bkit.thumbnail_background, "thumbnail_background_lightness": bkit.thumbnail_background_lightness, "thumbnail_resolution": bkit.thumbnail_resolution, "thumbnail_samples": bkit.thumbnail_samples, "thumbnail_denoising": bkit.thumbnail_denoising, "adaptive_subdivision": bkit.adaptive_subdivision, "texture_size_meters": bkit.texture_size_meters, }, s) proc = subprocess.Popen([ binary_path, "--background", "-noaudio", tfpath, "--python", os.path.join(script_path, "autothumb_material_bg.py"), "--", datafile, filepath, thumb_path, tempdir ], bufsize=1, stdout=subprocess.PIPE, stdin=subprocess.PIPE, creationflags=utils.get_process_flags()) eval_path_computing = "bpy.data.materials['%s'].blenderkit.is_generating_thumbnail" % mat.name eval_path_state = "bpy.data.materials['%s'].blenderkit.thumbnail_generating_state" % mat.name eval_path = "bpy.data.materials['%s']" % mat.name bg_blender.add_bg_process(eval_path_computing=eval_path_computing, eval_path_state=eval_path_state, eval_path=eval_path, process_type='THUMBNAILER', process=proc) mat.blenderkit.thumbnail = rel_thumb_path + '.png' mat.blenderkit.thumbnail_generating_state = 'Saving .blend file' except Exception as e: self.report({'WARNING'}, "Error while packing file: %s" % str(e)) return {'FINISHED'}
def start_material_thumbnailer(self, context): # Prepare to save the file mat = bpy.context.active_object.active_material mat.blenderkit.is_generating_thumbnail = True mat.blenderkit.thumbnail_generating_state = 'starting blender instance' binary_path = bpy.app.binary_path script_path = os.path.dirname(os.path.realpath(__file__)) basename, ext = os.path.splitext(bpy.data.filepath) if not basename: basename = os.path.join(basename, "temp") if not ext: ext = ".blend" asset_name = mat.name tempdir = tempfile.mkdtemp() file_dir = os.path.dirname(bpy.data.filepath) thumb_path = os.path.join(file_dir, asset_name) rel_thumb_path = os.path.join('//', mat.name) i = 0 while os.path.isfile(thumb_path + '.png'): thumb_path = os.path.join(file_dir, mat.name + '_' + str(i).zfill(4)) rel_thumb_path = os.path.join('//', mat.name + '_' + str(i).zfill(4)) i += 1 filepath = os.path.join(tempdir, "material_thumbnailer_cycles" + ext) tfpath = paths.get_material_thumbnailer_filepath() datafile = os.path.join(tempdir, BLENDERKIT_EXPORT_DATA_FILE) try: # save a copy of actual scene but don't interfere with the users models bpy.ops.wm.save_as_mainfile(filepath=filepath, compress=False, copy=True) with open(datafile, 'w') as s: bkit = mat.blenderkit json.dump({ "type": "material", "material": mat.name, "thumbnail_type": bkit.thumbnail_generator_type, "thumbnail_scale": bkit.thumbnail_scale, "thumbnail_background": bkit.thumbnail_background, "thumbnail_background_lightness": bkit.thumbnail_background_lightness, "thumbnail_samples": bkit.thumbnail_samples, "thumbnail_denoising": bkit.thumbnail_denoising, "adaptive_subdivision": bkit.adaptive_subdivision, "texture_size_meters": bkit.texture_size_meters, }, s) flags = BELOW_NORMAL_PRIORITY_CLASS if sys.platform != 'win32': # TODO test this on windows flags = 0 proc = subprocess.Popen([ binary_path, "--background", "-noaudio", tfpath, "--python", os.path.join(script_path, "autothumb_material_bg.py"), "--", datafile, filepath, thumb_path, tempdir ], bufsize=1, stdout=subprocess.PIPE, stdin=subprocess.PIPE, creationflags=flags) eval_path_computing = "bpy.data.materials['%s'].blenderkit.is_generating_thumbnail" % mat.name eval_path_state = "bpy.data.materials['%s'].blenderkit.thumbnail_generating_state" % mat.name eval_path = "bpy.data.materials['%s']" % mat.name bg_blender.add_bg_process(eval_path_computing=eval_path_computing, eval_path_state=eval_path_state, eval_path=eval_path, process_type='THUMBNAILER', process=proc) mat.blenderkit.thumbnail = rel_thumb_path + '.png' mat.blenderkit.thumbnail_generating_state = 'Saving .blend file' except Exception as e: self.report({'WARNING'}, "Error while packing file: %s" % str(e)) return {'FINISHED'}