Bpy.ops.text.new(): How to specify data-block name (or learn Blender-generated name)?

In my script I want to create new text data-block using bpy.ops.text.new, but I don’t see any way to specify the name of the data-block it creates. I also don’t see a way to get the name of the newly created data-block (e.g. Text, Text.001).

I can of course write a quick loop to find the data-block named Text | Text.NNN where NNN is the highest three-digit natural number. But this seems clumsy. Am I missing something?

you can specify the name using the data api , you can also query the name after creating it

>>> a = bpy.data.texts.new("test")
>>> a.name
'test'

>>> a = bpy.data.texts.new("test")
>>> a.name
'test.001'
1 Like

I see, thanks. TIL, bpy.ops = as if the user did something in the gui, bpy.data = just doing it directly. Thanks!

2 Likes