# 3ds Max SDK - 核心基础模块
核心基础模块是 3ds Max SDK 的基石,提供了所有其他模块依赖的基础接口机制、可动画对象体系、内存管理与数学工具。
baseinterface.hBaseInterface 是所有 3ds Max 接口(包括 FPInterface 及其派生)的基类,提供接口标识、子接口访问、生命周期管理与克隆能力。
接口标识宏:
#define BASEINTERFACE_ID Interface_ID(0, 1)
关键方法(已对照官方 cpp_ref 核实):
class BaseInterface : public InterfaceServer {
public:
UtilExport virtual ~BaseInterface();
// 身份标识
UtilExport virtual Interface_ID GetID();
// 子接口访问(继承自 InterfaceServer)
UtilExport BaseInterface* GetInterface(Interface_ID id);
// 生命周期类型枚举
enum LifetimeType { noRelease, immediateRelease, wantsRelease, serverControlled };
// 生命周期控制:返回该接口实际的生命周期策略
virtual LifetimeType LifetimeControl();
// 引用计数式获取/释放(非 AddRef/Release,注意方法名)
virtual BaseInterface* AcquireInterface();
virtual void ReleaseInterface();
virtual void DeleteInterface();
// 克隆
virtual BaseInterface* CloneInterface(void* remapDir = NULL);
// 通知回调注册/注销
virtual bool RegisterNotifyCallback(InterfaceNotifyCallback* incb);
virtual void UnRegisterNotifyCallback(InterfaceNotifyCallback* incb);
};
注意:基类
BaseInterface的LifetimeControl()默认返回noRelease(因为AcquireInterface/ReleaseInterface在基类层面不执行真正的引用计数)。派生类若改变获取/释放语义,需相应调整返回值。
baseinterface.h所有可"提供服务接口"的对象的基类。BaseInterface 即派生自 InterfaceServer。
class InterfaceServer : public MaxHeapOperators {
public:
UtilExport virtual ~InterfaceServer();
// 接口主入口
UtilExport virtual BaseInterface* GetInterface(Interface_ID id);
// 模板辅助:按类型取出子接口(ID 取自目标接口的 GetID())
template <class InterfaceType>
InterfaceType* GetTypedInterface() {
return static_cast<InterfaceType*>(GetInterface(InterfaceType::GetID()));
}
};
maxtypes.h(随 baseinterface.h 等一并引入)用于唯一标识接口:
// 自定义接口 ID 示例(高 32 位 + 低 32 位)
#define MY_INTERFACE_ID Interface_ID(0x12345678, 0x87654321)
Interface_ID id = MY_INTERFACE_ID;
BaseInterface* iface = server->GetInterface(id);
Animatable.hAnimatable : public InterfaceServer, public MaxSDK::Util::Noncopyable(注意方向:ReferenceMaker : public Animatable,而非相反)Animatable 是 3ds Max 中大量场景实体与插件类型的基类,提供 Track View 子动画枚举、参数块管理与动画属性等能力。
class Animatable : public InterfaceServer, public MaxSDK::Util::Noncopyable {
public:
// 类标识
virtual Class_ID ClassID();
virtual SClass_ID SuperClassID() = 0;
// 参数块管理
virtual int NumParamBlocks();
virtual IParamBlock2* GetParamBlock(int i);
// 子动画对象管理
virtual int NumSubs();
virtual Animatable* SubAnim(int i);
// 子动画名称:现代 SDK 返回 MSTR,并接受 localized 参数;
// 旧版(如 3ds Max 9 及更早 SDK)返回 TCHAR*、签名为 SubAnimName(int i)
virtual MSTR SubAnimName(int i, bool localized = true);
};
SubAnimName的返回类型随版本变化:现代 SDK(Animatable.h)返回MSTR;旧版实现返回TCHAR*。文档化代码应优先使用现代签名SubAnimName(int i, bool localized = true)。
Animatable.h(AppData 相关类型)Animatable 提供 AppData 挂接机制(如 SetAppData()/GetAppData() 等),可用于将自定义数据绑定到任意动画对象并随 .max 文件保存。(具体方法签名依版本而异,调用前请以本地 SDK 头文件为准。)
MaxHeap.hMaxHeapOperators 是所有核心类(如 InterfaceServer)的基类,提供统一的 operator new / operator delete 重载(含 nothrow、带文件名/行号的调试版本)。
class MaxHeapOperators {
public:
static void* operator new(size_t size);
static void operator delete(void* ptr);
// … 另有 nothrow、对齐、调试版本重载
};
没有
MAX_MALLOC/MAX_FREE/MAX_REALLOC宏,也没有MaxMalloc()/MaxFree()这类全局内存分配函数。 内存管理由MaxHeapOperators的new/delete重载(含nothrow、带文件名/行号的调试版本)构成。请勿使用文档中不存在的MAX_*内存宏或MaxMalloc函数。
autoptr.htemplate <class T>
class AutoPtr {
public:
explicit AutoPtr(T* ptr = NULL);
~AutoPtr();
AutoPtr(AutoPtr& a);
AutoPtr& operator=(const AutoPtr& a);
T& operator*() const;
T* operator->() const;
T* Get() const;
T* Release();
void Reset(T* p = NULL);
bool IsNull() const;
};
AutoPtr<T> 是一个引用计数智能指针封装:构造/赋值时对内部对象执行 Acquire(),析构时执行 Release(),从而自动管理引用计数对象的生命周期。
point3.hclass Point3 {
public:
float x, y, z;
Point3();
Point3(float X, float Y, float Z);
// 运算符重载
Point3 operator+(const Point3& p) const;
Point3 operator-(const Point3& p) const;
Point3 operator*(const Point3& p) const; // 逐分量相乘
Point3 operator/(const Point3& p) const;
Point3 operator^(const Point3& p) const; // 叉积(自由函数 CrossProd)
// 长度与归一化
float Length() const;
Point3& Unify(); // 归一化(返回单位向量引用,非 const)
float LengthUnify(); // 返回长度并归一化
// 点积运算符
float operator%(const Point3& p) const; // 点积(自由函数 DotProd)
};
点积使用
DotProd(a, b)自由函数或a % b运算符;叉积使用CrossProd(a, b)自由函数。
matrix3.hclass Matrix3 {
public:
// 行访问
Point3 GetRow(int i) const;
void SetRow(int i, Point3 p);
// 实例方法
void Translate(const Point3& p);
void RotateX(float angle);
void RotateY(float angle);
void RotateZ(float angle);
void Scale(const Point3& s, BOOL trans = FALSE);
void Invert(); // 原地求逆;另有自由函数 Matrix3 Inverse(const Matrix3& M)
void SetFromToUp(const Point3& from, const Point3& to, const Point3& up);
void GetYawPitchRoll(float* yaw, float* pitch, float* roll);
};
// 自由函数(matrix3.h 中以 friend/GEOMEXPORT 声明,非静态成员)
Matrix3 RotateXMatrix(float angle);
Matrix3 RotateYMatrix(float angle);
Matrix3 TransMatrix(const Point3& p);
Matrix3 ScaleMatrix(const Point3& s);
校验要点:
Translate/RotateX/RotateY/RotateZ/Scale/Invert/SetFromToUp/GetYawPitchRoll均为Matrix3的成员(返回void);RotateXMatrix/TransMatrix/ScaleMatrix等是matrix3.h中声明的自由函数(非静态成员),小写命名为常规写法。
box3.hclass Box3 : public MaxHeapOperators {
public:
Point3 pmin, pmax; // 注意字段名为 pmin / pmax
Box3();
Box3(const Point3& p, const Point3& q);
void Init(); // 重置为空盒
Box3& operator+=(const Point3& p); // 包含点 p 扩展包围盒(无 AddPoint 方法)
Box3& operator+=(const Box3& b); // 包含盒 b 扩展包围盒(无 AddBox 方法)
Point3 Center() const; // 返回中心点
Point3 Width() const; // 返回范围向量 (pmax - pmin),并非标量
// 无 Height()/Depth() 成员;范围可经 Width() 与 pmin/pmax 推导
};
quat.hclass Quat {
public:
float x, y, z, w;
Quat();
Quat(float x, float y, float z, float w);
Quat(const AngAxis& aa);
Quat operator*(const Quat& q) const;
Quat Inverse() const;
void Normalize();
};
GetCOREInterface.h(GetCOREInterface 系列)/ CoreFunctions.h(Get3DSMAXVersion 等)/ coreexp.h(导出宏 CoreExport/UtilExport)CoreExport Interface* GetCOREInterface();
CoreExport Interface14* GetCOREInterface14();
CoreExport Interface15* GetCOREInterface15();
// 更高版本接口在更新版 SDK 中继续递增(如 GetCOREInterface16/17 …)
// 系统版本信息
CoreExport int Get3DSMAXVersion(); // 返回版本编码,2022 约为 24000
CoreExport int Get3DSMAXBuildNumber(); // 返回构建号
没有
GetHInstance()函数。 主程序实例句柄是 SDK 中的全局变量hInstance,而非通过函数获取。同时也不存在GetResourceHInstance()之类的虚构函数(资源句柄相关以本地 SDK 头文件为准)。
maxversion.h(实际定义位置;3dsmaxconfig.h 不含这些宏)// 真实存在的版本宏(定义于 maxversion.h)
#ifndef MAX_VERSION_MAJOR
#define MAX_VERSION_MAJOR 24 // 2022 对应主版本 24,而非 2022
#endif
#ifndef MAX_VERSION_MINOR
#define MAX_VERSION_MINOR 0
#endif
// 没有 MAX_X64 宏:应以 _WIN64 / _M_X64 判定平台
没有
MAX_RELEASE/MAX_VERSION_NUMBER/MAX_X64_BUILD/MAX_X64这类宏。 正确宏为MAX_VERSION_MAJOR、MAX_VERSION_MINOR(定义于maxversion.h)。Get3DSMAXVersion()返回形如 24000 的编码(高 16 位为主版本,低 16 位为次版本)。
#include "baseinterface.h"
#include "GetCOREInterface.h"
class MyInterface : public BaseInterface {
public:
static Interface_ID ID() { return Interface_ID(0x12345678, 0x87654321); }
Interface_ID GetID() override { return ID(); }
// 自定义方法 ...
};
void UseInterface(InterfaceServer* server) {
MyInterface* myIface =
static_cast<MyInterface*>(server->GetInterface(MyInterface::ID()));
if (myIface) {
// 使用接口(按 LifetimeControl() 决定是否需要 ReleaseInterface)
myIface->ReleaseInterface(); // 若策略为 wantsRelease
}
}
#include "point3.h"
#include "matrix3.h"
void TransformExample() {
Point3 point(1.0f, 2.0f, 3.0f);
Matrix3 transform = Matrix3(1); // 单位矩阵
transform.Translate(Point3(10.0f, 0.0f, 0.0f));
Point3 transformed = transform * point; // 应用变换
float dot = point % point; // 点积
float len = point.Length();
}
GetInterface() 取得的接口,依据其 LifetimeControl() 返回值决定是否需要 ReleaseInterface()。MaxHeapOperators 重载的 new/delete;不存在 MAX_MALLOC/MAX_FREE 宏,也不存在 MaxMalloc()/MaxFree() 全局函数。Animatable 继承自 ReferenceMaker;子动画名称现代返回 MSTR。本文档基于 3ds Max 2022 SDK 官方 C++ API Reference 核对编写
典型 Utility(.dlu)或通用插件骨架(基类多为 MaxHeapOperators / Animatable 系)。
core.lib maxutil.lib geom.lib maxscrpt.lib Paramblk2.lib#include "MaxHeap.h"
#include "strclass.h"
#include "point3.h"
// 继承 MaxHeapOperators 的小工具类(内存分配统一走 Max 堆)
class MyData : public MaxHeapOperators {
public:
MSTR label;
Point3 pivot;
MyData(const MSTR& name, const Point3& p)
: label(name), pivot(p) {}
MSTR Describe() const {
MSTR s = label + _M(" @ pivot");
return s;
}
};
void CoreSample() {
MyData d(_M("Anchor"), Point3(1.0f, 2.0f, 3.0f));
MSTR desc = d.Describe(); // MSTR 拼接结果
float len = d.pivot.Length();
}
target_link_libraries(<Target> PRIVATE core maxutil geom maxscrpt Paramblk2)