Ejemplo n.º 1
0
def CreateNormalTag(op):
    """
     Creates a NormalTag on the passed PolygonObject.

    :param op: The PolygonObject that will received a normal Tag.
    :type op: c4d.PolygonObject
    :return: The created tag.
    :rtype: c4d.NormalTag
    """
    # Checks if the passed object is a polygon object.
    if not isinstance(op, c4d.PolygonObject):
        raise TypeError("op is not a c4d.PolygonObject.")

    # Retrieves the polygonCount
    polyCnt = op.GetPolygonCount()

    # Creates a Normal Tag in memory only
    normalTag = c4d.NormalTag(polyCnt)
    if normalTag is None:
        raise MemoryError("Failed to create a normal Tag.")

    # Inserts the tag to the passed object
    op.InsertTag(normalTag)

    # Notifies the object it need to update to take care of the newly created normal tag
    op.Message(c4d.MSG_UPDATE)
    return normalTag
Ejemplo n.º 2
0
        def parse_normals():
            normal = []
            if 'NORMAL' in prim.attributes:
                normal = BinaryData.get_data_from_accessor(
                    gltf, prim.attributes['NORMAL'])

            if normal:
                normaltag = c4d.NormalTag(nb_poly)
                for polyidx in range(nb_poly):
                    poly = c4d_mesh.GetPolygon(polyidx)
                    normal_a = self.switch_handedness_v3(
                        self.list_to_vec3(normal[poly.a]))
                    normal_b = self.switch_handedness_v3(
                        self.list_to_vec3(normal[poly.b]))
                    normal_c = self.switch_handedness_v3(
                        self.list_to_vec3(normal[poly.c]))
                    normal_d = c4d.Vector(0.0, 0.0, 0.0)

                    set_normals(normaltag, polyidx, normal_a, normal_b,
                                normal_c, normal_d)

                c4d_mesh.InsertTag(normaltag)

                # A Phong tag is needed to make C4D use the Normal Tag (seems to be done for Collada)
                phong = c4d.BaseTag(5612)
                c4d_mesh.InsertTag(phong)
def main():
    """Entry point."""
    # Raise an error when the primary selection is not a PolygonObject.
    if not isinstance(op, c4d.PolygonObject):
        raise ValueError("Please select a PolygonObject.")

    # Attempt to get the phong tag of the object.
    if op.GetTag(c4d.Tphong) is None:
        raise ValueError("Selected object does not carry a phong tag.")

    # Get the phong normals of the phong tag of the object and rotate them
    # all by 45° around the global z-axis.
    phongNormals = [
        nrm * c4d.utils.MatrixRotZ(QUARTER_PI)
        for nrm in op.CreatePhongNormals()
    ]

    # Create a new NormalTag with a size of the polygon count of our object.
    normalTag = c4d.NormalTag(count=op.GetPolygonCount())
    if normalTag is None:
        raise MemoryError("Failed to create a NormalTag.")

    # Get the normals from the newly allocated NormalTag.
    normals = ReadNormalTag(normalTag)
    print(f"Normals of the newly allocated tag: {normals}")

    # This should not happen.
    if len(phongNormals) != len(normals):
        raise RuntimeError("Unexpected NormalTag to phong normals mismatch.")

    # Write the phong normals into our NormalTag.
    WriteNormalTag(normalTag, phongNormals)

    # Inspect our write operation in the console.
    normals = ReadNormalTag(normalTag)
    print(f"Normals after writing the phong normals: {normals}")

    # Start an undo block.
    doc.StartUndo()
    # Insert our NormalTag.
    op.InsertTag(normalTag)
    # For insertions the undo has to be added after the operation.
    doc.AddUndo(c4d.UNDOTYPE_NEWOBJ, normalTag)
    # End the undo block.
    doc.EndUndo()

    # Notify Cinema and the object that we did made changes.
    op.Message(c4d.MSG_UPDATE)
    c4d.EventAdd()