3
$\begingroup$

To search or to open text blocks in blend files in text editors (as a couple of examples) it would be handy to read texts from a blend file without any need of blender or bpy.

There is a module for blend file io shipped with blender addons_contrib/io_blend_utils/blend/blendfile.py

I am using blender 2.8. The tools have been added to addons_contrib (come with blender) as of July 4

Scripts at Repository

Re blender 2.79. I imagine that script to read text lines from datablocks using either would be similar and put me on the right track.

The walker script gives insights on how to read the blocks.

Test script below lists the textblock name, and how many characters are on each line. I'm sure there's a simple way to get the body string of the TextLine data block, but it escapes me.

from io_blend_utils.blend import blendfile

def get_id_name(block):
    name = block[b'id', b'name'].decode()
    return name[2:]

filepath = "/home/batfinger/Desktop/test.blend"

bf = blendfile.open_blend(filepath)
texts = bf.find_blocks_from_code(b'TX')
for t in texts:
    i = s = bf.blocks.index(t) + 1
    print(get_id_name(t))
    while bf.blocks[i].dna_type_name == 'TextLine':
        print(f"line {i - s} chars {bf.blocks[i][b'len']}")
        i += 1

How do I read the data to give me the text buffer of a text block?

CRUD EDIT.

Thankyou to @lemon re the Reading piece of the crud puzzle.

As an addendum, would very much appreciate any details on usage of the blendfile api for:

Creating, Updating and Deleting text blocks.

$\endgroup$
  • $\begingroup$ I've it in 2.79 but not in 2.8. On which version are you working? $\endgroup$ – lemon Jul 26 '19 at 9:11
  • $\begingroup$ 2.80 . developer.blender.org/… $\endgroup$ – batFINGER Jul 26 '19 at 10:56
  • $\begingroup$ About the edit, have you asked Campbell? github.com/scorpion81/blender-addons/blob/master/io_blend_utils/… $\endgroup$ – lemon Jul 26 '19 at 15:21
  • 1
    $\begingroup$ Only at last resort.. he's a busy fella afterall. On the other hand could ask @dr.Sybren lol. For where I'm at with it reading was the most important. Really liking 2.8. $\endgroup$ – batFINGER Jul 27 '19 at 15:30
3
+100
$\begingroup$

I did it in 2.79.

The bad is it seems there is no documentation for this module and we don't know where to go. And of course I don't know if it is the better way to do it.

But, once you have the TextLine block:

Get the length property.

Get the pointer to the block which corresponds to the line content, it is a block too.

Seek the file to the file offset of this last block.

Get the value using DNA_IO.

from io_blend_utils.blend import blendfile

def get_id_name(block):
    name = block[b'id', b'name'].decode()
    return name[2:]

filepath = "/home/batfinger/Desktop/test.blend"

bf = blendfile.open_blend(filepath)
texts = bf.find_blocks_from_code(b'TX')
for t in texts:
    i = s = bf.blocks.index(t) + 1
    print(get_id_name(t))
    while bf.blocks[i].dna_type_name == 'TextLine':

        # Get the block
        block = bf.blocks[i]
        # Get the length
        length = block[b'len']
        # Get the line data pointer (a block)
        line = block.get_pointer( b'line')

        # Search for the line block position
        bf.handle.seek( line.file_offset, 0 )

        # Get the value
        line_content = blendfile.DNA_IO.read_string( bf.handle, length )

        print( i - s )
        print( bf.blocks[i][b'len'] )
        print( line_content )


        #print( bf.blocks[i].user_data )
        #print(f"line {i - s} chars {bf.blocks[i][b'len']}")
        i += 1

bf.close()
$\endgroup$
  • 1
    $\begingroup$ Thankyou ... blendfile.DNA_IO $\endgroup$ – batFINGER Jul 26 '19 at 12:24
  • $\begingroup$ @batFINGER, the worst was get_pointer( b'line' )... if not explicit error msg on get_pointer( b'xxx' ) never been able to find it (expected b'body'). $\endgroup$ – lemon Jul 26 '19 at 12:30
  • $\begingroup$ It achieves exactly what I need. ... a vital cog in an nvim addon / plugin. Would it be OTT to edit question re creating and writing, rather than asking as separate question? $\endgroup$ – batFINGER Jul 26 '19 at 14:14
  • $\begingroup$ @batFINGER, you're welcome to do exactly what you want. It is your question! $\endgroup$ – lemon Jul 26 '19 at 14:21

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.