-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathSubstanceExporter.py
201 lines (165 loc) · 7.92 KB
/
SubstanceExporter.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
import bpy
import os
import subprocess
bl_info = {
"name": "Auto exporter",
"author": "Radovan Stastny <[email protected]>",
"version": (1, 0),
"blender": (2, 85, 0),
"category": "Import-Export",
"doc_url": "https://www.youtube.com/watch?v=dQw4w9WgXcQ&ab_channel=RickAstley",
"location": "View3D > Side Panel > Auto export",
"description": "Addon to streamline blender->substance workflow",
}
# UI
# -----------------------------------------------------------------------------------------------------------------------#
class ExampleAddonPreferences(bpy.types.AddonPreferences):
bl_idname = __name__
substance_path_pref: bpy.props.StringProperty(
name="Substance path",
subtype='FILE_PATH',
default='C:/Users/radov/OneDrive/Plocha/3d_veci/Substance/Hra/FBX/'
)
SubstanceFoldersPath_pref: bpy.props.StringProperty(
name="substance folder path",
subtype='FILE_PATH',
default='C:/Users/radov/OneDrive/Plocha/3d_veci/shooter/Substance/'
)
Steampath_pref: bpy.props.StringProperty(
name="steam.exe location",
subtype='FILE_PATH',
default='C:\Program Files (x86)\Steam\Steam.exe'
)
AppID_pref: bpy.props.StringProperty(
name="Steam app ID",
default='1775390'
)
def draw(self, context):
layout = self.layout
layout.label(text="This is the path where FBX files for substance project will be saved")
layout.prop(self, "substance_path_pref")
layout.label(text="This is the path where folder for youre substance textures will be created")
layout.prop(self, "SubstanceFoldersPath_pref")
layout.label(text="Location of steam.exe")
layout.prop(self, "Steampath_pref")
layout.label(text="ID of your substance version, you can find this in steam link")
layout.prop(self, "AppID_pref")
class VIEW3D_PT_AUTOEXPORT(bpy.types.Panel):
bl_space_type = 'VIEW_3D'
bl_region_type = 'UI'
bl_category = "Auto export"
bl_label = "Auto export"
def draw(self, context):
filename = bpy.path.basename(bpy.context.blend_data.filepath).removesuffix(".blend")
col = self.layout.column(align=True)
export_prop_grp = context.window_manager.export_prop_grp
col.label(text="Export Name")
col.prop(export_prop_grp, "use_file_name_as_name")
col.prop(export_prop_grp, "export_name")
if export_prop_grp.use_file_name_as_name == True:
if filename == "":
export_prop_grp.export_name = "untitled"
else:
export_prop_grp.export_name = filename
col.label(text="")
col.label(text="Preferences:")
col.prop(export_prop_grp, "substance_open_type")
col.prop(export_prop_grp, "substance_folder")
col.label(text="")
col.prop(export_prop_grp, "create_in_folder")
if export_prop_grp.create_in_folder ==True:
col.prop(export_prop_grp, "name_of_folder")
else:
export_prop_grp.name_of_folder = filename
col.prop(export_prop_grp, "only_selected")
col.label(text="")
col.operator('mesh.substance_export', icon='EXPORT')
#Props
#-----------------------------------------------------------------------------------------------------------------------#
class ExportPropertyGroup(bpy.types.PropertyGroup):
substance_open_type: bpy.props.EnumProperty(
name="Method",
items=[
('1', "Create Project", "Will create project from exported mesh"),
('0', "Dont create project and rewrite", "Will just overwrite fbx file"),
],
default='1',
)
only_selected: bpy.props.BoolProperty(name="Export selected", default=True, description="Export just selection")
use_file_name_as_name: bpy.props.BoolProperty(name="Filename as name", default=True, description="Can use custom name")
export_name: bpy.props.StringProperty(name="Name", default="Asset",
description="Name of export")
substance_folder: bpy.props.BoolProperty(name="Open substance folder", default=True, description="Will open folder where textures will be exported")
create_in_folder: bpy.props.BoolProperty(name="Export to new folder", default=False, description="Create custom folder for export")
name_of_folder: bpy.props.StringProperty(name="Name", default="Asset",
description="Name of the custom folder")
class MESH_OT_AUTO_export(bpy.types.Operator):
"""export to substance"""
bl_idname = 'mesh.substance_export'
bl_label = "Substance Export"
bl_options = {'REGISTER', 'UNDO', 'UNDO_GROUPED', }
# Checking if it is possible to perform operator
@classmethod
def poll(cls, context):
export_prop_grp = context.window_manager.export_prop_grp
objs = context.selected_objects
if len(objs) != 0 or not export_prop_grp.only_selected:
current_mode = bpy.context.object.mode
return context.area.type == 'VIEW_3D' and current_mode == 'OBJECT'
else:
return False
def execute(self, context):
export_prop_grp = context.window_manager.export_prop_grp
if export_prop_grp.export_name=="":
self.report({'ERROR'}, "Bych pojmenoval mesh idk")
return {'CANCELLED'}
preferences = context.preferences
addon_prefs = preferences.addons[__name__].preferences
substance_path = addon_prefs.substance_path_pref
if export_prop_grp.create_in_folder:
substance_path = substance_path + export_prop_grp.name_of_folder+"/"
if not os.path.exists(substance_path):
os.makedirs(substance_path)
finalpath = substance_path + export_prop_grp.export_name + ".fbx"
bpy.ops.export_scene.fbx(filepath=finalpath,use_selection=export_prop_grp.only_selected,mesh_smooth_type='FACE')
# Substance cary mary
if export_prop_grp.substance_open_type=='1':
SubstanceExportPath = addon_prefs.SubstanceFoldersPath_pref + export_prop_grp.export_name
if not os.path.exists(SubstanceExportPath):
os.makedirs(SubstanceExportPath)
if export_prop_grp.substance_folder:
subprocess.run('explorer ' + SubstanceExportPath.replace("/","\\"))
steamPath = addon_prefs.Steampath_pref
command = '\"'+steamPath+'\" -applaunch \"'+ addon_prefs.AppID_pref +'\" --mesh ' + finalpath + " --export-path " + SubstanceExportPath
subprocess.run(command,shell=True)
return {'FINISHED'}
# ---------------------------------------------------------------------------------------------------------------------#
# Adds function to F3 search
def menu_func_origin(self, context):
self.layout.operator(MESH_OT_AUTO_export.bl_idname, icon='OBJECT_ORIGIN')
# --------------------------------------------------------------------------------------------------------------------#
# Registration
def register():
# UI
bpy.utils.register_class(VIEW3D_PT_AUTOEXPORT)
# operators
bpy.utils.register_class(MESH_OT_AUTO_export)
# F3 menu
bpy.types.VIEW3D_MT_edit_mesh.append(menu_func_origin)
# Properties
bpy.utils.register_class(ExportPropertyGroup)
bpy.types.WindowManager.export_prop_grp = bpy.props.PointerProperty(type=ExportPropertyGroup)
# Addon preferences
bpy.utils.register_class(ExampleAddonPreferences)
def unregister():
# UI
bpy.utils.unregister_class(VIEW3D_PT_AUTOEXPORT)
# Operators
bpy.utils.unregister_class(MESH_OT_AUTO_export)
# F3 menu
bpy.types.VIEW3D_MT_edit_mesh.remove(menu_func_origin)
# Properties
bpy.utils.unregister_class(ExportPropertyGroup)
del bpy.types.WindowManager.export_prop_grp
# Addon preferences
bpy.utils.unregister_class(ExampleAddonPreferences)