Search code examples
pythonlistblenderbpy

List to Notepad in Blender


import bpy

f = open("C:/Users/xxx/Desktop/CODING.txt", 'w')

for i in bpy.context.selected_objects:
    
    result = print(i.name.split(".")[1])
    result = str(result)
    f.write(result)

f.close()

I selected 5 objects in Blender and ran the script.

The names of the objects are cube.001 ~ cube.005

What I want is to write 001 to 005 in notepad.

However, "None" was written on the notepad (5 times)

Thank you.


Solution

  • print returns None. Just use result = i.name.split(".")[1] instead. And no need to convert it to string afterward. So this would suffice:

    import bpy
    
    with open("C:/Users/xxx/Desktop/CODING.txt", 'w') as f:
        for i in bpy.context.selected_objects:
            f.write(i.name.split(".")[1])
    

    Note that if you use context manager for opening a file (with statement) you don't need to close it manualy.