-
Notifications
You must be signed in to change notification settings - Fork 9
/
__init__.py
388 lines (316 loc) · 14.8 KB
/
__init__.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
import bpy, glob, re, subprocess, os, traceback
from collections import defaultdict
from pathlib import Path
bl_info = {
'name': 'Substance Import-Export Tools',
'version': (1, 3, 25),
'author': 'passivestar',
'blender': (4, 1, 0),
'location': '3D View N Panel',
'description': 'Simplifies Export to Substance Painter',
'category': 'Import-Export'
}
# @Util
def detect_substance_painter_path():
paths = []
current_os = os.name
if current_os == 'posix':
# MacOS
paths.extend([
f'/Applications/Adobe Substance 3D Painter.app/Contents/MacOS/Adobe Substance 3D Painter',
f'/Applications/Adobe Substance 3D Painter/Adobe Substance 3D Painter.app/Contents/MacOS/Adobe Substance 3D Painter',
f'~/Library/Application Support/Steam/steamapps/common/Substance 3D Painter/Adobe Substance 3D Painter.app/Contents/MacOS/Adobe Substance 3D Painter'
])
# MacOS with year
for year in range(2020, 2026):
paths.extend([
f'/Applications/Adobe Substance 3D Painter {year}.app/Contents/MacOS/Adobe Substance 3D Painter',
f'/Applications/Adobe Substance 3D Painter/Adobe Substance 3D Painter {year}.app/Contents/MacOS/Adobe Substance 3D Painter',
f'~/Library/Application Support/Steam/steamapps/common/Substance 3D Painter {year}/Adobe Substance 3D Painter.app/Contents/MacOS/Adobe Substance 3D Painter'
])
elif current_os == 'nt':
# Windows
for letter in 'CDEFGHIJKLMNOPQRSTUVWXYZ':
paths.extend([
# CC
f'{letter}:\\Program Files\\Adobe\\Adobe Substance 3D Painter\\Adobe Substance 3D Painter.exe',
f'{letter}:\\Program Files (x86)\\Adobe\\Adobe Substance 3D Painter\\Adobe Substance 3D Painter.exe',
# Steam without 3D
f'{letter}:\\Program Files\\Steam\\steamapps\\common\\Substance Painter\\Adobe Substance 3D Painter.exe'
f'{letter}:\\Program Files (x86)\\Steam\\steamapps\\common\\Substance Painter\\Adobe Substance 3D Painter.exe'
# Steam with 3D
f'{letter}:\\Program Files\\Steam\\steamapps\\common\\Substance 3D Painter\\Adobe Substance 3D Painter.exe'
f'{letter}:\\Program Files (x86)\\Steam\\steamapps\\common\\Substance 3D Painter\\Adobe Substance 3D Painter.exe'
])
# Windows with year
for year in range(2020, 2026):
paths.extend([
# CC
f'{letter}:\\Program Files\\Adobe\\Adobe Substance 3D Painter {year}\\Adobe Substance 3D Painter.exe',
f'{letter}:\\Program Files (x86)\\Adobe\\Adobe Substance 3D Painter {year}\\Adobe Substance 3D Painter.exe',
# Steam without 3D
f'{letter}:\\Program Files\\Steam\\steamapps\\common\\Substance Painter {year}\\Adobe Substance 3D Painter.exe'
f'{letter}:\\Program Files (x86)\\Steam\\steamapps\\common\\Substance Painter {year}\\Adobe Substance 3D Painter.exe'
# Steam with 3D
f'{letter}:\\Program Files\\Steam\\steamapps\\common\\Substance 3D Painter {year}\\Adobe Substance 3D Painter.exe'
f'{letter}:\\Program Files (x86)\\Steam\\steamapps\\common\\Substance 3D Painter {year}\\Adobe Substance 3D Painter.exe'
])
# Check each path for the current operating system and return the first one that exists
for path in paths:
path = os.path.expanduser(path)
try:
if Path(path).exists():
return path
except Exception as e:
pass
# If none of the paths exist, return an empty string
return ''
def material_needs_setup(material):
if material.node_tree is None:
return False
if len(material.node_tree.nodes) == 2:
return True
return False
# Mock data for testing through blender text editor without installing
mocks = {
'painter_path': detect_substance_painter_path(),
'textures_path': ''
}
def get_paths(context):
textures_path = get_preferences(context)["textures_path"]
if textures_path == '':
textures_path = Path(bpy.path.abspath('//'))
else:
textures_path = Path(textures_path)
collection_name_clean = re.sub(r'[^a-zA-Z0-9_]', '_', bpy.context.view_layer.active_layer_collection.name)
textures_path_for_collection = textures_path.joinpath('textures_' + collection_name_clean + '/')
fbx_path = textures_path_for_collection.joinpath(collection_name_clean + '.fbx')
spp_path = textures_path_for_collection.joinpath(collection_name_clean + '.spp')
return {
'fbx': fbx_path,
'spp': spp_path,
'directory': textures_path_for_collection,
'collection_name_clean': collection_name_clean
}
def get_preferences(context):
if __name__ == '__main__':
return mocks
else:
prefs = context.preferences.addons[__name__].preferences
return {
'painter_path': prefs.painter_path,
'textures_path': prefs.textures_path
}
def object_has_material(obj):
return len(obj.data.materials) > 0 and obj.data.materials[0] is not None
def create_material_for_object(obj):
material = bpy.data.materials.new(name=obj.name)
material.use_nodes = True
material.node_tree.nodes.clear()
principled_bsdf = material.node_tree.nodes.new('ShaderNodeBsdfPrincipled')
material_output = material.node_tree.nodes.new('ShaderNodeOutputMaterial')
material.node_tree.links.new(principled_bsdf.outputs['BSDF'], material_output.inputs['Surface'])
if len(obj.data.materials) > 0:
obj.data.materials[0] = material
else:
obj.data.materials.append(material)
# @Operators
class ExportToSubstancePainterOperator(bpy.types.Operator):
"""Export Collection to Substance Painter. Press Ctrl+Shift+R in Painter to reload after re-export"""
bl_idname, bl_label = 'st.open_in_substance_painter', 'Export Collection to Substance Painter'
run_painter: bpy.props.BoolProperty(name='Run Substance Painter', default=True)
def execute(self, context):
preferences = get_preferences(context)
painter_path = preferences["painter_path"]
paths = get_paths(context)
directory = paths['directory']
fbx = paths['fbx']
spp = paths['spp']
if bpy.data.filepath == '':
self.report({'ERROR'}, 'File is not saved. Please save your blend file')
return {'FINISHED'}
for o in bpy.context.view_layer.active_layer_collection.collection.objects:
# Check if the object is mesh:
if o.type != 'MESH':
self.report({'ERROR'}, f'Object {o.name} is not a mesh')
return {'FINISHED'}
# Check if the object has a material and create if necessary:
if not object_has_material(o):
create_material_for_object(o)
if not directory.exists():
directory.mkdir(parents=True, exist_ok=True)
# Export FBX
bpy.ops.wm.save_mainfile()
bpy.ops.export_scene.fbx(
mesh_smooth_type='EDGE',
use_mesh_modifiers=True,
add_leaf_bones=False,
apply_scale_options='FBX_SCALE_ALL',
bake_anim_use_nla_strips=False,
bake_space_transform=True,
use_active_collection=True,
filepath=str(fbx)
)
# If we only need to export the fbx, we're done
if not self.run_painter:
return {'FINISHED'}
if painter_path == '':
self.report({'ERROR'}, 'Please specify Substance Painter path in addon preferences')
return {'FINISHED'}
# Check if preferences.painter_path exists
if not Path(painter_path).exists():
self.report({'ERROR'}, 'Substance Painter path is not valid. Please set the corrent path to Substance Painter in addon preferences')
return {'FINISHED'}
# Check if a mac .app and add the executable part automatically first
if os.name == 'posix' and painter_path.endswith('.app'):
painter_path = painter_path + '/Contents/MacOS/Adobe Substance 3D Painter'
# Display an error message if the path is a directory
if os.path.isdir(painter_path):
self.report({'ERROR'}, 'Substance Painter path is set to a directory. Please set it to the executable file')
return {'FINISHED'}
try:
if os.name == 'nt':
subprocess.Popen([painter_path, '--mesh', fbx, '--export-path', directory, spp])
else:
subprocess.Popen(f'"{painter_path}" --mesh "{fbx}" --export-path "{directory}" "{spp}"', shell=True)
except Exception as e:
self.report({'ERROR'}, f'Error opening Substance Painter: {e}')
return {'FINISHED'}
return {'FINISHED'}
class LoadSubstancePainterTexturesOperator(bpy.types.Operator):
"""Load Substance Painter Textures"""
bl_idname, bl_label, bl_options = 'st.load_substance_painter_textures', 'Load Substance Painter Textures', {'REGISTER', 'UNDO'}
def execute(self, context):
preferences = get_preferences(context)
paths = get_paths(context)
directory = paths['directory']
# Check that node wrangler is enabled
if 'node_wrangler' not in bpy.context.preferences.addons:
self.report({'ERROR'}, 'Node Wrangler needs to be enabled! Please enable it in Edit -> Preferences -> Add-ons')
return {'FINISHED'}
# All of the materials in the blend file
material_names = [material.name for material in bpy.data.materials]
# Reload all of the unique images in materials of the current collection
unique_images = set()
for obj in bpy.context.view_layer.active_layer_collection.collection.objects:
if obj.type == 'MESH' and len(obj.data.materials) > 0:
for material in obj.data.materials:
if material is not None and material.use_nodes:
for node in material.node_tree.nodes:
if node.bl_idname == 'ShaderNodeTexImage' and node.image:
unique_images.add(node.image)
for image in unique_images:
image.reload()
# Return if the file is not save
if bpy.data.filepath == '':
self.report({'ERROR'}, 'File is not saved')
return {'FINISHED'}
# Return if the texture folder doesn't exist
if not directory.exists():
self.report({'ERROR'}, 'There is no texture folder')
return {'FINISHED'}
# Return if there are no materials in the scene
if len(bpy.data.materials) == 0:
self.report({'ERROR'}, 'There are no materials in the scene')
return {'FINISHED'}
# Iterate through all of the files and group them by texture set name (material)
texture_sets = defaultdict(list)
material_names = sorted([material.name for material in bpy.data.materials if material_needs_setup(material)], key=len, reverse=True)
for texture_file in directory.iterdir():
# If texture_file is not a common texture file extension, skip it
if texture_file.suffix not in ['.png', '.jpg', '.jpeg', '.tga', '.tif', '.tiff', '.bmp', '.exr']:
continue
for material_name in material_names:
if material_name in texture_file.name:
texture_sets[material_name].append(texture_file.name)
break
# Create an empty mesh object with an empty material slot and set it as active
# This is needed to be able to use the shader editor to assign textures with node wrangler
previous_active_object = context.view_layer.objects.active
temp_mesh = bpy.data.meshes.new(name="TempMesh")
temp_obj = bpy.data.objects.new(name="TempObject", object_data=temp_mesh)
temp_obj.data.materials.append(None)
context.scene.collection.objects.link(temp_obj)
context.view_layer.objects.active = temp_obj
# Set area type to node editor
previous_context = context.area.type
context.area.type = 'NODE_EDITOR'
context.area.ui_type = 'ShaderNodeTree'
# Try catch to make sure that the context is ALWAYS returned to the previous one
# Otherwise the UI may break
try:
# For all of the texture sets that have a material with matching name add nodes via node wrangler
for texture_set_name, texture_file_names in texture_sets.items():
if texture_set_name in material_names:
# Set node editor to current material
material = bpy.data.materials[texture_set_name]
context.object.data.materials[0] = material
context.space_data.node_tree = material.node_tree
# Select the Principled BSDF node
for node in context.space_data.node_tree.nodes:
if node.bl_idname == 'ShaderNodeBsdfPrincipled':
context.space_data.node_tree.nodes.active = node
break
# Add textures to node tree using node wrangler
directory = str(directory) + os.sep
files = [{'name':n} for n in texture_file_names]
bpy.ops.node.nw_add_textures_for_principled(directory=directory, files=files)
except Exception as e:
tb = traceback.format_exc()
self.report({'ERROR'}, f'Error occurred while adding textures: {e}\n{tb}')
finally:
context.area.type = previous_context
context.view_layer.objects.active = previous_active_object
bpy.data.objects.remove(temp_obj)
return {'FINISHED'}
# @UI
class SubstanceToolsPanel(bpy.types.Panel):
"""Substance Tools Panel"""
bl_idname = 'SCENE_PT_substance_tools'
bl_label = 'Substance Painter Tools'
bl_space_type = 'VIEW_3D'
bl_region_type = 'UI'
bl_category = 'Substance'
def draw(self, context):
layout = self.layout
paths = get_paths(context)
fbx = paths['fbx']
collection_name_clean = paths['collection_name_clean']
fbx_exists = Path(fbx).exists()
box_column = layout.box().column(align=True)
if collection_name_clean == 'Scene_Collection':
box_column.label(text='Select a collection in the outliner')
else:
box_column.label(text=f'Collection: {collection_name_clean}')
box_column.separator()
column = box_column.column(align=True)
column.operator('st.open_in_substance_painter', text=f'Export', icon='EXPORT').run_painter = False
column.operator('st.open_in_substance_painter', text=f'Export and Open in Painter', icon='WINDOW').run_painter = True
if fbx_exists:
# Load textures button
if 'node_wrangler' in bpy.context.preferences.addons:
column.operator('st.load_substance_painter_textures', text='Load Painter Textures', icon='IMPORT')
else:
column.label(text='Node Wrangler addon needs to be enabled!')
column.label(text='Please enable it in Edit -> Preferences -> Add-ons')
# @Preferences
class SubstanceToolsPreferences(bpy.types.AddonPreferences):
bl_idname = __name__
painter_path: bpy.props.StringProperty(name='Substance Painter Executable', default=detect_substance_painter_path(), subtype='FILE_PATH')
textures_path: bpy.props.StringProperty(name='Export Path (Blank for blend file path)', default='', subtype='DIR_PATH')
def draw(self, context):
layout = self.layout
layout.prop(self, 'painter_path')
layout.prop(self, 'textures_path')
# @Register
classes = (
ExportToSubstancePainterOperator,
LoadSubstancePainterTexturesOperator,
SubstanceToolsPanel,
SubstanceToolsPreferences
)
def register():
for c in classes: bpy.utils.register_class(c)
def unregister():
for c in classes: bpy.utils.unregister_class(c)
if __name__ == '__main__': register()