volume_grid_read

Reads the Float value of the first channel available from a Volume stored in a Volume Builder.

volume_grid_read.py

"""Reads the Float value of the first channel available from a Volume stored in a Volume Builder.

Topics:
    * maxon.GridAccessorInterface
    * GridAccessorRef.GetValue()
"""
__author__ = "Maxime Adam"
__copyright__ = "Copyright (C) 2026 MAXON Computer GmbH"
__license__ = "Apache-2.0 License"
__version__ = "R20"

import c4d
import maxon


def main():
    # Checks if there is an active object
    if op is None:
        raise RuntimeError("Failed to retrieve op.")

    # Checks if the active object is a Volume builder
    if not op.IsInstanceOf(c4d.Ovolumebuilder):
        raise TypeError("op is not a c4d.Ovolumebuilder.")

    # Gets the C4D Volume Object from the cache
    cache = op.GetCache()
    if cache is None:
        raise RuntimeError("Failed to retrieve the cache of the volume builder.")

    # Checks if the cache is a C4D Volume Object
    if not cache.IsInstanceOf(c4d.Ovolume):
        raise TypeError("cache is not a c4d.Ovolume.")

    # Gets the Volume object linked to this Ovolume object.
    volume = cache.GetVolume()
    if volume is None:
        raise RuntimeError("Failed to retrieve the maxon.VolumeRef.")

    # Initializes a Float Grid with our existing volume data
    access = maxon.GridAccessorInterface.Create(maxon.Float32)
    access.Init(volume)

    # Reads the Float value stored at the c4d.Vector(0, 0, 0) position
    vec = c4d.Vector(0, 0, 0)
    value = access.GetValue(vec)

    print(value)


if __name__ == '__main__':
    main()