How To > Move Objects to a Surface |
The following will let the user move objects along their Z axis to fit a specified surface. Such a script can be used to automatically plant trees, poles etc. on the ground.
The macroScript will be called MoveToSurface . To use the script, you can go to Customize... and drag the script from the category "HowTo" to a toolbar, a menu, a quad menu or assign to a keyboard shortcut.
This custom function will be used as filter for geometry objects. It will accept an object as parameter and will return true when the object is of Geometry Superclass and false otherwise.
This custom function will do the actual ray intersection. It will accept two parameters – the target object and the object to be moved to the target object’s surface.
Here we define a custom Ray value with a starting position at the position of the object to be moved and direction along the -Z axis.
We also get the max. Z coordinate of the surface to intersect with.
Then we move the Z position of the ray slightly above the highest Z point of the surface to intersect with. This way we can be sure that the ray will have a chance to hit the surface along the -Z axis.
This built-in intersectRay function is given a node and a ray (spatial vector with a start point and direction) and returns the point in space where the ray hits the surface of the node, or undefined if there is no intersection. The result of this intersection will be also the return value of the function as it is the last value calculated.
The script can only do meaningful job when at least one object is selected. If there are no selected objects in the scene, the script will be disabled. The 'on isEnabled' handler controls the enabled state by evaluating the expression after the 'return' statement. When the result is false , the script’s button resp. Menu item is greyed out and cannot be activated.
Macroscript_Body_Event_Handlers
The body of the script is contained in the 'on Execute' handler. It is executed when the script is started by pressing its button, selecting its menu item or pressing assigned shortcut keys.
Macroscript_Body_Event_Handlers
The pickObject function lets the user pick objects in the scene. The filter function we provided allows the picking of Geometry objects only – the mouse will not register any other objects like Lights, Helpers etc. when moving over them! The result will be written to the user variable target_mesh. It can be either the picked node or undefined in case the picking has been canceled.
If the picked object is a valid object (which means the user actually picked an object and did not press Escape or right-clicked the mouse to cancel)...
...then we define an undo context to be able to reset all objects back to their original state if necessary...
...and create a loop that will go through all objects in the current selection. Each iteration of the loop, the variable i will contain another object from the selection.
Here we call the user function we defined and pass the target_mesh object picked by the user and the current objects from the loop through the selection. The result will be either undefined or the point of intersection – the projection of the object’s position onto the surface of the target object along –Z.
If the intersection really exists, we simply set the position of the current object to that point. If it doesn’t (the intersection is undefined), we skip this step.
Evaluate the script. To use it, you can use Customize... to drag the script from the category "HowTo" to a toolbar, a menu, a quad menu or to assign to a keyboard shortcut.
Select any number of objects you wish to move to a surface. Start the script and pick the object to move to.