matyalatte / Blender-DDS-Addon

Blender addon to import and export dds textures
MIT License
70 stars 5 forks source link

Support for blender scripting #29

Open Slluxx opened 1 month ago

Slluxx commented 1 month ago

I need to convert a PNG to a DDS file in my plugin for a custom export. It would be helpful if DDS integrates like a native file format supported by blender.

bpy_struct: item.attr = val: enum "DDS" not found in ('BMP', 'IRIS', 'PNG', 'JPEG', 'JPEG2000', 'TARGA', 'TARGA_RAW', 'CINEON', 'DPX', 'OPEN_EXR_MULTILAYER', 'OPEN_EXR', 'HDR', 'TIFF', 'WEBP', 'FFMPEG')
matyalatte commented 1 month ago

You can call save_dds from export_dds.py. As far as I know, there is no way to override built-in features (like the enum you mentioned) to export textures.

import os
import bpy
from blender_dds_addon.ui.export_dds import save_dds

tex = bpy.data.images.load(os.path.abspath("my_texture.png"))
save_dds(tex, "my_texture.dds", "BC1_UNORM")
bpy.data.images.remove(tex)
Slluxx commented 1 month ago

I dont want to save the dds, just get the data to then write the bytes in a specific format into a file (which is handled by a few functions i already have). I have not looked too much into your plugin, so forgive me if this is already possible.

matyalatte commented 1 month ago

You mean, you want to embed dds data into another file format, like game assets? Even in that case, you need to export textures as dds files because my addon uses a file converter, not a dds library.

import os
import tempfile
import bpy
from blender_dds_addon.ui.export_dds import save_dds
from blender_dds_addon.directx.dds import DDSHeader

tex = bpy.data.images.load(os.path.abspath("my_texture.png"))

with tempfile.TemporaryDirectory() as temp_dir:
    temp_dds = os.path.join(temp_dir, "temp.dds")
    save_dds(tex, temp_dds, "BC1_UNORM")
    with open(temp_dds, 'rb') as f:
        header = DDSHeader.read(f)
        pixel_data = f.read()  # compressed pixel data

bpy.data.images.remove(tex)