Repository navigation
Expand file tree
/
Copy pathmemory_file_data.py
More file actions
95 lines (68 loc) · 2.38 KB
/
Copy pathmemory_file_data.py
File metadata and controls
95 lines (68 loc) · 2.38 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
"""Writes/Reads simple data to/from memory.
Topics:
* c4d.storage.ByteSeq
* c4d.storage.MemoryFileStruct
* c4d.storage.HyperFile
"""
__author__ = "XXX"
__copyright__ = "Copyright (C) 2026 MAXON Computer GmbH"
__license__ = "Apache-2.0 License"
__version__ = "R13"
import c4d
def WriteMemoryFile():
"""Writes data to a memory file.
Returns:
The byte sequence or None
"""
# Creates a MemoryFile, data will be written in
mfs = c4d.storage.MemoryFileStruct()
# Sets the memory file ready to be written to
mfs.SetMemoryWriteMode()
# Initializes a HyperFile
file = c4d.storage.HyperFile()
# Opens the HyperFile, with the MemoryFile used
if not file.Open(0, mfs, c4d.FILEOPEN_WRITE, c4d.FILEDIALOG_NONE):
raise RuntimeError("Failed to open the HyperFile.")
# Writes a string to the memory file
file.WriteString("MemoryFileStruct Example")
# Writes an integer to the memory file
file.WriteInt32(1214)
if c4d.GetC4DVersion() > 22600:
# Writes an byte array to the memory file
file.WriteMemory(bytearray("Bytes Array data", "utf-8"))
# Closes the file
file.Close()
# Returns the memory file data (the byte sequence)
return mfs.GetData()[0]
def ReadMemoryFile(data):
"""Reads data from a memory file.
Args:
data: the byte sequence to read
"""
# Creates a MemoryFile, data will be read in
mfs = c4d.storage.MemoryFileStruct()
# Sets the memory file ready to be read from
mfs.SetMemoryReadMode(data, len(data))
file = c4d.storage.HyperFile()
# Opens the memory file and set it ready for reading
if not file.Open(0, mfs, c4d.FILEOPEN_READ, c4d.FILEDIALOG_NONE):
raise RuntimeError("Failed to open the HyperFile.")
# Reads the string from the memory file
value = file.ReadString()
print("The string value is :", value)
# Reads the int from the memory file
value = file.ReadInt32()
print("The int value is :", value)
if c4d.GetC4DVersion() > 22600:
# Reads the bytes from the memory file
value = file.ReadMemory()
print("The memory value is :", value)
# Closes file
file.Close()
def main():
# Write data in a MemoryFile and get the byte sequence
bytes = WriteMemoryFile()
# Read data from the byte sequence
ReadMemoryFile(bytes)
if __name__ == '__main__':
main()