Z

Hi,
your code looks a bit overly complicated and c-ish. There might very well be a reason for all the gymnastics that code does, but at least I am not able to see that reason. Here is how I would do it in Python.
Cheers,
zipit
"""Simple example for generating uvw data.
"""
import c4d
# To implement other projections, you have just to implement another
# projection function ...
def planar_projection(frame, points):
"""Projects points into the XY plane.
Args:
frame (c4d.Matrix): The projection frame.
points (list[c4d.Vector]): The points to project.
Returns:
list[c4d.Vector]:The projected points.
"""
points = [p * frame for p in points]
return [c4d.Vector(p.x, p.y, 0) for p in points]
def remap_to_texcoords(points):
"""Remaps a list of points to the common uv half range.
The function assumes the x and y coordinates of the points to be the
relevant coordinates and the points are being remapped based on the XY
bounding rectangle.
Args:
points (list[c4d.Vector]): The points to remap.
Returns:
list[c4d.Vector]:The remapped points.
"""
# Get the texture coordinates bounding rectangle.
xmin, xmax = float("inf"), float("-inf")
ymin, ymax = float("inf"), float("-inf")
for p in points:
xmin = p.x if p.x < xmin else xmin
xmax = p.x if p.x > xmax else xmax
ymin = p.y if p.y < ymin else ymin
ymax = p.y if p.y > ymax else ymax
# Remap the points from object coordinates to texture coordinates
# in the common uv half range.
points = [c4d.Vector(c4d.utils.RangeMap(p.x, xmin, xmax, 0., 1., True),
c4d.utils.RangeMap(p.y, ymin, ymax, 0., 1., True),
0) for p in points]
return points
def create_uvw_tag(node, frame, f):
"""Creates an uvw tag for an object, a frame and a projection.
Args:
node (c4d.PolygonObject): The node.
frame (c4d.Matrix): The texture projection frame.
f (callable): The projection function.
Returns:
c4d.UVWTag: The uvw tag.
"""
# Project the points of the node.
points = f(frame, node.GetAllPoints())
# Convert these points to texture space.
uv_points = remap_to_texcoords(points)
# Create the actual tag
polygons = node.GetAllPolygons()
uvw_tag = c4d.UVWTag(len(polygons))
# We just go over all polygons and with that index our projected and
# remapped points into the uvw tag.
for i, cpoly in enumerate(polygons):
a, b = uv_points[cpoly.a], uv_points[cpoly.b]
c, d = uv_points[cpoly.c], uv_points[cpoly.d]
uvw_tag.SetSlow(i, a, b, c, d)
return uvw_tag
def main():
"""
"""
if not isinstance(op, c4d.PolygonObject):
msg = "Please select a polygon object."
raise TypeError(msg)
tag = create_uvw_tag(op, c4d.Matrix(), f=planar_projection)
op.InsertTag(tag)
c4d.EventAdd()
if __name__ == "__main__":
main()