0
$\begingroup$

I keep getting an error from this scrip and am unsure where I am making a mistake.

import bpy
import os

for ob in bpy.data.objects:
    file_object = open('blender_coordinate.txt', 'w') as f:
       f.write(ob.name)
        f.write(', ')
        f.write(ob.location)
        f.write(', ')
        f.write(ob.rotation)
        f.write('\n')
    file_object = close()
    return {'Finished'} 

Here is the error i am recieving

  File "Text.py", line 5
    file_object = open('blender_coordinate.txt', 'w') as f:
                                                                                                                                                           ^
SyntaxError: invalid syntax

location: <unknown location>:-1

Python script fail, look in the console for now...

I am still new to scripting and appreciate any help.

$\endgroup$
2
$\begingroup$

It seems that you mixed two methods. You can either use :

file_object = open('myfilename','w')
file_object.write(ob.name)
file_object.close()

or

with open('myfilename','w') as file_object:
    file_object.write(ob.name)
    #no need to close

The second option is better (see https://www.pythonforbeginners.com/files/with-statement-in-python).

Both options only require you to import bpy

$\endgroup$
  • $\begingroup$ Consider accepting the answer @Nburgess $\endgroup$ – brockmann Mar 17 at 18:57
  • $\begingroup$ I dont know how to do that. $\endgroup$ – Nburgess Mar 18 at 13:26

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.