I have devised a way to do this kind of hassle free.
I include my .
To set up your scene like me, you need:
- a plane with 1 subdivision per 1 meter centered around world origin
(limitations of math used)
- a material 'Material.001' with Image Texture as the source for color
When you hit run script you should see
The function runs from -32 to +32 on the X and Y axes to make the script a bit easier thanks to vertices with integer X and Y coordinates, but we would typically require a finer grid around 0 and so I use (kind of improperly named)
zScale when displacing the vertices
v.co.z = -0.2 * (((v.co.x * zScale) ** 2) - ((v.co.y * zScale) ** 2))
Feel free to adjust
zScale to quickly preview how the script behaves when the Z extremities are more extreme.
How does it work
- displace the vertices
- get the min and max Z coordinates for coloring purposes (in separate for loop for clarity, could be done in one loop)
- create a square image with the same amount of pixels, as there are vertices
- loop over vertices and color pixels at XY coords matching the XY coords of vertex (that's why they are integer) based on the Z coordinate, utilizing the min and max Z coordinates to go from green to red
- assign the texture
Edits I leave up to you
allow non-integer XY coordinates
you might want a fixed color scale, not an automatically adjusting as in my file
rotate the image (might be necessary for non-symmetrical functions, have not checked) seems to work correctly as is
always rewrite a single image file to reduce the clutter
add a script to set up the scene automatically for a drop-in capability
fixed resolution image for high density meshes (for speed reasons)
The commented out section at the end of the code
I was playing with an idea to use the depth buffer to color the graph.
- Set up an ortographic camera above the graph, looking down at it
- use Viewer node to use the depth buffer in script
- create image same size as the depth map
- loop over pixels in depth map, assign colors to the new image based on that
Works even if you displace the vertices on the Z axis by hand.
The how does it work section should allow for the code to be reconstructed at any time, I will drop here some answers for reference: