1
$\begingroup$

I start with a plane, subdivide it into many small tiles, raise the vertices to create a terrain. I also have a vertical gradient texture: the bottom edge of the texture is blue (sea), the top edge is white (snow), the middle levels are shades of green and brown. The terrain is assigned the texture.

The question is how to do the UV programmatically? I want a Python script that can assign each vertices' v equal to its normalized z.

Ideally, I can shape the terrain (move mesh vertices) in 3D view, and run the script anytime. Then the terrain will have a height-dependent texture.

It seems all I need is to do is:

  1. Run through all vertices in the mesh to find zMin and zMax

  2. Run through all vertices and set u=0.5, v=(z-zMin)/(zMax-zMin)

But I don't know how to do it in Blender with Python, Can anyone please help?

$\endgroup$
  • $\begingroup$ You should be able to do this in Material Nodes. Add an Output Geometry node and feed the Position into a Vector Mapping node. Rotate the Point X to 90 and feed that into a Converter Color Ramp where the colour stops are White through Green/Brown and Blue. You can set the Type to Constant to get a hard line. $\endgroup$ – rob Jul 31 '18 at 16:57
  • $\begingroup$ Just found this blender.stackexchange.com/questions/67142/… very detailed look at setting material based on location in Cycles. $\endgroup$ – rob Jul 31 '18 at 17:03
  • $\begingroup$ Thanks for the suggestions. I will look at the node based approach. I am looking for a Python script because I want to control the uv mapping and terrain programmatically. I am very comfortable with programming in general and want to experiment with different algorithms. I just need some help getting started with reading/writing uv in Blender. $\endgroup$ – Kam Mlv Jul 31 '18 at 19:40
  • $\begingroup$ How to find zMin and zMax using nodes? I need to normalize z-coord of a vertex's position, $\endgroup$ – Kam Mlv Jul 31 '18 at 20:01

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Browse other questions tagged or ask your own question.