Quote:
Originally Posted by zoharl
Please supply a script that creates an example scene (if it's a too complicated scene, then please attach one). On this example scene tell me how to manually do (or approximately do) what you need, and I'll try to help you with the code that do that.

zoharl, thanks. Here the test scene (look attachment):
Just open it and run this code (it generates some useful data such as normal, binormal, centerP and tangent) and def getOrient  it rotates cube:
PS: Forum breaks the code after copying in maya, there will have to be corrected in line 28: tangents (the word breaks into two parts), line 34: binormals (word breaks into two parts) and line 84 MEulerRotation (the word breaks into two parts). I attached .txt file with this part of code for just in case.
Code:
import maya.cmds as cmds
import maya.OpenMaya as OpenMaya
import math
import random
cmds.select ("pPlane1")
selList = OpenMaya.MSelectionList()
OpenMaya.MGlobal.getActiveSelectionList(selList)
selListIter = OpenMaya.MItSelectionList(selList)
dagPath = OpenMaya.MDagPath()
component = OpenMaya.MObject()
selListIter.getDagPath(dagPath, component)
myMesh=OpenMaya.MFnMesh(dagPath)
faceIter=OpenMaya.MItMeshPolygon( dagPath, component)
centerP=faceIter.center(OpenMaya.MSpace.kWorld)
normal=OpenMaya.MVector()
faceIter.getNormal(0,normal,OpenMaya.MSpace.kWorld )
tangents=OpenMaya.MFloatVectorArray()
myMesh.getFaceVertexTangents(faceIter.index(),tang ents,OpenMaya.MSpace.kWorld)
tangent=tangents[0]
binormals=OpenMaya.MFloatVectorArray()
myMesh.getFaceVertexBinormals(faceIter.index(),bin ormals,OpenMaya.MSpace.kWorld)
binormal=binormals[0]
cmds.select(cl=True)
def DegreesToRads(degree):
return degree*(math.pi/180)
def radsToDegrees(rads):
return rads * 180.0 / math.pi
a=0
#function for rotate:
def getOrient(centerP, tangent, binormal, normal, a):
cmds.select ("pCube1")
#rotation formula
x1 = normal[0]*normal[1]*(1math.cos(a))  normal[2]*math.sin(a)
x2 = math.cos(a)+normal[1]*normal[1]*(1math.cos(a))
x3 = normal[2]*normal[1]*(1math.cos(a))+normal[0]*math.sin(a)
#if uncomment this, cube will stand on normal
#x1 = binormal[0]
#x2 = binormal[1]
#x3 = binormal[2]
y1=normal[0]
y2=normal[1]
y3=normal[2]
matrix = OpenMaya.MMatrix()
OpenMaya.MScriptUtil.setDoubleArray(matrix[0], 0, x1)
OpenMaya.MScriptUtil.setDoubleArray(matrix[0], 1, x2)
OpenMaya.MScriptUtil.setDoubleArray(matrix[0], 2, x3)
OpenMaya.MScriptUtil.setDoubleArray(matrix[0], 3, 0)
OpenMaya.MScriptUtil.setDoubleArray(matrix[1], 0, y1)
OpenMaya.MScriptUtil.setDoubleArray(matrix[1], 1, y2)
OpenMaya.MScriptUtil.setDoubleArray(matrix[1], 2, y3)
OpenMaya.MScriptUtil.setDoubleArray(matrix[1], 3, 0)
tmMatrix = OpenMaya.MTransformationMatrix(matrix)
rotate = tmMatrix.eulerRotation().reorder(OpenMaya.MEulerRo tation.kXYZ)
RAD_to_DEG = (180/math.pi)
return [rotate[0]*RAD_to_DEG, rotate[1]*RAD_to_DEG, rotate[2]*RAD_to_DEG]
Then run this (just hold right enter or run it multiple times) and you will see that cube is rotate by pPlane1 normal, but it is not exactly on it, not parallel to the normal:
Code:
#a  angle which varies
a=a+0.1
test = getOrient([centerP.x,centerP.y,centerP.z], [tangent.x,tangent.y,tangent.z], [binormal.x,binormal.y,binormal.z], [normal.x,normal.y,normal.z], a)
cmds.rotate(test[0],test[1],test[2])
I hope my code will work for you, i tested on maya 2012 and it works.
I need possible to rotate object parallel to normal of "ground" by custom angle ("a" in my case). If you can help me it will be great!