Access Violation Error I have a small Blender 2.78c file with one (active) object, one material which is assigned to the object. It uses a UVMap with one images. 

Blender crashes at the last line of the script below and the crash report is nothing but 

Error: EXCEPTION_ACCESS_VIOLATION


I have reduced my script so that the error and crash still appear, but the code does not make sense anymore. Any hints on why this happens would be highly appreciated.

object = bpy.context.scene.objects.active
uv = object.data.uv_textures[0].data  
for slot in object.material_slots:
    bpy.ops.object.mode_set(mode = 'EDIT')
    bpy.ops.object.material_slot_assign()
    bpy.ops.object.mode_set(mode = 'OBJECT')

uv[1].image


Please find the file under:
https://www.dropbox.com/s/poirg3qf70a5c7y/Testfile.blend?dl=0
I think that there is something around this reason:


You take an internal pointer "uv = object.data.uv_textures[0].data"
This pointer is modified/reallocated during "bpy.ops.object.material_slot_assign()"
So "uv[x]" is no longer available


You can instead do something like that:

import bpy

object = bpy.context.scene.objects.active

uv = object.data.uv_textures[0].data  

bpy.ops.object.mode_set(mode = 'OBJECT')

for i, slot in enumerate( object.material_slots ):
    for p in [p for p in object.data.polygons if p.select]:
        p.material_index = i
    #bpy.ops.object.mode_set(mode = 'EDIT')
    #bpy.ops.object.material_slot_assign()
    #bpy.ops.object.mode_set(mode = 'OBJECT')

print( uv[1].image )


Or, of course, access to an available pointer again:

import bpy


object = bpy.context.scene.objects.active

uv = object.data.uv_textures[0].data  

bpy.ops.object.mode_set(mode = 'OBJECT')

for i, slot in enumerate( object.material_slots ):
    bpy.ops.object.mode_set(mode = 'EDIT')
    bpy.ops.object.material_slot_assign()
    bpy.ops.object.mode_set(mode = 'OBJECT')

uv = object.data.uv_textures[0].data  
print( uv[1].image )


... but I don't get the meaning of this loop, as it will assign to the last material at the end (probably because this is an extract of the whole code...).