Getting Camera Loc in python

I have been busy working on the RenderB*tch export code.

SO far, things are progressing rapidly, I can handle mutiple objects, and
correctly handle the single vertex list.

One thing I am stuck on is getting the LocX, LocZ, and LocY of the main camera.

I tried the following code:


	#Get Camera Position
	Cams = Blender.Camera.Get()
	OorCam = Cams[0]
	Pos = OorCam.getPosition()
	Xloc = round(OorCam.LocX,3)
	Yloc = round(OorCam.LocY,3)
	Zloc = round(OorCam.LocZ,3)

but Blender doesn’t seem to like this, as it spits the following error
message out:


Traceback (most recent call last):
  File "xport03.py", line 54, in EventGUI
  File "xport03.py", line 163, in Xport
SystemError: error return without exception set

Line, 163 being

Xloc = round(OorCam.LocX,3)

Any suggestions? I would have thought this would have worked…

But it doesn’t.

help!

thanks in advance

brian

Looks like you need surround something with a try-catch clause there…

WARNING: My python skills are newbie at best so I might be uttering complete nonsense - but a matrix might solve your problems.

Not sure if this is the right way to do it but:


Cams = Blender.Object.Get("Camera")
CM = Cams.matrix

XLoc = CM[3][0]
YLoc = CM[3][1]
ZLoc = CM[3][2]

It’s probably a screw up but it might inspire a solution :wink:

Regards
Fabrizio

Fabrizio: actually, the matrix is always the good solution, since it updates itself even if the object is linked to an armature, which doesn’t always hapen when using .Loc*

Becareful though printing the matrix might crash Blender!

also, it is not a list of list of float, like one might expect. To convert it to a list of list, just use the folowing function:


def convert_float(mtx):
      final = []
      row = []
      for oRow in mtx:
            row = [x for x in oRow]
            final.append(row)
      return final

Martin

Theeth: Well, thanks to you for pointing me to Matrices a few weeks ago :slight_smile: though as you can see I haven’t had time to learn much about them.

Hasn’t anybody yet figured out how to make days a ‘few’ hours longer yet?! :stuck_out_tongue:

Rgds
Fabrizio

Cheers Fabrizio. Copied that in to the code, and it works!

thanks.

brian

Cheers Fabrizio. Copied that in to the code, and it works!

thanks.

brian

I’m the king of the road, baby :smiley: !!!

Joking aside, make sure you double x100 check it. I’d take Martin’s advice over mine anytime when it cames to python and he was probably anticipating something my still-developing-python-mind cannot yet fathom. It might prevent some nasty bug in your script turning out later on!

Oh, and good luck with the script! :slight_smile:

There doesn’t seem to be any error in that bit of code! It is certainly exporting the camera location OK!

The main problem I’m having is that the file format for RenderBitch isn’t documented, so I’m having to look at the demo scene files, and look through the source code! :-?

Oh, and the RenderBitch website seems to be down as the author has exceeded his monthly bandwidth limit :x

brian

You don’t really have to pay attention to this, since everything works now as you say, but just to inform:
Blender.Camera.Get() gets you the camera data only, that is, things like Lens, clipping values and so on. To get the location you want the data from the object, which is done with Blender.Object.Get(cameraname).
So it would be something like:


cam = Blender.Object.Get(cameraname)
locX, locY, locZ = cam.LocX, cam.LocY, cam.LocZ

or


cam = Blender.Object.Get(cameraname)
loc = cam.loc

loc will be a 3-float tuple with the location values.

But the matrix method of course works just as well…

Where did that getPosition() come from? There is a ob.getLocation() function in Publisher though…