-1
$\begingroup$

Im trying to export X & Y coordinates of certain object to file with this script I found years ago somewhere:

import bpy  


def write_some_data(context, filepath, use_some_setting):
    print("running write_some_data...")
    camera = context.active_object
    mw = camera.matrix_world
    scene = context.scene
    frame = scene.frame_start
 
    f = open(filepath, 'w', encoding='utf-8')
    while frame <= scene.frame_end:
        scene.frame_set(frame)
        x, y, z = mw.to_translation()
        rx, ry, rz = mw.to_euler('XYZ')
        f.write("%d" % frame)
        f.write(", ")   
        f.write("%5.3f, %5.3f" % (x, y))
        f.write("\n")
        frame += 1 
    f.close()  

    return {'FINISHED'}

It almost works, but output looks like this:

0, 0.510, -0.370
1, 0.518, -0.190
2, 0.524, -0.010
3, 0.536, 0.177
4, 0.544, 0.365
5, 0.553, 0.548
6, 0.559, 0.730
7, 0.568, 0.912
8, 0.575, 1.094
9, 0.575, 1.283
10, 0.574, 1.471

My purpose is to save X & Y positions inside area of 512,512px (rendered 2D size) and output should have coordinates between 0 - 512px. I dont know python or scripting well, been looking for answer for couple of days now.

$\endgroup$
1
  • $\begingroup$ Don't edit in "SOLVED" into your question, instead "accept" the answer by clicking the button near the up arrow for voting up (you may vote up the answer as well). $\endgroup$ Oct 4 at 10:35

1 Answer 1

1
$\begingroup$

You can get the location coordinates of an object called 'Cube' with the following code:

import bpy

scene = bpy.context.scene
cube = bpy.data.objects['Cube']

f = open(filepath, 'w', encoding='utf-8')

for frame in range(1,scene.frame_end):
    scene.frame_set(frame)
    x, y, z = cube.location
    
    f.write("%d" % frame)
    f.write(", ")   
    f.write("%5.3f, %5.3f" % (x, y))
    f.write("\n")

    print(frame, x, y, z)

f.close()
$\endgroup$
5
  • $\begingroup$ Thanks for reply. I get error "filepath is not defined" from that script. $\endgroup$ Oct 2 at 13:24
  • $\begingroup$ You need to define a filepath elsewhere in your script. This you can for example do by, on the line before f = etc., write filepath = "C:/tmp/output.txt" $\endgroup$
    – MSDMMM
    Oct 2 at 13:45
  • $\begingroup$ Almost works, but output is "1, 0.000, 0.000" all lines. Im using object called "Tracker1" that is "empty cube" and it has Object Constraint: Child of vertex im tracking. $\endgroup$ Oct 2 at 15:38
  • $\begingroup$ Got it working! Had to "object -> bake action -> visual keying:on" I still must calculate the real x and y values from meters but the aspect ratio seems right. $\endgroup$ Oct 4 at 9:15
  • $\begingroup$ Would be better to use a context manager to write to a file. $\endgroup$ Oct 4 at 11:22

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .