Skip to content

Bpyproj for Addon Developers

Vladimir Elistratov edited this page May 25, 2018 · 1 revision

bpyproj was designed as a general library which can be used by any Blender addon dealing with geographical data. It is distributed as a separated Blender addon and can be easily accessed by any Blender addon.

A typical usage scenario assumes that bpyproj is installed and activated as an ordinary Blender addon. It is also assumed that if bpyproj is deactivated, it should not be used by another Blender addon.

bpyproj exposes two public functions for other Blender addons:

  • draw(context, layout) provides the GUI to choose a projection:
    • context is a Blender Context object
    • layout is a Blender Layout object
  • getProjection(lat, lon) is used to get a Python projection object:
    • lat and lon are geographical coordinates (in degrees) of origin of the Blender global coordinate system

Here is a sample code to get bpyproj GUI in your addon:

class PanelMyBpyProj(bpy.types.Panel):
    bl_label = "Projection"
    bl_space_type = "VIEW_3D"
    bl_region_type = "TOOLS"
    bl_context = "objectmode"
    bl_category = "my category"
    
    @classmethod
    def poll(cls, context):
        return "bpyproj" in context.user_preferences.addons
    
    def draw(self, context):
        import bpyproj
        bpyproj.draw(context, self.layout)

Here is a sample code to get a bpyproj projection object:

def getProjection(lat, lon):
    import sys
        
    projection = None
    # check if <bpyproj> is activated and is available in sys.modules
    bpyproj = "bpyproj" in bpy.context.user_preferences.addons and sys.modules.get("bpyproj")
    if bpyproj:
        projection = bpyproj.getProjection(lat, lon)
    if not projection:
        # <bpyproj> isn't available
        # set a default projection object to the variable <projection>   
    return projection

The Python object returned by bpyproj.getProjection(lat, lon) provides two methods fromGeographic(lat, lon) and toGeographic(x, y) for conversion between geographical and projected coordinates:

# <lat> and <lon> are geographical coordinates of an arbitrary point on the Earth surface
# <x>, <y> and <z> are the corresponding coordinates in the chosen projection
x, y, z = projection.fromGeographic(lat, lon)
# <x> and <y> are the coordinates in the chosen projection
# <lat> and <lon> are the corresponding geographical coordinates
lat, lon = projection.toGeographic(x, y)
Clone this wiki locally