2020from bpy .utils import register_class , unregister_class
2121from subprocess import Popen
2222from os import system , path , makedirs , sep
23- import os , sys
24- import distutils .dir_util
23+ from distutils import dir_util
2524import json
2625from pathlib import Path as pathlib_path
2726
28- _preferences = {};
27+ _preferences = {}
2928
30- def dks_ue_get_export_path ():
31-
29+ def dks_ue_get_export_sub ():
30+
3231 global _preferences
33-
32+
3433 _file_name = bpy .context .blend_data .filepath
3534 _file_path = _file_name [0 :len (_file_name )- len (bpy .path .basename (_file_name ))]
3635 _file_sub = _file_path [len (_preferences ["option_ue_src" ]):]
36+
37+ return _file_sub
38+
39+ def dks_ue_get_export_path ():
40+
41+ global _preferences
42+
43+ _file_sub = dks_ue_get_export_sub ()
3744 _export_path = _preferences ["option_ue_dst" ]+ _file_sub
3845
3946 if not path .exists (_export_path ):
@@ -47,9 +54,6 @@ def dks_ue_filename(self, context):
4754 _export_path = dks_ue_get_export_path ()
4855 _export_file = _export_path + _object_name + '.fbx'
4956
50- if not bpy .context .preferences .addons [__package__ ].preferences .option_save_before_export :
51- bpy .ops .wm .save_mainfile ()
52-
5357 return _export_file
5458
5559
@@ -92,7 +96,7 @@ def dks_ue_folder_crawl(directory):
9296
9397 found = ""
9498
95- path_check = os . path .join (directory , "blender_addon_ue.json" )
99+ path_check = path .join (directory , "blender_addon_ue.json" )
96100
97101 if path .exists (path_check ):
98102 found = path_check
@@ -102,7 +106,25 @@ def dks_ue_folder_crawl(directory):
102106 found = dks_ue_folder_crawl (path_parent )
103107
104108 return found
105- #
109+
110+ def dks_ue_get_file_name ():
111+
112+ return bpy .path .basename (bpy .context .blend_data .filepath ).replace ('.blend' ,'' )
113+
114+ def dks_ue_get_texture_file (texture_path ,mesh_name ,mat_name ,texture_name ,texture_ext ):
115+
116+ if path .exists (texture_path + mesh_name + '_' + mat_name + '_' + texture_name + '.' + texture_ext ):
117+ return texture_path + mesh_name + '_' + mat_name + '_' + texture_name + '.' + texture_ext
118+ elif path .exists (texture_path + mat_name + '_' + texture_name + '.' + texture_ext ):
119+ return texture_path + mat_name + '_' + texture_name + '.' + texture_ext
120+ else :
121+ _blend = dks_ue_get_file_name ()
122+ if path .exists (texture_path + _blend + '_' + mesh_name + '_' + texture_name + '.' + texture_ext ):
123+ return texture_path + _blend + '_' + mesh_name + '_' + texture_name + '.' + texture_ext
124+ elif path .exists (texture_path + _blend + '_' + mat_name + '_' + texture_name + '.' + texture_ext ):
125+ return texture_path + _blend + '_' + mat_name + '_' + texture_name + '.' + texture_ext
126+ else :
127+ return ""
106128
107129class dks_ue_export (bpy .types .Operator ):
108130
@@ -114,13 +136,14 @@ def execute(self, context):
114136
115137 global _preferences
116138
139+ if bpy .context .preferences .addons [__package__ ].preferences .option_save_before_export :
140+ bpy .ops .wm .save_mainfile ()
141+
117142 _file_name = bpy .context .blend_data .filepath
118143 _file_path = _file_name [0 :len (_file_name )- len (bpy .path .basename (_file_name ))]
119144
120145 _file_json = dks_ue_folder_crawl (_file_path )
121146
122- print ("_file_json:" ,_file_json )
123-
124147 if _file_json != "" :
125148
126149 try :
@@ -164,6 +187,9 @@ def execute(self, context):
164187 if "option_textures_folder" not in _preferences :
165188 _preferences ["option_textures_folder" ]= bpy .context .preferences .addons [__package__ ].preferences .option_textures_folder
166189
190+ if "option_ue_json" not in _preferences :
191+ _preferences ["option_ue_json" ]= bpy .context .preferences .addons [__package__ ].preferences .option_ue_json
192+
167193 else :
168194
169195 _preferences ["option_ue_src" ]= bpy .context .preferences .addons [__package__ ].preferences .option_ue_src
@@ -176,21 +202,72 @@ def execute(self, context):
176202 _preferences ["option_camera_rotation" ] = (math .radians (bpy .context .preferences .addons [__package__ ].preferences .option_camera_rotation [0 ]),math .radians (bpy .context .preferences .addons [__package__ ].preferences .option_camera_rotation [1 ]),math .radians (bpy .context .preferences .addons [__package__ ].preferences .option_camera_rotation [2 ]))
177203 _preferences ["option_copy_textures" ]= bpy .context .preferences .addons [__package__ ].preferences .option_copy_textures
178204 _preferences ["option_textures_folder" ]= bpy .context .preferences .addons [__package__ ].preferences .option_textures_folder
205+ _preferences ["option_ue_json" ]= bpy .context .preferences .addons [__package__ ].preferences .option_ue_json
179206
180207 if _preferences ["option_create_icon" ]:
181208
182209 screenshot (self , context )
183210
211+ _export_file = dks_ue_fbx_export (self , context )
212+
184213 if _preferences ["option_copy_textures" ]:
185214
186215 _path_textures_from = bpy .path .abspath ('//' ) + _preferences ["option_textures_folder" ] + sep
187216 _path_textures_to = dks_ue_get_export_path () + _preferences ["option_textures_folder" ] + sep
188217
189218 if path .exists (_path_textures_from ):
190- distutils .dir_util .copy_tree (_path_textures_from , _path_textures_to )
191-
192- _export_file = dks_ue_fbx_export (self , context )
193-
219+ dir_util .copy_tree (_path_textures_from , _path_textures_to )
220+
221+ blender_data = {}
222+ blender_data ['path' ]= dks_ue_get_export_sub ().replace ("\\ " ,"/" )
223+ blender_data ['materials' ] = []
224+
225+ _objects = bpy .context .scene .objects
226+ _texture_ext = "png"
227+
228+ for _obj in _objects :
229+
230+ if _obj .type == 'MESH' :
231+
232+ _obj_name = _obj .name
233+
234+ _materials = _obj .data .materials
235+
236+ for _material in _materials :
237+
238+ _material_name = _material .name
239+
240+ _material_data = {}
241+ _material_data ["name" ]= _material_name
242+
243+ _file_Base_Color = dks_ue_get_texture_file (_path_textures_from ,_obj_name ,_material_name ,'Base_Color' ,_texture_ext )
244+ if _file_Base_Color == "" :
245+ _file_Base_Color = dks_ue_get_texture_file (_path_textures_from ,_obj_name ,_material_name ,'BaseColor' ,_texture_ext )
246+ _file_Ambient_occlusion = dks_ue_get_texture_file (_path_textures_from ,_obj_name ,_material_name ,'Ambient_occlusion' ,_texture_ext )
247+ _file_Metallic = dks_ue_get_texture_file (_path_textures_from ,_obj_name ,_material_name ,'Metallic' ,_texture_ext )
248+ _file_Roughness = dks_ue_get_texture_file (_path_textures_from ,_obj_name ,_material_name ,'Roughness' ,_texture_ext )
249+ _file_ORM = dks_ue_get_texture_file (_path_textures_from ,_obj_name ,_material_name ,'OcclusionRoughnessMetallic' ,_texture_ext )
250+ _file_Opacity = dks_ue_get_texture_file (_path_textures_from ,_obj_name ,_material_name ,'Opacity' ,_texture_ext )
251+ _file_Normal = dks_ue_get_texture_file (_path_textures_from ,_obj_name ,_material_name ,'Normal' ,_texture_ext )
252+ if _file_Base_Color == "" :
253+ _file_Normal = dks_ue_get_texture_file (_path_textures_from ,_obj_name ,_material_name ,'Normal_OpenGL' ,_texture_ext )
254+
255+ _material_data ['base_color' ]= _file_Base_Color [len (bpy .path .abspath ('//' )):].replace ("\\ " ,"/" ).replace ("." + _texture_ext ,"" )
256+ _material_data ['normal' ]= _file_Normal [len (bpy .path .abspath ('//' )):].replace ("\\ " ,"/" ).replace ("." + _texture_ext ,"" )
257+ _material_data ['orm' ]= _file_ORM [len (bpy .path .abspath ('//' )):].replace ("\\ " ,"/" ).replace ("." + _texture_ext ,"" )
258+ _material_data ['opacity' ]= _file_Opacity [len (bpy .path .abspath ('//' )):].replace ("\\ " ,"/" ).replace ("." + _texture_ext ,"" )
259+ _material_data ['ambient_occlusion' ]= _file_Ambient_occlusion [len (bpy .path .abspath ('//' )):].replace ("\\ " ,"/" ).replace ("." + _texture_ext ,"" )
260+ _material_data ['metallic' ]= _file_Metallic [len (bpy .path .abspath ('//' )):].replace ("\\ " ,"/" ).replace ("." + _texture_ext ,"" )
261+ _material_data ['roughness' ]= _file_Roughness [len (bpy .path .abspath ('//' )):].replace ("\\ " ,"/" ).replace ("." + _texture_ext ,"" )
262+
263+ blender_data ['materials' ].append (_material_data )
264+
265+ json_data = json .dumps (blender_data , sort_keys = False , indent = 3 )
266+ json_data_filename = dks_ue_get_export_path ()+ 'blender_addon_ue_data.json'
267+
268+ with open (json_data_filename , 'w' ) as f :
269+ json .dump (blender_data , f )
270+
194271 return {'FINISHED' }
195272
196273classes = (
0 commit comments