GE3 part 1: transformation basics in GHPython

Here’s an intro to basic transformations in python. These are the basic building blocks of the the exercise in GE part 2. As always in programming GHPython you can mix GHComponents with your own GHPython script. Mixing both to get to your goal at the beginning is a good way to dive into Python scripting in Grasshopper. In future exercises we will try to have less and less vanilla GHComponents and focus more on GHPython components built by youw own!

Here we show only the transformations we use the most in our own practice, but there are other types (i.e. skewing), be curious to have a look at others at the official RhinoCommon API.

🦗⬇️⬇️⬇️ Download the script file here ⬇️⬇️⬇️🦗

Basic transformations in this tutorial:


Translation

The transformation consisting in moving one geometry from one position to another one.

Translate by vector

import Rhino.Geometry as rg

a = i_point + i_vect

Translate by vector (within)

import Rhino.Geometry as rg
a = i_point + rg.Vector3d(300, 300, 0)

Translate by vector (with slider)

import Rhino.Geometry as rg

a = i_point + rg.Vector3d(i_x, i_y, i_z)

Move by transform

import Rhino.Geometry as rg

xform = rg.Transform.Translation(i_x, i_y, i_z)
i_point.Transform(xform)
a = i_point

Rotation

Rotate around a geometry i.e., around a point as rotation center.

import Rhino.Geometry as rg

xform = rg.Transform.Rotation(i_angle, i_center)
i_box.Transform(xform)
a = i_box

Scale

Scale transform by a scale factor. The following is an uniform scaling (you can also assign different scale factor to each scaling vector).

import Rhino.Geometry as rg

xform = rg.Transform.Scale(i_center, i_scale_f)
i_circle.Transform(xform)
a = i_circle

Combine transformations (move + rotate)

Often you will need to combine multiple transformations to obtain the desired result. It is possible to do it in two seperate GHPython components or in one single one.

With multiple components

The first translation component:

import Rhino.Geometry as rg

xform = rg.Transform.Translation(i_x, i_y, i_z)
i_crv.Transform(xform)
a = i_crv

The second rotation component:

import Rhino.Geometry as rg

xform = rg.Transform.Rotation(i_angle, i_center)
i_crv.Transform(xform)
a = i_crv

Q: what would happen if you change the order of the two transformations? 🙉

In one single GHPython component

The previous two components can be merged into a single GHPython component. This can shorten your grasshopper script and make it more compact.

import Rhino.Geometry as rg

# create transform translation
xform_transl = rg.Transform.Translation(i_x, i_y, i_z)

# create transform rotation
xform_rot = rg.Transform.Rotation(i_angle, i_center)

# couple them (*! order matters !)
xform_total = xform_transl * xform_rot

# apply it to the object
i_crv.Transform(xform_total)

# output the result
a = i_crv

Plane-to-plane

This is one of the most useful transforms. It is very convenient since you just need two planes: the starting one (source), and the one describing the wished new pose.

import Rhino.Geometry as rg

xtransform = rg.Transform.PlaneToPlane(i_plane_src, i_plane_tgt)
i_geo.Transform(xtransform)
a = i_geo