# Rotation of a camera around an object

On 15/01/2013 at 04:35, xxxxxxxx wrote:

My camera is in a specified distance to my object which I want to render. I want to rotate the camera around this object in a steady distance. For this purpose I use matrix calculations to calculate the position and orientation of the cam. I use the functions of C4D to get the rotation and translation matrices and multiply them (together with the inverse of the translation matrix) to the full transformation matrix. By the way: Why is there no such method in C4D? Why do I have to do this quite useful task by myself?
The transformation matrix is correct which was checked several times. The problem lies within the values which I get from the cam on which I use my transformation matrix or in the usage of the matrix itself.
For better understanding: The camera starts in offset (0,0,0) with v1: (1, 0, 0); v2: (0, 1, 0); v3: (0, 0, 1). The first rotation is always successful because the global coordinate system and the object coordinate system of the camera are congruent. But if the camera starts the rotation with v1: (0.707, 0, 0.707); v2: (0.5, 0.707, -0.5); v3: (-0.5, 0.707, 0.5) [Rotation Heading und Pitch each 45°] the rotation no longer works. I assume that this happens because rotation uses the object coordinate system but the translation matrix is calculated for global coordinate system. GetAbsRot(), GetRelRot(), GetMg(), GetMl() and other all deliever the same data, whereby a change of the coordinate system is impossible.
So the question is: Whats the python code to get such a simple transformation to work? Since there is no documentation about the interna of the matrices the only thing left to do for me is to guess.

``````import c4d
from c4d import documents
import time
import Global_lokal

SINGLE = False
ROTANGLE_X = 15
ROTANGLE_Y = 15
ROTANGLESINGLE_X = 45
ROTANGLESINGLE_Y = 45
SLEEPTIME = 0.2

def RotAroundPoint(op = c4d.BaseObject, rotPoint = c4d.Vector, H = float(), P = float(), B = float()) :
rotMat = c4d.utils.HPBToMatrix(hpb)
print 'RotMat: {0}'.format(rotMat)

moveVec = rotPoint - op.GetMg().off
print 'moveVec: {0}'.format(moveVec)
moveMat = c4d.utils.MatrixMove(moveVec)
print 'moveMat: {0}'.format(moveMat)

mulRotMoveMat = rotMat
mulRotMoveMat = mulRotMoveMat.__rmul__(moveMat)
print 'mulRotMoveMat: {0}'.format(mulRotMoveMat)

complRotMoveMat = moveMat.__invert__()
print 'MoveMatInv: {0}'.format(complRotMoveMat)
complRotMoveMat = complRotMoveMat.__rmul__(mulRotMoveMat)
print 'complRotMoveMat: {0}'.format(complRotMoveMat)

rotMat = op.GetMg() * complRotMoveMat
rotMat.off = op.GetMg().off + complRotMoveMat.off

op.SetMg(rotMat)

distVec = rotPoint - op.GetMg().off
print 'Distance: {0}'.format(distVec.GetLength())

def main() :
doc = documents.GetActiveDocument()
cube = c4d.BaseObject(c4d.Opyramid)
cubeMg = cube.GetMg()
cubeMgOff = cubeMg.off
cubeMgOff.z = 1000
cubeMg.off = cubeMgOff
cube.SetMg(cubeMg)
cam = c4d.CameraObject()
cam[c4d.CAMERAOBJECT_TARGETDISTANCE] = cubeMgOff.z
stage = c4d.BaseObject(c4d.Ostage)
doc.InsertObject(cube)
doc.InsertObject(cam)
doc.InsertObject(stage)

if not SINGLE:
print 'CamMl before Rotation: {0}'.format(cam.GetMl())
for i in range(24) :
print '-------------------------------------------------------------------------'
RotAroundPoint(cam, cubeMgOff, ROTANGLE_X, ROTANGLE_Y, 0)
print 'CamMlrot: H:{0}, P: {1}, B:{2}'.format(cam[c4d.ID_BASEOBJECT_REL_ROTATION,c4d.VECTOR_X],
cam[c4d.ID_BASEOBJECT_REL_ROTATION,c4d.VECTOR_Y],
cam[c4d.ID_BASEOBJECT_REL_ROTATION,c4d.VECTOR_Z])
if c4d.CheckIsRunning(c4d.CHECKISRUNNING_EXTERNALRENDERING) :
while c4d.CheckIsRunning(c4d.CHECKISRUNNING_EXTERNALRENDERING) :
time.sleep(SLEEPTIME)
c4d.CallCommand(12099)
else:
RotAroundPoint(cam, cubeMgOff, ROTANGLESINGLE_X, ROTANGLESINGLE_Y, 0)
print 'CamMg: {0}'.format(cam.GetMg())

if __name__ == '__main__':
main()
``````

I hope I made myself understandable enough. Thank you!