Now using new BDK addon operators to load the materials
This commit is contained in:
@@ -4,6 +4,7 @@ import typing
|
|||||||
from collections import Counter
|
from collections import Counter
|
||||||
from typing import List, Iterable
|
from typing import List, Iterable
|
||||||
|
|
||||||
|
import addon_utils
|
||||||
import bpy.types
|
import bpy.types
|
||||||
from bpy.types import NlaStrip, Object, AnimData
|
from bpy.types import NlaStrip, Object, AnimData
|
||||||
|
|
||||||
@@ -176,3 +177,7 @@ def get_export_bone_names(armature_object: Object, bone_filter_mode: str, bone_g
|
|||||||
f'Additional debugging information has been written to the console.')
|
f'Additional debugging information has been written to the console.')
|
||||||
|
|
||||||
return bone_names
|
return bone_names
|
||||||
|
|
||||||
|
|
||||||
|
def is_bdk_addon_loaded():
|
||||||
|
return addon_utils.check('bdk_addon')[1]
|
||||||
|
|||||||
@@ -21,7 +21,7 @@ class PsaExportSequence:
|
|||||||
|
|
||||||
class PsaBuildOptions:
|
class PsaBuildOptions:
|
||||||
def __init__(self):
|
def __init__(self):
|
||||||
self.animation_data: AnimData
|
self.animation_data: AnimData = None
|
||||||
self.sequences: List[PsaExportSequence] = []
|
self.sequences: List[PsaExportSequence] = []
|
||||||
self.bone_filter_mode = 'ALL'
|
self.bone_filter_mode = 'ALL'
|
||||||
self.bone_group_indices: List[int] = []
|
self.bone_group_indices: List[int] = []
|
||||||
|
|||||||
@@ -1,7 +1,6 @@
|
|||||||
import os
|
import os
|
||||||
import sys
|
import sys
|
||||||
from math import inf
|
from math import inf
|
||||||
from pathlib import Path
|
|
||||||
from typing import Optional, List
|
from typing import Optional, List
|
||||||
|
|
||||||
import bmesh
|
import bmesh
|
||||||
@@ -14,8 +13,7 @@ from mathutils import Quaternion, Vector, Matrix
|
|||||||
|
|
||||||
from .data import Psk
|
from .data import Psk
|
||||||
from .reader import read_psk
|
from .reader import read_psk
|
||||||
from ..bdk import UReference
|
from ..helpers import rgb_to_srgb, is_bdk_addon_loaded
|
||||||
from ..helpers import rgb_to_srgb
|
|
||||||
|
|
||||||
|
|
||||||
class PskImportOptions:
|
class PskImportOptions:
|
||||||
@@ -55,30 +53,6 @@ class PskImportResult:
|
|||||||
self.warnings: List[str] = []
|
self.warnings: List[str] = []
|
||||||
|
|
||||||
|
|
||||||
def load_bdk_material(reference: UReference):
|
|
||||||
if reference is None:
|
|
||||||
return None
|
|
||||||
asset_libraries = bpy.context.preferences.filepaths.asset_libraries
|
|
||||||
asset_library_name = 'bdk-library'
|
|
||||||
try:
|
|
||||||
asset_library = next(filter(lambda x: x.name == asset_library_name, asset_libraries))
|
|
||||||
except StopIteration:
|
|
||||||
return None
|
|
||||||
asset_library_path = Path(asset_library.path)
|
|
||||||
# TODO: going to be very slow for automation!
|
|
||||||
blend_files = [fp for fp in asset_library_path.glob(f'**/{reference.package_name}.blend') if fp.is_file()]
|
|
||||||
if len(blend_files) == 0:
|
|
||||||
return None
|
|
||||||
blend_file = str(blend_files[0])
|
|
||||||
with bpy.data.libraries.load(blend_file, link=True, relative=False, assets_only=True) as (data_in, data_out):
|
|
||||||
if reference.object_name in data_in.materials:
|
|
||||||
data_out.materials = [reference.object_name]
|
|
||||||
else:
|
|
||||||
return None
|
|
||||||
material = bpy.data.materials[reference.object_name]
|
|
||||||
return material
|
|
||||||
|
|
||||||
|
|
||||||
def import_psk(psk: Psk, context, options: PskImportOptions) -> PskImportResult:
|
def import_psk(psk: Psk, context, options: PskImportOptions) -> PskImportResult:
|
||||||
result = PskImportResult()
|
result = PskImportResult()
|
||||||
armature_object = None
|
armature_object = None
|
||||||
@@ -156,15 +130,16 @@ def import_psk(psk: Psk, context, options: PskImportOptions) -> PskImportResult:
|
|||||||
if options.should_import_materials:
|
if options.should_import_materials:
|
||||||
for material_index, psk_material in enumerate(psk.materials):
|
for material_index, psk_material in enumerate(psk.materials):
|
||||||
material_name = psk_material.name.decode('utf-8')
|
material_name = psk_material.name.decode('utf-8')
|
||||||
|
material = None
|
||||||
if options.should_reuse_materials and material_name in bpy.data.materials:
|
if options.should_reuse_materials and material_name in bpy.data.materials:
|
||||||
# Material already exists, just re-use it.
|
# Material already exists, just re-use it.
|
||||||
material = bpy.data.materials[material_name]
|
material = bpy.data.materials[material_name]
|
||||||
elif psk.has_material_references:
|
elif is_bdk_addon_loaded() and psk.has_material_references:
|
||||||
# Material does not yet exist, attempt to load it using BDK.
|
# Material does not yet exist and we have the BDK addon installed.
|
||||||
reference = UReference.from_string(psk.material_references[material_index])
|
# Attempt to load it using BDK addon's operator.
|
||||||
material = load_bdk_material(reference)
|
material_reference = psk.material_references[material_index]
|
||||||
else:
|
if material_reference and bpy.ops.bdk.link_material(reference=material_reference) == {'FINISHED'}:
|
||||||
material = None
|
material = bpy.data.materials[material_name]
|
||||||
mesh_data.materials.append(material)
|
mesh_data.materials.append(material)
|
||||||
|
|
||||||
bm = bmesh.new()
|
bm = bmesh.new()
|
||||||
|
|||||||
Reference in New Issue
Block a user