cross product of two vectors

814 views
Skip to first unread message

s...@weacceptyou.com

unread,
Mar 21, 2016, 7:35:08 AM3/21/16
to Python Programming for Autodesk Maya
Hello,

i have been using this method to get the cross product of two vectors:

#####

pt1=cmds.pointPosition('locator1')
pt2=cmds.pointPosition('locator2')
pt3=cmds.pointPosition('locator3')

vec1=pm.dt.vector=(pt2[0]-pt1[0],pt2[1]-pt1[1],pt2[2]-pt1[2])
vec2=pm.dt.vector=(pt3[0]-pt1[0],pt3[1]-pt1[1],pt3[2]-pt1[2])

perpenVec=vec1^vec2

####

with the '^' symbol this process works when i tried it at home on maya 2015 in python tab of script editor.

I dont know what has changed but when i tried it at work using maya 2016 in script editor it returns this:

# Error: TypeError: file <maya console> line 14: unsupported operand type(s) for ^: 'tuple' and 'tuple' #

i don't think i have done anything different in my new script as it seems to match my other one. Can anyone tell me why the cross product operator ^ doesn't seem to work here?

thanks alot,
Sam

Nicolas Chaverou

unread,
Mar 21, 2016, 10:38:14 AM3/21/16
to python_in...@googlegroups.com
Hi Sam,

As I was surprised your code used to work I just tried it in Maya 2014 and I have the same error than (which I expected)
Python doesn't define any vector type, only arrays or list. Thus cross product does not mean anything on arrays :)

Better store your vectors to the MVector api class. Something like:

import maya.api.OpenMaya as om
v1, v2 = om.MVector(vec1), om.MVector(vec2)
print v1 ^ v2

Hope that helps,
N.


--
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/f119d42e-75bf-44b5-88bf-d39e660e1aed%40googlegroups.com.
For more options, visit https://groups.google.com/d/optout.

s...@weacceptyou.com

unread,
Mar 21, 2016, 10:55:53 AM3/21/16
to Python Programming for Autodesk Maya
thanks man,

i cant figure out why this works in my other script, but i will use that Mvector class. thanks

Sam

s...@weacceptyou.com

unread,
Mar 21, 2016, 11:10:08 AM3/21/16
to Python Programming for Autodesk Maya, s...@weacceptyou.com
sorry i meant to write this the first time. this seems to work:

####
pt1=cmds.pointPosition('locator1')
pt2=cmds.pointPosition('locator2')
pt3=cmds.pointPosition('locator3')

vec1=pm.dt.Vector(pt2[0]-pt1[0],pt2[1]-pt1[1],pt2[2]-pt1[2])
vec2=pm.dt.Vector(pt3[0]-pt1[0],pt3[1]-pt1[1],pt3[2]-pt1[2])

perpenVec=vec1^vec2
###

Geordie Martinez

unread,
Mar 21, 2016, 10:58:58 PM3/21/16
to python_inside_maya

you can make it a LOT easier of you just stay in pymel and skip maya.cmds. here is an example of what you’re doing:

import pymel.core as pm

# make pynodes out of each locator or transform
loc1=pm.PyNode('locator1')
loc2=pm.PyNode('locator2')
loc3=pm.PyNode('locator3')

# get the World space position of each locator
# each of these returns a Point 
pt1=loc1.getTranslation(space='world')
pt2=loc2.getTranslation(space='world')
pt3=loc3.getTranslation(space='world')

# to get vectors subtract one point from another
# returns a Vector
vec1=pt2 - pt1
vec2=pt3 - pt1

# .cross() is a method on any vector
cross = vec1.cross(vec2).normal() # add .normal() to normalize

--
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,
Mar 22, 2016, 5:36:00 AM3/22/16
to python_in...@googlegroups.com
cool thanks Geordie,

yea i guess subtraction bit is easier, but i would think overall both methods would take as long to write out with having to make pynodes to begin with. But its a nice alternative

thanks,
Sam

--
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/Fbk_J8KVzaQ/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/CABPXW4igxQVyBuiaLuSQ8BjSeFWmMZygBxGGnm2XbYFapykfPA%40mail.gmail.com.
Reply all
Reply to author
Forward
0 new messages