Jump to content

Sign in to follow this  
Chani

Export cam pos/orientations to text file

Recommended Posts

Hi, I am hoping someone on here can help or point me in the right direction. I am not a programmer. I have searched extensively but cannot find anything that helps.

 

I am looking for a way to export multiple (static) camera positions & orientations to a single text file that includes the name of each camera then the position followed by the orientation.

 

I will be taking this info into a spreadsheet for importing into a photogrammetry system for camera matching, hence why it needs to be a text (or CSV or similar).

 

Any ideas folks?

 

I currently have C4D R18/19 Studio version.

Share this post


Link to post
Share on other sites

I can make a script, but you should show me an example of the output that you are looking for. What kind of formatting do you want (degrees, radians, matrices , x, y , z coords ?)

Share this post


Link to post
Share on other sites
  • Topic Author
  • 11 hours ago, kalugin said:

    I can make a script, but you should show me an example of the output that you are looking for. What kind of formatting do you want (degrees, radians, matrices , x, y , z coords ?)

     

     

    The system I need to match has the cameras taken in by photogrammetry software. The spreadsheet columns I need to format it into are as follows:

     

    n5 is obviously the camera name with x7, y8, z9 being the absolute camera position coordinates and then x10 y11 z12 are the xyz rotation axis coordinates.

     

    n5                                                   x7                              y8                          z9                         x10                           y11                        z12

    IU1L - 001_b                           288.0453281         614.0887056       35.85327133        90.27735873         0.366070578       116.0101527

     

    See the image below for the camera selected in C4D. I need to be able to export every camera into one text file (if that is possible?)

     

    image.thumb.png.5adc38be004f4bbc1984a387bff52f73.png

     

     

    Hope this makes it more clear what I am after?

     

    Many thanks for taking the time to look into this for me. 

     

    Share this post


    Link to post
    Share on other sites

    will CSV like this work ?
    IU1L - 001_b, 288.0453281, 614.0887056, 35.85327133, 90.27735873, 0.366070578, 116.0101527

    IU1L - 001_b, 288.0453281, 614.0887056, 35.85327133, 90.27735873, 0.366070578, 116.0101527

    IU1L - 001_b, 288.0453281, 614.0887056, 35.85327133, 90.27735873, 0.366070578, 116.0101527

    (with different values, obviously :)

     

    Share this post


    Link to post
    Share on other sites

    the script is very simple, you should be able to change the formatting to your own liking

    Share this post


    Link to post
    Share on other sites
  • Topic Author
  • 4 hours ago, kalugin said:

    Wow, that is (almost) exactly what I am after! Amazing.

     

    The only thing is the rotation does not seem to export correctly.

    My cam has a value of

    R X: 90.277       R Y: 0.366      R Z: -116.01

    image.thumb.png.5fd5dfae22fbc369f42eef8b2d51d3f8.png

     

    Your script returns 

    90.3086     1-26.0081     270.5014

    image.thumb.png.720246c92d84309ee504b40a1539a217.png

     

    Not sure how to troubleshoot this to make it work but I will look it over.

     

    EDIT: Looking at the code and result, it appears to be saving the HPB order rather than XYZ.

     

    I managed to get it to work (I think) by adding some code to the rotation_rad

                rotation_rad = c4d.utils.MatrixToHPB(obj_matrix,c4d.ROTATIONORDER_XYZGLOBAL)

     

    Massive thanks for even bothering to take a look!

     

     

    import c4d
    #Welcome to the world of Python
    
    
    def Walker(obj):
        if not obj: return
    
        elif obj.GetDown():
            return obj.GetDown()
        while obj.GetUp() and not obj.GetNext():
            obj = obj.GetUp()
        return obj.GetNext()
    
    
    def main():
        file = c4d.storage.SaveDialog(c4d.FILESELECTTYPE_ANYTHING, title='Save csv file as', force_suffix='csv')
        csv_file = open(file, 'w')
        obj = doc.GetFirstObject()
        while obj:
            if obj.GetType() == 5103:
                name = obj.GetName()
                obj_matrix = obj.GetMg()
                position = obj_matrix.off
                rotation_rad = c4d.utils.MatrixToHPB(obj_matrix,c4d.ROTATIONORDER_XYZGLOBAL)
                rotation_deg = c4d.Vector(c4d.utils.Deg(rotation_rad.x), c4d.utils.Deg(rotation_rad.y), c4d.utils.Deg(rotation_rad.z))
                line = '%s, %s, %s, %s, %s, %s, %s'%(name,
                                                     position.x,
                                                     position.y,
                                                     position.z,
                                                     rotation_deg.x,
                                                     rotation_deg.y,
                                                     rotation_deg.z)
                csv_file.write(line + '\n')
            obj = Walker(obj)
        csv_file.close()
    
    
    if __name__=='__main__':
        main()

     

    Share this post


    Link to post
    Share on other sites

    Oops, my bad :)

    H = Y

    P = X

    B = Z

     

    camera to csv.py

    Share this post


    Link to post
    Share on other sites

    yeah, that should work. I only switched the x and the y. Maybe it won't give you correct values. Try it.

    Share this post


    Link to post
    Share on other sites
  • Topic Author
  • 14 hours ago, kalugin said:

    yeah, that should work. I only switched the x and the y. Maybe it won't give you correct values. Try it.

     

    Thanks for looking at this again but switching the values did not give the correct results. 

     

    The only way that appears to work correctly is changing to rotation order to be XYZ at the matrix level by adding the little extra code at the end of: rotation_rad

     

    rotation_rad = c4d.utils.MatrixToHPB(obj_matrix,c4d.ROTATIONORDER_XYZGLOBAL)

     

    It appears to be working perfectly now though so again, many many thanks for putting this together for me. It will be a massive help.

     

     

    Share this post


    Link to post
    Share on other sites

    Hello,  I've been trying to use this script to output the XYZ position, rotation and scale of an animated object but cant make it what i want to do.

    i have an object following a spline path and id like to output on each frame, its positional data to a CSV file with each position being a new line in the csv file.

    I'm slowly trying to teach myself python inside C4D but could do with some pointers. 

     

    Thankyou!

     

    Andy

     

    image.png

    Share this post


    Link to post
    Share on other sites
    On 10/23/2019 at 5:42 PM, Andrew Coates said:

    Hello,  I've been trying to use this script to output the XYZ position, rotation and scale of an animated object but cant make it what i want to do.

    i have an object following a spline path and id like to output on each frame, its positional data to a CSV file with each position being a new line in the csv file.

    I'm slowly trying to teach myself python inside C4D but could do with some pointers. 

     

    Thankyou!

     

    Andy

     

    image.png

     

    If you want to write the behavior of an object over time, then you need to loop over that range and set the proper time before you write out the values.

    Here I have changed the script above to set the time. Note that this is very primitive, it uses a hardcoded object name "toFile" to find the proper object, and a hardcoded frame range of 0 to 20. You may want to build a dialog around this.

    import c4d
    #Welcome to the world of Python
    
    
    def Walker(obj):
        if not obj: return
    
        elif obj.GetDown():
            return obj.GetDown()
        while obj.GetUp() and not obj.GetNext():
            obj = obj.GetUp()
        return obj.GetNext()
    
    
    def main():
        obj = doc.SearchObject("toFile")
        if obj == None : return
        name = obj.GetName()
    
        startFrame = 0
        endFrame = 20
    
    
        file = c4d.storage.SaveDialog(c4d.FILESELECTTYPE_ANYTHING, title='Save csv file as', force_suffix='csv')
        csv_file = open(file, 'w')
        
        for frame in range(startFrame, endFrame+1) :
            btNum = frame
            btDen = doc.GetFps()
            bt = c4d.BaseTime(btNum, btDen)
            doc.SetTime(bt)
            doc.ExecutePasses(None, True, True, True, c4d.BUILDFLAGS_0)
    
            obj_matrix = obj.GetMg()
            position = obj_matrix.off
            rotation_rad = c4d.utils.MatrixToHPB(obj_matrix,c4d.ROTATIONORDER_XYZGLOBAL)
            rotation_deg = c4d.Vector(c4d.utils.Deg(rotation_rad.x), c4d.utils.Deg(rotation_rad.y), c4d.utils.Deg(rotation_rad.z))
            line = '%s, %s, %s, %s, %s, %s, %s'%(name,
                                                 position.x,
                                                 position.y,
                                                 position.z,
                                                 rotation_deg.y,
                                                 rotation_deg.x,
                                                 rotation_deg.z)
            csv_file.write(line + '\n')
            
        csv_file.close()
    
    
    if __name__=='__main__':
        main()

    I have only briefly tested this with a positional animation, but not with a spline path.

     

    If you are interested in Python programming, also have a look at my current tutorial series Python: Spoonfed that can be found here:

    https://www.patreon.com/cairyn

    (Although you may know more Python than the tutorial currently uses 😄 )

     

    Share this post


    Link to post
    Share on other sites

    Join the conversation

    You can post now and register later. If you have an account, sign in now to post with your account.

    Guest
    Reply to this topic...

    ×   Pasted as rich text.   Paste as plain text instead

      Only 75 emoji are allowed.

    ×   Your link has been automatically embedded.   Display as a link instead

    ×   Your previous content has been restored.   Clear editor

    ×   You cannot paste images directly. Upload or insert images from URL.

    Sign in to follow this  

    • Recently Browsing   0 members

      No registered users viewing this page.

    ×
    ×
    • Create New...