[Maya-Python] Re: Maya API: rotate around certain point without affecting rotate pivot

2018-08-27 Thread igo rov
Hi thanks, 
your code has some helpful information for me...
this is what i got so far, though the locator is no vertex... and you have 
to execute first part, then rotate reference locator and then execute 
second part. Since api 1.0 has no" MMatrix.setElement" I find the very 
useful MTransformationMatrix. But as always when using API I find myself 
going long and indirect ways to reach my goal...

import maya.OpenMaya as om

# execute this to initialize the selections and get the positions

selCube = om.MSelectionList()
selCube.add("pCube1")
mDagCube = om.MDagPath()
selCube.getDagPath(0, mDagCube)
fnTransCube = om.MFnTransform(mDagCube)
posCube = fnTransCube.translation(4)

selLoc = om.MSelectionList()
selLoc.add("locator1")
mDagLoc = om.MDagPath()
selLoc.getDagPath(0, mDagLoc)
fnTransLoc = om.MFnTransform(mDagLoc)
posLoc = fnTransLoc.translation(4)

# execute this to get the hypothetical child Matrix
vec = posCube - posLoc 

mChildTransMatr = om.MTransformationMatrix()
mChildTransMatr.setTranslation(vec, 4)
childMatr = mChildTransMatr.asMatrix()

# now you can move and rotate your parent object and again get his matrix
parentMatr = mDagLoc.inclusiveMatrix()


# this gets the child's world matrix
childWorldMatr = childMatr * parentMatr

# last step is to apply the matrix to the hypothetical child
chMatr = om.MTransformationMatrix(childWorldMatr)
quatRotation = om.MQuaternion()
rot = chMatr.rotation()
trans = chMatr.translation(4)
fnTransCube.setTranslation(trans, 4)
fnTransCube.setRotation(rot,4)



Am Montag, 27. August 2018 05:22:13 UTC+2 schrieb Pedro Bellini:
>
>  Hi, see if this works
>
> Basically you need to compute the transformation matrix that represents 
> the rotation from a specific point ("vertex" in your case). That can be 
> generally done by matrix composition m1*m2-1*transform*m2. Where m1 is 
> the matrix of the object you want to move, m2-1 is the inverse matrix of 
> the object you want to use as reference (parent space), transform is your 
> rotation matrix, then you move it back to world by multiplying back m2.
>
> A quick example in maya can look like this:
>
> from maya.api import OpenMaya
> from maya import cmds
>
> cube = cmds.polyCube()[0]
> vtx = cube+'.vtx[2]'
> vtx_pos = cmds.xform(vtx, q=1,ws=1,t=1)
> # vertex does not have orientation, so will keep world
> # you can use vertex normals or custom...
> # ill just extract the position
> vtx_world_mmat = OpenMaya.MMatrix()
> for i in xrange(3):
> vtx_world_mmat.setElement(3,i, vtx_pos[i])
>
> grp = cmds.createNode('transform')
> cmds.xform(grp, t=(5,0,0))
> obj = cmds.polyPrism()[0]
> cmds.parent(obj,grp,r=1)
> obj_world_mat = cmds.xform(obj, q=1,ws=1,m=1)
>
> # compose the rotation matrix however you want, ill just make from euler 
> rotation
> rot_eu = OpenMaya.MEulerRotation()
> rot_eu.y=3.1415*.5 # 90 deg (ish)
> rot_mmat = rot_eu.asMatrix()
>
> obj_world_mmat = OpenMaya.MMatrix(obj_world_mat)
>
> # transform
> *resulting_mmat = obj_world_mmat * vtx_world_mmat.inverse() * rot_mmat * 
> vtx_world_mmat*
> # rotate
> cmds.xform(obj, ws=1, m=list(resulting_mmat))
>
> If I understood you correctly this should be it.
>
> Cheers.
>

-- 
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_maya+unsubscr...@googlegroups.com.
To view this discussion on the web visit 
https://groups.google.com/d/msgid/python_inside_maya/07851383-12c6-4119-b941-ee486a1e94c8%40googlegroups.com.
For more options, visit https://groups.google.com/d/optout.


Re: [Maya-Python] MMeshIntersector: Getting world position / matrix of point in object space

2018-08-04 Thread igo rov
...yes thats true, and I wasnt`t expecting that there could be a bug in the
API :)  Deciding for api 1.0 or 2.0 ist not too easy.
I know the courses of cgcircuit, but for python. Hopefully taking the next
step to c++ won`t bee too hard for somebody coming more from the artist
side of maya. Though I will stick with python also, and defintely come back
here soon.

2018-08-04 18:08 GMT+02:00 Luiz Amaral :

> Hey igo, happy to see you solved the puzzle! Its such a great feeling to
> have our tools working. Specially after some struggle ;)
>
> Maya API can be tricky sometimes.
>
> I highly recomend Chad Vernon’s courses on cgcircuit for learning c++ API
>
> https://www.cgcircuit.com/
>
>  http://www.chadvernon.com/blog/resources/maya-api-programming/
>
> Good luck and enjoy the ride :D
>
> --
> 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/zGdhBD6SuUE/unsubscribe.
> To unsubscribe from this group and all its topics, send an email to
> python_inside_maya+unsubscr...@googlegroups.com.
> To view this discussion on the web visit https://groups.google.com/d/
> msgid/python_inside_maya/02874409-af3c-4fc9-afa2-
> 083f3cf40a95%40googlegroups.com.
> For more options, visit https://groups.google.com/d/optout.
>

-- 
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_maya+unsubscr...@googlegroups.com.
To view this discussion on the web visit 
https://groups.google.com/d/msgid/python_inside_maya/CANAXgk-JKK34Rr02TdRabLPwgnbDTesjF4e%3DE6j-SUUWWHhEDg%40mail.gmail.com.
For more options, visit https://groups.google.com/d/optout.


Re: [Maya-Python] MMeshIntersector: Getting world position / matrix of point in object space

2018-08-03 Thread igo rov
Hi again,

I don`t know how much sense this code still makes, however for me its for 
learning purposes and for me its one step towards my goal to learn c++ api. 
Especially converting between vectors, MFloatPoint and Matrix type might be 
horrible code...but I didn*t find a more direct way.at least it works 
for me.
If you have some hints for me, how to do things more clever, please don`t 
hesitate

import maya.OpenMaya as om

selList = om.MSelectionList()
selList.add("pCube1")
dagCube = om.MDagPath()
selList.getDagPath(0, dagCube)
mesh = dagCube.child(0)
trFn = om.MFnTransform(dagCube)
matrCube = trFn.transformationMatrix()
##
mesh.apiTypeStr() # should be kMesh
##
intersector = om.MMeshIntersector()
intersector.create(mesh, matrCube) # no error in API 1.0...Yeah ;)

selListLoc1 = om.MSelectionList()
selListLoc1.add("locator1")
dagLoc1 = om.MDagPath()
selListLoc1.getDagPath(0, dagLoc1)
trFnLoc1 = om.MFnTransform(dagLoc1)
vecLoc1 = trFnLoc1.translation(4)
pointLoc1 = om.MPoint(vecLoc1.x, vecLoc1.y, vecLoc1.z)
mPointOnMesh = om.MPointOnMesh()
intersector.getClosestPoint(pointLoc1,mPointOnMesh, 1000)
resultPoint = mPointOnMesh.getPoint()

# first I create vector
resultVector = om.MVector(resultPoint.x, resultPoint.y,resultPoint.z)

# then I convert to matrix
matrTrans = om.MTransformationMatrix()
matrTrans.setTranslation(resultVector,4)
localMatrix = matrTrans.asMatrix(1)
# this is the important step to get the world matrix, multiply child/local 
with the parent Matrix
worldMatr = localMatrix*matrCube
worldMatrTrans = om.MTransformationMatrix(worldMatr)

selListLoc2 = om.MSelectionList()
selListLoc2.add("locator2")
dagLoc2 = om.MDagPath()
selListLoc2.getDagPath(0, dagLoc2)
trFnLoc2 = om.MFnTransform(dagLoc2)
trFnLoc2.set(worldMatrTrans)

-- 
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_maya+unsubscr...@googlegroups.com.
To view this discussion on the web visit 
https://groups.google.com/d/msgid/python_inside_maya/55ad67e8-62b9-48a5-8155-4bc90a7de78f%40googlegroups.com.
For more options, visit https://groups.google.com/d/optout.


Re: [Maya-Python] MMeshIntersector: Getting world position / matrix of point in object space

2018-08-02 Thread igo rov
I get the same error

-- 
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_maya+unsubscr...@googlegroups.com.
To view this discussion on the web visit 
https://groups.google.com/d/msgid/python_inside_maya/16e2c4e6-565a-41f8-b559-83c20ac28095%40googlegroups.com.
For more options, visit https://groups.google.com/d/optout.


Re: [Maya-Python] MMeshIntersector: Getting world position / matrix of point in object space

2018-08-02 Thread igo rov
Can you execute this:

import maya.api.OpenMaya as om2

selList = om2.MGlobal.getActiveSelectionList()
dag = selList.getDagPath(0)
matr = dag.inclusiveMatrix()
mesh = dag.child(0)
meshIntersect = om2.MMeshIntersector()
meshIntersect.create(mesh, matr)


I get an error, because it won`t accept the "matr" argument

Am Donnerstag, 2. August 2018 02:19:20 UTC+2 schrieb Luiz Amaral:
>
> You should at least pass the identity matrix like this
>
> meshIntersect.create(mesh, om2.MMatrix.identity)
>

-- 
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_maya+unsubscr...@googlegroups.com.
To view this discussion on the web visit 
https://groups.google.com/d/msgid/python_inside_maya/6455b845-aa61-4458-932b-867c1c16c895%40googlegroups.com.
For more options, visit https://groups.google.com/d/optout.


Re: [Maya-Python] MMeshIntersector: Getting world position / matrix of point in object space

2018-08-01 Thread igo rov
Hi,
as for the missing Matrix parameter, there is this thread:
https://groups.google.com/forum/#!topic/python_inside_maya/axX7W8imeEU

I have the same problems as described there, if I pass the matrix I will
get this error: More keyword list entries (4) than format specifiers (2)

But you are right, there has to be a Matrix parameter as the documentation
says:.

then the matrix passed to create() should provide the mapping
from world space to the mesh's object space.


So for me the MMeshIntersector is not working

2018-08-01 23:47 GMT+02:00 Luiz Amaral :

> Hey there,
>
> “ ‘Point’ Specifies the location for which to evaluate the closest point
> on the mesh. `point' is transformed using the matrix parameter passed to
> the create() method, so for example if your matrix maps world to object
> space, then `point' should be specified in world space.”
>
> You should either pass the cube’s world matrix on creation or pass the
> point localized to the cube’s space on get closest point.
>
> Since you created the MMeshIntercector without passing a matrix to it the
> get closest point function expects a localized position.
>
> That’s why you are getting a “random” point instead of the closest point
> to locator2.
>
> If you pass the cube’s world matrix to the create just multiply the result
> of the get closest point by the same matrix and your locator will snap to
> the expected place.
>
> Or you can keep create the way it is and multiply the point by the inverse
> of the cube’s world matrix before passing it to the get closest point and
> then multiply the result by the world matrix again.
>
> Cheers,
>
> Luiz
>
> --
> 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/zGdhBD6SuUE/unsubscribe.
> To unsubscribe from this group and all its topics, send an email to
> python_inside_maya+unsubscr...@googlegroups.com.
> To view this discussion on the web visit https://groups.google.com/d/
> msgid/python_inside_maya/82d9cdd0-b19f-4da4-b36d-
> 883ef7f86deb%40googlegroups.com.
> For more options, visit https://groups.google.com/d/optout.
>

-- 
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_maya+unsubscr...@googlegroups.com.
To view this discussion on the web visit 
https://groups.google.com/d/msgid/python_inside_maya/CANAXgk8_jZvjo2%3Dp0jvNk06iWC_Oq584yEiUv%2BeS8Z12HSt5Cg%40mail.gmail.com.
For more options, visit https://groups.google.com/d/optout.


[Maya-Python] MMeshIntersector: Getting world position / matrix of point in object space

2018-07-31 Thread igo rov
Hi,

I utilized the MMeshintersector.getClosestPoint()  to get closest point on 
a cube. my problem is, that I can`t make use of it (e.g.  set the world 
position of a locator) because its in local space.

- MMeshIntersector.getClosestPoint gives me a MPointOnMesh
- MPointOnMesh.point gives me a MFloatPoint

I have less knowledge of Matrix, I guess if i would have a child matrix I 
could multiply with the parent. Her is the full code:


import maya.api.OpenMaya as om2

selList = om2.MGlobal.getActiveSelectionList()
dag = selList.getDagPath(0)

matr = dag.inclusiveMatrix()

mesh = dag.child(0)
meshIntersect = om2.MMeshIntersector()
meshIntersect.create(mesh)  # documentation says, mesh and matrix as 
parametres, ...
# but matrix throws me an error, without Matrix 
it works I guess

selLoc = om2.MSelectionList()
selLoc.add("locator1")
dagLoc = selLoc.getDagPath(0)
trLoc = om2.MFnTransform(dagLoc)
vec = trLoc.translation(4)

point = om2.MPoint(vec)
pOnMesh = meshIntersect.getClosestPoint(point,1000)
pos3d = pOnMesh.point #this gives the local translation of the mesh`s 
transform as MFloatPoint() 

# the following is kind of a mess... trying to get the world position of 
the point in object space. But it gives me wrong results

XVal = pos3d.x
YVal = pos3d.y
ZVal = pos3d.z

childMatr = om2.MMatrix()
childMatr.setToIdentity()
childMatr.setElement(3,0,XVal)
childMatr.setElement(3,1,YVal)
childMatr.setElement(3,2,ZVal)
worldM = childMatr * matr

targetX = worldM.getElement(3,0)
targetY = worldM.getElement(3,1)
targetZ = worldM.getElement(3,2)
newVec = om2.MVector(targetX, targetY, targetZ)


selLoc2 = om2.MSelectionList()
selLoc2.add("locator2")
dagLoc2 = selLoc2.getDagPath(0)
trLoc2 = om2.MFnTransform(dagLoc2)
trLoc2.setTranslation(newVec, 4)



-- 
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_maya+unsubscr...@googlegroups.com.
To view this discussion on the web visit 
https://groups.google.com/d/msgid/python_inside_maya/405bdeae-e71d-43bd-a1f9-442ccbf5e586%40googlegroups.com.
For more options, visit https://groups.google.com/d/optout.


Re: [Maya-Python] Find the closest point on mesh

2018-07-30 Thread igo rov
Hi , I know its a old post...but how to you get the world position of the 
point which is MFloatPoint, 3d Point in objects space?

Am Freitag, 5. Dezember 2014 05:42:18 UTC+1 schrieb illunara:
>
> Thank Chad, it works beautifully :D 
>
> On Fri, Dec 5, 2014 at 6:40 AM, Chad Vernon  > wrote:
>
>> You're passing in the wrong matrix.  You don't want the inverse you just 
>> want the inclusive matrix :
>>  
>>
>> mat = meshPath.inclusiveMatrix()
>> resultPoint = om.MPoint()
>> polyIntersect = om.MMeshIntersector()
>> polyIntersect.create(shape, mat)
>> ptON = om.MPointOnMesh()
>> polyIntersect.getClosestPoint(pt, ptON);
>> resultPoint = om.MPoint(ptON.getPoint().x, ptON.getPoint().y, 
>> ptON.getPoint().z)
>>
>>
>> On Thursday, December 4, 2014 12:56:20 PM UTC-8, illunara wrote:
>>>
>>> Hi Chad
>>> I attach the code using MMeshIntersector in the first post, but the 
>>> result gone wrong :( 
>>> can you take a quick look at it please?
>>>
>>> -- 
>> 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/EznnAtM0Hgw/unsubscribe
>> .
>> To unsubscribe from this group and all its topics, send an email to 
>> python_inside_maya+unsubscr...@googlegroups.com .
>> To view this discussion on the web visit 
>> https://groups.google.com/d/msgid/python_inside_maya/68f88735-8cb6-44db-8655-f93d7aff80a1%40googlegroups.com
>>  
>> 
>> .
>>
>> For more options, visit https://groups.google.com/d/optout.
>>
>
>

-- 
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_maya+unsubscr...@googlegroups.com.
To view this discussion on the web visit 
https://groups.google.com/d/msgid/python_inside_maya/0de6a80f-a472-4acc-9970-1d2a92924bc2%40googlegroups.com.
For more options, visit https://groups.google.com/d/optout.