Skip to content

Commit

Permalink
added 'export all images' operation
Browse files Browse the repository at this point in the history
  • Loading branch information
matyalatte committed Jan 19, 2023
1 parent 5dff580 commit c455f2b
Show file tree
Hide file tree
Showing 17 changed files with 369 additions and 120 deletions.
2 changes: 1 addition & 1 deletion .github/workflows/build.yml
Original file line number Diff line number Diff line change
Expand Up @@ -76,7 +76,7 @@ jobs:
cp -r addons/${{ env.ZIP_NAME }} ../release
cp changelog.txt ../release/${{ env.ZIP_NAME }}
cp LICENSE ../release/${{ env.ZIP_NAME }}
cp external/Texconv-Custom-DLL/*texconv.* ../release/${{ env.ZIP_NAME }}
cp external/Texconv-Custom-DLL/*texconv.* ../release/${{ env.ZIP_NAME }}/directx
shell: bash

- name: Archive Release
Expand Down
4 changes: 2 additions & 2 deletions .github/workflows/test.yml
Original file line number Diff line number Diff line change
Expand Up @@ -78,13 +78,13 @@ jobs:
if: runner.os=='Windows'
run: |
external/Texconv-Custom-DLL/batch_files/build_dds_full_support.bat
cp external/Texconv-Custom-DLL/texconv.dll addons/blender_dds_addon
cp external/Texconv-Custom-DLL/texconv.dll addons/blender_dds_addon/directx
- name: build shared library (for Unix)
if: runner.os!='Windows'
run: |
bash external/Texconv-Custom-DLL/shell_scripts/build_dds_full_support.sh
cp external/Texconv-Custom-DLL/libtexconv.* addons/blender_dds_addon
cp external/Texconv-Custom-DLL/libtexconv.* addons/blender_dds_addon/directx
- name: Set up Python v${{ matrix.python-version }}
uses: actions/setup-python@v3
Expand Down
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@ htmlcov
*.so
*.dylib
*.exe
*.zip

# doc
*.txt
Expand Down
10 changes: 7 additions & 3 deletions addons/blender_dds_addon/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
bl_info = {
'name': 'DDS textures',
'author': 'Matyalatte',
'version': (0, 1, 3),
'version': (0, 2, 0),
'blender': (2, 83, 20),
'location': 'Image Editor > Sidebar > DDS Tab',
'description': 'Import and export .dds files',
Expand Down Expand Up @@ -32,18 +32,22 @@ def reload_package_recursive(current_dir, module_dict):
if ".import_dds" in locals():
reload_package(locals())

from . import import_dds, export_dds
from .ui import import_dds, export_dds, set_properties, preferences

def register():
"""Add addon."""
preferences.register()
import_dds.register()
export_dds.register()
set_properties.register()

def unregister():
"""Remove addon."""
preferences.unregister()
import_dds.unregister()
export_dds.unregister()
set_properties.unregister()

except ModuleNotFoundError as exc:
print(exc)
print('bpy not found.')
print('Failed to load the addon.')
Original file line number Diff line number Diff line change
@@ -1,13 +1,13 @@
"""Class for DDS files."""

import ctypes as c
from enum import Enum
from enum import IntEnum

from . import util
from .dxgi_format import DXGI_FORMAT, FOURCC_TO_DXGI, BITMASK_TO_DXGI


class PF_FLAGS(Enum):
class PF_FLAGS(IntEnum):
'''dwFlags for DDS_PIXELFORMAT'''
# DDS_ALPHAPIXELS = 0x00000001
# DDS_ALPHA = 0x00000002
Expand Down Expand Up @@ -164,7 +164,7 @@ def write(self, f):
f.write(self)
# DXT10 header
if self.fourCC == b'DX10':
util.write_uint32(f, self.dxgi_format.value)
util.write_uint32(f, self.dxgi_format)
util.write_uint32(f, 3)
util.write_uint32(f, 4 * self.is_cube())
util.write_uint32(f, 1)
Expand All @@ -179,7 +179,7 @@ def is_bit_mask(self, bit_mask):
def get_dxgi_from_header(self):
'''Similar method as GetDXGIFormat in DirectXTex/DDSTextureLoader/DDSTextureLoader12.cpp'''
# Try to detect DXGI from fourCC.
if self.pfflags & PF_FLAGS.DDS_FOURCC.value:
if self.pfflags & PF_FLAGS.DDS_FOURCC:
for cc_list, dxgi in FOURCC_TO_DXGI:
if self.fourCC in cc_list:
return dxgi
Expand All @@ -194,7 +194,7 @@ def get_dxgi_from_header(self):
print("Failed to detect dxgi format. It'll be loaded as B8G8R8A8.")
return DXGI_FORMAT.DXGI_FORMAT_B8G8R8A8_UNORM

if self.pfflags & PF_FLAGS.DDS_BUMPDUDV.value:
if self.pfflags & PF_FLAGS.DDS_BUMPDUDV:
# DXGI format should be signed.
return DXGI_FORMAT.get_signed(detected_dxgi)
else:
Expand All @@ -213,7 +213,7 @@ def is_bc5(self):
return 'BC5' in self.dxgi_format.name

def get_format_as_str(self):
return self.dxgi_format.name
return self.dxgi_format.name[12:]

def is_srgb(self):
return 'SRGB' in self.dxgi_format.name
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,10 +6,10 @@
- Official repo for DDS
https://github.com/microsoft/DirectXTex
'''
from enum import Enum
from enum import IntEnum


class DXGI_FORMAT(Enum):
class DXGI_FORMAT(IntEnum):
"""Enum for DDS format."""
DXGI_FORMAT_UNKNOWN = 0
DXGI_FORMAT_R32G32B32A32_TYPELESS = 1
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -26,12 +26,17 @@ def __init__(self, dll_path=None):
dll_name = "libtexconv.so"
else:
raise RuntimeError(f'This OS ({util.get_os_name()}) is unsupported.')
dll_path = os.path.join(os.path.dirname(file_path), dll_name)
dirname = os.path.dirname(file_path)
dll_path = os.path.join(dirname, dll_name)
dll_path2 = os.path.join(os.path.dirname(dirname), dll_name) # allow ../texconv.dll

dll_path = os.path.abspath(dll_path)

if not os.path.exists(dll_path):
raise RuntimeError(f'texconv not found. ({dll_path})')
if os.path.exists(dll_path2):
dll_path = dll_path2
else:
raise RuntimeError(f'texconv not found. ({dll_path})')

self.dll = ctypes.cdll.LoadLibrary(dll_path)

Expand Down Expand Up @@ -65,7 +70,7 @@ def convert_to_tga(self, file, out=None, cubemap_layout="h-cross", invert_normal
raise RuntimeError('Can not convert 3D textures with texconv.')

if verbose:
print(f'DXGI_FORMAT: {dds_header.get_format_as_str()[12:]}')
print(f'DXGI_FORMAT: {dds_header.get_format_as_str()}')

args = []

Expand Down
File renamed without changes.
66 changes: 66 additions & 0 deletions addons/blender_dds_addon/ui/bpy_util.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,66 @@
import os
import bpy


def dds_properties_exist():
return hasattr(bpy.types.Image, "dds_props")


def get_addon_preferences(context, addon_name):
preferences = context.preferences
addon_prefs = preferences.addons[addon_name].preferences
return addon_prefs


def get_image_editor_space(context):
area = context.area
if area.type == 'IMAGE_EDITOR':
space = area.spaces.active
else:
raise RuntimeError('Failed to get Image Editor. This is unexpected.')
return space


def load_texture(file, name, color_space='Non-Color'):
"""Load a texture file.
Args:
file (string): file path for dds
name (string): object name for the texture
color_space (string): color space
Returns:
tex (bpy.types.Image): loaded texture
"""
tex = bpy.data.images.load(file)
tex.colorspace_settings.name = color_space
tex.name = name
tex.pack()
tex.filepath = os.path.join('//textures', tex.name + '.' + file.split('.')[-1])
tex.filepath_raw = tex.filepath
return tex


def save_texture(tex, file, fmt):
"""Save a texture.
Args:
tex (bpy.types.Image): an image object
file (string): file path
fmt (string): file format
"""
file_format = tex.file_format
filepath_raw = tex.filepath_raw

try:
tex.file_format = fmt
tex.filepath_raw = file
tex.save()

tex.file_format = file_format
tex.filepath_raw = filepath_raw

except Exception as e:
tex.file_format = file_format
tex.filepath_raw = filepath_raw
raise e
Loading

0 comments on commit c455f2b

Please sign in to comment.