Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Import image as plane in blender script

Tags:

python

blender

Import images as planes work great in blender GUI, but when I try to use it in python module, I've got this error:

RuntimeError: Operator bpy.ops.mesh.primitive_plane_add.poll() Missing 'window' in context

My code is:

import bpy
import addon_utils

# enable plugins
addon_utils.enable("io_import_images_as_planes")

# remove Cube object
bpy.data.objects['Cube'].select = True
bpy.ops.object.delete()

file = "test.jpg"
bpy.ops.import_image.to_plane(files=[{'name':file}], directory='.')
like image 289
Tabzou Avatar asked Nov 10 '22 12:11

Tabzou


1 Answers

The import images as planes operator only functions within the 3dview, the current context is the window under the cursor when the script is run which would be the text editor where the script is. It is possible to override the current context, read this answer for more info.

Another option is to put your code into an operator and either run it by searching in the spacebar menu or from a button you add to the sidebar in the 3dview. You can find a template for creating a simple operator in blenders text editor or view it online.

like image 184
sambler Avatar answered Nov 15 '22 11:11

sambler