3ds Max SDK 提供了一组基础工具类与实用程序,用于数学运算、字符串处理、路径操作、内存管理与全局入口访问。这些工具分散在多个头文件中,核心数学类位于根命名空间下,字符串与路径类位于 MaxSDK::Util 命名空间。
注意:本模块不包含任何名为
MathUtil、StringUtil、FileUtil、PathUtil、LogSystem、ConfigManager或Utility命名空间的公开类——这些均不存在于 SDK 中。SDK 中调试输出相关的DebugPrint()是自由函数/宏,而非类。
头文件:point3.h
基类:MaxHeapOperators
三维点(向量)类,使用 float x, y, z 坐标。
#include "point3.h"
class Point3 : public MaxHeapOperators
{
public:
float x, y, z;
Point3();
Point3(float X, float Y, float Z);
float Length() const;
float LengthSquared() const;
Point3 Normalize() const; // 返回归一化单位向量
Point3& Unify(); // 就地归一化,返回原向量长度
float LengthUnify();
int MaxComponent() const; // 绝对值最大的分量下标
int MinComponent() const;
Point3 operator-() const;
Point3 operator+(const Point3&) const;
Point3 operator-(const Point3&) const;
Point3 operator*(const Point3&) const; // 逐分量相乘
Point3 operator/(const Point3&) const;
Point3 operator^(const Point3&) const; // 叉乘(cross product)
float operator%(const Point3&) const; // 点乘(dot product)
static const Point3 Origin;
static const Point3 XAxis;
static const Point3 YAxis;
static const Point3 ZAxis;
};
// 自由函数(point3.h)
float DotProd(const Point3& a, const Point3& b);
Point3 CrossProd(const Point3& a, const Point3& b);
float Length(const Point3& p);
Point3 Normalize(const Point3& p);
头文件:matrix3.h
基类:MaxHeapOperators
4×3 三维变换矩阵(float m[4][3])。在 3ds Max 中向量默认为行向量。
#include "matrix3.h"
class Matrix3 : public MaxHeapOperators
{
public:
Matrix3(); // 默认初始化为单位矩阵
const Point3& operator[](int i) const; // 返回第 i 行 Point3
const Point3& GetTrans() const;
void RotateX(float angle); // 增量旋转
void RotateY(float angle);
void RotateZ(float angle);
bool IsIdentity() const;
static const Matrix3 Identity;
};
// 矩阵构建自由函数
Matrix3 TransMatrix(const Point3& p); // 平移矩阵
Matrix3 ScaleMatrix(const Point3& s); // 缩放矩阵
Matrix3 RotateXMatrix(float angle); // X 轴旋转
Matrix3 RotateYMatrix(float angle);
Matrix3 RotateZMatrix(float angle);
Matrix3 RotAngleAxisMatrix(const Point3& axis, float angle);
Matrix3 Inverse(const Matrix3& M);
头文件:box3.h
轴对齐包围盒(AABB),由两个 Point3(最小角 lo 与最大角 hi)定义。
#include "box3.h"
class Box3 : public MaxHeapOperators
{
public:
Point3 pmin, pmax;
Box3();
Box3(const Point3& p, const Point3& q);
void Init();
Point3 operator[](int i) const;
Box3& operator+=(const Point3& p);
Box3& operator+=(const Box3& b);
void Scale(float s);
void Translate(const Point3& p);
void EnlargeBy(float s);
bool IsEmpty() const;
bool IsInitialized() const;
bool Contains(const Point3& p) const;
bool Contains(const Box3& b) const;
bool Intersects(const Box3& b) const;
Point3 Center() const;
Point3 Width() const;
};
头文件:quat.h
基类:MaxHeapOperators
四元数,紧凑表示三维朝向(float x, y, z, w)。左手法则是 3ds Max 的约定。
#include "quat.h"
class Quat : public MaxHeapOperators
{
public:
float x, y, z, w;
Quat(float X, float Y, float Z, float W);
Quat(const Matrix3& mat);
Quat(const AngAxis& aa);
Quat& Set(float X, float Y, float Z, float W);
Quat& Set(const Matrix3& mat);
Quat& SetEuler(float X, float Y, float Z);
void GetEuler(float* X, float* Y, float* Z) const;
Quat Inverse() const;
Quat Conjugate() const;
void Normalize();
void MakeMatrix(Matrix3& mat, bool flag = false) const;
int operator==(const Quat& a) const;
Quat operator*(const Quat& a) const; // 四元数相乘
};
头文件:strclass.h(由 Path.h 等包含;_M() 宏定义于 strbasic.h)
MSTR 与 WStr 都定义于 strclass.h。MSTR 是宽/窄字符串类的 typedef:在 MCHAR_IS_WCHAR(即 Unicode 构建,3ds Max 2022 默认)下为 typedef WStr MSTR;,非 Unicode 构建下为 typedef CStr MSTR;。因此实际底层类是 WStr(Unicode 宽字符串),它自动按内容长度分配内存,便于国际化。TSTR 则是根据 _UNICODE 在 WStr/CStr 之间切换的 typedef。_M(x) 宏用于包裹字符串字面量(等价于 _T())。
#include "strclass.h"
// MSTR 定义于 strclass.h: WStr MSTR ...
MSTR s1 = _M("This is string1.");
MSTR s2 = _M("This is string2.");
MSTR concat = s1 + s2; // 自动分配足够空间
int len = s1.Length(); // 字符串长度
bool equal = (s1 == s2); // 比较
const MCHAR* p = s1; // 隐式转换为 const MCHAR*
const MCHAR* d = s1.data(); // 取内部字符指针
常见方法(均已验证存在):Length()、operator+/operator+=、operator==/operator!=、operator const MCHAR*()、data()。
头文件:Path.h
命名空间:MaxSDK::Util
基类:MaxHeapOperators
路径类,在符号字符串之上提供丰富的路径操作能力(支持 3ds Max 符号解析、UNC 转换、相对/绝对转换等)。
#include "Path.h"
namespace MaxSDK { namespace Util {
class Path : public MaxHeapOperators
{
public:
Path();
Path(const MSTR& pathString);
Path(const MCHAR* pathString);
Path(const Path& other);
~Path();
Path& SetPath(const Path& other);
Path& SetPath(const MCHAR* pathString);
Path& SetPath(const MSTR& pathString);
Path& operator=(const Path& other);
bool IsAbsolute() const;
bool IsEmpty() const;
bool IsLegal() const;
bool IsRoot() const;
bool IsUnc() const;
bool HasUnresolvedSymbols() const;
bool IsEqual(const Path& other) const;
Path& AddTrailingBackslash();
Path& RemoveTrailingBackslash();
Path& ConvertToAbsolute(); // 转为相对当前工程目录的绝对路径
Path& ConvertToUNC(); // 映射盘符路径转为 UNC 路径
Path& ConvertToRelativeToProjectFolder();
bool Exists() const; // 文件或目录是否存在
MSTR GetString() const; // 当前字符串表示(不解析符号)
const MCHAR* GetCStr() const; // 内部 c 字符串
};
} } // namespace MaxSDK::Util
头文件:Noncopyable.h
命名空间:MaxSDK::Util
一个便捷的 mix-in 基类,使任何派生类不可拷贝(删除拷贝构造与拷贝赋值)。
#include "Noncopyable.h"
namespace MaxSDK { namespace Util {
class Noncopyable : public MaxHeapOperators
{
protected:
Noncopyable();
~Noncopyable();
private:
Noncopyable(const Noncopyable&);
Noncopyable& operator=(const Noncopyable&);
};
} } // namespace MaxSDK::Util
// 用法:
class MySingleton : public MaxSDK::Util::Noncopyable { /* ... */ };
MaxSDK::Util 命名空间下还包含其它真实工具类,如 AutoPtr、MaxString、BufferedFile、StopWatch、Exception 等(以官方命名空间参考页为准)。
头文件:autoptr.h
命名空间:MaxSDK
MaxSDK::AutoPtr 是 RAII(资源获取即初始化)式的自动所有权指针,语义等同于标准库的 std::auto_ptr,支持破坏性拷贝(destructive copy):拷贝后源指针会放弃所有权。它不是引用计数智能指针,没有 Acquire()/Release() 引用计数方法。
#include "autoptr.h"
namespace MaxSDK {
template<typename Type, typename DestructorPolicy>
class AutoPtr
{
public:
typedef Type element_type;
explicit AutoPtr(Type* p = NULL); // 构造并取得所有权
~AutoPtr(); // 析构时自动清理
AutoPtr(AutoPtr& a); // 拷贝构造,取得源的所有权
Type& operator*() const;
Type* operator->() const;
Type* Get() const; // 取裸指针
Type* Release(); // 放弃所有权,交还调用方
void Reset(Type* p = NULL); // 接管新对象,删除旧对象
bool IsNull() const;
};
} // namespace MaxSDK
常见变体:默认 AutoPtr(单对象)、ArrayAutoPtr(数组)、DeleteThisAutoPtr(DeleteThis() 语义)。
头文件:MaxHeap.h
MaxHeapOperators 是控制内存管理的基类。几乎所有 SDK 类都直接或间接继承自它。它仅重载 new/delete 操作符,使派生类的内存分配统一走 Max 核心堆——这保证插件与 Max 核心即便使用不同 CRT 也能安全跨 DLL 边界传递内存。
#include "MaxHeap.h"
class MaxHeapOperators
{
public:
static void* operator new(size_t size);
static void operator delete(void* ptr);
// ... 多种 new/delete 重载(文件名/行号、nothrow、对齐等)
// 对齐分配(静态成员)
static void* aligned_malloc(size_t size, size_t alignment);
static void aligned_free(void* ptr);
static void* aligned_realloc(void* ptr, size_t size, size_t alignment);
};
说明:
MaxHeapOperators不提供MAX_MALLOC/MAX_FREE宏。对于未派生自MaxHeapOperators、需要直接堆分配的对象,官方内存管理文档建议使用MAX_malloc()/MAX_free()等宏(定义于maxheapdirect.h),而非标准malloc/free。
GetCOREInterface() 是返回全局 Interface* 的自由函数,用于访问主应用接口(视口、节点、场景等)。
#include "GetCOREInterface.h"
Interface* ip = GetCOREInterface(); // 全局主接口
ViewExp* vp = &ip->GetActiveViewExp(); // 当前活动视口
#include "point3.h"
#include "strclass.h"
#include "Path.h"
#include "MaxHeap.h"
void ToolsExample()
{
// ---- MSTR 字符串 ----
MSTR name = _M("MyPlugin");
name += _M("_v1");
const MCHAR* cstr = name; // 隐式转换为 const MCHAR*
// ---- Point3 数学运算 ----
Point3 a(1.0f, 0.0f, 0.0f);
Point3 b(0.0f, 1.0f, 0.0f);
float dot = a % b; // 点乘 => 0.0
Point3 cross = a ^ b; // 叉乘 => (0,0,1)
Point3 n = a.Normalize(); // 归一化
// ---- MaxSDK::Util::Path 路径处理 ----
MaxSDK::Util::Path p(_M("C:\\temp\\scene.max"));
if (p.IsAbsolute())
{
MSTR s = p.GetString(); // 取字符串表示
bool ex = p.Exists(); // 文件是否存在
}
}
// 不可拷贝类的典型用法
class Config : public MaxSDK::Util::Noncopyable
{
public:
static Config& Instance() { static Config c; return c; }
private:
Config() {}
};
本模块覆盖的真实 API:
Point3、Matrix3、Box3、Quat(坐标/矩阵/包围盒/四元数)。MSTR(strclass.h,Unicode)。MaxSDK::Util::Path、MaxSDK::Util::Noncopyable。MaxSDK::AutoPtr(RAII 破坏性拷贝)。MaxHeapOperators(new/delete 重载)。GetCOREInterface()。这些工具类为插件开发提供基础能力,使用时需注意跨 DLL 边界的内存分配应统一走 Max 堆。
Utility(.dlu)。
core.lib maxutil.lib geom.lib maxscrpt.lib Paramblk2.lib#include "utilapi.h" // UtilityObj / UtilityDesc
#include "strclass.h" // MSTR
#include "Path.h" // MaxSDK::Util::Path
class MyUtility : public UtilityObj {
public:
MSTR name;
void BeginEditParams(Interface* ip, IUtil* iu) override { }
void EndEditParams(Interface* ip, IUtil* iu) override { }
void DoPathWork() {
MSTR base = _M("C:\\temp\\");
MSTR file = _M("scene.max");
MSTR full = base + file; // MSTR 拼接
MaxSDK::Util::Path p(full);
if (p.IsAbsolute())
name = p.GetString(); // 路径处理真实用法
}
};
// UtilityObj 标准描述符(以本地 SDK 头文件为准)
class MyUtilityDesc : public UtilityDesc {
public:
void* Create(TCHAR* /*className*/) { return new MyUtility(); }
const TCHAR* ClassName() { return _M("MyUtility"); }
};
target_link_libraries(<Target> PRIVATE core maxutil geom maxscrpt Paramblk2)