mapteksdk.workflows.workflow_selection module

ConnectorType subclasses dependent on the data module.

class WorkflowSelection(selection_string, use_active_selection=False)

Bases: ConnectorType

Class representing a read-only list of ObjectIDs.

Pass this to declare_input_connector for input connectors expecting a selection - the lists of objects given by the ‘Maptek Database Object’ connectors of many components.

Iterating over this object will iterate over the ObjectIDs in the selection.

You should not access the contents of this object until after Project() has been called.

Parameters:
  • selection_string (str) – String representing the selection.

  • use_active_selection (bool) – If True, the selection_string will be ignored and when ids is called it will return the current active selection. Typically use WorkflowSelection.active_selection() instead of setting this parameter to True.

Raises:
  • NoConnectedApplicationError – If the contents are accessed before Project() has been called.

  • ValueError – If part of the selection cannot be converted to an ObjectID.

Warning

Ensure the ObjectIDs passed to this class are from the same project as is opened with Project() otherwise the ObjectIDs may refer to a completely different object.

Notes

This class does not support object paths which contain quotation marks or commas.

Examples

Script which takes a selection of objects and returns their centroid via a list output connector. This script would have one input connector “Selection” which accepts a selection. There is also one output connector “Centroid” which will be set to the centroid of all of the points in the objects in the selection. Note that this script does not honour point selection.

>>> from mapteksdk.project import Project
>>> from mapteksdk.workflows import (WorkflowArgumentParser,
...                                  WorkflowSelection,
...                                  Point3DConnectorType)
>>> import numpy as np
>>> parser = WorkflowArgumentParser(
...     description="Get the centroid of objects with points")
>>> parser.declare_input_connector(
...     "selection",
...     WorkflowSelection,
...     description="Objects to find the centroid of.")
>>> parser.declare_output_connector(
...     "Centroid",
...     Point3DConnectorType,
...     description="The centroid of the points in the objects.")
>>> parser.parse_arguments() # Must call before Project().
>>> project = Project() # Must call before get_ids().
>>> sums = np.zeros(3)
>>> count = 0
>>> for oid in parser["selection"]:
...     with project.read(oid) as read_object:
...         if not hasattr(read_object, "points"): continue
...         sums += np.sum(read_object.points, axis=0)
...         count += read_object.point_count
>>> result = sums / count
>>> parser.set_output("Centroid", result)
>>> parser.flush_output()
classmethod type_string()

The string representation of the type to report to the Workflow as the type for the Connector.

Returns:

String representation of the type to report to the workflow.

Return type:

str

classmethod from_string(string_value)

Convert a string value from an input connector to the corresponding python type and returns it.

Returns:

The python representation of the string value.

Return type:

any

Raises:
  • TypeError – If string_value is not a supported type.

  • ValueError – If string_value is the correct type but an invalid value.

Parameters:

string_value (str) –

classmethod to_json(value)

Converts the value to a json-serializable value.

This is used to convert python values to json values to be passed to the workflow for output connectors.

Returns:

Json serializable representation of value.

Return type:

json-serializable

Raises:
  • TypeError – If value is not a supported type.

  • ValueError – If value is the correct type but an invalid value.

Parameters:

value (str | Iterable) –

classmethod to_default_json(value)

Converts the value to a json serializable default.

This allows for specifying a different representation for default values. The output of this function should not include lists.

Overwrite this function to raise an error to indicate that default values are not supported.

By default this calls to_json.

Returns:

String representation of value.

Return type:

str

Raises:
  • TypeError – If value is not a supported type.

  • ValueError – If value is the correct type but an invalid value.

Parameters:

value (Any) –

classmethod active_selection()

Construct a WorkflowSelection which contains the active selection.

Reading the ids property on the returned WorkflowSelection will return the ids of the currently selected objects in the application when ids was first called on that instance. (i.e. repeated reads to ids will always return the same list).

Notes

The active selection is not read until the ids property of the returned object is accessed. This allows this constructor to be called without any connected application, thus allowing this to be used as a default value for connectors.

Once the ids property has been accessed, accessing the property again will always return the same list, even if the active selection in the application changes.

Examples

The following example script prints the path and type of every object in the selection. If there is no value from the “selection” input (e.g. If the corresponding connector was deleted or the script is run from outside of a workflow) then it will use the active selection instead.

>>> from mapteksdk.project import Project
>>> from mapteksdk.workflows import (
...   WorkflowArgumentParser, WorkflowSelection)
>>> if __name__ == "__main__":
...   parser = WorkflowArgumentParser()
...   parser.declare_input_connector(
...     "selection",
...     WorkflowSelection,
...     default=WorkflowSelection.active_selection()
...   )
...   parser.parse_arguments()
...   with Project() as project:
...     for oid in parser["selection"]:
...       print(oid.path, ":", oid.type_name)

The returned object can be used anywhere in place of a WorkflowSelection. For example, the following script sets the output selection to be the active selection in the application:

>>> from mapteksdk.workflows import (
...   WorkflowArgumentParser, WorkflowSelection)
>>> from mapteksdk.project import Project
>>> if __name__ == "__main__":
...   parser = WorkflowArgumentParser()
...   parser.declare_output_connector("selection", WorkflowSelection)
...   parser.parse_arguments()
...   with Project() as project:
...     parser.set_output("selection", WorkflowSelection.active_selection())
Return type:

WorkflowSelection

property selection: list[str]

The input selection split into strings.

Accessing the contents of this list is not recommended.

  • This list will have the same length as the list returned by ids (Assuming all the selected objects exist).

  • Each item in the list will be a string.

  • The contents of each string in the list are unspecified.

  • Accessing this will not raise an error when not connected to an application.

property ids: list[mapteksdk.data.objectid.ObjectID[mapteksdk.data.base.DataObject]]

Return the IDs in the selection as a list.

This must be called after Project() has been called. Object IDs only have meaning within a Project.

Returns:

ObjectIDs in the selection.

Return type:

list of ObjectID

Raises:
  • ValueError – If any string cannot be converted to an ObjectID.

  • NoConnectedApplicationError – If called before Project() is called.