I am trying to write a python script using the info panel.

The first part of the code is unwrapping a mesh which is executing successfully.

The second part is to change the viewport shading to MATERIAL which is used for rendering. But when I try to run the script I get this error .

Attribute Error

Thirdly, I am trying to import a .png image. But its not being displayed in the UV Editor.

The code is shown below:

import bpy
   for obj in bpy.data.objects:
     if(obj.type == 'MESH'):
       bpy.context.scene.objects.active = obj
       bpy.ops.uv.unwrap(method='ANGLE_BASED', margin=0.001)
       bpy.context.space_data.context = 'MATERIAL'
       bpy.ops.image.open(filepath="//finger1.png", directory="C:\\Users\\KM\\Desktop\\blender\\Test 1\\", files=[{"name":"finger1.png", "name":"finger1.png"}], relative_path=True, show_multiview=False)

Anything regarding this would be helpful. Thanks in advance.

  • $\begingroup$ bpy.context.space_data depends on the active window (clicking on the materials button the the property editor). This line can be removed. $\endgroup$
    – ideasman42
    Commented Jun 24, 2015 at 21:11
  • $\begingroup$ @ideasman42 - So if i need to render it using the python script, I will have to keep the 3D view in Materials mode and then render it? Or is there any other way to do it? $\endgroup$ Commented Jun 24, 2015 at 21:46
  • $\begingroup$ To put it differently, the script would not give this exception if you ran it within the properties editor. However changing this button is not even important for the running of the script, so the line can be removed. $\endgroup$
    – ideasman42
    Commented Jun 24, 2015 at 22:34

2 Answers 2


I am a big advocate of avoiding bpy.ops as much as possible. I wrote an example at http://web.purplefrog.com/~thoth/blender/python-cookbook/load-image-texture.html which might help you understand how to use python to create a material using an image texture:

Here is the most important bit:

def material_for_texture(fname):
    img = bpy.data.images.load(fname)

    tex = bpy.data.textures.new(fname, 'IMAGE')
    tex.image = img

    mat = bpy.data.materials.new(fname)
    ts = mat.texture_slots[0]
    ts.texture = tex
    ts.texture_coords = 'ORCO'

    return mat

fname = "/var/tmp/blender/mohawk-seal0001.png"
obj = bpy.context.active_object
mat = material_for_texture(fname)
if len(obj.data.materials)<1:
    obj.data.materials[0] = mat

I had a similar problem as your second point. Solved it like this:

threeD_viewport = None

for area in bpy.context.screen.areas: 
    if area.type == 'VIEW_3D':
        for space in area.spaces: 
            if space.type == 'VIEW_3D':
                threeD_viewport = space

threeD_viewport.shading.type = 'MATERIAL'
  • $\begingroup$ Or a one-liner next(s for a in bpy.context.screen.areas if a.type=='VIEW_3D' for s in a.spaces if s.type=='VIEW_3D').shading.type = 'MATERIAL' $\endgroup$ Commented May 22, 2021 at 22:16

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .