Correct - packed textures does not work. I gave you a zip of a blend file with textures packed. - So unpack the textures into a “textures” folder - then set the texture paths to Absolute.
File - External Data - Make Paths Absolute.
Then do an export. - result no textures in the PackagesSources / …/ texture folder.
Here is my fix - it is a Kludge code fix - but it works for me.
# region Textures
def export_gltf_textures(self, context, gltf_path, texture_dir):
if not os.path.exists(gltf_path):
return
gltf_dir_path = os.path.dirname(gltf_path)
#print("export_gltf_textures - texture_dir before abs", texture_dir, gltf_path, gltf_dir_path)
if not os.path.isabs(texture_dir):
texture_dir = os.path.join(gltf_dir_path, texture_dir)
texture_dir = os.path.abspath(texture_dir)
#print("export_gltf_textures - texture_dir abs", texture_dir)
if not os.path.exists(texture_dir):
try:
os.mkdir(texture_dir)
except OSError:
self.report({'ERROR'}, f"[TEXTURE] Folder {texture_dir} could not be created")
return
json_file_object = None
try:
with open(gltf_path, 'r', encoding="utf-8") as file:
json_file_object = json.load(file)
except IOError:
self.report({'ERROR'}, f"[TEXTURE] File '{gltf_path}' could not be opened. File access denied")
self.report({'ERROR'}, "[TEXTURE] Textures will not be written")
return
if json_file_object is None:
return
gltf_images = json_file_object.get("images")
if gltf_images is None:
return
# make a file hash for 4.2 - ronh
image_dict = {}
for im in bpy.data.images:
#print("export_gltf_textures - image", im, im.name, im.filepath)
if im.name != "Render Result":
image_dict[im.name] = im.filepath.replace('/', '\\')
for gltf_image in gltf_images:
# uri image path in Blender 4.2 is not same as Blender 3.6 - does not use relative path added - ronh
image_path = gltf_image.get("uri")
#print("export_gltf_textures - original image path found", gltf_image, image_path, image_dict[image_path])
if image_path is None:
print("export_gltf_textures - no image path found", gltf_image)
continue
# added the 4.2 check - ronh
if bpy.app.version < (4, 2, 0):
image_path = os.path.join(gltf_dir_path, image_path)
image_path = image_path.replace('/', '\\')
else:
#image_path = os.path.join(texture_dir, image_path)
# because there is no proper uri in 4.2+ - build texture_dir path
texture_path = os.path.join(gltf_dir_path, texture_dir)
image_path = os.path.join(texture_path, image_dict[image_path])
#print("export_gltf_textures - image_path before", texture_path, image_path)
image_path = os.path.abspath(image_path)
#print("export_gltf_textures - image_path after", image_path)
# Check if there is a whitespace and replace it in path
if '%20' in image_path:
image_path = image_path.replace('%20', ' ')
#print("export_gltf_textures \n", gltf_dir_path, texture_dir, image_path)
# commented out - ronh
#if not os.path.exists(image_path):
# print(f"[TEXTURE] File '{image_path}' do not exists")
# continue
image_name = os.path.basename(image_path)
new_image_path = os.path.join(texture_dir, image_name)
new_image_path = os.path.abspath(new_image_path)
#print("export_gltf_textures - new image path", image_path, new_image_path)
# Change texture path in gltf
if len(os.path.commonprefix([new_image_path, gltf_path])) != 0:
gltf_image['uri'] = os.path.relpath(path=new_image_path, start=gltf_dir_path)
# Copy image if the image not already exists in the folder
if _samefile(image_path, new_image_path):
print("export_gltf_textures - same file", image_path, new_image_path)
continue
if p4.us_p4():
p4.p4_edit(new_image_path)
copyfile(image_path, new_image_path)
print(f"[TEXTURE][{image_name}] Texture copied from '{image_path}' to '{new_image_path}'")
# to remove misplaced png files. I don't know why - kludge code - ronh
bad_image_path = os.path.join(gltf_dir_path, image_name)
bad_image_path = os.path.abspath(bad_image_path)
try:
os.remove(bad_image_path)
except:
pass
# Serializing json
json_object = json.dumps(json_file_object, indent=4)
# Write in file
try:
file = open(gltf_path, 'w+', encoding="utf-8")
if file:
file.write(json_object)
file.close()
#print("export_gltf_textures - json write")
except IOError:
self.report({'ERROR'}, f"[TEXTURE] File '{gltf_path}' could not be written. File access denied")
return
return
EDIT: my testing has resulted in the requirement that - “ALL Textures in the blend file MUST be set to absolute” 4.2 has really messed up path joins etc.