Skip to content

共享内存 API

dcc_mcp_core (shm 模块)

大型 DCC 场景数据的零拷贝共享内存传输。

概述

DCC 场景数据(几何体、动画缓存、帧缓冲区)可以轻松达到千兆字节。dcc-mcp-shm 提供了零拷贝替代方案:DCC 端将数据直接写入内存映射文件;消费者从同一映射区域读取,无需任何复制或序列化。

PySharedSceneBuffer

共享场景数据的高级包装器。

write()

python
from dcc_mcp_core import PySharedSceneBuffer, PySceneDataKind

# DCC 端:写入场景数据
vertices = bytes(1024 * 1024)  # 1 MiB 顶点数据
ssb = PySharedSceneBuffer.write(
    data=vertices,
    kind=PySceneDataKind.Geometry,
    source_dcc="Maya",
    use_compression=True
)

read()

python
# Agent 端:读回原始字节
recovered = ssb.read()
assert recovered == vertices

descriptor_json()

python
# 通过 IPC 发送 JSON 描述符到 Agent 端
json_descriptor = ssb.descriptor_json()
print(json_descriptor)

from_descriptor_json()

python
# 从 JSON 描述符重构
ssb = PySharedSceneBuffer.from_descriptor_json(json_descriptor)
data = ssb.read()

属性

属性类型描述
kindPySceneDataKind数据类型枚举
source_dccstr源 DCC 名称
use_compressionbool是否使用 LZ4 压缩
size_bytesint大小(字节)

PySceneDataKind

对数据进行分类的枚举:

类型描述
Geometry网格/顶点数据
AnimationCache动画缓存数据
Screenshot截图/图像数据
Arbitrary任意二进制数据

PyBufferPool

高性能场景的预分配缓冲区池。

构造函数

python
from dcc_mcp_core import PyBufferPool

pool = PyBufferPool(capacity=4, buffer_size=256 * 1024 * 1024)

参数:

  • capacity — 池中缓冲区槽位数量
  • buffer_size — 每个缓冲区的字节大小

方法

方法返回描述
acquire()PySharedBuffer从池中获取缓冲区(所有槽位占用时抛出 RuntimeError
available()int当前可用(空闲)槽位数
capacity()int缓冲区池总容量
buffer_size()int每个缓冲区的字节大小

示例

python
pool = PyBufferPool(capacity=4, buffer_size=1024 * 1024)
buf = pool.acquire()
buf.write(b"scene snapshot")
# 缓冲区在 buf 被垃圾回收时自动归还池
print(pool.available())  # 3

PySharedBuffer

内存映射共享缓冲区的直接访问。

create()

python
from dcc_mcp_core import PySharedBuffer

buf = PySharedBuffer.create(capacity=1024 * 1024)  # 1 MiB
buf_id = buf.id   # str 属性(非方法)
buf_path = buf.path()  # 底层内存映射文件路径

open()

python
# 跨进程传递:从路径 + id 重建
buf2 = PySharedBuffer.open(path=buf.path(), id=buf.id)
assert buf2.read() == buf.read()

方法

方法返回描述
write(data)int写入字节;返回已写入字节数。超出容量时抛出 RuntimeError
read()bytes读取当前数据
data_len()int当前已存储字节数
capacity()int缓冲区最大字节容量
clear()None重置 data_len 为 0
path()str底层内存映射文件路径
descriptor_json()str用于跨进程传递的 JSON 描述符

属性

属性类型描述
idstr唯一缓冲区标识符

性能说明

  • 零拷贝:使用内存映射文件时数据永远不会被复制
  • LZ4 压缩:可选压缩以 CPU 成本减少传输时间
  • 池重用:缓冲区池消除重复传输的分配开销

Released under the MIT License.