def polygonToVolume(obj):
    # Checks if the input obj is a PolygonObject
    if not obj.IsInstanceOf(c4d.Opolygon):
        raise TypeError("obj is not a c4d.Opolygon.")

    # Retrieves the world matrices of the object
    matrix = obj.GetMg()

    # Creates a BaseArray (list) of all points position in world space
    vertices = maxon.BaseArray(maxon.Vector)
    vertices.Resize(obj.GetPointCount())
    for i, pt in enumerate(obj.GetAllPoints()):
        vertices[i] = pt * matrix

    # Sets polygons
    polygons = maxon.BaseArray(maxon.frameworks.volume.VolumeConversionPolygon)
    polygons.Resize(obj.GetPolygonCount())
    for i, poly in enumerate(obj.GetAllPolygons()):
        newPoly = maxon.frameworks.volume.VolumeConversionPolygon()
        newPoly.a = poly.a
        newPoly.b = poly.b
        newPoly.c = poly.c

        if poly.IsTriangle():
            newPoly.SetTriangle()
        else:
            newPoly.d = poly.d

        polygons[i] = newPoly

    # Sets the matrice used for local grid translation and rotation
    polygonObjectMatrix = maxon.Matrix()
    polygonObjectMatrix.off = obj.GetMg().off
    polygonObjectMatrix.v1 = obj.GetMg().v1
    polygonObjectMatrix.v2 = obj.GetMg().v2
    polygonObjectMatrix.v3 = obj.GetMg().v3
    gridSize = 1
    bandWidthInterior = 1
    bandWidthExterior = 1

    # Before R21
    if c4d.GetC4DVersion() < 21000:
        volumeRef = maxon.frameworks.volume.VolumeToolsInterface.MeshToVolume(vertices,
                                                                              polygons, polygonObjectMatrix,
                                                                              gridSize,
                                                                              bandWidthInterior, bandWidthExterior,
                                                                              maxon.ThreadRef(), None)
    else:
        volumeRef = maxon.frameworks.volume.VolumeToolsInterface.MeshToVolume(vertices,
                                                                              polygons, polygonObjectMatrix,
                                                                              gridSize,
                                                                              bandWidthInterior, bandWidthExterior,
                                                                              maxon.ThreadRef(),
                                                                              maxon.POLYGONCONVERSIONFLAGS.NONE, None)

    FogVolumeRef = maxon.frameworks.volume.VolumeToolsInterface.ConvertSDFToFog(volumeRef, 0.1)
    return FogVolumeRef
Esempio n. 2
0
def main():
    # Creates a VectorVolume
    vecVolumeRef = CreateVectorVolume(maxon.Vector32(100.0))

    # Inserts these vector volume into a VolumeObject
    vecVolumeObj = CreateVectorObject(vecVolumeRef, "Vector Volume")

    # Mixes both vector volume together using cross product
    fogVolumeRef = volume.VolumeToolsInterface.ConvertVectorToFog(
        vecVolumeRef, maxon.ThreadRef())

    # Inserts the mixed volume into the scene
    fogVolumeObj = CreateVectorObject(fogVolumeRef, "Fog Volume")

    # Creates a VolumeMesher and adds the volumeObject
    volumeMesher = c4d.BaseObject(c4d.Ovolumemesher)
    if volumeMesher is None:
        raise MemoryError("Failed to create a volume mesher object.")

    # Inserts the fog volume object under the volume mesher
    fogVolumeObj.InsertUnder(volumeMesher)

    # Inserts the volume mesher within the scene
    doc.InsertObject(volumeMesher, None, None)

    # Pushes an update event to Cinema 4D
    c4d.EventAdd()
Esempio n. 3
0
def main():

    # Creates a Volume Object
    volumeObj = c4d.BaseObject(c4d.Ovolume)
    if volumeObj is None:
        raise MemoryError("Failed to create a volume object.")

    # Creates a Sphere Volume
    sphereVolume = maxon.frameworks.volume.VolumeToolsInterface.CreateSphereVolume(
        maxon.Float(100.0), maxon.Vector(0, 100, 0), maxon.Float(1.0),
        maxon.Int32(2), maxon.ThreadRef(), None)

    # Fills the volume with a Gradient
    gradientVolume = maxon.frameworks.volume.VolumeToolsInterface.CreateGradientVolume(
        sphereVolume, maxon.ThreadRef())

    # Sets the VolumeRef in the Volume Object and insert it in the scene
    volumeObj.SetVolume(gradientVolume)
    doc.InsertObject(volumeObj)

    # Pushes an update event to Cinema 4D
    c4d.EventAdd()
def main():
    # Checks if selected object is valid
    if op is None:
        raise ValueError("op is none, please select one object.")

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

    # Checks if the volume builder is set a vector since Curl only works with vector volume
    if not op[c4d.ID_VOLUMEBUILDER_VOLUMETYPE] == c4d.ID_VOLUMEBUILDER_VECTOR:
        raise TypeError("Volume buidler is not set as vector volume.")

    # Retrieves the volume builder cache
    cache = op.GetCache()
    if cache is None:
        raise RuntimeError("Failed to retrieve the cache.")

    # Checks if the cache object is a volume object
    if not cache.IsInstanceOf(c4d.Ovolume):
        raise TypeError("cache is not a c4d.Ovolume.")

    # Retrieves the core volume interface
    vecVolume = cache.GetVolume()
    if vecVolume is None:
        raise RuntimeError(
            "Failed to retrieve the core volume, most likely there is no volume set."
        )

    # Creates a new curl volume based on the vector VolumeRef we retrieved
    curlVecVolume = volume.VolumeToolsInterface.CreateCurlVolume(
        vecVolume, maxon.ThreadRef())

    # Creates a Volume Object
    volumeObj = c4d.BaseObject(c4d.Ovolume)
    if volumeObj is None:
        raise MemoryError("Failed to create a volume object.")

    # Defines the VolumeRef used by the VolumeObject
    volumeObj.SetVolume(curlVecVolume)

    # Inserts the volume object into the document
    doc.InsertObject(volumeObj)

    # Pushes an update event to Cinema 4D
    c4d.EventAdd()
Esempio n. 5
0
def main():
    # Checks if there is an active object
    if op is None:
        raise ValueError("op is None, please select one object.")

    # Checks if the input obj is a PolygonObject
    if not op.IsInstanceOf(c4d.Opolygon):
        raise TypeError("obj is not a c4d.Opolygon.")

    # Retrieves the world matrices of the object
    matrix = op.GetMg()

    # Creates a BaseArray (list) of all points position in world space
    vertices = maxon.BaseArray(maxon.Vector)
    vertices.Resize(op.GetPointCount())
    for i, pt in enumerate(op.GetAllPoints()):
        vertices[i] = pt * matrix

    # Sets polygons
    polygons = maxon.BaseArray(maxon.frameworks.volume.VolumeConversionPolygon)
    polygons.Resize(op.GetPolygonCount())
    for i, poly in enumerate(op.GetAllPolygons()):
        newPoly = maxon.frameworks.volume.VolumeConversionPolygon()
        newPoly.a = poly.a
        newPoly.b = poly.b
        newPoly.c = poly.c

        if poly.IsTriangle():
            newPoly.SetTriangle()
        else:
            newPoly.d = poly.d

        polygons[i] = newPoly

    polygonObjectMatrix = maxon.Matrix()
    gridSize = 10
    bandWidthInterior = 1
    bandWidthExterior = 1

    # Converts the polygon into a volume
    # Before R21
    if c4d.GetC4DVersion() < 21000:
        volumeRef = maxon.frameworks.volume.VolumeToolsInterface.MeshToVolume(
            vertices, polygons, polygonObjectMatrix, gridSize,
            bandWidthInterior, bandWidthExterior, maxon.ThreadRef(), None)
    else:
        volumeRef = maxon.frameworks.volume.VolumeToolsInterface.MeshToVolume(
            vertices, polygons, polygonObjectMatrix, gridSize,
            bandWidthInterior, bandWidthExterior, maxon.ThreadRef(),
            maxon.POLYGONCONVERSIONFLAGS.NONE, None)
    # Creates a Volume Object to store the previous volume calculated
    volumeObj = c4d.BaseObject(c4d.Ovolume)
    if volumeObj is None:
        raise MemoryError("Failed to create a volume object.")

    doc.InsertObject(volumeObj)
    volumeObj.SetVolume(volumeRef)

    # Converts back to Polygon
    polyObject = maxon.frameworks.volume.VolumeToolsInterface.VolumeToMesh(
        volumeRef, 0.0, 1)
    doc.InsertObject(polyObject)

    # Pushes an update event to Cinema 4D
    c4d.EventAdd()