Views

It is now possible for Python scripts to open new views in the connected application via the open_new_view() function.

The following script demonstrates how to open a new view containing a newly created object.

from mapteksdk.project import Project
from mapteksdk.data import Marker
from mapteksdk.pointstudio.operations import open_new_view
 
project = Project()
 
with project.new_or_edit("cad/marker_hello", Marker) as marker:
  marker.text = "Hello world"
  marker.shape = Marker.Shape.DIAMOND
 
open_new_view(marker)

You can create a new view containing multiple objects by passing a list of objects to open_new_view(). This is demonstrated in the example below, where different types of markers are created and added to the view:

from mapteksdk.project import Project
from mapteksdk.data import Marker
from mapteksdk.pointstudio.operations import open_new_view
 
project = Project()
 
markers = []
 
for i, shape in enumerate(Marker.Shape):
  if shape == Marker.Shape.CUSTOM:
    # Skip custom shape.
    continue
 
  with project.new_or_edit(f"cad/all_shapes/{shape.name}", Marker) as marker:
    col = i // 4
    row = i % 4
    marker.location = [row, col, 0]
    marker.text = f"{i}"
    marker.shape = shape
    markers.append(marker.id)
 
open_new_view(markers)

In addition to creating views, it is now possible to query the active view using the active_view() function. The next script uses the active_view() function to print the path to all of the objects in the active view.

from mapteksdk.project import Project
from mapteksdk.pointstudio.operations import active_view
 
project = Project()
 
view = active_view()
 
if view is None:
  print("There is no active view")
else:
  print("Objects in active view:")
  for oid in view.objects_in_view():
    print(oid.path)

The next script demonstrates how to add and remove objects from a view. In each iteration of the loop, a marker with the next shape is created and added to the view. After three seconds, the marker is removed from the view and the next marker is added. This results in a view which cycles through all of the marker shapes (excluding custom, which is skipped).

import time
 
from mapteksdk.project import Project
from mapteksdk.data import Marker
from mapteksdk.pointstudio.operations import open_new_view
 
project = Project()
 
view = open_new_view(wait=True)
 
marker = None
 
for i, shape in enumerate(Marker.Shape):
  if shape == Marker.Shape.CUSTOM:
    # Skip custom shape.
    continue
 
  if marker:
    view.remove_objects([marker.id])
 
  with project.new_or_edit(f"cad/cycle_shapes/{shape.name}", Marker) as marker:
    marker.location = [0, 0, 0]
    marker.text = f"{shape.name}"
    marker.shape = shape
 
  view.add_objects([marker.id])
  time.sleep(3)

There is also the active_or_new_view() function which will return the active view if one exists and a new view if it does not. Here is a code example of how to use the active_or_new_view() function:

from mapteksdk.project import Project
from mapteksdk.pointstudio.operations import active_view
 
project = Project()
 
view = active_or_new_view()
 
print("Objects in active view:")
for oid in view.objects_in_view():
    print(oid.path)