String slices

Paul St George email at paulstgeorge.com
Fri Aug 9 16:28:16 EDT 2019


On 09/08/2019 16:29, Rhodri James wrote:
> On 09/08/2019 15:13, Paul St George wrote:
>> In the code (below) I want a new line like this:
>>
>> Plane rotation X: 0.0
>> Plane rotation Y: 0.0
>> Plane rotation Z: 0.0
>>
>> But not like this:
>>
>> Plane rotation X:
>> 0.0
>> Plane rotation Y:
>> 0.0
>> Plane rotation Z:
>> 0.0
>>
>> Is it possible?
>> (I am using Python 3.5 within Blender.)
>>
>> #
>> import os
>>
>> outstream = open(os.path.splitext(bpy.data.filepath)[0] + ".txt",'w')
>>
>> print(
>>
>> "Plane rotation X:",bpy.data.objects["Plane"].rotation_euler[0],
>>
>> "Plane rotation Y:",bpy.data.objects["Plane"].rotation_euler[1],
>>
>> "Plane rotation Z:",bpy.data.objects["Plane"].rotation_euler[2],
>>
>> file=outstream, sep="\n"
>>
>> )
>>
>> outstream.close()
>
> The 'sep="\n"' parameter to print() means "put a newline between each 
> item."  So don't do that.  Put the newlines you do want in explicitly, 
> or use separate calls to print():
>
> (I'm abbreviating because I really can't be bothered to type that much 
> :-)
>
>   print("X:", thing[0],
>         "\nY:", thing[1],
>         "\nZ:", thing[2],
>         file=outstream)
>
> or
>
>   print("X:", thing[0], file=outstream)
>   print("Y:", thing[1], file=outstream)
>   print("Z:", thing[2], file=outstream)
>
> I would probably use the latter, but it's just a matter of personal 
> preference.
>
> (Actually I would probably use outstream.write() and do my own 
> formatting, but let's not get side-tracked ;-)
>
>
>
>
So, I am going with your second suggestion (see below) but I would love 
to hear your outstream.write() side-track!


import os

with open(os.path.splitext(bpy.data.filepath)[0] + ".txt", "w") as outstream:

    plane = bpy.data.objects["Plane"]

    print("Plane rotation X:",plane.rotation_euler[0], file=outstream)

    print("Plane rotation Y:",plane.rotation_euler[1], file=outstream)

    print("Plane rotation Z:",plane.rotation_euler[2], file=outstream)

    print("Focal length:", bpy.context.object.data.lens, file=outstream)

    and so on...







More information about the Python-list mailing list