project(building-parts)
set(PARTS_FILES pier.blend)
set(FURNITURE_FILES furniture.blend furniture-sofa.blend)
set(PARTS_OUTPUT_DIRS)
foreach(PARTS_FILE ${PARTS_FILES})
	get_filename_component(FILE_NAME ${PARTS_FILE} NAME_WE)
	set(PARTS_OUTPUT_DIR ${CMAKE_BINARY_DIR}/resources/buildings/parts/${FILE_NAME})
	add_custom_command(
		OUTPUT ${PARTS_OUTPUT_DIR}
		COMMAND ${CMAKE_COMMAND} -E make_directory ${PARTS_OUTPUT_DIR}
		COMMAND ${BLENDER} ${CMAKE_CURRENT_SOURCE_DIR}/${PARTS_FILE}
			-b -Y -P
			${CMAKE_SOURCE_DIR}/assets/blender/scripts/export_building_parts.py
			-- ${PARTS_OUTPUT_DIR}
		DEPENDS ${CMAKE_CURRENT_SOURCE_DIR}/${PARTS_FILE} ${CMAKE_SOURCE_DIR}/assets/blender/scripts/export_building_parts.py)
	list(APPEND PARTS_OUTPUT_DIRS ${PARTS_OUTPUT_DIR})
endforeach()
foreach(FURNITURE_FILE ${FURNITURE_FILES})
	get_filename_component(FILE_NAME ${FURNITURE_FILE} NAME_WE)
	set(PARTS_OUTPUT_DIR ${CMAKE_BINARY_DIR}/resources/buildings/parts/${FILE_NAME})
	add_custom_command(
		OUTPUT ${CMAKE_CURRENT_BINARY_DIR}/${FILE_NAME}_baked.blend
		COMMAND ${CMAKE_COMMAND} -E copy ${CMAKE_CURRENT_SOURCE_DIR}/${FURNITURE_FILE} ${CMAKE_CURRENT_BINARY_DIR}/${FURNITURE_FILE}
		COMMAND ${BLENDER} -b ${CMAKE_CURRENT_BINARY_DIR}/${FURNITURE_FILE} -Y -P
			${CMAKE_CURRENT_SOURCE_DIR}/bake_furniture.py
		DEPENDS ${CMAKE_CURRENT_SOURCE_DIR}/${FURNITURE_FILE} ${CMAKE_CURRENT_SOURCE_DIR}/bake_furniture.py)
	add_custom_command(
		OUTPUT ${PARTS_OUTPUT_DIR}
		COMMAND ${CMAKE_COMMAND} -E make_directory ${PARTS_OUTPUT_DIR}
		COMMAND ${BLENDER} -b ${CMAKE_CURRENT_BINARY_DIR}/${FILE_NAME}_baked.blend
			-Y -P
			${CMAKE_CURRENT_SOURCE_DIR}/export_furniture_parts.py
			-- ${PARTS_OUTPUT_DIR}
		DEPENDS ${CMAKE_CURRENT_BINARY_DIR}/${FILE_NAME}_baked.blend ${CMAKE_CURRENT_SOURCE_DIR}/export_furniture_parts.py)
	list(APPEND PARTS_OUTPUT_DIRS ${PARTS_OUTPUT_DIR})
endforeach()
add_custom_target(import_building_parts ALL DEPENDS ${PARTS_OUTPUT_DIRS})
