# Poor man's scripting

a proof of concept exercise using numbers.app to create macros with variables & repeatability…

don’t laugh (ok. please do )

exported columnC to .txt then `_-readcommandfile`

fwiw–
B4=B1*11
C4=B\$2
C10= B\$3
C12 =IF(ROW()>B\$4,"",C1)

i’ll post some more experiments if i come up with anything else successful (i ultimately have some goals with this which i can use for real world stuff. not sure if i’ll lose interest prior to getting to that point but i’m pretty sure i’ll avoid learning python at all costs ; ) )

1 Like

Awww, c’mon Jeff, Python is fun!! And in any case, you can do a lot with a little knowledge and just scripting rs.Command() with normal Rhino commands… It’s easier than going out to Numbers/Excel…

–Mitch

And in V5 as well…!

-C-H-A-R-L-E-S-

1 Like

Oops, you’re right… I remember having scripted this (for V4 I guess) and I continued using my script and never realized it had been added to V5… asleep at the wheel again.

ha… that’s the problem… i think it will be too fun.
and i’ll never sleep

but hey, if with python i could just write things like this:

``````_Box
0,11,5.25
40,12.5,6.75
6.75
_SelLast
_ArrayLinear
6
0,11,5.25
0,22,12
``````

(which will work as a macro)

and some of those numbers are variables (like 40, 5.25, and 6.75 in this example) then i’d be willing to give it a shot…
it just doesn’t seem simple to me for whatever reason…
how hard would it be to draw that with python?

Something like this?

``````import rhinoscriptsyntax as rs
rs.Command("_Box 0,11,5.2540 12.5,6.75 6.75")
lco=rs.LastCreatedObjects(select=True)
rs.Command("_ArrayLinear 6 0,11,5.25 0,22,12")
``````

Of course, there are more sophisticated ways to write this in Python, but it works…
–Mitch

hmm. wow. that doesn’t look so bad.

so I just save exactly that as .py and it will work?

I’ll mess around with it in a few hours.

Yep… There’s of course no error checking or any kind of UI, so if you feed it bad info it will error out.

The cool thing about diving a bit deeper into scripting is the ability to create a real tool that has a nice UI - ask user for input, check that it’s valid, etc… as well as do more complex stuff like iterate (loop) through items and/or decide to do things based on results (conditionals)…

But I warn you in advance, once you get hooked, you’ll never be able to quit… Take that from an addict…

–Mitch

I also started scripting by bundling lines of rs.Command"add macro here" and gradually evolved as I had stuff I wanted to solve. Each time a bit more complex than I could handle. I should never have started…

ok… first question

how do i use width here?

``````width=32
rs.Command("_Box 0,11,5.25 width,12.5,6.75 6.75")
``````

(and how did you get the syntax coloring in the forum? that’s how it looks in Xcode too… with the red)

here you go:

``````width=32
rs.Command("_Box 0,11,5.25 "+str(width)+",12.5,6.75 6.75")
``````

If you want Rhino to ask you for the width value you do like this:

``````width=rs.GetReal("Width: ",32)
rs.Command("_Box 0,11,5.25 "+str(width)+",12.5,6.75 6.75")
``````

The rs.GetReal() command asks for a number and uses that input as the width value. I put ,32 in there so the default value is 32. You can also add a minimum and maximum value for the input.

I put the width in a str() to tell python that it is a string (text) since it can not add text and numbers, but it can add text and text.

To get the code to look right you have to add some formatting by hand:

Awesome… i like how you answer my next question before i even ask it

i’m sure i’ll be back in a bit.

Hi Jeff,

To format code do this:

It results in this:

``````import rhinoscriptsyntax as rs

startpoint = '0,11,5.25 '
width='10 '
depth='20 '
height='30 '

rs.Command('_Box '+startpoint+width+depth+height)
``````

Below the code with comments to elaborate on how it works:

``````import rhinoscriptsyntax as rs

# rs.Command take a string (ascii characters as input)
# to form a string in python you need to place in in either quotes 'string' or double quotes "string"
# as mostly each input in a command macro requires a space to confirm I set them behind the values like so:
startpoint = '0,11,5.25 '
width='10 '
depth='20 '
height='30 '

# to combine strings in python you can use the + sign
rs.Command('_Box '+startpoint+width+depth+height)

# working with strings to represent values is not very useful
# here is a way to keep the variables as numbers as opposed to strings so you can do math with them

x = 0
y = -50
z = 5.25
width=10
depth=20
height=30

#in python there is a way to format a string in such a way that you can fill in the positions marked by {}
# the {} items will be filled in order of items placed withon the parentheses after format

#        '_Box x,y,z width depth height'
string = '_Box {},{},{} {} {} {} '.format(x,y,z,width,depth,height)
rs.Command(string)

#now lets change the x to 100

x = 100
# all other variables remain the same but x is now 100
string = '_Box {},{},{} {} {} {} '.format(x,y,z,width,depth,height)
rs.Command(string)

# one more step multiply

x= x*-1
y = y*0.5
z = z*11
width=width*2
depth=depth * 5
height=height *0.2

string = '_Box {},{},{} {} {} {} '.format(x,y,z,width,depth,height)
rs.Command(string)
``````

@jeff_hammond I saw a small mistake in a comment in the code I deleted it. it mentioned something being a python list but it was no longer.

enjoy
-Willem

1 Like

ok… thanks. i saw the edit
and yeah, i need to do math with some of these so i get what you’re saying about not using strings as values.

heh… going good so far.

one other thing i think i need to know how to do right now…

just to make sure my calculations are correct, i need to be able to see a list of results. like, if i were do width÷height, and x÷y, can i popup a dialog in rhino which lists the results?

just something i can use while writing the script but will be removed when finished.

(if it matters-- i’m using Xcode then saving versions there then a -RunPythonScript macro to run the script in rhino… maybe there’s a way to see the results in Xcode but i’d rather not learn about Xcode right now (well, unless that’s the better solution to get the results)… a list in rhino would be fine)

Hi Jeff,

Below a script I wrote while waiting for Rhino to finish some calculations.

I do not know about Xcode but try and see what the print method is giving you:

``````width = 7
height = 2
print width/height
``````

The code for a simple winding stairs:

``````import rhinoscriptsyntax as rs

width=40
depth=8
height=0.8

x= 5
y = depth/-2 #creates the box centered on x axis
z = 0

#notice the w befor x,y,x this is to make sure world coordinates are set instaed of cplane coordinates
#this makes it not neccasery to set the correct view active
string = '_Box w{},{},{} {} {} {} '.format(x,y,z,width,depth,height)
rs.Command(string)

rs.Command('_SelNone _SelLast ') #make sure nothing is selected and select the box

#copy and rotate the box
step_height= 10

rs.Command('_Copy w0,0,0 w0,0,{} _Enter _SelNone _SelLast '.format(step_height))

step_angle = 15
#using Rotate3D to make sure the active cplane is irrelevant
rs.Command('_Rotate3D w0,0,0 w0,0,1 {} '.format (step_angle))

# at this point we could start copying all the code N numert of times for each step
# but we start doing itterations:

number_of_steps = 50

# a for loop will loop number_of_steps times
for N in xrange(number_of_steps):
#the last step is still selected so we can do this
rs.Command('_Copy w0,0,0 w0,0,{} _Enter _SelNone _SelLast '.format(step_height))
step_angle = 15
rs.Command('_Rotate3D w0,0,0 w0,0,1 {} '.format (step_angle))
#notice these lines are indented
#this means we are inside the for loop

#now we are out of the for loop

#Note I have notice some stalling of the script in the execution of the copying macro
``````

Like it wrote at the end it seems here on windows that the macro to copy has an issue and installing at times.
So next lesson is doing it with rhinoscriptsyntax.

-Willem

Hi Jeff,

final version for tonight using rhinoscriptsyntax methids to do the copying and rotating

``````import rhinoscriptsyntax as rs

width=40
depth=8
height=0.8

x= 5
y = depth/-2 #creates the box centered on x axis
z = 0

#notice the w befor x,y,x this is to make sure world coordibates are set insted of cplane coordinates
#this makes it not neccasery to set the correct view active
string = '_Box w{},{},{} {} {} {} '.format(x,y,z,width,depth,height)
rs.Command(string)
rs.Command('_SelNone _SelLast ') #make sure nothing is selected and select the box
# I left creating a box via rs.AddBox for now as that requires A list of eight 3-D points that define the corners of the box.

# we can get the ID's of the selected objects in Rhino via
selected_objects = rs.SelectedObjects()
#notice this is plural, this means we get a list retuned instead of a single object
#we work around this for now by simply getting the first item in the list
#items in a list are counted starting at 0 so to get the first item we do
box_object = selected_objects[0]

#now we set some variables for our steps
step_height= 10
step_angle = 15
number_of_steps = 50

#and run through the loop  'number_of_steps' times

for N in xrange(number_of_steps):
# the method rs.CopyObject returns the ID of the copied object
# we input the id of the object to copy and
# a list [0,0,10] representing the vector (direction) to copy to
copied_object = rs.CopyObject(box_object, [0,0,step_height])

# rotate the copy around center-point [0,0,0]
# at angle 15 an axis=[0,0,1] (vertical)
rs.RotateObject(copied_object,[0,0,0],15,axis=[0,0,1])

#for the next loop we need to use this copied and rotated object
# so the variable box_object is set to contain the new rotated copy
box_object = copied_object

``````

enjoy
-Willem

great, thank you

I’m trying to automate the stair system I use which doesn’t spiral but your example will be a good reference.

(oh, and print works for me.)

1 Like
``````#as mostly each input in a command macro requires a space to confirm I set them behind the values like so:
``````

how do i make it let go of a polyline? i’ve tried multiple spaces and _Enter but i can’t get it to work right.
thx

`````` rs.Command("_Polyline 0 2,2,2 5,5,5 ")
``````

?