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