point a vector (on axis) in direction of another vector

196 views
Skip to first unread message

e955...@gmail.com

unread,
Nov 2, 2014, 7:55:07 AM11/2/14
to python_in...@googlegroups.com
hi guys,

i know how to use the angleBetween command to find the angle between two vectors, and then use the rotateBy method to rotate one vector to align with the other. My problem is though, imagine that the first vector has its freedom of movement limited by an axis, so all it can do is rotate along a particular plane.

Is there a way of finding that angle to rotate the first vector to so that it is pointing in the direction of the second vector, but restricted by its axis.

hope this makes sense
thanks,
Sam

Marcus Ottosson

unread,
Nov 2, 2014, 8:09:19 AM11/2/14
to python_in...@googlegroups.com
Hey Sam,

Would it be helpful to visualise your requirements by physically creating nodes, such as locators, to represent your points and vectors, and to utilise the standard Maya constraints, such as aim and pointConstraint as opposed to coding directly?

This way you could more easily illustrate to us what it is you're looking to do, maybe via a plain GIF image.

I may also add that, unless you're expecting your computations to run across a large number of items, sticking with nodes may even be the faster way, development-time wise.

Best,
Marcus


--
You received this message because you are subscribed to the Google Groups "Python Programming for Autodesk Maya" group.
To unsubscribe from this group and stop receiving emails from it, send an email to python_inside_m...@googlegroups.com.
To view this discussion on the web visit https://groups.google.com/d/msgid/python_inside_maya/888778fe-c9d8-41a4-b344-5c2d24345b42%40googlegroups.com.
For more options, visit https://groups.google.com/d/optout.



--
Marcus Ottosson
konstr...@gmail.com

e955...@gmail.com

unread,
Nov 2, 2014, 9:22:10 AM11/2/14
to python_in...@googlegroups.com
hi Marcus,

ive got an image here im trying to upload, but theres no where to post an image. Do you know how ha

sam

Marcus Ottosson

unread,
Nov 2, 2014, 10:52:10 AM11/2/14
to python_in...@googlegroups.com
It's a mailing list, you can email the image, along with your message.


sam

--
You received this message because you are subscribed to the Google Groups "Python Programming for Autodesk Maya" group.
To unsubscribe from this group and stop receiving emails from it, send an email to python_inside_m...@googlegroups.com.

For more options, visit https://groups.google.com/d/optout.



--
Marcus Ottosson
konstr...@gmail.com

sam williams

unread,
Nov 2, 2014, 12:42:43 PM11/2/14
to python_in...@googlegroups.com

​like this...;)

--
You received this message because you are subscribed to a topic in the Google Groups "Python Programming for Autodesk Maya" group.
To unsubscribe from this topic, visit https://groups.google.com/d/topic/python_inside_maya/z1z0PXZe12M/unsubscribe.
To unsubscribe from this group and all its topics, send an email to python_inside_m...@googlegroups.com.
To view this discussion on the web visit https://groups.google.com/d/msgid/python_inside_maya/CAFRtmOBAtru4sjvXeL84PQDonXTV1qy4daXX%3DK8Abj%2BVYQ6Eow%40mail.gmail.com.

Joe Weidenbach

unread,
Nov 3, 2014, 2:31:46 AM11/3/14
to python_in...@googlegroups.com
Image aside, from what I'm reading, you're trying to get basically a 2D vector on a given plane that matches the rotation (within its plane) of another vector?

Or, put another way, you'd like to use this code to project an arbitrary vector onto a given plane?

Something like this?  If so, I can probably come up with something.


This email is free from viruses and malware because avast! Antivirus protection is active.


sam williams

unread,
Nov 3, 2014, 4:13:54 AM11/3/14
to python_in...@googlegroups.com
ye, i guess its hard to explain. but imagine the vector thats attached to the axis is being magnetically pulled in the direction of the other vector, but obviously it can only point in that direction within the rotation plane of the axis its attached to.

ha sorry, im not a technical guy really, 
Sam

Marcus Ottosson

unread,
Nov 3, 2014, 4:37:13 AM11/3/14
to python_in...@googlegroups.com

ye, i guess its hard to explain.

This is why I suggested that you mock it up with plain nodes in Maya. Posting a generic image of a vector operation won’t do anyone much good, unless you also annotate on it to illustrate your intent.

It sounds very doable with an aim constraint, as it also does blending back and forth.


--
You received this message because you are subscribed to the Google Groups "Python Programming for Autodesk Maya" group.
To unsubscribe from this group and stop receiving emails from it, send an email to python_inside_m...@googlegroups.com.

For more options, visit https://groups.google.com/d/optout.



--
Marcus Ottosson
konstr...@gmail.com

sam williams

unread,
Nov 3, 2014, 4:53:35 AM11/3/14
to python_in...@googlegroups.com
Yo Marcus, its not generic, i did it in photoshop and its very specific to this problem. vector one is rotating around its axis to point in the direction of vector two. I need to find the angle it rotates to.

This is going to be part of a script that will calculate this particular rotation problem hundreds of times. to orient circles in the direction of another abitrary vector, but all the circles will be only able to rotate around a particular axis. Its like an aim contstraint, but the object aiming will itself be constrained to an axis. I dont know if i can constrain vectors. I was sure its some sort of dot/cross product type of combo.

thanks, 
Sam

Marcus Ottosson

unread,
Nov 3, 2014, 5:03:12 AM11/3/14
to python_in...@googlegroups.com

I dont know if i can constrain vectors.

A vector is merely a point stuck at the origin with rotational information. When you aim-constrain a locator at the origin, it’s rotation values is (one of the possible) XYZ vector values.

Its like an aim contstraint, but the object aiming will itself be constrained to an axis

If there are two points involved in an aim-constrain setup, Source and Target, with Source pointing towards Target, are you saying that the Target will also be constrained to something else? In that case, why not merely constrain it that way?


--
You received this message because you are subscribed to the Google Groups "Python Programming for Autodesk Maya" group.
To unsubscribe from this group and stop receiving emails from it, send an email to python_inside_m...@googlegroups.com.

For more options, visit https://groups.google.com/d/optout.



--
Marcus Ottosson
konstr...@gmail.com

Marcus Ottosson

unread,
Nov 3, 2014, 5:05:53 AM11/3/14
to python_in...@googlegroups.com

When you aim-constrain a locator at the origin, it’s rotation values is (one of the possible) XYZ vector values.

Sorry, I’m talking garbage. When you aim constrain a locator at the origin, and offset a child of that locator by 1 unit in the direction of the target, it’s worldspace transform values is vector values.

--
Marcus Ottosson
konstr...@gmail.com

sam williams

unread,
Nov 3, 2014, 6:33:21 AM11/3/14
to python_in...@googlegroups.com
im saying the source is constrained to rotate around a particular axis. 

So say the axis vector is pointing straight up in y (0,1,0) and the vector source is pointing in x (1,0,0) and there is a target vector which is -45 in x and z. so pointing diagonally down from the horizontal. I need to know how much i need to rotate the source vector so that it is pointing in the direction of the target vector. But obviously all it can do is swivel around its axis (0,1,0). In this particular case it would need to rotate -45 degrees in y to be pointing at the destination vec. 

i hope this clears it up better.

thanks,
Sam

sam williams

unread,
Nov 3, 2014, 6:52:55 AM11/3/14
to python_in...@googlegroups.com
actually it wouldnt be -45 in y, it would be -135 in y

Anthony Tan

unread,
Nov 3, 2014, 7:54:44 AM11/3/14
to python_in...@googlegroups.com
This may be me jumping in late, so stop me if I've missed something, but what's the angleBetween not giving you? As I read it, sounds like you want to rotate on the plane as defined by A and B, which means you want to rotate around the vector C which is perpendicular to A and B, by the amount which would be angleBetween(A, B). If you want to constrain to an arbitrary vector C which *isn't* perpendicular to A and B, there's no solve that will make A == B with a single angle.
--
You received this message because you are subscribed to the Google Groups "Python Programming for Autodesk Maya" group.
To unsubscribe from this group and stop receiving emails from it, send an email to python_inside_m...@googlegroups.com.

sam williams

unread,
Nov 3, 2014, 10:01:37 AM11/3/14
to python_in...@googlegroups.com, antho...@greenworm.net
the 'angleBetween' gives you the angle you should rotate vector 1 to so that it aligns perfectly with vector 2. In my example vector 1 can only partially point in the direction of vector 2 because its only allowed to rotate around a specific axis (in my example (0,1,0))

see it like vector 1 is the metal arm on a compass and vector 2 is magnetically pulling it in its direction. But obviously the compass arm cannot leave its axis, but it still points in the direction of vector 2 as much as it can within its circle of rotation. The other vector maybe pointing anywhere. I just need a formula that will calculate how many degrees the compass arm needs to rotate to be pointing at vector 2 (within its axis).

does this make more sense?
Sam

Anthony Tan

unread,
Nov 3, 2014, 11:57:20 PM11/3/14
to sam williams, python_in...@googlegroups.com
It does, but you're not giving many constraints so you'll have to deal with the general case. Since your three vectors have no actual relationship between them, and if i understand correctly that the axis of rotation is the major constraint, you need to wrangle them like this:
 
A' = Project A onto plane defined by normal C  
B' = Project B onto plane defined by normal C
and then angleBetween (A', B')
 
If A' == A, but B' != B or vice versa, you can't get a clean solve since you're either trying rotate on a plane that doesn't contain B (A==A, but B' != B) or your source vector doesn't lie on the correct starting plane (A' != A). Either way you need to crunch your source and target vectors down so it becomes a straightforward angle problem.
 
Does that help any? I'm mangling some data (well, building a mesh) right now but in my head this is how I'd attack the problem. If my 3D maths isn't too rusty, i haven't forgotten anything.

Joe Weidenbach

unread,
Nov 5, 2014, 10:47:49 AM11/5/14
to python_in...@googlegroups.com
What you're describing should work perfectly, Anthony, however, there's one optimization -- Vector A is already on the plane defined by normal C based on the constraints Sam gave, so we really (using your numbers) only need to find B', and then the angleBetween(A, B').  For the actual 3D math, and allowing normal C to be an arbitrary vector, I'd do something like this:

A = Current Vector
B = Desired Direction Vector
C = Normalize(Desired Normal axis)
tangent = Normalize(A Cross C)
# Probably unnecessary, but a valid step just to check.  Find the "Correct" A' based on the normal and tangent :)
A' = C Cross tangent
A' = Normalize(A')
# Ok, now we have a proper A' that is perpendicular to the normal and the tangent, and normalized
# Project the B onto the plane defined by A' and tangent
B'x = Dot(A', B)
B'y = Dot(tangent, B)
B' = (A' * B'x) + (tangent * B'y)
angleBetween(A', B')

# If you want to take this one more step and find the final vector A, it's pretty simple at this point, even without the angleBetween.
# Just normalize B' and multiply by A's original length
FinalA = Normalize(B') * A.Length
--
You received this message because you are subscribed to the Google Groups "Python Programming for Autodesk Maya" group.
To unsubscribe from this group and stop receiving emails from it, send an email to python_inside_m...@googlegroups.com.

For more options, visit https://groups.google.com/d/optout.

Brad Friedman

unread,
Nov 5, 2014, 11:30:34 AM11/5/14
to python_in...@googlegroups.com
Cross Product!!!  It's called a cross product.  You need to execute two of them. Don't know the python to execute. But the math is common and easily implemented if for some reason, it can't be found in the existing library. Go read up. 


On Nov 2, 2014, at 12:42 PM, sam williams <e955...@gmail.com> wrote:

<vectorRotationDiagram.jpg>
​like this...;)

Joe Weidenbach

unread,
Nov 5, 2014, 1:34:27 PM11/5/14
to python_in...@googlegroups.com
The easiest way I've found is to tie into the API--maya has a great vector class (OpenMaya.MVector) that handles cross products and dot products.

Joe Weidenbach

unread,
Nov 5, 2014, 1:36:10 PM11/5/14
to python_in...@googlegroups.com
Brad's right, though, it's easy to roll your own as well.  I wrote a vector class midway through my thesis for calculations, only to dive into the API a week later and discover MVectors.

Joe Weidenbach

unread,
Nov 5, 2014, 1:41:26 PM11/5/14
to python_in...@googlegroups.com
You could also use PyMEL--it wraps the MVector class for easy access.  I believe it's pymel.datatypes.Vector.  I prefer not to use PyMEL, myself (mainly because it's my understanding that a lot of big companies have legacy systems that don't support it, so you need to be well-versed in strict Maya and Python rather than relying on the admittedly less work PyMEL layer--also, using PyMEL broke my code when I switched to 2013 from 2012 because of a bug in the UI wrapper).

Marcus Ottosson

unread,
Nov 5, 2014, 2:56:14 PM11/5/14
to python_in...@googlegroups.com

I wrote a vector class midway through my thesis for calculations, only to dive into the API a week later and discover MVectors.

Me too. :) I posted this in an answer to Sam, never heard back on whether or not it was actually useful, but posting it here again as it might be relevant.

Anthony Tan

unread,
Nov 5, 2014, 8:28:15 PM11/5/14
to python_in...@googlegroups.com
I reckon everyone at some point started (completed) writing a vector class only to find that the relevant software package had it's own, probably better optimised, version.. at least doing it from first principles sort of encodes the maths in your head I guess? Either that or its a cruel rite of passage and code lesson...I can't decide.

Marcus Ottosson

unread,
Nov 6, 2014, 1:55:29 AM11/6/14
to python_in...@googlegroups.com
In my case, I was studying OpenGL at the time and the math was already at the front of my mind; writing that probably took less than an hour. It was also a case of not knowing there was already built-in functionality for it, although I remember there was some things PyMEL and OpenMaya had trouble with for the task I had at hand, if it wasn't cross-product then it may have been something else..

Either way, "a cruel rite of passage" is a little dramatic, in my opinion. :)


For more options, visit https://groups.google.com/d/optout.



--
Marcus Ottosson
konstr...@gmail.com

Reply all
Reply to author
Forward
0 new messages