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 #####
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
31 # noinspection PyUnresolvedReferences
33 from shutil
import copyfile
34 from os
import path
, listdir
36 # noinspection PyUnresolvedReferences
37 import bpy_extras
.image_utils
as img_utils
38 # noinspection PyUnresolvedReferences
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'}
51 def poll(cls
, context
):
52 # Ensure CYCLES engine is available
53 # hasattr(context.scene, 'cycles')
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.
66 # "--addons", addon_name,
67 "--python", generator
,
69 "addon:" + addon_name
,
70 "matlib:" + matlib_path
,
75 # print(" ".join(cmd))
77 popen
= subprocess
.Popen(cmd
, stdout
=subprocess
.PIPE
, universal_newlines
=True)
78 for stdout_line
in iter(popen
.stdout
.readline
, ""):
83 def copy_to_user_path(self
, category
):
85 Copy factory presets to writeable presets folder
87 1 there is not presets thumbs found (official version)
88 2 thumbs already are there (unofficial)
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
):
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
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"):
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
)
124 def scan_files(self
, category
):
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
144 def rebuild_thumbs(self
, context
):
146 dir_path
= path
.dirname(path
.realpath(__file__
))
148 presets_path
= path
.join(dir_path
, sub_path
)
149 # print(presets_path)
150 if path
.exists(presets_path
):
151 dirs
= listdir(presets_path
)
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
])
159 for i
, preset
in enumerate(file_list
):
162 # context.scene.archipack_progress = (100 * i / ttl)
164 for l
in self
.background_render(context
, cls
, file + ".py"):
167 elif "blender.crash" in l
:
168 print("Unexpected error")
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
190 bpy
.utils
.register_class(ARCHIPACK_OT_render_thumbs
)
194 bpy
.utils
.unregister_class(ARCHIPACK_OT_render_thumbs
)