Hello @karpique,
Thank you for reaching out to us. There are two things you could optimize out in your code. Frist, the loop around targetObj.SetPoint
, since there is PointObject.SetAllPoints
, taking a list of points and being much faster. Secondly, you could also optimize away the branching in your code with poly.IsTriangle()
, since both tris and quads are being handled as quads by Cinema 4D (a tri is a handled as a CPolygon
where c == d
). So, you could just write:
for i in range(0, polygonCount):
poly = sourceObj.GetPolygon(i)
targetObj.SetPolygon(i, c4d.CPolygon(poly.a, poly.b, poly.c, poly.d))
Unlike for points, there is unfortunately no method in Python to write CPolygon
in bulk with, making writing polygons in Python quite slow. As you mentioned, you could also use C4DAtom.CopyTo
, but there is no clean way to "copy only (the) mesh", since geometry information is being stored in tags and not the node itself. This will also copy n-gons:
"""Example for copying polygons and n-gons from one node to another.
Run this with a polygon object selected in the script manger.
"""
import c4d
def main():
"""
"""
# Create a Opolygon node of matching size
res = c4d.PolygonObject(op.GetPointCount(), op.GetPolygonCount())
# We first have to insert that new node into a document, this could also
# be a temporary document.
doc.InsertObject(res)
c4d.EventAdd()
# Then we just copy over stuff from the source. We cannot use the
# COPYFLAGS_NO_BRANCHES flag, as this will prevent copying tag data,
# which is also being used to store point and polygon information.
flags = c4d.COPYFLAGS_NO_ANIMATION | c4d.COPYFLAGS_NO_HIERARCHY | c4d.COPYFLAGS_NO_BITS
op.CopyTo(res, flags)
if __name__=='__main__':
main()
There are some other routes you could take, like. c4d.utils.SendModelingCommand
and joining your mesh to copy with an empty mesh or handling it in a more hands-on-fashion by copying over the Tpoint
and Tpolygon
tags manually, and thereby avoid copying unwanted tag information.
Cheers,
Ferdinand