On 18/03/2018 at 08:16, xxxxxxxx wrote:
I want to place an object B perpendicular on a polygon of object A.
So I have to use the normal of the selected polygon.
However, as soon as I move or rotate object A, object B is not positioned perpendicular.
Here the test code.
Polygon 0 of object A is used and object B is a Cylinder.
import c4d
from c4d import gui
#Welcome to the world of Python
# Calculate a polygons' face normal
def CalculateNormal(index, obj) :
poly = obj.GetPolygon(index)
pA = obj.GetPoint(poly.a)
pB = obj.GetPoint(poly.b)
pC = obj.GetPoint(poly.c)
pD = obj.GetPoint(poly.d)
normal = (pA - pC).Cross(pB - pD)
normal.Normalize()
# another way to calculate the normal: (pB-pC)%(pC-pA)
return normal
def CalculatePolyMid(index,obj) :
poly = obj.GetPolygon(index)
pA = obj.GetPoint(poly.a)
pB = obj.GetPoint(poly.b)
pC = obj.GetPoint(poly.c)
pD = obj.GetPoint(poly.d)
if (pC == pD) : return (pA+pB+pC)/3
return (pA+pB+pC+pD)/4
def CalcNormalPoint(index, op) :
normal = CalculateNormal(index, op)
polyMid = CalculatePolyMid(index,op)
return polyMid, normal
def main() :
cyl = doc.SearchObject("Cylinder")
point, normal = CalcNormalPoint(0, op)
normalPHB = c4d.utils.VectorToHPB(normal) #nodig!
cyl[c4d.ID_BASEOBJECT_REL_POSITION] = point
cyl[c4d.ID_BASEOBJECT_REL_ROTATION] = normalPHB
c4d.EventAdd()
if __name__=='__main__':
main()