# vertex slope to vertex weights

to enable placing particles just on more or less flat parts of a mesh I wrote a tiny addon that converts vertex slopes to weights in a vertex group (which can be used to influence the density of the particle distribution)

an example:

Thank you for this!

Would it be possible to modify it in oder to change the vertex colors according to slope instead? Also it would be useful to be able to modulate the output, in order to get sharper areas. For instance, in the example presented, I would find it useful to be able to only have the very flat areas covered in grass, and have the rest as rock.

I tried having a look at the source code. I canât understand why youâre dividing the normal vector in âangleâ by pi? Sorry if this is a nooby question, just trying to understand what youâre doing. Thanks

@superflea: vertex colors instead of weights is simple, in fact that was the first version I wrote but then I realized I cannot use a vertex color as a density map in a particle system. I uploaded that version to GitHub. You can use it vertex paint mode and there you can find it in the paint menu. It only uses the red channel.

About dividing by pi: the angle() member function returns a value in radians, so any angle between the normal and the z-axis will be between 0 and pi. However, I want those weights to be in the range [0,1] so thatâs why I divide and then subtract from 1.

I intend to add options to control this mapping for precisely the reason you mention For now you could tweak the sourcecode to read something like below to make steep slope far less likely to receive grass (tweak the power of 2 to taste :-):

Thanks a lot for the add-on, and for the code explanation. I find vertex colors useful because I can use them as a mask to blend materials/textures, and bake them easily as an image. Looking forward to seeing this evolve!

New version available on GitHub, simply called slope.py which unifies the generation of vertex colors and vertex weights. Now you have two menu entries which behave similar (note that there is no longer a menu entry in object mode, that one is obsolete and the new ones let you actually see wht you are doing

weight paint mode:
3dview -> Weights -> Slope will create/replace active vertex group weights

vertex paint mode:
3dview -> Paint -> Slope will create/replace active vertex color layer (red channel)

There are now 4 options in the toolbar which will be documented better later on:

low : slopes with lower angle than this will receive a unit value
high : slopes with steeper angle than this will receive a zero value
power: influences the shape of the mapping curve
mirror: maps slope angles > 90 to 180 - angle

This is a very clever and useful addition to the toolbox. Will the next enhancement include a curve node so the user can have complete control over the clipping and influence curve?

@orinoco: I donât know, although that is an interesting challenge I also think that is very difficult to implement, that is I currently donât see much support for it in the Python API (but if someone thinks otherwise, iâd love to get a pointer :-). Another way would be to implement a curve manipulation widget using the opengl bindings but thatâs easier said than done âŚ

edit1: the template_curve_mapping() function looks interesting âŚ
edit2: for vertex groups I think you could simply add a vertex weight edit modifier

Indeed. Add modifier, select the correct group, Falloff Type->Custom curve

This can be used to tweak weights after theyâre distributed across the vertices of a Subsurfâd mesh. In this case it might be useful for gaining sharper definition between differently sloped areas, without applying the Subsurf.

Great. Regarding vertex colors, I found it useful to modify the code slightly in order use all three channels rather than just red, so I get a grayscale map. I had a go at exposing the individual channel through checkboxes but no luck. I also played around with the normal value, here too it might be interesting to expose the individual x y z values through checkboxes to get horizontal or vertical maps or play around with xz or yz combinations.

Again thanks so much for this, Iâve been trying to get slopes forever with material nodes, and attempted a few times to write my own script. I understand the general requirements and functionality, but always stumble when it comes to exposing the values I need in Blender.

This is extremely useful for working with terrain and rock formations.

Added a mirror option and the possibility to modify the resuting vertex colors with the brush curve (thanks to Anthony Riakiotakis (psy-fi) for fixing a bug in Blender curve mapping api real quick!). The brush curve functionality is only available for builds >= 60054 so you will have to wait for a new Blender release or build your own/download the daily build if you want to use it. The script checks for the release number so it wonât crash if you have an older release. Also unlike the bevel weight edit modifier, there is no direct feedback of the curve mapping in the vertex color mode because I canât find a way to attach a callback to the curvemapping. This means you have to alter the brush curve and then click Paint -> Slope to apply the curve mapping. This isnât ideal but does work

@Superflea: I probably will implement you greyscale suggestion in a next version. At first I thought to put the z-height in the g-channeld but it is probably more intuitive to add more than one vcol layers/ vgroup to represent different things and then add a greyscale option for each of them.

this is a really useful plugin! The only problem is that when I go in weight or vertex paint mode and click on the plugin, nothing happens to the meshâŚ I see that the plugin menu comes up, but I cannot see any effect on the meshâŚ

@bernardo: does this happen with any mesh or a specific mesh? do you have perhaps more than one vertex color layer on that mesh? if possible, could you post a screenshot and/or .blend?

(if the mesh is very simple, for example a plane on its side with just one face, it might of course be possible that none of the faces is sufficiently horizontal to be coloredâŚ)

I checked a bit and this happens only with an ANT generated landscape. With a normal mesh it is working fine!

I have other two questionsâŚ why did you decide to make the vertex paint color red? Wouldnât it be better black and white?

Also, would it be difficult to have the same thing that works with height? It would be great to be able even to mix the slope and the height, we would clouse a lot towards soft like vue and terragen in terms of ease of shader creation.

However, thanks a lot, the plugin is already great!

a quick test I did using it.

### Attachments

1 Like

@bernardo: thanks for the info, I will try some ANT landscapes later on. About that color: the original idea was to put slope in the red channel, height in the green channel, but Actually it might be more sensible to put normal/slope info and height info in separate vcol layers and vertex groups and in fact right now I am working on just that

â Michel.

ps> nice image BTW

@ varkenvarken,
i really selfishly hope you continue with this project, thank-you sooo much for your workâŚ

I think projects like this makes Blender much more user friendly and attractive to node challenged people like me.

Big Thanks and happy Friday.

Oh I see your point now! I think to see which method is best we should try it out a bit. Well, what can I say, however you are going to implement this stuff, you are the man! Waiting for the updated plugin!

version 0.0.4 is available on GitHub

adds possibility to calc slope in either worldspace or object space coordinates in both vcol and vweight modes. vcol mode now produces grey scale values for the slope unless normal map is checked in which case the x,y,z components are simply mapped to the r,g,b component of the vcol layer.

The world coordinate option is handy in case a mesh has its local coordinates differently aligned compared to the world coordinates (as is the case with the default Suzanne mesh).

Next version will probably contain the height mapping although I am currently thinking to put in a separate operator to keep matters organized. First Iâll check a bit on ANT meshes (or actually Iâll go for a beer first and do that tomorrow :-).

cheers,

â Michel.

About those ANT meshes: they appear to have the normals in the negative-z direction (see img below). So flip the normals of the ANT mesh or check the mirror checkbox of the slope operator (which will treat upside down normals just like right way up normals) and you should be good to go

cool, did not notice that! Thanks for digging this out!