Python call of import image as plane I am building a simple python script that creates a plane with an image (image as plane addon). I call blender by the command line :

blender -P pattern_blender_smartphone.py


Here is the content of pattern_blender_smartphone.py :

import os
import bpy

def initScene():
    file = os.path.abspath('C:/Users/John/PycharmProjects/untitled/cowboy_bebop.jpg')
    bpy.ops.import_image.to_plane(
        use_shadeless=True,
        files=[{'name': os.path.basename(file)}],
        directory=os.path.dirname(file)
    )

initScene()


When i execute blender by the command line with this script i get these errors :

========================
   SCRIPT STARTING
========================
Error: Traceback (most recent call last):
  File "C:\Program Files\Blender Foundation\Blender\2.75\scripts\addons\io_import_images_as_planes.py", line 320, in execute
    self.import_images(context)
  File "C:\Program Files\Blender Foundation\Blender\2.75\scripts\addons\io_import_images_as_planes.py", line 333, in import_images
    self.set_image_options(img)
  File "C:\Program Files\Blender Foundation\Blender\2.75\scripts\addons\io_import_images_as_planes.py", line 449, in set_image_options
    image.use_alpha = self.use_transparency
AttributeError: 'NoneType' object has no attribute 'use_alpha'

location: C:\Program Files\Blender Foundation\Blender\2.75\scripts\modules\bpy\ops.py:189


Traceback (most recent call last):
  File "&lt;string&gt;", line 1, in &lt;module&gt;
  File "&lt;string&gt;", line 43, in &lt;module&gt;
  File "&lt;string&gt;", line 11, in initScene
  File "C:\Program Files\Blender Foundation\Blender\2.75\scripts\modules\bpy\ops.py", line 189, in __call__
    ret = op_call(self.idname_py(), None, kw)
RuntimeError: Error: Traceback (most recent call last):
  File "C:\Program Files\Blender Foundation\Blender\2.75\scripts\addons\io_import_images_as_planes.py", line 320, in execute
    self.import_images(context)
  File "C:\Program Files\Blender Foundation\Blender\2.75\scripts\addons\io_import_images_as_planes.py", line 333, in import_images
    self.set_image_options(img)
  File "C:\Program Files\Blender Foundation\Blender\2.75\scripts\addons\io_import_images_as_planes.py", line 449, in set_image_options
    image.use_alpha = self.use_transparency
AttributeError: 'NoneType' object has no attribute 'use_alpha'

location: C:\Program Files\Blender Foundation\Blender\2.75\scripts\modules\bpy\ops.py:189


Traceback (most recent call last):
  File "C:\Program Files\Blender Foundation\Blender\2.75\scripts\addons\io_import_images_as_planes.py", line 320, in execute
    self.import_images(context)
  File "C:\Program Files\Blender Foundation\Blender\2.75\scripts\addons\io_import_images_as_planes.py", line 333, in import_images
    self.set_image_options(img)
  File "C:\Program Files\Blender Foundation\Blender\2.75\scripts\addons\io_import_images_as_planes.py", line 449, in set_image_options
    image.use_alpha = self.use_transparency
AttributeError: 'NoneType' object has no attribute 'use_alpha'

location: C:\Program Files\Blender Foundation\Blender\2.75\scripts\modules\bpy\ops.py:189

location: C:\Program Files\Blender Foundation\Blender\2.75\scripts\modules\bpy\ops.py:189
Read new prefs: C:\Users\John\AppData\Roaming\Blender Foundation\Blender\2.75\config\userpref.blend
found bundled python: C:\Program Files\Blender Foundation\Blender\2.75\python
Saved session recovery to 'C:\Users\John\AppData\Local\Temp\quit.blend'

Blender quit
AL lib: (EE) UpdateDeviceParams: Failed to set 44100hz, got 48000hz instead


I have already tried to execute a blender instance and run the script with the GUI and it works but when i launch it by the command line i get errors.
Have you guys any ideas why it doesn't work?
The python docs for os.abspath() says


  os.path.abspath(path) 
   Return a normalized absolutized version of the pathname path. On most platforms, this is equivalent to calling the function normpath() as follows: normpath(join(os.getcwd(), path)).


that is it is adding the current directory (from os.getcwd()) onto the start of the absolute filepath that you have entered. This causes the load image to fail which then gives an error when attempting to set the use_alpha property.

The simple solution is to remove the os.abspath() call.