Skip to content

lucianoambrosini.it

Calling Python custom scripts with Buttons and Aliases

✨ In this article, I demonstrate how to simplify your workflow when modelling a generic object within Rhino… using custom buttons and aliases.

It is more likely that many of you will already know that Rhinon’s flexibility in customizing its UI is one of its greatest strengths. In fact, through the methods that I will illustrate in the video at the bottom of this page, you will be able to quickly recall, through a button or through an alias (this is my favourite method) a Python script that you have coded specifically to perform any repetitive action that you would otherwise have to perform manually.

The actions in the video show two Python scripts I have created personally.

In the first case, the first script will allow you to mirror an object through a Median Axis defined by the user – (“Mirror_Between.py”); in the second example instead, I will run a script that will allow you to mirror the object-based on its axis of symmetry (X, Y, Z) – (“Mirror_Symmetrical.py”).

Mirror_Between script

This simple script lets to perform a mirror operation by providing a median axis as input, obtained by selecting two extreme points. The script also applies to a multiple selection of objects.

Mirror_Symmetrical script

This simple script lets to perform a mirror operation by selecting one object-symmetry axes (X, Y, Z). The script also applies to a multiple selection of objects.

🧙🏼⌨️ Magic trigger string-code
    ! _-RunPythonScript (your code) or the "filename"

Button creation

Mouse-right click on the Rhino toolbar > New Button… > (fill in the content as described in the video demo)

"Mirror_adv" customized toolbar icon
    ! _-RunPythonScript (
import rhinoscriptsyntax as rs

objs = rs.GetObjects("Select objects to mirror")

if objs==None:
  print("\nMirror Between (custom cmd) failed to run!\n")
  
else:
    # Give the two between axes points
    p1 = rs.GetPoint("Start point of the Mirror between axes")
    p2 = rs.GetPoint("End point of the Mirror between axes")
    
    # Mid point
    mid = rs.PointDivide(rs.PointAdd(p1, p2), 2)
    
    rs.UnselectAllObjects() # unselect all objs
    rs.SelectObjects(objs) # select only objs mirror to
    
    MirrorBetween=rs.Command("_Mirror " + str(mid))
)

Aliases creation

Rhino Options > Aliases > (fill in the content as described in the video demo)

For aliases in the command macro area, brackets must not be inserted. Very important, in this case, is to insert in quotes exactly the name of the Python file (.py) with your custom code/script. 

Rhino Aliases customization - "Mirror_Between" & "Mirror_Symmetrical"
Python files

Video demo

if you watch it on my YouTube channel it is possible to jump among different highlights 😉

Interested in my works? see *About Me* section and contact me by email and/or by social