Convert to Numpy Array
To convert a VtArray to a Numpy Array, simply pass the VtArray object to numpy.array constructor.
import numpy
from pxr import Vt
def convert_vt_to_np(my_array: Vt.Vec3fArray) -> numpy.ndarray:
return numpy.array(my_vec3_array)
#############
# Full Usage
#############
# Create a Vt.Vec3fArray and convert it to a numpy array
my_vec3_array = Vt.Vec3fArray([(1,2,3),(4,5,6),(7,8,9)])
np_array: numpy.ndarray = convert_vt_to_np(my_vec3_array)
# print the numpy array to check the values
print(np_array)
# check the size and length of the numpy array
assert np_array.size == 9
assert len(np_array) == 3
Convert from Numpy Array
To convert a Numpy Array to a VtArray, you can use FromNumpy() from the VtArray class you want to convert to.
import numpy
from pxr import Vt
def convert_np_to_vt(my_array: numpy.ndarray) -> Vt.Vec3fArray:
return Vt.Vec3fArray.FromNumpy(my_array)
#############
# Full Usage
#############
# Create a numpy array and convert it into a Vt.Vec3fArray
np_array = numpy.array([(1,2,3),(4,5,6),(7,8,9)])
from_numpy: Vt.Vec3fArray = convert_np_to_vt(np_array)
# Print the Vt.Vec3fArray to check the values
print(from_numpy)
# Check the length of the numpy array
assert len(np_array) == 3