1
$\begingroup$

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

$\endgroup$
3
$\begingroup$

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...).

$\endgroup$
1
  • $\begingroup$ This was exactly the problem. Additionally, you gave me that most helpful hint with polygon.material_index. My script does work now. Thank You. $\endgroup$
    – Dimali
    Apr 5 '17 at 14:46

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.