How to avoid RhinoScript repetitive mouse interactions in a "For Loop"


#1

Hello,
Is there any method to avoid mouse Interactions in a “For Loop”?
The goal is to pick a curve once and let the code store and call it automatically.
Here is the Example:

Option Explicit

Call Main()
Sub Main()
    For i=10 To 50 Step 10
	Rhino.CopyObject obj, array(i, 0, 0)
    Next
End Sub

Function obj
    a = Rhino.GetObject("Pick a Curve")
    obj = a
End Function

(Steve Baer) #2

Maybe I’m confused about what you are trying to do, but if you are trying to copy a single object a bunch of times you should just get the object once and then call CopyObject a bunch of times in your loop.


#3

@stevebaer
Yep
This is exactly what I want to do,
but In this particular code you have to pick the curve for each loop.
My question is how to avoid it.
Thanks


(Rajaa Issa) #4

@Clive This is how I would write the script

Call Main()
Sub Main()
	Dim strObj, i
	strObj = Rhino.GetObject("Pick a Curve", 4)

	If Not IsNull(strObj) Then
		For i=10 To 50 Step 10
			Rhino.CopyObject strObj, array(i, 0, 0)
		Next
	End If
End Sub

(Steve Baer) #5

The equivalent in python would be

import rhinoscriptsyntax as rs

curve = rs.GetObject("Pick a Curve", rs.filter.curve)
if curve:
    for i in range(10, 50, 10):
        rs.CopyObject(curve, (i, 0, 0))

#6

@rajaa
@stevebaer
Thanks
My apologies I could not clarify it better.
As it is reflected on my code, I am looking for a way to get the data of the initial curve from another function.
Yours is all running in one single procedure.
And the initial curve has to be picked from the scene.


(Steve Baer) #7

Using your sample, wouldn’t you just want?

Option Explicit

Call Main()
Sub Main()
    Dim curve
    curve = obj
    Dim i
    For i=10 To 50 Step 10
	Rhino.CopyObject curve, array(i, 0, 0)
    Next
End Sub

Function obj
    Dim a
    a = Rhino.GetObject("Pick a Curve")
    obj = a
End Function

There may be ways to shorten this; my VBScript is pretty rusty


#8

@stevebaer
I appreciate your time,
If you replaced curve with obj in this line:
Rhino.CopyObject curve, array(i, 0, 0)
You would need to pick the curve in each loop.
and that is what I missed in my sample.
However I do not understand the logic behind this substitution.


#9

Clive,

Every time you use your ‘obj’ function, even if in the end it returns a curve object, the entire body is being executed, including prompting to get curve object. So by doing curve=obj, Steve just runs the function once and assigns the ‘obj’ function result to a variable called ‘curve’. So down the road your copying operation is performed on that defined object instead of running the function in a loop every time, forcing you to pick curve every time.

I would also suggest using more elaborate naming in your code, for example idCurve, strCurve for object string variables, and more descriptive function names as well. Once your code gets more complex it will help you greatly.

hth,

–jarek


(Steve Baer) #10

If you are just getting started with scripting, I would also recommend trying python out.


#11

Thanks for your comprehensive explanation.
Correct me please if I am wrong in this:
The ‘obj’ function result has been assigned to two different variables within this code:

  1. obj =a in Function obj
  2. curve = obj in Sub Main

About…

All well noted, thanks for your time to mention. :slight_smile:


#12

Yeah,
I am also learning Python aligned with my investigation in RhinoScript.


#13

‘obj’ is not really a variable in your function - by assigning ‘a’ to the function name you basically determine what the function returns.

by doing ‘obj=a’ you are assigning ‘a’ to ‘obj’, not the other way around.


#14

Got it,
Many thanks


(Steve Baer) #15

Guess I’m just saying skip why not skip straight to python. You’ll get cross platform scripts, grasshopper compatibility, direct use of RhinoCommon, and all of .NET