3
$\begingroup$

My goal is to import a .shp file into blender within my python script. I found the add-on BlenderGIS that offers this functionality. I can add the add-on to blender make it work "manually", but I want to call the import function from a python script.

I am new to Blender and I cannot find what the right way is to import a add-on/BlenderGIS into a python script.

Among other things, I tried the following:

import bpy
import sys
import os

dir = os.path.dirname(bpy.data.filepath)
if not dir in sys.path:
    sys.path.append(dir )

import io_shapefile

file = "path/to/file.shp"

bpy.ops.importgis(file)

This gives the error:

Traceback (most recent call last):
  File "<blender_console>", line 1, in <module>
TypeError: 'BPyOpsSubMod' object is not callable

Can someone point me in the right direction on how to import a .shp from a python script?

Thanks in advance

$\endgroup$

2 Answers 2

2
$\begingroup$

Have a look at the operator name when you mouse over it, in this case bpy.ops.importgis.shapefile()

Now in the python console type in command and using autocomplete ctrl-space

>> bpy.ops.importgis.shapefile(
shapefile()
bpy.ops.importgis.shapefile(filepath="", filter_glob="*.shp", useFieldElev=False, fieldElevName="", useFieldExtrude=False, fieldExtrudeName="", extrusionAxis='Z', angCoords=False, separateObjects=False, useFieldName=False, fieldObjName="")

You get the operator call and the parameters. Testing with a local filepath

>>> bpy.ops.importgis.shapefile(filepath="/mnt/.../GDAL/contour.shp")
Read shapefile...
Feature type : PolyLine
DBF fields : ['id', 'elev']
0%
10%
20%
...
90%
100%
Imported mesh had some problem, check the result!
{'FINISHED'}

>>> 
$\endgroup$
0
$\begingroup$

"bpy.ops.importgis(file)" is not a function. The right way to import a .shp file with BlenderGIS is:

bpy.ops.importgis.shapefile("EXEC_DEFAULT", filepath=file)

where the first parametere can be one of these options:

'INVOKE_DEFAULT', 'INVOKE_REGION_WIN', 'INVOKE_REGION_CHANNELS', 'INVOKE_REGION_PREVIEW', 'INVOKE_AREA', 'INVOKE_SCREEN', 'EXEC_DEFAULT', 'EXEC_REGION_WIN', 'EXEC_REGION_CHANNELS', 'EXEC_REGION_PREVIEW', 'EXEC_AREA', 'EXEC_SCREEN'

So, the following code will import a .shp file:

import bpy
file = "/path/to/file.shp"
bpy.ops.importgis.shapefile("EXEC_DEFAULT", filepath=file)
$\endgroup$

You must log in to answer this question.

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