project(characters)

set(EDITED_BLENDS_LIST
        male
        female
)
set(EDITED_BLEND_TARGETS)
set(CHARACTER_GLBS)
foreach(EDITED_BLEND ${EDITED_BLENDS_LIST})
add_custom_command(
        OUTPUT ${CMAKE_CURRENT_BINARY_DIR}/edited-normal-${EDITED_BLEND}.blend
        DEPENDS ${CMAKE_CURRENT_SOURCE_DIR}/edited-normal-${EDITED_BLEND}.blend
                ${CMAKE_BINARY_DIR}/assets/blender/vrm-vroid-normal-${EDITED_BLEND}.blend
		${CMAKE_CURRENT_SOURCE_DIR}/copy_animations.py
		${CMAKE_BINARY_DIR}/assets/blender/mixamo
		${CMAKE_CURRENT_BINARY_DIR}/blender-addons-installed
        COMMAND ${CMAKE_COMMAND}
                -E copy ${CMAKE_CURRENT_SOURCE_DIR}/edited-normal-${EDITED_BLEND}.blend
                ${CMAKE_CURRENT_BINARY_DIR}/edited-normal-${EDITED_BLEND}.blend
        COMMAND ${BLENDER} -b -Y
                ${CMAKE_CURRENT_BINARY_DIR}/edited-normal-${EDITED_BLEND}.blend
		-P ${CMAKE_CURRENT_SOURCE_DIR}/copy_animations.py --
                ${CMAKE_BINARY_DIR}/assets/blender/vrm-vroid-normal-${EDITED_BLEND}.blend ${EDITED_BLEND}
        WORKING_DIRECTORY ${CMAKE_BINARY_DIR}
)
list(APPEND EDITED_BLEND_TARGETS ${CMAKE_BINARY_DIR}/assets/blender/characters/edited-normal-${EDITED_BLEND}.blend)
list(APPEND CHARACTER_GLBS ${CMAKE_BINARY_DIR}/characters/${EDITED_BLEND}/normal-${EDITED_BLEND}.glb)
endforeach()
list(APPEND CHARACTER_GLBS ${CMAKE_BINARY_DIR}/characters/male/male-clothes-toprobe.glb)
add_custom_command(OUTPUT ${CMAKE_BINARY_DIR}/assets/blender/mixamo
	COMMAND ${CMAKE_COMMAND} -E copy_directory ${CMAKE_SOURCE_DIR}/assets/blender/mixamo ${CMAKE_BINARY_DIR}/assets/blender/mixamo
	DEPENDS ${CMAKE_SOURCE_DIR}/assets/blender/mixamo
)

set(VRM_IMPORTED_BLENDS
        ${CMAKE_BINARY_DIR}/assets/blender/vrm-vroid-normal-female.blend
        ${CMAKE_BINARY_DIR}/assets/blender/vrm-vroid-normal-male.blend
        ${CMAKE_BINARY_DIR}/assets/blender/shapes/male/vrm-vroid-normal-male-chibi.blend
)

#add_custom_command(
#        OUTPUT ${CHARACTER_GLBS}
#        COMMAND ${CMAKE_COMMAND} -E make_directory ${CREATE_DIRECTORIES}
#        COMMAND ${BLENDER} -b -Y -P ${CMAKE_SOURCE_DIR}/assets/blender/scripts/export_models.py
#	COMMAND ${CMAKE_COMMAND} -D FILE=${CMAKE_BINARY_DIR}/characters/male/normal-male.glb -P ${CMAKE_CURRENT_SOURCE_DIR}/cmake/check_file_size.cmake
#	COMMAND ${CMAKE_COMMAND} -D FILE=${CMAKE_BINARY_DIR}/characters/female/normal-female.glb -P ${CMAKE_CURRENT_SOURCE_DIR}/cmake/check_file_size.cmake
#        COMMAND ${CMAKE_COMMAND} -E touch_nocreate ${CHARACTER_GLBS}
#        DEPENDS ${CMAKE_SOURCE_DIR}/assets/blender/scripts/export_models.py ${VRM_IMPORTED_BLENDS} ${EDITED_BLEND_TARGETS}
#        WORKING_DIRECTORY ${CMAKE_BINARY_DIR})
add_custom_command(
	OUTPUT ${CMAKE_BINARY_DIR}/characters/male/normal-male.glb
	COMMAND ${CMAKE_COMMAND} -E make_directory ${CREATE_DIRECTORIES}
	COMMAND ${BLENDER} -b -Y -P ${CMAKE_SOURCE_DIR}/assets/blender/scripts/export_models2.py --
       		${CMAKE_CURRENT_BINARY_DIR}/edited-normal-male-consolidated.blend
		${CMAKE_BINARY_DIR}/characters/male/normal-male.glb
		"male"
		tmp-edited-male.blend
	COMMAND ${CMAKE_COMMAND} -D FILE=${CMAKE_BINARY_DIR}/characters/male/normal-male.glb
		-P ${CMAKE_CURRENT_SOURCE_DIR}/cmake/check_file_size.cmake
	COMMAND ${CMAKE_COMMAND} -D FILE=${CMAKE_BINARY_DIR}/characters/male/normal-male.scene
		-P ${CMAKE_CURRENT_SOURCE_DIR}/cmake/check_file_size.cmake
	DEPENDS ${CMAKE_SOURCE_DIR}/assets/blender/scripts/export_models2.py
		${VRM_IMPORTED_BLENDS}
		${CMAKE_CURRENT_BINARY_DIR}/edited-normal-male-consolidated.blend
		${CMAKE_CURRENT_BINARY_DIR}/blender-addons-installed
        WORKING_DIRECTORY ${CMAKE_BINARY_DIR}
	VERBATIM
)

add_custom_command(
	OUTPUT ${CMAKE_BINARY_DIR}/characters/female/normal-female.glb
	COMMAND ${CMAKE_COMMAND} -E make_directory ${CREATE_DIRECTORIES}
	COMMAND ${BLENDER} -b -Y -P ${CMAKE_SOURCE_DIR}/assets/blender/scripts/export_models2.py --
		${CMAKE_CURRENT_BINARY_DIR}/edited-normal-female-consolidated.blend
		${CMAKE_BINARY_DIR}/characters/female/normal-female.glb
		"female"
		tmp-edited-female.blend
	COMMAND ${CMAKE_COMMAND} -D FILE=${CMAKE_BINARY_DIR}/characters/female/normal-female.glb
		-P ${CMAKE_CURRENT_SOURCE_DIR}/cmake/check_file_size.cmake
	COMMAND ${CMAKE_COMMAND} -D FILE=${CMAKE_BINARY_DIR}/characters/female/normal-female.scene
		-P ${CMAKE_CURRENT_SOURCE_DIR}/cmake/check_file_size.cmake
	DEPENDS ${CMAKE_SOURCE_DIR}/assets/blender/scripts/export_models2.py
		${VRM_IMPORTED_BLENDS}
		${CMAKE_CURRENT_BINARY_DIR}/edited-normal-female-consolidated.blend
		${CMAKE_CURRENT_BINARY_DIR}/blender-addons-installed
        WORKING_DIRECTORY ${CMAKE_BINARY_DIR}
	VERBATIM
)
add_custom_command(
	OUTPUT ${CMAKE_BINARY_DIR}/characters/male/male-clothes-toprobe.glb
	COMMAND ${BLENDER} -b -Y ${CMAKE_CURRENT_SOURCE_DIR}/edited-normal-male.blend
		-P ${CMAKE_CURRENT_SOURCE_DIR}/export_clothes.py
		-- ${CMAKE_BINARY_DIR}/characters/male/male-clothes-toprobe.glb 
		BodyTopRobe
	COMMAND ${CMAKE_COMMAND} -D FILE=${CMAKE_BINARY_DIR}/characters/male/male-clothes-toprobe.glb
		-P ${CMAKE_CURRENT_SOURCE_DIR}/cmake/check_file_size.cmake
	DEPENDS ${CMAKE_CURRENT_BINARY_DIR}/edited-normal-male.blend
		${CMAKE_CURRENT_BINARY_DIR}/blender-addons-installed
)

set(VRM_SOURCE)

set(VRM_FILES buch1-chibi.vrm buch1.vrm jane2-dress.vrm jane2.vrm)
file(GLOB MIXAMO_FILES RELATIVE ${CMAKE_SOURCE_DIR}/assets/blender/mixamo ${CMAKE_SOURCE_DIR}/assets/blender/mixamo/**/*.fbx)
foreach(VRM_FILE ${VRM_FILES})
        set(OUTPUT_FILE "${CMAKE_BINARY_DIR}/assets/vroid/${VRM_FILE}")
        set(INPUT_FILE "${CMAKE_SOURCE_DIR}/assets/vroid/${VRM_FILE}")
        add_custom_command(OUTPUT "${OUTPUT_FILE}"
                COMMAND ${CMAKE_COMMAND} -E copy  "${INPUT_FILE}" "${OUTPUT_FILE}"
                DEPENDS "${INPUT_FILE}" VERBATIM)
        list(APPEND VRM_SOURCE "${OUTPUT_FILE}")
endforeach()
foreach(MIXAMO_FILE ${MIXAMO_FILES})
        set(OUTPUT_FILE "${CMAKE_BINARY_DIR}/assets/blender/mixamo/${MIXAMO_FILE}")
        set(INPUT_FILE "${CMAKE_SOURCE_DIR}/assets/blender/mixamo/${MIXAMO_FILE}")
        add_custom_command(OUTPUT "${OUTPUT_FILE}"
                COMMAND ${CMAKE_COMMAND} -E copy  "${INPUT_FILE}" "${OUTPUT_FILE}"
                DEPENDS "${INPUT_FILE}" VERBATIM)
        list(APPEND VRM_SOURCE "${OUTPUT_FILE}")
endforeach()
add_custom_command(OUTPUT ${CMAKE_CURRENT_BINARY_DIR}/blender-addons-installed
	COMMAND ${BLENDER} -b -Y -P ${CMAKE_SOURCE_DIR}/assets/blender/scripts/install_addons.py
	COMMAND ${CMAKE_COMMAND} -E touch ${CMAKE_CURRENT_BINARY_DIR}/blender-addons-installed
	DEPENDS ${CMAKE_BINARY_DIR}/assets/blender/scripts/addons/3.6/VRM_Addon_for_Blender-release.zip
	${CMAKE_BINARY_DIR}/assets/blender/scripts/addons/3.6/io_ogre.zip
		${CMAKE_SOURCE_DIR}/assets/blender/scripts/install_addons.py
	WORKING_DIRECTORY ${CMAKE_BINARY_DIR}
)
add_custom_command(OUTPUT ${CMAKE_BINARY_DIR}/assets/blender/scripts/addons/3.6/VRM_Addon_for_Blender-release.zip
	COMMAND ${CMAKE_COMMAND} -E copy
		${CMAKE_SOURCE_DIR}/assets/blender/scripts/addons/3.6/VRM_Addon_for_Blender-release.zip
		${CMAKE_BINARY_DIR}/assets/blender/scripts/addons/3.6/VRM_Addon_for_Blender-release.zip
	DEPENDS ${CMAKE_SOURCE_DIR}/assets/blender/scripts/addons/3.6/VRM_Addon_for_Blender-release.zip
	WORKING_DIRECTORY ${CMAKE_BINARY_DIR}
)
add_custom_command(OUTPUT ${CMAKE_BINARY_DIR}/assets/blender/scripts/addons/3.6/io_ogre.zip
	COMMAND zip -r ${CMAKE_BINARY_DIR}/assets/blender/scripts/addons/3.6/io_ogre.zip io_ogre
	DEPENDS ${CMAKE_SOURCE_DIR}/assets/blender/scripts/blender2ogre/io_ogre
		${CMAKE_SOURCE_DIR}/assets/blender/scripts/blender2ogre/io_ogre/ui/export.py
		${CMAKE_SOURCE_DIR}/assets/blender/scripts/blender2ogre/io_ogre/ogre/skeleton.py
	WORKING_DIRECTORY ${CMAKE_SOURCE_DIR}/assets/blender/scripts/blender2ogre
)
add_custom_target(install_addons ALL DEPENDS ${CMAKE_CURRENT_BINARY_DIR}/blender-addons-installed)


#add_custom_command(OUTPUT ${VRM_IMPORTED_BLENDS}
#        COMMAND ${CMAKE_COMMAND} -E make_directory ${CREATE_DIRECTORIES}
#	COMMAND ${BLENDER} -b -Y -P ${CMAKE_CURRENT_SOURCE_DIR}/import_vrm.py
#        COMMAND ${CMAKE_COMMAND} -E touch_nocreate ${VRM_IMPORTED_BLENDS}
#	DEPENDS ${CMAKE_CURRENT_SOURCE_DIR}/import_vrm.py
#	${CMAKE_CURRENT_BINARY_DIR}/blender-assons-installed
#        ${VRM_SOURCE}
#        WORKING_DIRECTORY ${CMAKE_BINARY_DIR})

function(blender_import_vrm BLEND VRM EDITABLE RIG)
	get_filename_component(TARGET_NAME ${BLEND} NAME_WE)
	get_filename_component(VRM_NAME ${VRM} NAME_WE)
	add_custom_command(OUTPUT ${BLEND}
		COMMAND ${CMAKE_COMMAND} -E make_directory ${CREATE_DIRECTORIES}
		COMMAND ${BLENDER} -b -Y -P ${CMAKE_SOURCE_DIR}/assets/blender/scripts/import_vrm2.py -- ${VRM_NAME}.vrm ${BLEND} ${EDITABLE} ${RIG}
		COMMAND ${CMAKE_COMMAND} -D FILE=${BLEND} -P ${CMAKE_CURRENT_SOURCE_DIR}/cmake/check_file_size.cmake
		COMMAND ${CMAKE_COMMAND} -E touch_nocreate ${BLEND}
		DEPENDS ${CMAKE_SOURCE_DIR}/assets/blender/scripts/import_vrm2.py 
		${CMAKE_CURRENT_BINARY_DIR}/blender-addons-installed
		${VRM} ${CMAKE_BINARY_DIR}/assets/blender/mixamo
		WORKING_DIRECTORY ${CMAKE_BINARY_DIR}
	)
endfunction()

blender_import_vrm(${CMAKE_BINARY_DIR}/assets/blender/vrm-vroid-normal-female.blend
	${CMAKE_BINARY_DIR}/assets/vroid/jane2-dress.vrm
	modelling-vroid-normal-female.blend female)
blender_import_vrm(${CMAKE_BINARY_DIR}/assets/blender/vrm-vroid-normal-male.blend
	${CMAKE_BINARY_DIR}/assets/vroid/buch1.vrm
	modelling-vroid-normal-male.blend male)
blender_import_vrm(${CMAKE_BINARY_DIR}/assets/blender/shapes/male/vrm-vroid-normal-male-chibi.blend
	${CMAKE_BINARY_DIR}/assets/vroid/buch1-chibi.vrm
	modelling-shape-male-chibi.blend male)

#add_custom_command(OUTPUT ${VRM_IMPORTED_BLENDS}
#        COMMAND ${CMAKE_COMMAND} -E make_directory ${CREATE_DIRECTORIES}
#	COMMAND ${BLENDER} -b -Y -P ${CMAKE_CURRENT_SOURCE_DIR}/import_vrm2.py jane2-dress.vrm vrm-vroid-normal-female.blend modelling-vroid-normal-female.blend female
#	COMMAND ${BLENDER} -b -Y -P ${CMAKE_CURRENT_SOURCE_DIR}/import_vrm2.py buch1.vrm vrm-vroid-normal-male.blend modelling-vroid-normal-male.blend male
#	COMMAND ${BLENDER} -b -Y -P ${CMAKE_CURRENT_SOURCE_DIR}/import_vrm2.py buch1-chibi.vrm shapes/male/vrm-vroid-normal-male-chibi.blend modelling-shape-male-chibi.blend male
#        COMMAND ${CMAKE_COMMAND} -E touch_nocreate ${VRM_IMPORTED_BLENDS}
#	DEPENDS ${CMAKE_CURRENT_SOURCE_DIR}/import_vrm.py
#	${CMAKE_CURRENT_BINARY_DIR}/blender-assons-installed
#        ${VRM_SOURCE}
#        WORKING_DIRECTORY ${CMAKE_BINARY_DIR})

set(COPY_BLENDS edited-shape-test-male.blend edited-normal-male-base.blend)

foreach (COPY_BLEND_FILE ${COPY_BLENDS})
        add_custom_command(OUTPUT ${CMAKE_BINARY_DIR}/assets/blender/${COPY_BLEND_FILE}
                DEPENDS ${CMAKE_SOURCE_DIR}/assets/blender/${COPY_BLEND_FILE}
                COMMAND ${CMAKE_COMMAND} -E copy
                        ${CMAKE_SOURCE_DIR}/assets/blender/${COPY_BLEND_FILE}
                        ${CMAKE_BINARY_DIR}/assets/blender/${COPY_BLEND_FILE}
                )
        list(APPEND EDITED_BLEND_TARGETS ${CMAKE_BINARY_DIR}/assets/blender/${COPY_BLEND_FILE})
endforeach()

add_custom_command(OUTPUT ${CMAKE_BINARY_DIR}/characters/shapes/male/chibi/vroid-normal-male-chibi.glb
	DEPENDS ${CMAKE_CURRENT_BINARY_DIR}/shapes/male/chibi/vroid-normal-male-chibi.glb
	COMMAND ${CMAKE_COMMAND} -E copy ${CMAKE_CURRENT_BINARY_DIR}/shapes/male/chibi/vroid-normal-male-chibi.glb
       ${CMAKE_BINARY_DIR}/characters/shapes/male/chibi/vroid-normal-male-chibi.glb)
add_custom_target(morph ALL DEPENDS MorphTargetsResearch ${CHARACTER_GLBS})


add_custom_target(edited-blends ALL DEPENDS ${EDITED_BLEND_TARGETS})

add_custom_target(import_vrm DEPENDS ${CHARACTER_GLBS})

function(weight_clothes SRC)
	get_filename_component(TARGET_NAME ${SRC} NAME_WE)
	add_custom_command(
		OUTPUT ${CMAKE_CURRENT_BINARY_DIR}/clothes/${TARGET_NAME}_weighted.stamp
		${CMAKE_CURRENT_BINARY_DIR}/clothes/${TARGET_NAME}_weighted.blend
		COMMAND ${CMAKE_COMMAND} -E make_directory ${CMAKE_CURRENT_BINARY_DIR}/clothes
		COMMAND ${BLENDER} -b -Y -P ${CMAKE_CURRENT_SOURCE_DIR}/process_clothes.py -- ${CMAKE_CURRENT_SOURCE_DIR}/${TARGET_NAME}.blend ./  ${CMAKE_CURRENT_BINARY_DIR}/clothes
		COMMAND ${CMAKE_COMMAND} -E touch ${CMAKE_CURRENT_BINARY_DIR}/clothes/${TARGET_NAME}_weighted.stamp
		COMMAND ${CMAKE_COMMAND} -D FILE=${CMAKE_CURRENT_BINARY_DIR}/clothes/${TARGET_NAME}_weighted.blend
		-P ${CMAKE_CURRENT_SOURCE_DIR}/cmake/check_file_size.cmake
		DEPENDS ${SRC}
			${CMAKE_CURRENT_BINARY_DIR}/blender-addons-installed
	)
endfunction()
function(transfer_shape_keys SRC CLOTH DST)
	add_custom_command(
		OUTPUT ${DST}
		COMMAND ${BLENDER} -b -Y -P ${CMAKE_CURRENT_SOURCE_DIR}/transfer_shape_keys.py -- ${SRC} ${CLOTH} ${DST}
		COMMAND ${CMAKE_COMMAND} -D FILE=${DST}
		-P ${CMAKE_CURRENT_SOURCE_DIR}/cmake/check_file_size.cmake
		DEPENDS ${SRC} ${CLOTH}
			${CMAKE_CURRENT_BINARY_DIR}/blender-addons-installed
			${CMAKE_CURRENT_SOURCE_DIR}/transfer_shape_keys.py
	)
endfunction()

# Function to combine clothes into a blend file
# Parameters:
#   INPUT_BLEND     - Input blend file (required)
#   WEIGHTED_BLEND  - Weighted clothes blend file to combine (required)
#   COMBINED_BLEND  - Output combined blend file (required)
function(add_clothes_combination INPUT_BLEND WEIGHTED_BLEND COMBINED_BLEND)
    # Parse optional arguments
    set(options "")
    set(oneValueArgs OUTPUT_DIR)
    set(multiValueArgs "")
    cmake_parse_arguments(COMBINE "${options}" "${oneValueArgs}" "${multiValueArgs}" ${ARGN})

    # Validate required arguments
    if(NOT INPUT_BLEND)
        message(FATAL_ERROR "INPUT_BLEND is required for add_clothes_combination")
    endif()
    if(NOT WEIGHTED_BLEND)
        message(FATAL_ERROR "WEIGHTED_BLEND is required for add_clothes_combination")
    endif()
    if(NOT COMBINED_BLEND)
        message(FATAL_ERROR "COMBINED_BLEND output path is required for add_clothes_combination")
    endif()

    # Get the base name from the weighted blend file for stamp file
    get_filename_component(WEIGHTED_BLEND_NAME "${WEIGHTED_BLEND}" NAME_WE)
    # Remove "_weighted" suffix if present
    string(REGEX REPLACE "_weighted$" "" TARGET_BASE "${WEIGHTED_BLEND_NAME}")

    # Set default output directory for stamp if not provided
    if(NOT COMBINE_OUTPUT_DIR)
        set(COMBINE_OUTPUT_DIR "${CMAKE_CURRENT_BINARY_DIR}/clothes")
    endif()

    # Define stamp file using the derived base name
    set(STAMP_FILE "${COMBINE_OUTPUT_DIR}/${TARGET_BASE}-combined.stamp")

    # Derive the weighted stamp dependency
    get_filename_component(WEIGHTED_DIR "${WEIGHTED_BLEND}" DIRECTORY)
    get_filename_component(WEIGHTED_BASE "${WEIGHTED_BLEND}" NAME_WE)
    set(WEIGHTED_STAMP "${WEIGHTED_DIR}/${WEIGHTED_BASE}.stamp")

    # Ensure the output directory for COMBINED_BLEND exists
    get_filename_component(COMBINED_DIR "${COMBINED_BLEND}" DIRECTORY)

    add_custom_command(
        OUTPUT ${STAMP_FILE} ${COMBINED_BLEND}
        COMMAND ${CMAKE_COMMAND} -E make_directory ${COMBINED_DIR}
        COMMAND ${BLENDER} -b -Y -P ${CMAKE_CURRENT_SOURCE_DIR}/combine_clothes.py --
                ${INPUT_BLEND}
                ${WEIGHTED_BLEND}
                ${COMBINED_BLEND}
        COMMAND ${CMAKE_COMMAND} -E touch ${STAMP_FILE}
        COMMAND ${CMAKE_COMMAND} -D FILE=${COMBINED_BLEND}
                -P ${CMAKE_CURRENT_SOURCE_DIR}/cmake/check_file_size.cmake
        DEPENDS ${WEIGHTED_STAMP}
                ${WEIGHTED_BLEND}
                ${CMAKE_CURRENT_BINARY_DIR}/blender-addons-installed
		${CMAKE_CURRENT_SOURCE_DIR}/combine_clothes.py
        COMMENT "Combining clothes from ${WEIGHTED_BLEND} into ${COMBINED_BLEND}"
    )
endfunction()

# Function to consolidate blend files
# Parameters:
#   INPUT_BLEND     - Input blend file to consolidate into (required)
#   COMBINED_BLEND  - Combined blend file to consolidate (required)
#   OUTPUT_BLEND    - Output consolidated blend file (required)
function(add_blend_consolidation INPUT_BLEND COMBINED_BLEND OUTPUT_BLEND)
    # Parse optional arguments
    set(options "")
    set(oneValueArgs "")
    set(multiValueArgs "")
    cmake_parse_arguments(CONSOLIDATE "${options}" "${oneValueArgs}" "${multiValueArgs}" ${ARGN})
    
    # Validate required arguments
    if(NOT INPUT_BLEND)
        message(FATAL_ERROR "INPUT_BLEND is required for add_blend_consolidation")
    endif()
    if(NOT COMBINED_BLEND)
        message(FATAL_ERROR "COMBINED_BLEND is required for add_blend_consolidation")
    endif()
    if(NOT OUTPUT_BLEND)
        message(FATAL_ERROR "OUTPUT_BLEND output path is required for add_blend_consolidation")
    endif()
    
    # Get the base name from the combined blend file for stamp derivation
    get_filename_component(COMBINED_NAME "${COMBINED_BLEND}" NAME_WE)
    # Remove "_combined" or "_shaped" suffix if present
    string(REGEX REPLACE "_(combined|shaped)$" "" TARGET_BASE "${COMBINED_NAME}")
    
    # Derive stamp dependency
    get_filename_component(COMBINED_DIR "${COMBINED_BLEND}" DIRECTORY)
    set(COMBINE_STAMP "${COMBINED_DIR}/${TARGET_BASE}-combined.stamp")
    
    # Ensure output directory exists
    get_filename_component(OUTPUT_DIR "${OUTPUT_BLEND}" DIRECTORY)
    
    add_custom_command(
        OUTPUT ${OUTPUT_BLEND}
        DEPENDS ${COMBINED_BLEND}
                ${CMAKE_CURRENT_SOURCE_DIR}/consolidate.py
                ${COMBINE_STAMP}
                ${INPUT_BLEND}
        COMMAND ${CMAKE_COMMAND} -E make_directory ${OUTPUT_DIR}
        COMMAND ${BLENDER} -b -Y ${INPUT_BLEND}
                -P ${CMAKE_CURRENT_SOURCE_DIR}/consolidate.py --
                ${COMBINED_BLEND}
                ${OUTPUT_BLEND}
        COMMAND ${CMAKE_COMMAND} -D FILE=${OUTPUT_BLEND}
                -P ${CMAKE_CURRENT_SOURCE_DIR}/cmake/check_file_size.cmake
        COMMENT "Consolidating ${COMBINED_BLEND} into ${OUTPUT_BLEND}"
    )
endfunction()

# Combined pipeline function
# Parameters:
#   INPUT_BLEND        - Input blend file (required)
#   WEIGHTED_BLEND     - Weighted clothes blend file (required)
#   FINAL_OUTPUT_BLEND - Final consolidated output (required)
function(add_clothes_pipeline INPUT_BLEND WEIGHTED_BLEND FINAL_OUTPUT_BLEND)
    # Parse optional arguments
    set(options "")
    set(oneValueArgs COMBINED_BLEND INTERMEDIATE_DIR)
    set(multiValueArgs "")
    cmake_parse_arguments(PIPELINE "${options}" "${oneValueArgs}" "${multiValueArgs}" ${ARGN})

    # Validate required arguments
    if(NOT INPUT_BLEND)
        message(FATAL_ERROR "INPUT_BLEND is required for add_clothes_pipeline")
    endif()
    if(NOT WEIGHTED_BLEND)
        message(FATAL_ERROR "WEIGHTED_BLEND is required for add_clothes_pipeline")
    endif()
    if(NOT FINAL_OUTPUT_BLEND)
        message(FATAL_ERROR "FINAL_OUTPUT_BLEND is required for add_clothes_pipeline")
    endif()

    # Get the base name for deriving intermediate filenames
    get_filename_component(WEIGHTED_NAME "${WEIGHTED_BLEND}" NAME_WE)
    string(REGEX REPLACE "_weighted$" "" TARGET_BASE "${WEIGHTED_NAME}")

    # Set intermediate directory
    if(NOT PIPELINE_INTERMEDIATE_DIR)
        set(PIPELINE_INTERMEDIATE_DIR "${CMAKE_CURRENT_BINARY_DIR}/clothes")
    endif()

    # Define intermediate combined blend file
    if(PIPELINE_COMBINED_BLEND)
        set(COMBINED_BLEND "${PIPELINE_COMBINED_BLEND}")
    else()
        set(COMBINED_BLEND "${PIPELINE_INTERMEDIATE_DIR}/${TARGET_BASE}_combined.blend")
    endif()
    set(SHAPED_BLEND "${PIPELINE_INTERMEDIATE_DIR}/${TARGET_BASE}_shaped.blend")


    # Step 1: Combine clothes
    add_clothes_combination(
        "${INPUT_BLEND}"
	"${WEIGHTED_BLEND}"
        "${COMBINED_BLEND}"
        OUTPUT_DIR "${PIPELINE_INTERMEDIATE_DIR}"
    )

    transfer_shape_keys(${INPUT_BLEND}
	    ${COMBINED_BLEND}
	    ${SHAPED_BLEND}
    )
    # Step 2: Consolidate (use shaped blend which has shape keys transferred)
    add_blend_consolidation(
        "${INPUT_BLEND}"
        "${SHAPED_BLEND}"
        "${FINAL_OUTPUT_BLEND}"
    )

    # Create a custom target to drive the whole pipeline
    add_custom_target(${TARGET_BASE}_pipeline ALL
	    DEPENDS ${FINAL_OUTPUT_BLEND} ${SHAPED_BLEND}
        COMMENT "Running complete clothes pipeline for ${TARGET_BASE}"
    )
endfunction()

weight_clothes(${CMAKE_CURRENT_SOURCE_DIR}/clothes-male-top.blend)
weight_clothes(${CMAKE_CURRENT_SOURCE_DIR}/clothes-male-bottom.blend)
weight_clothes(${CMAKE_CURRENT_SOURCE_DIR}/clothes-male-feet.blend)
weight_clothes(${CMAKE_CURRENT_SOURCE_DIR}/clothes-female-top.blend)
weight_clothes(${CMAKE_CURRENT_SOURCE_DIR}/clothes-female-bottom.blend)
weight_clothes(${CMAKE_CURRENT_SOURCE_DIR}/clothes-female-feet.blend)

# male
add_clothes_pipeline(
    "${CMAKE_CURRENT_SOURCE_DIR}/edited-normal-male.blend"                   # INPUT_BLEND
    "${CMAKE_CURRENT_BINARY_DIR}/clothes/clothes-male-bottom_weighted.blend" # WEIGHTED_BLEND
    "${CMAKE_CURRENT_BINARY_DIR}/edited-normal-male-consolidated-bottom.blend"      # BOTTOM_OUTPUT_BLEND
)

add_clothes_pipeline(
    "${CMAKE_CURRENT_BINARY_DIR}/edited-normal-male-consolidated-bottom.blend" # INPUT_BLEND
    "${CMAKE_CURRENT_BINARY_DIR}/clothes/clothes-male-top_weighted.blend" # WEIGHTED_BLEND
    "${CMAKE_CURRENT_BINARY_DIR}/edited-normal-male-consolidated-top.blend"      # FINAL_OUTPUT_BLEND
)

add_clothes_pipeline(
    "${CMAKE_CURRENT_BINARY_DIR}/edited-normal-male-consolidated-top.blend" # INPUT_BLEND
    "${CMAKE_CURRENT_BINARY_DIR}/clothes/clothes-male-feet_weighted.blend" # WEIGHTED_BLEND
    "${CMAKE_CURRENT_BINARY_DIR}/edited-normal-male-consolidated.blend"      # FINAL_OUTPUT_BLEND
)

# female
add_clothes_pipeline(
    "${CMAKE_CURRENT_SOURCE_DIR}/edited-normal-female.blend"                   # INPUT_BLEND
    "${CMAKE_CURRENT_BINARY_DIR}/clothes/clothes-female-bottom_weighted.blend" # WEIGHTED_BLEND
    "${CMAKE_CURRENT_BINARY_DIR}/edited-normal-female-consolidated-bottom.blend"      # FINAL_OUTPUT_BLEND
)

add_clothes_pipeline(
    "${CMAKE_CURRENT_BINARY_DIR}/edited-normal-female-consolidated-bottom.blend"                   # INPUT_BLEND
    "${CMAKE_CURRENT_BINARY_DIR}/clothes/clothes-female-top_weighted.blend" # WEIGHTED_BLEND
    "${CMAKE_CURRENT_BINARY_DIR}/edited-normal-female-consolidated-top.blend"      # FINAL_OUTPUT_BLEND
)

add_clothes_pipeline(
    "${CMAKE_CURRENT_BINARY_DIR}/edited-normal-female-consolidated-top.blend"                   # INPUT_BLEND
    "${CMAKE_CURRENT_BINARY_DIR}/clothes/clothes-female-feet_weighted.blend" # WEIGHTED_BLEND
    "${CMAKE_CURRENT_BINARY_DIR}/edited-normal-female-consolidated.blend"      # FINAL_OUTPUT_BLEND
)

