-
Notifications
You must be signed in to change notification settings - Fork 0
/
rgba_tools.py
228 lines (184 loc) · 8.02 KB
/
rgba_tools.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
bl_info = {
"name": "RGBa Tools",
"author": "Henri Hebeisen, RGBa",
"version": (1, 0),
"blender": (2, 83, 0),
"location": "View3D >T Menu",
"description": "Various tools to help production of RGba's project",
"warning": "",
"doc_url": "",
"category": "Object",
}
import bpy
from bpy.types import Operator,AddonPreferences,Panel,PropertyGroup
from bpy.props import StringProperty,PointerProperty,EnumProperty
from os import listdir,mkdir,path,stat
from os.path import isfile, join,basename,dirname,exists
import json
import datetime
class RGBaAddonPreferences(AddonPreferences):
bl_idname = __name__
godot_project_folder: StringProperty(
name="Godot source folder",
default='',
subtype='DIR_PATH'
)
user_export_categories: StringProperty(
name="Export Categories (separated by a coma)",
default='props, chars, level'
)
props_prefix : StringProperty(
name="Props instance prefix",
default='p_'
)
def get_items(self, context):
l = [];
# for cat in user_export_categories.split(','):
# cat = cat.strip()
# l.append((cat, cat, cat))
l.append(('props','Props',""))
l.append(('chars','Chars',""))
l.append(('level','Level',""))
return l;
categories :EnumProperty( items = get_items)
def draw(self, context):
layout = self.layout
layout.prop(self, "godot_project_folder")
layout.prop(self, "props_prefix")
#layout.prop(self, "export_categories")
def get_addon_prefs(context):
preferences = context.preferences
return preferences.addons[__name__].preferences
def get_full_path(self,context):
preferences = context.preferences
addon_prefs = preferences.addons[__name__].preferences
godot_folder = bpy.path.abspath(addon_prefs.godot_project_folder)
godot_cat = addon_prefs.categories
folder_name = bpy.path.basename(bpy.context.blend_data.filepath)[:-6]
file_name = f"{folder_name}.glb"
return godot_folder,godot_cat,folder_name,file_name
#Methods
def export_selected_collection(self,context):
godot_folder,godot_cat,folder_name,file_name = get_full_path(self,context)
if not exists(join(godot_folder,godot_cat)):
mkdir(join(godot_folder,godot_cat))
print(f"The folder {godot_cat} was created")
if not exists(join(godot_folder,godot_cat,folder_name)):
mkdir(join(godot_folder,godot_cat,folder_name))
print(f"The folder {folder_name} was created")
export_path = join(godot_folder,godot_cat,folder_name,file_name)
to_export = context.scene.godot_collection_export
self.report({'WARNING'}, f'The collection to export is {export_path}')
collection_to_export = context.scene.godot_collection_export
current_selection = context.selected_objects #we store the current selection
bpy.ops.object.select_all(action='DESELECT')
col = context.scene.collection.children[collection_to_export]
props_prefix = get_addon_prefs(context).props_prefix
for obj in col.objects:
if obj.type == 'EMPTY' and obj.name.startswith(props_prefix):
print('On a trouvé un EMpty !!')
obj.instance_type = 'NONE'
obj.select_set(True)
bpy.ops.export_scene.gltf(export_format='GLB',
ui_tab='GENERAL',
export_copyright="RGba",
export_image_format='AUTO',
export_texture_dir="",
export_texcoords=True,
export_normals=True,
export_draco_mesh_compression_enable=False,
export_tangents=False,
export_materials=True,
export_colors=True,
export_cameras=False,
export_selected=False,
use_selection=True,
export_extras=False,
export_yup=True,
export_apply=False,
export_animations=True,
export_frame_range=True,
export_frame_step=1,
export_force_sampling=True,
export_nla_strips=True,
export_def_bones=False,
export_current_frame=False,
export_skins=True,
export_all_influences=False,
export_morph=False,
export_lights=False,
export_displacement=False,
will_save_settings=False,
filepath=export_path,
check_existing=False)
# We restore instances dupligroup
for obj in col.objects:
if obj.type == 'EMPTY' and obj.name.startswith(props_prefix):
obj.instance_type = 'COLLECTION'
bpy.ops.object.select_all(action='DESELECT')
for obj in current_selection:
obj.select_set(True)
class RGBA_PT_export_collection_operator(Operator):
"""Export the chosen collection to godot folder"""
bl_idname = "rgba.export_collection"
bl_label = "Export Collection"
bl_options = {'REGISTER', 'UNDO'}
def execute(self, context):
export_selected_collection(self,context)
return {'FINISHED'}
# User Interface
class RGBA_PT_export_to_godot(Panel):
"""Export a whole collection as a glb file to godot"""
bl_label = "Export to Godot"
bl_category = "RGBa"
bl_idname = "RGBA_PT_export_to_godot"
bl_space_type = "VIEW_3D"
bl_region_type = "UI"
bl_context = "objectmode"
bl_options = {'DEFAULT_CLOSED'}
def draw(self, context):
layout = self.layout
scene = context.scene
preferences = context.preferences
addon_prefs = preferences.addons[__name__].preferences
layout.prop_search(scene, "godot_collection_export",
bpy.data,
"collections",
text="Collection to export")
row = layout.row(align=True)
layout.prop(addon_prefs,"categories")
row = layout.row(align=True)
godot_folder,godot_cat,folder_name,file_name = get_full_path(self,context)
if not bpy.data.is_saved:
row.label(text='Please save the file before exporting it !',
icon='FILE_BLEND')
elif not godot_folder:
row.label(text="Please setup the godot folder in the addon preferences.",
icon="ERROR")
else:
filename = bpy.path.basename(bpy.context.blend_data.filepath)[:-6]
row = layout.row(align=True)
row.operator("rgba.export_collection",icon="EXPORT")
row = layout.row(align=True)
full_path = join(godot_folder,godot_cat,folder_name,file_name)
if exists(full_path):
info = stat(full_path)
row.label(text=f"File {full_path} already exists and will overwritten !",
icon="INFO")
row = layout.row(align=True)
row.label(text=f"File was last modified on :{datetime.datetime.fromtimestamp(info.st_mtime).strftime('%Y-%m-%d-%H:%M')}")
else:
row.label(text=f"File will be saved at {full_path}.")
# Registration
def register():
bpy.utils.register_class(RGBA_PT_export_collection_operator)
bpy.utils.register_class(RGBA_PT_export_to_godot)
bpy.utils.register_class(RGBaAddonPreferences)
bpy.types.Scene.godot_collection_export = StringProperty()
def unregister():
bpy.utils.unregister_class(RGBA_PT_export_collection_operator)
bpy.utils.unregister_class(RGBA_PT_export_to_godot)
bpy.utils.unregister_class(RGBaAddonPreferences)
del bpy.types.Scene.godot_collection_export
if __name__ == "__main__":
register()