# Setting and retrieving the data using UserDataBlob.Array32
#
import array
Application.NewScene("", False)
blob = Application.ActiveSceneRoot.AddProperty("UserDataBlob", False, "myBlob");
Application.LogMessage("----------------Setting the Array32 Property----------------")
myString = "This is a string!"
# Convert the string to an unsigned byte array
myStringAsAByteArray = array.array('B', myString)
Application.LogMessage("myStringAsAByteArray = " + str(myStringAsAByteArray))
# To convert the unsigned byte (8 bit) array to an unsigned integer (32 bit) array,
# the size of the byte array needs to be a multiple of 4.
# To do so, let's pad the end of the byte array with null bytes
NumBytesPadded = 0
while len(myStringAsAByteArray) % 4 != 0:
myStringAsAByteArray.append( 0 )
NumBytesPadded += 1
Application.LogMessage("myStringAsAByteArray = " + str(myStringAsAByteArray))
Application.LogMessage("NumBytesPadded = " + str(NumBytesPadded))
# Convert the unsigned byte array to an unsigned integer array
# and add the number of null bytes padded as the first index of the integer array
myStringAsAnIntArray = array.array('I', myStringAsAByteArray.tostring())
myStringAsAnIntArray.insert(0, NumBytesPadded)
Application.LogMessage("myStringAsAnIntArray = " + str(myStringAsAnIntArray))
# Set the UserDataBlob.Array32 property
blob.Array32 = myStringAsAnIntArray
Application.LogMessage("----------------Getting the Array32 Property----------------")
# Get the UserDataBlob.Array32 property
BlobIntTuple = blob.Array32
Application.LogMessage("BlobIntTuple = " + str(BlobIntTuple))
# Convert the integer tuple to an unsigned integer array
BlobIntArray = array.array('I', BlobIntTuple)
# Get the number of null bytes padded to the end of the byte array
# and removes it from the integer array
BlobNumBytesPadded = BlobIntArray.pop(0)
Application.LogMessage("BlobNumBytesPadded = " + str(BlobNumBytesPadded))
Application.LogMessage("BlobIntArray = " + str(BlobIntArray))
# Convert the unsigned integer array to an unsigned byte array
BlobByteArray = array.array('B', BlobIntArray.tostring() )
Application.LogMessage("BlobByteArray = " + str(BlobByteArray))
# Remove the number of bytes padded from the unsigned byte array
BlobByteArray = BlobByteArray[0:len(BlobByteArray)-BlobNumBytesPadded]
Application.LogMessage("BlobByteArray = " + str(BlobByteArray))
# Print the string
Application.LogMessage("BlobString = " + BlobByteArray.tostring())
# Expected results:
# INFO : ----------------Setting the Array32 Property----------------
# INFO : myStringAsAByteArray = array('B', [84, 104, 105, 115, 32, 105, 115, 32, 97, 32, 115, 116, 114, 105, 110, 103, 33])
# INFO : myStringAsAByteArray = array('B', [84, 104, 105, 115, 32, 105, 115, 32, 97, 32, 115, 116, 114, 105, 110, 103, 33, 0, 0, 0])
# INFO : NumBytesPadded = 3
# INFO : myStringAsAnIntArray = array('I', [3L, 1936287828L, 544434464L, 1953701985L, 1735289202L, 33L])
# INFO : ----------------Getting the Array32 Property----------------
# INFO : BlobIntTuple = (3, 1936287828, 544434464, 1953701985, 1735289202, 33)
# INFO : BlobNumBytesPadded = 3
# INFO : BlobIntArray = array('I', [1936287828L, 544434464L, 1953701985L, 1735289202L, 33L])
# INFO : BlobByteArray = array('B', [84, 104, 105, 115, 32, 105, 115, 32, 97, 32, 115, 116, 114, 105, 110, 103, 33, 0, 0, 0])
# INFO : BlobByteArray = array('B', [84, 104, 105, 115, 32, 105, 115, 32, 97, 32, 115, 116, 114, 105, 110, 103, 33])
# INFO : BlobString = This is a string! |