Cleanup: minor wording clarification for OBJ import
[blender-addons.git] / archipack / archipack_rendering.py
blobf8bb07f9d115a544d9d0399672a96aaf0b74a7f0
1 # -*- coding:utf-8 -*-
3 # ##### BEGIN GPL LICENSE BLOCK #####
5 # This program is free software; you can redistribute it and/or
6 # modify it under the terms of the GNU General Public License
7 # as published by the Free Software Foundation; either version 2
8 # of the License, or (at your option) any later version.
10 # This program is distributed in the hope that it will be useful,
11 # but WITHOUT ANY WARRANTY; without even the implied warranty of
12 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 # GNU General Public License for more details.
15 # You should have received a copy of the GNU General Public License
16 # along with this program; if not, write to the Free Software Foundation,
17 # Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110- 1301, USA.
19 # ##### END GPL LICENSE BLOCK #####
21 # <pep8 compliant>
23 # ----------------------------------------------------------
24 # support routines for render measures in final image
25 # Author: Antonio Vazquez (antonioya)
26 # Archipack adaptation by : Stephen Leger (s-leger)
28 # ----------------------------------------------------------
29 # noinspection PyUnresolvedReferences
30 import bpy
31 # noinspection PyUnresolvedReferences
32 import bgl
33 from shutil import copyfile
34 from os import path, listdir
35 import subprocess
36 # noinspection PyUnresolvedReferences
37 import bpy_extras.image_utils as img_utils
38 # noinspection PyUnresolvedReferences
39 from math import ceil
40 from bpy.types import Operator
41 # from bl_ui import properties_render
44 class ARCHIPACK_OT_render_thumbs(Operator):
45 bl_idname = "archipack.render_thumbs"
46 bl_label = "Render presets thumbs"
47 bl_description = "Setup default presets and update thumbs"
48 bl_options = {'REGISTER', 'INTERNAL'}
50 @classmethod
51 def poll(cls, context):
52 # Ensure CYCLES engine is available
53 # hasattr(context.scene, 'cycles')
54 return context.scene
56 def background_render(self, context, cls, preset):
57 generator = path.dirname(path.realpath(__file__)) + path.sep + "archipack_thumbs.py"
58 addon_name = __name__.split('.')[0]
59 matlib_path = context.preferences.addons[addon_name].preferences.matlib_path
60 # Run external instance of blender like the original thumbnail generator.
61 cmd = [
62 bpy.app.binary_path,
63 "--background",
64 "--factory-startup",
65 "-noaudio",
66 # "--addons", addon_name,
67 "--python", generator,
68 "--",
69 "addon:" + addon_name,
70 "matlib:" + matlib_path,
71 "cls:" + cls,
72 "preset:" + preset
75 # print(" ".join(cmd))
77 popen = subprocess.Popen(cmd, stdout=subprocess.PIPE, universal_newlines=True)
78 for stdout_line in iter(popen.stdout.readline, ""):
79 yield stdout_line
80 popen.stdout.close()
81 popen.wait()
83 def copy_to_user_path(self, category):
84 """
85 Copy factory presets to writeable presets folder
86 Two cases here:
87 1 there is not presets thumbs found (official version)
88 2 thumbs already are there (unofficial)
89 """
90 file_list = []
91 # load default presets
92 dir_path = path.dirname(path.realpath(__file__))
93 sub_path = "presets" + path.sep + category
94 source_path = path.join(dir_path, sub_path)
95 if path.exists(source_path):
96 file_list.extend([f
97 for f in listdir(source_path)
98 if (f.endswith('.py') or f.endswith('.txt')) and
99 not f.startswith('.')])
101 target_path = path.join("presets", category)
102 presets_path = bpy.utils.user_resource('SCRIPTS', path=target_path, create=True)
103 # files from factory not found in user doesn't require a recompute
104 skipfiles = []
105 for f in file_list:
106 # copy python/txt preset
107 if not path.exists(presets_path + path.sep + f):
108 copyfile(source_path + path.sep + f, presets_path + path.sep + f)
110 # skip txt files (material presets)
111 if f.endswith(".txt"):
112 skipfiles.append(f)
114 # when thumbs already are in factory folder but not found in user one
115 # simply copy them, and add preset to skip list
116 thumb_filename = f[:-3] + ".png"
117 if path.exists(source_path + path.sep + thumb_filename):
118 if not path.exists(presets_path + path.sep + thumb_filename):
119 copyfile(source_path + path.sep + thumb_filename, presets_path + path.sep + thumb_filename)
120 skipfiles.append(f)
122 return skipfiles
124 def scan_files(self, category):
125 file_list = []
127 # copy from factory to user writeable folder
128 skipfiles = self.copy_to_user_path(category)
130 # load user def presets
131 preset_paths = bpy.utils.script_paths(subdir="presets")
132 for preset in preset_paths:
133 presets_path = path.join(preset, category)
134 if path.exists(presets_path):
135 file_list += [presets_path + path.sep + f[:-3]
136 for f in listdir(presets_path)
137 if f.endswith('.py') and
138 not f.startswith('.') and
139 f not in skipfiles]
141 file_list.sort()
142 return file_list
144 def rebuild_thumbs(self, context):
145 file_list = []
146 dir_path = path.dirname(path.realpath(__file__))
147 sub_path = "presets"
148 presets_path = path.join(dir_path, sub_path)
149 # print(presets_path)
150 if path.exists(presets_path):
151 dirs = listdir(presets_path)
152 for dir in dirs:
153 abs_dir = path.join(presets_path, dir)
154 if path.isdir(abs_dir):
155 files = self.scan_files(dir)
156 file_list.extend([(dir, file) for file in files])
158 ttl = len(file_list)
159 for i, preset in enumerate(file_list):
160 dir, file = preset
161 cls = dir[10:]
162 # context.scene.archipack_progress = (100 * i / ttl)
163 log_all = False
164 for l in self.background_render(context, cls, file + ".py"):
165 if "[log]" in l:
166 print(l[5:].strip())
167 elif "blender.crash" in l:
168 print("Unexpected error")
169 log_all = True
170 if log_all:
171 print(l.strip())
173 def invoke(self, context, event):
174 addon_name = __name__.split('.')[0]
175 matlib_path = context.preferences.addons[addon_name].preferences.matlib_path
177 if matlib_path == '':
178 self.report({'WARNING'}, "You should setup a default material library path in addon preferences")
179 return context.window_manager.invoke_confirm(self, event)
181 def execute(self, context):
182 # context.scene.archipack_progress_text = 'Generating thumbs'
183 # context.scene.archipack_progress = 0
184 self.rebuild_thumbs(context)
185 # context.scene.archipack_progress = -1
186 return {'FINISHED'}
189 def register():
190 bpy.utils.register_class(ARCHIPACK_OT_render_thumbs)
193 def unregister():
194 bpy.utils.unregister_class(ARCHIPACK_OT_render_thumbs)