Can I use a Python generator to create geometry and set its color?
For example, I can use the following code in a Python generator to generate a 3D surface that represents the modulus of a complex function.
import c4d
import math
import cmath
min_x = -2.0*math.pi
max_x = 2.0*math.pi
min_y = -2.0*math.pi
max_y = 2.0*math.pi
resolution = 40 # number of points along each axis
point_count = resolution*resolution
poly_count = (resolution - 1)*(resolution - 1)
def maprange(xx, min_in, max_in, min_out, max_out):
return min_out + (xx - min_in)/float(max_in - min_in)*(max_out - min_out)
def getpoints(time_perc):
tt = 2*math.pi*time_perc*8
vecs = []
for yy_r in range(resolution):
yp = maprange(yy_r, 0, resolution-1, min_y, max_y)
for xx_r in range(resolution):
xp = maprange(xx_r, 0, resolution-1, min_x, max_x)
com = cmath.exp(xp + yp*1j)
argument = cmath.phase(com)
zp = abs(com) # modulus
vecs.append(c4d.Vector(xp, zp, yp))
return vecs
SCALE = 100
def main():
time = doc.GetTime().Get()
maxx = doc.GetMaxTime().Get()
tt = time/maxx
node = c4d.PolygonObject(pcnt=point_count, vcnt=poly_count)
points = [p * SCALE for p in getpoints(tt)]
node.SetAllPoints(points)
polynum = 0
for yy in range(resolution - 1):
for xx in range(resolution - 1):
aa = xx + yy*resolution
bb = xx + yy*resolution + 1
cc = xx + (yy + 1)*resolution
dd = xx + (yy + 1)*resolution + 1
cpoly = c4d.CPolygon(aa, bb, dd, cc)
node.SetPolygon(polynum, cpoly)
polynum += 1
node.Message(c4d.MSG_UPDATE)
return node
But I want to color the generated surface so that the hue of the surface at various points represents the argument of the complex variable (in other words, map the value of "argument" in the code above from [-pi, pi] to [0 (red), 360 (red)] through the hue wheel). Here's a visual example of what the code above plots in C4D:
But I want it to be colored like this:
This could be done using the mapping I described above, but I don't know how to color the surface with the Python generator. Is there some way to set color data based on coordinates? What's the best way to get the desired hue into a C4D material?
I don't really want to export a texture map PNG if I don't have to since I intend to animate the surface over time, meaning that the argument will change every frame and the color of the surface will depend on time. It would best if I could do this all built-in.
Any ideas?