1
$\begingroup$

I've written a pair of functions for exporting the currently selected indicies to a file, then reimporting them.

There's no particular reason for this other than to get more comfortable with writing scripts for Blender.

Here's what I have:

import bmesh
import bpy
import json

def exportSelectedIndicies(filename):
    mesh = bmesh.from_edit_mesh(bpy.context.active_object.data)
    selectedIndicies = [i.index for i in mesh.verts if i.select]
    with open(filename, 'w') as f:
        json.dump(selectedIndicies, f)

def importSelectedIndicies(filename):
    mesh = bmesh.from_edit_mesh(bpy.context.active_object.data)
    with open(filename) as f:
        selectedIndicies = json.load(f)

    for i in mesh.verts:
        i.select = i.index in selectedIndicies

    mesh.select_flush_mode()

The export portion does exactly what I want. I'm happy with it.

The import portion doesn't seem to do anything when you run it at first. It's not until you click away from the console and into the 3D View that it suddenly updates your selection.

It seems to me that the import portion is just missing a little part to tell the 3D View that it needs to redraw itself. I searched around online and found some stuff saying that there's a Blender.Redraw method that can be used, but there's no such module Blender or Redraw that I can import. I suspect that either those functions aren't exposed to the Python API, or they're from an older version of Blender and have since been deprecated.

I see that I can access the SpaceView3D object, but it doesn't seem to have any redraw method that I can use.

So what am I missing here? Is there some kind of flush, mark_dirty, draw, refresh, or redraw method that needs to be called for it to trigger that it needs to be redrawn (and on what object, and how would I get at that object?)

Is this a bug? I am using version 2.74... do newer versions have the bug fixed? On that note, do newer versions add in a function that I need (I just haven't updated because I don't use Blender much, and because my network is slow so updating takes several hours. But I'll go ahead and update if that'll fix my problems.)

If it is a bug or something that's been overlooked... is there a workaround I can use? Maybe something to emulate clicking on the 3D View, then clicking back on the Console (so I can enter more commands afterwards...)?

$\endgroup$
  • 2
    $\begingroup$ At a first glance you can try: bmesh.update_edit_mesh(mesh) and scene.update(), related: blender.stackexchange.com/questions/414/… $\endgroup$ – p2or Jun 25 '16 at 17:25
  • $\begingroup$ @poor - Actually, I was using bmesh.update_edit_mesh(mesh) incorrectly! I just realized bmesh is supposed to be the module itself, not an instance of the BMesh class. So just adding this line worked for me: bmesh.update_edit_mesh(bpy.context.active_object.data). Add an answer saying that and I'll mark it as correct. Thanks! $\endgroup$ – ArtOfWarfare Jun 25 '16 at 17:49
  • 1
    $\begingroup$ @poor - I had actually seen that question/answer before asking my own. Because I had used bmesh.update_edit_mesh(mesh) with the incorrect arguments though, it didn't work for me. So I've proposed an edit to the answer with some notes to hopefully clarify the arguments that should be used with that function. Also, since my question was about changes in selection, not geometry, I thought maybe mine would have an unrelated solution. $\endgroup$ – ArtOfWarfare Jun 25 '16 at 18:43