Python / move a object with a Vector

hello guys ,

im new to python scripting , so i was working on a script in which i wanted to move a point with a vector , but i got an error .
as you can see in first image , i had a Vector in ((Vector)) wich i print then i append that to ((Vectors)) then i print(( Vectors)) , they are the same , but when i try to point with ((Vectors)) it give me error while i can move the same point with(( Vector)) .
so plz help me if you can .
thx
Arash

it looks like the coordinates are the second item in the list Vectors…
so you’ll have to use Vectors[1] in the MoveObject line to feed it only the numbers.
?

that should allow you to move the point for now until someone more knowledgeable can come along and explain why the list ended up that way.

–edit… or wait. the coordinates are nested in Vectors so Vectors[1] might not work.
I don’t know :slight_smile:

i’d use this:

for i, v in enumerate(Vectors):
    print i, v

which will break down the list into something more readable (more readable for me at least)

but yeah, someone with skills should be along shortly.


edit 2-- last try then i’m out:)

i think you need to use Vectors[0] in the MoveObject line to get it to work in this exact state… if you just move using Vector (the vector) instead of Vectors (the list), it should work ok… MoveObject is expecting a vector and not a list.

hello Jeff ,
thx for quick replay , yes i noticed that Vectors[0] would work but it will only work if i have 1 point and 1 vector to move it , if i have array of points and array of vectors i dont think i can use this method for all of the vectors , right ?
and plz explain to me how my Vector became a list ? :smile:
thx again Jeff

[quote=“WinG, post:3, topic:15539”]
hello Jeff ,
thx for quick replay , yes i noticed that Vectors[0] would work but it will only work if i have 1 point and 1 vector to move it , if i have array of points and array of vectors i dont think i can use this method for all of the vectors , right ?[/quote]

right, not exactly… thing is, you can’t just put a list in MoveObjects and have it do a bunch of multiple moves of a point… you’d have to use a python shortcut called list comprehension which is a simpler way to make a loop which will cycle through a list and apply each item to the command individually.
it would look something like this:

mymoves=[rs.MoveObject(point) for vecs in Vectors]

Vectors being your list… vecs being each item in the list as it loops through.

( @Helvetosaur …can you please correct me if i’m wrong :wink: )


i can’t see your whole script but somewhere you’ve created a list called Vectors… then on line 105, you’ve added Vector to the list"Vectors" with the append function.

Vectors.append(Vector) 

is saying “take the list Vectors and add the object Vector to it”

you are right Jeff , but thats when i have 1 point and multiple Vectors to move that 1 point , then mymoves=[rs.MoveObject(point) for vecs in Vectors] is the way to go , but in here i have multiple points and multiple Vectors each one related to one of the points , and i want to move each point with the related Vector , so i guess i need a list of vectors and to do so , right ?
and as you can see in the images i am creating my vectors in a loop (( Line 101 )) , and i want to save all of the vectors that i created in the loop , so i append them to Vectors (( Line 105 )) so for each of my points in first loop ((line 83)) i would have a vector saved in Vectors list and in next step i want to move all of my points with vectors related to them (( saved in Vectors list )) all at once (( not with Loop but all at once)) .

as i said im new to scripting , so i apology for it and for my bad english .
thx
Arash

hmm… i haven’t looked at what exactly you’re trying to accomplish with the script but if i understand correctly, your MoveObject line shouldn’t be inside the loop then…

the loop can be used to generate the list but then whatever you want to do with that list should be outside the loop instead of indented along with it.

as it’s set up now, each time it goes through the loop, it’s going to move the same point over and over…
or-- if the loop is set up to go through 100 times… the first object which is added to the list will be moved 100 times… then the next item will be moved 99 times etcetc… when really, i think you’re trying to get 100 vectors then move 100 points with those vectors… instead of thousands of moves.

but hey, i’ve gotta go do some errands and breakfast… if someone hasn’t looked at this by the time i’m back, i’ll try to better understand what the loop is doing and offer any advice i can come up with… that said, there are a thousand people at this forum more qualified than i to give scripting advice so hopefully one of them will come along first :smile:


i don’t know if this will help or add confusion but… i made a simple script the other day which has a loop in it, it moves points, and it generates a list…

the code can be seen in this post:
link to orignal thread

import rhinoscriptsyntax as rs
import math

ptlist = []
cPt= rs.coerce3dpoint([0,0,0])
angle = math.radians(15)
angleD = 15
x=0
y=1
z=0

ptlist.append(rs.AddPoint (x,y,z))
x= math.tan(angle)*y 

for i in range(1,102):
    y= x/math.sin(angle)
    x= math.tan(angle)*y
    point=rs.AddPoint(x,y,z)
    rs.RotateObject(point,cPt,-angleD * i)
    ptlist.append(point)

rs.AddInterpCurve(ptlist)

so while it’s inside the loop, the points are being moved (rotated) as they’re created… they are then added to a list but i don’t use the list until after the loop is finished when a curve is drawn through points in the list…

if all i wanted to do was rotate (or move) the points, i wouldn’t of needed the list… so if all you’d like to do is move the points, i don’t think you’ll need the list either. (but again, i’m not 100% sure on the goal of your script yet)

Hi again jeff ,
first thx for your time , i realy botherd you today :smile:
you are right my move shouldnt be inside the loop , i just rememberd that i put it inside the loop so i examin my script . but at the end it should go outside of it .
the thing i want to do is , i have some points stored in a list named Pts and i want to produce some vectors for each one of the points that are stored in Pts and store Vectors in a list named Vectors , the purpose of the loop and the whole script (( right before the move part )) is to creat a list from the Vectors i created .
in next step i want to move each points of Pts with related Vectors of each point that stored In Vectors , all together ((not with loop and step by step but all together )) .
the problem is no matter how many vectors i have after i append them to a list it cant be used as a translation .
i think like you said the problem is that you cant feed multiple Vectors as translation to rs.moveobjects , so i want to know what should i do if i have multiple objects and multiple vectors to translate them and i dont want to use loop to move them (( something like what move component in grasshopper do which move each object with a vector which has equal index of the object )).
i hope i could explain myself correctly this time :smile:
and thats some some realy cool script you linked , i dont get properly how it works yet but its veru cool .
Thx
Arash

That is correct. MoveObject() can only do one move, so if you have a list of objects and a list of moves, you need to create a loop which takes one item from the objects list and the corresponding vector from the vector list and executes the move, then iterates along both lists until done.

objs=[a,b,c,d,e,f]
vecs=[v1,v2,v3,v4,v5,v6]

for i in range(len(objs)): rs.MoveObject(objs[i],vecs[i])

This is unlike Grasshopper which has automatic data matching of two input lists and implied loops - if you were expecting it to work like that, it doesn’t.

–Mitch

1 Like

Hi Arash,

Python scripting is somewhat different from Grasshopper.
Grasshopper actions are specified by a graph, that cannot contain loops.
So Grasshopper has to operate on Rhino object collections as if they were single objects.
And that is fine for Grasshopper :smiley:

Python, being a programming languages that executes text lines and not graphs, has not this limitation and can loop freely over data any time that is needed.
Looping over data is the way scripts work usually.
The only thing that could slow down your script when it moves objects in a loop is Rhino’s redraw.
But using rs.EnableRedraw() you can suspend the redraw before running the loop and then restart it after the objects have been moved.
That way you will not notice any difference between a script (with loops) and a Grasshopper definition.

Or is there another reason to avoid loops ?

EDIT:
hehe … double response.
But Mitch has been faster :smile:
/EDIT

Hello Mitch and emilio ,

thx for your helpful replays , yeah i kinda expected python to work like grasshopper but like Jeff tried to tell me its not work like that , as i said im a noob in python programming , so i guess my mistake was becuse of my brain adapted to the way that grasshopper works … :smiley:

so now i know wut to do , and the reason for not useing loops was beacuse of the slowness of the program , which it looks like i have to deal with it the way emilio said .

thx again guys for your helps , and i hope you dont get tired of me in anytime soon , cause i gonna have alot of question in python programming to ask you guys :smiley:

thx

Arash

Yes, exactly, loops themselves are not inherently slow - depending on how you structure them - what is slow is other stuff like redrawing the display, so if you cut the redraw during the loop it will go much faster.

–Mitch

Hehe … I think here on the forum there is enough people for a large number of questions … :smile: :smile:

This might be beyond what you are looking to do. But this example (kind of) clones the Move command:

https://github.com/dalefugier/SamplePy/blob/master/SampleMove.py

something in there crashes mac rhino… i sent the report but here’s the (whatever it’s called-- the miles of info accompanying a crash report).

sampleMoveCrash.txt (82.5 KB)

I suspect that this is caused by the dynamic draw calls… Maybe the mac display pipeline doesn’t like those. But that’s just a theory at this point… --Mitch

hopefully that’s the problem… because then, i can quit considering whether or not i should attempt to add dynamic drawing to an extrude multiple curves script.
:smile:

@stevebaer can you look into why the above script is crashing MacRhino?

Please add these issues to youtrack for me. I am in no position this week to look into this.

Thanks

http://mcneel.myjetbrains.com/youtrack/issue/MR-1234

1 Like

fwiw… just to make sure i wasn’t sending you all on a wild goose chase, i tried the script again on a fresh boot / rhino launch and yeah… same thing. it’s a crash


jan27 edit
@dale @stevebaer
i re-tried the sample move script in the latest mac rhino release (5A683) and it appears to be working properly now… no more crash.
thanks!