2222from os import system , path , makedirs , sep
2323import os , sys
2424import distutils .dir_util
25+ import json
26+ from pathlib import Path as pathlib_path
27+
28+ _preferences = {};
2529
2630def dks_ue_get_export_path ():
2731
32+ global _preferences
33+
2834 _file_name = bpy .context .blend_data .filepath
2935 _file_path = _file_name [0 :len (_file_name )- len (bpy .path .basename (_file_name ))]
30- _file_sub = _file_path [len (bpy . context . preferences . addons [ "blender_addon_ue" ]. preferences . option_ue_src ):]
31- _export_path = bpy . context . preferences . addons [ "blender_addon_ue" ]. preferences . option_ue_dst + _file_sub
36+ _file_sub = _file_path [len (_preferences [ "option_ue_src" ] ):]
37+ _export_path = _preferences [ "option_ue_dst" ] + _file_sub
3238
3339 if not path .exists (_export_path ):
3440 makedirs (_export_path )
@@ -49,22 +55,23 @@ def dks_ue_filename(self, context):
4955
5056def screenshot (self , context ):
5157
58+ global _preferences
59+
5260 _object_name = bpy .path .basename (bpy .context .blend_data .filepath ).replace ('.blend' ,'' )
5361 _object_file = dks_ue_get_export_path () + _object_name + '_Icon.png'
54-
55- if bpy .context .preferences .addons [__package__ ].preferences .option_override_camera :
5662
57- bpy .context .scene .camera .location = bpy .context .preferences .addons [__package__ ].preferences .option_camera_location
58- bpy .context .scene .camera .rotation_euler = bpy .context .preferences .addons [__package__ ].preferences .option_camera_rotation
63+ if _preferences ["option_override_camera" ]:
5964
65+ bpy .context .scene .camera .location = _preferences ["option_camera_location" ]
66+ bpy .context .scene .camera .rotation_euler = _preferences ["option_camera_rotation" ]
6067
6168 bpy .ops .render .render (use_viewport = True )
6269
6370 bpy .data .scenes ["Scene" ].render .filepath = _object_file
6471 bpy .context .scene .render .image_settings .file_format = 'PNG'
6572 bpy .context .scene .render .filepath = _object_file
66- bpy .context .scene .render .resolution_x = bpy . context . preferences . addons [ __package__ ]. preferences . option_icon_resolution_x
67- bpy .context .scene .render .resolution_y = bpy . context . preferences . addons [ __package__ ]. preferences . option_icon_resolution_y
73+ bpy .context .scene .render .resolution_x = _preferences [ " option_icon_resolution_x" ]
74+ bpy .context .scene .render .resolution_y = _preferences [ " option_icon_resolution_y" ]
6875 bpy .context .scene .render .resolution_percentage = 100
6976
7077 bpy .context .scene .cycles .film_transparent = True
@@ -81,22 +88,103 @@ def dks_ue_fbx_export(self, context):
8188
8289 return _export_file
8390
91+ def dks_ue_folder_crawl (directory ):
92+
93+ found = ""
94+
95+ path_check = os .path .join (directory , "blender_addon_ue.json" )
96+
97+ if path .exists (path_check ):
98+ found = path_check
99+ else :
100+ path_parent = pathlib_path (directory ).parent
101+ if path_parent != directory :
102+ found = dks_ue_folder_crawl (path_parent )
103+
104+ return found
105+ #
106+
84107class dks_ue_export (bpy .types .Operator ):
85108
86109 bl_idname = "dks_ue.export"
87110 bl_label = "UE"
88111 bl_description = "Export to UE"
89112
90113 def execute (self , context ):
91-
92- if bpy .context .preferences .addons [__package__ ].preferences .option_create_icon :
114+
115+ global _preferences
116+
117+ _file_name = bpy .context .blend_data .filepath
118+ _file_path = _file_name [0 :len (_file_name )- len (bpy .path .basename (_file_name ))]
119+
120+ _file_json = dks_ue_folder_crawl (_file_path )
121+
122+ print ("_file_json:" ,_file_json )
123+
124+ if _file_json != "" :
125+
126+ try :
127+ with open (_file_json ) as _file_json_data :
128+ _preferences = json .load (_file_json_data )
129+ except Exception as e :
130+ print ("blender_addon_ue.json is invalid: " ,str (e .reason ))
131+ return {'FINISHED' }
132+
133+ if "option_ue_src" not in _preferences :
134+ _preferences ["option_ue_src" ]= bpy .context .preferences .addons [__package__ ].preferences .option_ue_src
135+
136+ if "option_ue_dst" not in _preferences :
137+ _preferences ["option_ue_dst" ]= bpy .context .preferences .addons [__package__ ].preferences .option_ue_dst
138+
139+ if "option_create_icon" not in _preferences :
140+ _preferences ["option_create_icon" ]= bpy .context .preferences .addons [__package__ ].preferences .option_create_icon
141+
142+ if "option_override_camera" not in _preferences :
143+ _preferences ["option_override_camera" ]= bpy .context .preferences .addons [__package__ ].preferences .option_override_camera
144+
145+ if "option_icon_resolution_x" not in _preferences :
146+ _preferences ["option_icon_resolution_x" ]= bpy .context .preferences .addons [__package__ ].preferences .option_icon_resolution_x
147+
148+ if "option_icon_resolution_y" not in _preferences :
149+ _preferences ["option_icon_resolution_y" ]= bpy .context .preferences .addons [__package__ ].preferences .option_icon_resolution_y
150+
151+ if "option_camera_location" in _preferences :
152+ _preferences ["option_camera_location" ]= (_preferences ["option_camera_location" ]["x" ],_preferences ["option_camera_location" ]["y" ],_preferences ["option_camera_location" ]["z" ])
153+ else :
154+ _preferences ["option_camera_location" ] = bpy .context .preferences .addons [__package__ ].preferences .option_camera_location
155+
156+ if "option_camera_rotation" in _preferences :
157+ _preferences ["option_camera_rotation" ]= (math .radians (_preferences ["option_camera_rotation" ]["x" ]),math .radians (_preferences ["option_camera_rotation" ]["y" ]),math .radians (_preferences ["option_camera_rotation" ]["z" ]))
158+ else :
159+ _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 ]))
160+
161+ if "option_create_icon" not in _preferences :
162+ _preferences ["option_create_icon" ]= bpy .context .preferences .addons [__package__ ].preferences .option_create_icon
163+
164+ if "option_textures_folder" not in _preferences :
165+ _preferences ["option_textures_folder" ]= bpy .context .preferences .addons [__package__ ].preferences .option_textures_folder
166+
167+ else :
168+
169+ _preferences ["option_ue_src" ]= bpy .context .preferences .addons [__package__ ].preferences .option_ue_src
170+ _preferences ["option_ue_dst" ]= bpy .context .preferences .addons [__package__ ].preferences .option_ue_dst
171+ _preferences ["option_create_icon" ]= bpy .context .preferences .addons [__package__ ].preferences .option_create_icon
172+ _preferences ["option_override_camera" ]= bpy .context .preferences .addons [__package__ ].preferences .option_override_camera
173+ _preferences ["option_icon_resolution_x" ]= bpy .context .preferences .addons [__package__ ].preferences .option_icon_resolution_x
174+ _preferences ["option_icon_resolution_y" ]= bpy .context .preferences .addons [__package__ ].preferences .option_icon_resolution_y
175+ _preferences ["option_camera_location" ] = bpy .context .preferences .addons [__package__ ].preferences .option_camera_location
176+ _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 ]))
177+ _preferences ["option_copy_textures" ]= bpy .context .preferences .addons [__package__ ].preferences .option_copy_textures
178+ _preferences ["option_textures_folder" ]= bpy .context .preferences .addons [__package__ ].preferences .option_textures_folder
179+
180+ if _preferences ["option_create_icon" ]:
93181
94182 screenshot (self , context )
95183
96- if bpy . context . preferences . addons [ __package__ ]. preferences . option_copy_textures :
184+ if _preferences [ " option_copy_textures" ] :
97185
98- _path_textures_from = bpy .path .abspath ('//' ) + "Textures" + sep
99- _path_textures_to = dks_ue_get_export_path () + "Textures" + sep
186+ _path_textures_from = bpy .path .abspath ('//' ) + _preferences [ "option_textures_folder" ] + sep
187+ _path_textures_to = dks_ue_get_export_path () + _preferences [ "option_textures_folder" ] + sep
100188
101189 if path .exists (_path_textures_from ):
102190 distutils .dir_util .copy_tree (_path_textures_from , _path_textures_to )
0 commit comments