generated from specklesystems/speckle_automate_python_example
-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
a14a5ae
commit 1d92dfa
Showing
4 changed files
with
57 additions
and
50 deletions.
There are no files selected for viewing
Empty file.
Empty file.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,50 +1,57 @@ | ||
import random | ||
|
||
from speckle_automate import AutomationContext | ||
|
||
from inputs import FunctionInputs | ||
from Utilities.flatten import flatten_base | ||
|
||
|
||
def automate_function( | ||
automate_context: AutomationContext, | ||
function_inputs: FunctionInputs, | ||
) -> None: | ||
"""This is an example Speckle Automate function. | ||
Args: | ||
automate_context: A context helper object, that carries relevant information | ||
about the runtime context of this function. | ||
It gives access to the Speckle project data, that triggered this run. | ||
It also has convenience methods attach result data to the Speckle model. | ||
function_inputs: An instance object matching the defined schema. | ||
""" | ||
|
||
# the context provides a convenient way, to receive the triggering version | ||
version_root_object = automate_context.receive_version() | ||
|
||
flat_list_of_objects = flatten_base(version_root_object) | ||
|
||
# filter the list to only include objects that are displayable. | ||
# this is a simple example, that checks if the object has a displayValue | ||
displayable_objects = [ | ||
speckle_object | ||
for speckle_object in flat_list_of_objects | ||
if ( | ||
getattr(speckle_object, "displayValue", None) | ||
or getattr(speckle_object, "@displayValue", None) | ||
) | ||
and getattr(speckle_object, "id", None) is not None | ||
] | ||
|
||
if len(displayable_objects) == 0: | ||
automate_context.mark_run_failed( | ||
"Automation failed: No displayable objects found." | ||
) | ||
|
||
else: | ||
# select a random object from the list | ||
random_object = random.choice(displayable_objects) | ||
|
||
automate_context.attach_info_to_objects( | ||
category="Selected Object", | ||
object_ids=[random_object.id], | ||
message=function_inputs.comment_phrase, | ||
) | ||
|
||
automate_context.mark_run_success("Added a comment to a random object.") | ||
|
||
# set the automation context view, to the original model / version view | ||
automate_context.set_context_view() | ||
"""This is an example Speckle Automate function. | ||
Args: | ||
automate_context: A context helper object, that carries relevant information | ||
about the runtime context of this function. | ||
It gives access to the Speckle project data, that triggered this run. | ||
It also has convenience methods attach result data to the Speckle model. | ||
function_inputs: An instance object matching the defined schema. | ||
""" | ||
|
||
# the context provides a convenient way, to receive the triggering version | ||
version_root_object = automate_context.receive_version() | ||
|
||
flat_list_of_objects = flatten_base(version_root_object) | ||
|
||
# filter the list to only include objects that are displayable. | ||
# this is a simple example, that checks if the object has a displayValue | ||
displayable_objects = [ | ||
speckle_object | ||
for speckle_object in flat_list_of_objects | ||
if ( | ||
getattr(speckle_object, "displayValue", None) | ||
or getattr(speckle_object, "@displayValue", None) | ||
) and getattr(speckle_object, "id", None) is not None | ||
] | ||
|
||
if len(displayable_objects) == 0: | ||
automate_context.mark_run_failed( | ||
"Automation failed: No displayable objects found." | ||
) | ||
|
||
else: | ||
# select a random object from the list | ||
random_object = random.choice(displayable_objects) | ||
|
||
automate_context.attach_info_to_objects( | ||
category="Selected Object", | ||
object_ids=[random_object.id], | ||
message=function_inputs.comment_phrase, | ||
) | ||
|
||
automate_context.mark_run_success("Added a comment to a random object.") | ||
|
||
# set the automation context view, to the original model / version view | ||
automate_context.set_context_view() |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters