Export Camera Coordinates
-
On 16/05/2013 at 08:04, xxxxxxxx wrote:
Hello,
I need to export the world coordinates of some Cameras along with their Targets and add an extra field for the camera Focal Length, this in a text file or simply by printing the data on screen, so that I can copy them.
Previously I was doing it manually, but now with a lot of cameras to export it's becoming a tedious work.
I've already search the internet, and read the Python documentation, but I couldn't find a way to get what I need, and also I get extra data that I don't need.
The coordinates should be in (Camera) X, Y, Z (Target) X, Y, Z and Focal Length.
Thanks in advance.
-
On 16/05/2013 at 08:37, xxxxxxxx wrote:
please post your existing code or describe your problems specifically (or hire someone if you
do not have any idea where to start). all your data can be found in the cameras bc. you could
write a script which searches the documents active selection for cameras with c4datom.checktype()
and then prints the desired BaseContainer data elements to the console.writing a string to a file is pretty simple in python. search the python docs for fileobject.
with open(myFilePath, 'w') as f:
f.write('My fancy Text.\n') -
On 16/05/2013 at 11:53, xxxxxxxx wrote:
I think you can put me in the category of "I don't even know where to start", but looking at the values, they are clearly visible in the UI, so I thought that was a simple task to get them to show ordered in some way.
I'll try to document myself better, for now I was testing this code:
import c4d #Welcome to the world of Python def main() : pos = op.GetMg() FOV = op[c4d.CAMERA_FOCUS] print pos print FOV if __name__=='__main__': main()
That give me what I need for the selected object only, but then if I try something like
import c4d #Welcome to the world of Python def main() : Camera = c4d.BaseObject(5103) pos = Camera.GetMg() print pos if __name__=='__main__': main()
The coordinates have all 0 values.
So I need to get this working with all the objects in the scene (I can copy only the cameras with target in a blank project to simplify the code), there is some documentation that explain this?
-
On 16/05/2013 at 13:23, xxxxxxxx wrote:
it would have taken longer to explain everything, so instead i wote a little script, doing
more or less what you want. added quite a lot of comments, hope it helps.import c4d from c4d import storage def main() : data = ['CameraData (Name : Position, FocusDistance, TaregetPosition)\n\n'] # empty list with just the header selection = doc.GetActiveObjects(c4d.GETACTIVEOBJECTFLAGS_CHILDREN) # selected objects in the document -> list path = c4d.storage.SaveDialog(type = c4d.FILESELECTTYPE_ANYTHING, force_suffix = 'txt') # ask for path for obj in selection: #loop selection if obj.CheckType(c4d.Ocamera) : # check for cam target = obj[c4d.CAMERAOBJECT_TARGETOBJECT] # get target if target : target = shortVec(target.GetMg().off) # if target is not none get pos # add string name, pos, focus, target.pos data.append('{0} : {1}, {2}, {3}\n'.format(obj.GetName(), shortVec(obj.GetMg().off), obj[c4d.CAMERAOBJECT_TARGETDISTANCE], target)) with open(path, 'w+') as f: # save to file for string in data: f.write(string) # little helper method for a short vector repr def shortVec(vector) : return '{0},{1},{2}'.format(vector.x,vector.y,vector.z) if __name__=='__main__': main()
edit : codepad version http://codepad.org/o37su6IU
-
On 16/05/2013 at 14:07, xxxxxxxx wrote:
Thanks a lot! Really, this will help me.
I've only to find why the exporter doesn't picks the Target NULL position
CameraData (Name : Position, FocusDistance, TaregetPosition) Camera : 814.368783498,171.015353366,-1053.32218525, 2000.0, None
The Target should be at 0,445.885,215.832, I've tried to put it as a child of the camera, and the camera as a child of it, but none of them seams to work, selecting multiple cameras do work.
Thanks again.
-
On 16/05/2013 at 14:19, xxxxxxxx wrote:
not sure what you do mean with target. a target expression ? the script reads the focus object
as target its called CAMERAOBJECT_TARGETOBJECT. to read a target expression tag target
object you have to get the basetag and read the linked object. -
On 16/05/2013 at 14:28, xxxxxxxx wrote:
Yeah, the Target Tag, I mean the actual NULL object position, but I couldn't get it to work:
Is it a different Cinema 4D version problem? I'm using R14.
-
On 16/05/2013 at 14:33, xxxxxxxx wrote:
as i tried to explain in my previous posting, the script does read the focus object of the camera
not the target expression. the focus object is called target in c4d.use something like that for reading target expressions instead :
-
On 16/05/2013 at 14:41, xxxxxxxx wrote:
Now I'm getting:
Traceback (most recent call last) : File "'scriptmanager'", line 33, in <module> File "'scriptmanager'", line 15, in main IndexError: string index out of range
I had already tried the TARGETEXPRESSIONTAG_LINK and didn't worked (by dragging the Tag on the console to get the actual name)
I could also solve this by simply create another camera, rename it Target (to difference it from the Cameras), parent it to the Target NULL, and use the Center to Parent command, then export and I get the data I need
-
On 16/05/2013 at 15:39, xxxxxxxx wrote:
its has to be tobj = tag[c4d.TARGETEXPRESSIONTAG_LINK] instead of tobj = target[c4d.TARGETEXPRESSIONTAG_LINK]
-
On 17/05/2013 at 06:16, xxxxxxxx wrote:
Thank you, it works smoothly now.
I've edited a little bit your script to get the right formatting and the FOV as an additional parameter
import c4d from c4d import storage def main() : data = [''] # empty list selection = doc.GetActiveObjects(c4d.GETACTIVEOBJECTFLAGS_CHILDREN) # selected objects in the document -> list path = c4d.storage.SaveDialog(type = c4d.FILESELECTTYPE_ANYTHING, force_suffix = 'txt') # ask for path for obj in selection: #loop selection if obj.CheckType(c4d.Ocamera) : # check for cam tag = obj.GetTag(c4d.Ttargetexpression) # get target expression target = 'None' if tag: tobj = tag[c4d.TARGETEXPRESSIONTAG_LINK] if tobj: target = shortVec(tobj.GetMg().off) # add string name, pos, focus, target.pos data.append('{1} {3} {4}\n'.format(obj.GetName(), shortVec(obj.GetMg().off), obj[c4d.CAMERAOBJECT_TARGETDISTANCE], target, obj[c4d.CAMERA_FOCUS])) with open(path, 'w+') as f: # save to file for string in data: f.write(string) # little helper method for a short vector repr def shortVec(vector) : return '{0} {1} {2}'.format(vector.x,vector.y,vector.z) if __name__=='__main__': main()