# How to align object on normal vector between vertex and line

Update 29-08-2023 22:20

Dear community,

Thank you for the input! The responses gave me some useful tips and nodes to integrate in the desired workflow. Since not everything was clear, I will give some more context about the project.

I am working on an urban Digital Twin. On this project I am focussing on adding more detail to city by converting 2D points to 3D objects. The government of the Netherlands provides us with open datasets. This open datasets contain for example the position and measurements of street signs and streetlights.

By using QGIS I can retrieve the center axis of a road. The streetlights should light up the road. Therefore, I want to align the lights perpendicular to the center axis of the road. In my test project the road is a simple line. The points are just references to place the object on the correct x and y coordinate. The desired iterations on the project are as following:

• Modelling streetlights with geometry nodes perpendicular to the street.
• Modelling street signs with geometry nodes parallel to the street.

The question in both situations is how can the rotation of instances be based on nearest lines. The 2D points are just reference points from a dataset which tell the position. This position does not contain any information about the orientation of the object.

At the moment I am working on a Geometry Nodes setup to place 3D geometry on some random points. The 3D geometry contains a street light. The light bulb of the street light should be orientated to the line. In other words the street light is perpendicular to the line of the road. I am using the Instances on Points node and hope to achieve my goal by retrieving a normal vector.

This normal vector should illustrate the shortest distances between the line and the vertex. I can retrieve the normal of the line and a normal of the vertex. For my usecase I think I need to capture the normal of the line. I tried a combination of the Capture Attribute node and the Align Euler to Vector node. Are there anymore options left? Perhaps the Raycast node?

Questions and desired solution

In short are my questions as following:

• How can I retrieve the shortest distance between a line and a vertex with a node with Geometry Nodes?
• How can I align an object with the orientation of the shortest distance vector?

I hope to achieve a method in which the light bulb (the source object is orientated to the positive x-axis) get automatically aligned with the shortest distance between a line and a vertex.

Examples

Orientation of source object

Random points on the right side of the line

Current workflow geometry nodes

Desired solution; the blue lines illustrate the normal vector and the rotation of the street light.

I would highly appreciate your input on my research and look forward to hearing from you at your earliest convenience.

Kind regards,

Let me make sure I understand, because it's kind of weird for streetlights:

Here, we have a line that's been given a skin modifier for visibility, and cuboids that are aligned with the normal of that line-- with the nearest point on that line. This would be a weird thing to do with streetlights, because then they'd slant on hills. So I want to make sure that's what you want.

If that's what you want, yup, a line from any point to the nearest point on another line will be perpendicular to that second line. So long as that second line is infinitely long.

But unfortunately, GN does not support sampling "nearest edge interpolated". At least, not at this time. We can sample the nearest point-type entity, we can sample a surface, we can raytrace (which will almost never intersect our line thanks to precision problems) but we can't directly sample nearest edge interpolated. Which is a problem for the cases where our instances don't bear any relationship to the line we want to sample.

Using GN, if we're willing to accept some tiny imprecision, we can point instances at close to the nearest point on that line:

I'm showing a copy of our line with a skin modifier still, but it's just to display the line; it's not used by the GN. The geometry nodes are turning that line into a very, very thin cylinder so that we can sample the nearest surface position; then I'm using "align Euler to vector" to rotate our instances, about their Y axes, to point their Z axis in the same direction as the vector from the line to the instance.

Notice that the monkeys are parallel up until the line ends-- after that, they point at the end of the line.

I'm assuming that you have separate objects for the street signs and the street lights as a starting point here (At least that could probably be easily separated as well, if not).

In this case, and to keep the Geometry Nodes node group flexible to use, I suggest the following setup:

Since you mention in your example that you are dealing with real data, I have to assume that neither the points of the road nor the points at which the objects are to be instantiated are on a plane.

Therefore, since streetlights and signs are usually upright (hopefully this is also the case in the Netherlands), you would have to do the calculation of the direction vector on a plane, or discard the Z positions in the process.

In this example I created a reusable node group that you can use on all objects consisting of points where street signs or lanterns should be positioned.

You just have to define here the object that contains the mesh of the street line, the object that should be instantiated and additionally you have the possibility to influence the local rotation of the objects.

What I do here:

1. First I discard the Z-positions of the street line
2. Then I extrude it upwards, because you probably don't want to get the closest point or the center of the closest edge, but any closest point on the face of the object
3. So it is now possible to get the actual nearest point on the upward extruded mesh line with the help of the node 'Sample Nearest Surface'. But since the points at which the objects are to be instantiated are also at different Z-positions, I also use the positions of the points with discarded Z-axes as Sample Position.
4. After that this vector only has to be converted into a rotation with the help of 'Align Euler to Vector', and if necessary the rotation has to be corrected according to the orientation of the object.
5. Finally, the objects are instantiated at the desired points, with the previously calculated rotation.

You can then apply this node group as often as you like to any objects.

(Blender 3.6+)

• Dear Blender professional (since I can't see your username because of the edits by quellenform), Thank you for your input and detailed step by step plan. I will look further into the script this weekend. Have a nice weekend! Sep 1, 2023 at 12:29
• @YosriSem Don't worry, take the time you need and get back to me if you have any questions. Btw: I am quellenform and I created and edited this post ;-) Sep 1, 2023 at 12:36
• Dear @quellenform, The past months I have been experimenting with the script. There are occurring some problems when I insert a shapefile with a roadmap of a town. But I think I can fix them if I know every function of the nodes in the script. Nov 2, 2023 at 20:37
• @YosriSem ...Let me know if you need help ...there is a solution for (almost) everything. ;-) Nov 2, 2023 at 20:42
• After using and experimenting with the script I do have some questions. At the moment I am experiencing some minor problems with inserting a shapefile, in which a collection of roads is saved. I am convinced I can solve these problems if I understand the function of every node. Could you answer the following questions or explain in depth the function of some nodes. 1. What is the function of the extrude node group? 2. Why is z vector being used in the get direction node group? 3. What are the pros of using the group input node instead of the object info node? Nov 2, 2023 at 20:46