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

    polygonObjectMatrix = maxon.Matrix()
    polygonObjectMatrix.off = obj.GetMg().off
    polygonObjectMatrix.v1 = obj.GetMg().v1
    polygonObjectMatrix.v2 = obj.GetMg().v2
    polygonObjectMatrix.v3 = obj.GetMg().v3
    gridSize = 10
    bandWidthInterior = 1
    bandWidthExterior = 1
    thread = maxon.ThreadRef()

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

    return volumeRef
def main():
    # Gets selected objects
    objList = doc.GetActiveObjects(c4d.GETACTIVEOBJECTFLAGS_0)
    if not objList:
        raise RuntimeError("Failed to retrieve selected objects")

    # Opens a LoadDialog to define the path of the VDB file to save
    filePath = c4d.storage.SaveDialog()

    # Leaves if nothing was selected
    if not filePath:
        return

    # Add the vdb extension if needed
    if not filePath.endswith(".vdb"):
        filePath += ".vdb"

    # Creates a maxon.BaseArray with all our obj, we want to convert
    volumesArray = maxon.BaseArray(maxon.frameworks.volume.VolumeRef)
    volumesArray.Resize(len(objList))
    for i, obj in enumerate(objList):
        volumesArray[i] = polygonToVolume(obj)

    # Generates the final file path to save the vdb
    path = maxon.Url(filePath)
    scale = 1.0
    metaData = maxon.DataDictionary()
    try:
        maxon.frameworks.volume.VolumeToolsInterface.SaveVDBFile(
            path, scale, volumesArray, metaData)
    except IOError:
        raise IOError("Failed to Save the VDB file.")

    print("File saved to ", path)
def main():
    # Creates a BaseArray of a maxon.Int
    intArray = maxon.BaseArray(maxon.Int)

    # Resizes the BaseArray
    intArray.Resize(10)

    # Iterates over the BaseArray and assign values
    for i in xrange(len(intArray)):
        intArray[i] = i

        print intArray[i]
def main():
    # Retrieves a path to load the imported file
    selectedFile = c4d.storage.LoadDialog(title="Load File for Volume Import",
                                          type=c4d.FILESELECTTYPE_ANYTHING,
                                          force_suffix="vdb")
    if not selectedFile:
        return

    # Define the path of the file.vdb located in the same folder of this script
    path = maxon.Url(selectedFile)

    # Try to load the VDB File, and apply a Scale of 1 to this VDB
    scale = 1.0
    gridNames = maxon.BaseArray(maxon.String)
    gridIndices = None
    metaData = maxon.DataDictionary()
    volumeArr = None
    try:
        volumeArr = maxon.frameworks.volume.VolumeToolsInterface.LoadVDBFile(
            path, scale, gridNames, gridIndices, metaData)
    except Exception as e:
        print("LoadVDBFile error {}, {}".format(e, e.args))

    if volumeArr is None:
        raise RuntimeError("Unknown error.")

    if len(volumeArr) == 0:
        raise RuntimeError("Selected Vdb store 0 volume.")

    # Retrieves the first volume
    volRef = volumeArr[0]

    # 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(volRef)

    # Pushes an update event to Cinema 4D
    c4d.EventAdd()
예제 #5
0
def main():
    # Gets selected objects
    objList = doc.GetActiveObjects(c4d.GETACTIVEOBJECTFLAGS_0)
    if not objList:
        raise RuntimeError("Failed to retrieve selected objects")

    # Creates a maxon.BaseArray with all our obj, we want to convert
    volumesArray = maxon.BaseArray(maxon.frameworks.volume.VolumeRef)
    volumesArray.Resize(len(objList))
    for i, obj in enumerate(objList):
        volumesArray[i] = polygonToVolume(obj)

    try:
        # Generates the final file path to save the vdb
        path = maxon.Url(os.path.join(os.path.dirname(__file__), "file.vdb"))
        scale = 1.0
        metaData = maxon.DataDictionary()
        maxon.frameworks.volume.VolumeToolsInterface.SaveVDBFile(
            path, scale, volumesArray, metaData)
        print "File saved to ", path
    except Exception as e:
        print "SaveVDBFile error {}, {}".format(e.message, e.args)
예제 #6
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()