Skip to content

Instantly share code, notes, and snippets.

@D-KeyChen
Last active November 12, 2023 10:53
Show Gist options
  • Save D-KeyChen/5a4c5b4b8f93a61c5c9274927996aaa5 to your computer and use it in GitHub Desktop.
Save D-KeyChen/5a4c5b4b8f93a61c5c9274927996aaa5 to your computer and use it in GitHub Desktop.
3ds Max SDK CMAKE
cmake_minimum_required(VERSION 3.15)
message(VERBOSE "package module begin: Max")
if (NOT CMAKE_SIZEOF_VOID_P EQUAL 8)
message(FATAL_ERROR "Please switch to x64 build.")
endif ()
# Set Default Max SDK Version
set(MAX_VERSION 2024 CACHE INTERNAL "3ds Max SDK Build Version")
message(VERBOSE "Max SDK Version: ${MAX_VERSION}")
# Set Default Max SDK Path
if (DEFINED ENV{MAX_SDK_ROOT})
set(MAX_SDK_ROOT $ENV{MAX_SDK_ROOT} CACHE STRING "3ds Max SDK root path")
else ()
if (NOT DEFINED ENV{ADSK_3DSMAX_SDK_${MAX_VERSION}})
set(ENV{ADSK_3DSMAX_SDK_${MAX_VERSION}} "C:/Program Files/Autodesk/3ds Max ${MAX_VERSION} SDK/maxsdk")
endif ()
set(MAX_SDK_ROOT $ENV{ADSK_3DSMAX_SDK_${MAX_VERSION}} CACHE STRING "3ds Max SDK root path")
endif ()
message(VERBOSE "Max SDK directory: ${MAX_SDK_ROOT}")
# include and library directory
find_path(MAX_INCLUDE_DIR CoreExport.h PATHS ${MAX_SDK_ROOT} PATH_SUFFIXES include)
find_path(MAX_LIBRARY_DIR core.lib PATHS ${MAX_SDK_ROOT} PATH_SUFFIXES lib/x64/Release)
message(VERBOSE "Max include directory: ${MAX_INCLUDE_DIR}")
message(VERBOSE "Max library directory: ${MAX_LIBRARY_DIR}")
include_directories(SYSTEM ${MAX_INCLUDE_DIR} ${MAX_INCLUDE_DIR}/geom)
link_directories(${MAX_LIBRARY_DIR})
# necessary macros
add_definitions(-D_UNICODE -DUNICODE -D_USRDLL)
# link Max libraries
function(link_max_libraries)
message(VERBOSE "function begin: link_max_libraries")
set(prefix PREFIX)
set(all_libs ALL_LIBS)
set(options ${all_libs})
cmake_parse_arguments(${prefix} "${options}" "" "" ${ARGN})
set(targets ${${prefix}_UNPARSED_ARGUMENTS})
message(VERBOSE "Target List: ${targets}")
message(VERBOSE "${prefix}_${all_libs}: ${${prefix}_${all_libs}}")
foreach (each_target IN LISTS targets)
message(VERBOSE "Current target: ${each_target}")
if (NOT TARGET ${each_target})
message(WARNING "This is not a target name: ${each_target}")
continue()
endif ()
# get target suffix
get_target_property(curr_suffix ${each_target} SUFFIX)
message(VERBOSE "Current target suffix: ${curr_suffix}")
set(ext ${curr_suffix})
if (ext)
string(LENGTH ${ext} string_length)
math(EXPR substring_length "${string_length} - 1")
string(SUBSTRING ${curr_suffix} 1 ${substring_length} ext)
string(TOLOWER ${ext} ext)
else ()
set(ext "NONE")
endif ()
_get_plugin_libs(${ext} libs ${${prefix}_${all_libs}})
target_link_libraries(${each_target} ${libs})
endforeach ()
message(VERBOSE "function end: link_max_libraries")
endfunction()
# Add dependent libraries based on suffix. Data comes from 2024 SDK Visual Studio template
function(_get_plugin_libs ext out-var all)
message(VERBOSE "function begin: get_plugin_libs")
message(VERBOSE "ext var: ${ext}")
message(VERBOSE "link all libraries: ${all}")
set(libs "core.lib;maxutil.lib;maxscrpt.lib;paramblk2.lib")
set(ext_list
"bmi, bmf, dlv, bms, dlh, NONE"
dlc
"dle, dlf, dli"
"dlm, dlo, dlr, dlt, dlb, dlk"
"dlu, gup"
flt
dlf
dle # No difference 3DXI
)
set(lib_list
bmm.lib
"expr.lib, geom.lib, gfx.lib, mesh.lib"
"geom.lib, gfx.lib, mesh.lib"
"bmm.lib, geom.lib, gfx.lib, mesh.lib, manipsys.lib"
"bmm.lib, geom.lib, gfx.lib, mesh.lib, gup.lib"
"bmm.lib, flt.lib"
viewFile.lib
igame.lib
)
set(desc_list
"Bitmap manager library"
"Expression, geometry, graphics, mesh library"
"Geometry, graphics, mesh library"
"Bitmap manager, geometry, graphics, mesh, graphic manipulators library"
"Bitmap manager, geometry, graphics, mesh, global utility library"
"Bitmap manager, filters library"
"File viewer library"
"Data exchange interfacee library"
)
list(LENGTH ext_list num)
math(EXPR num "${num} - 1")
foreach (i RANGE ${num})
list(GET ext_list ${i} each_ext)
list(GET lib_list ${i} each_lib)
list(GET desc_list ${i} each_desc)
string(REPLACE ", " ";" each_ext_list ${each_ext})
string(REPLACE ", " ";" each_lib_list ${each_lib})
list(FIND each_ext_list ${ext} index)
if (all OR NOT index EQUAL -1)
list(APPEND libs ${each_lib_list})
message(VERBOSE "Link ${each_desc}: ${each_lib}")
endif ()
endforeach ()
list(REMOVE_DUPLICATES libs)
set(${out-var} ${libs} PARENT_SCOPE)
message(VERBOSE "function end: get_plugin_libs")
endfunction()
message(VERBOSE "package module end: Max")
@D-KeyChen
Copy link
Author

3ds Max SDK cmake package module

Call method:

list(APPEND CMAKE_MODULE_PATH <FindMax.cmake directory>)
find_package(Max REQUIRED)

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment