script
Provide basic utilities for pyRevit scripts.
Examples:
from pyrevit import script
script.clipboard_copy('some text')
data = script.journal_read('data-key')
script.exit()
Attributes
mlogger = logger.get_logger(__name__)
module-attribute
DATAFEXT = 'pym'
module-attribute
ICON_SMALL = 16
module-attribute
ICON_MEDIUM = 24
module-attribute
ICON_LARGE = 32
module-attribute
Classes
Functions
get_info()
Return info on current pyRevit command.
Returns:
Type | Description |
---|---|
GenericUICommand
|
Command info object |
get_command_from_path(comp_path)
Returns a pyRevit command object from the given bundle directory.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
comp_path |
str
|
Full directory address of the command bundle |
required |
Returns:
Type | Description |
---|---|
GenericUICommand
|
A subclass of pyRevit command object. |
Source code in pyrevitlib/pyrevit/script.py
get_script_path()
get_alt_script_path()
Return config script path of the current pyRevit command.
Returns:
Type | Description |
---|---|
str
|
config script path |
get_bundle_name()
Return bundle name of the current pyRevit command.
Returns:
Type | Description |
---|---|
str
|
bundle name (e.g. MyButton.pushbutton) |
get_extension_name()
Return extension name of the current pyRevit command.
Returns:
Type | Description |
---|---|
str
|
extension name (e.g. MyExtension.extension) |
get_unique_id()
Return unique id of the current pyRevit command.
Returns:
Type | Description |
---|---|
str
|
command unique id |
get_results()
Return command results dictionary for logging.
Returns:
Type | Description |
---|---|
CommandCustomResults
|
Command results dict |
Source code in pyrevitlib/pyrevit/script.py
get_pyrevit_version()
Return pyRevit version.
Returns:
Type | Description |
---|---|
_PyRevitVersion
|
pyRevit version provider |
get_logger()
Create and return logger named for current script.
Returns:
Type | Description |
---|---|
LoggerWrapper
|
Logger object |
get_output()
Return object wrapping output window for current script.
Returns:
Type | Description |
---|---|
PyRevitOutputWindow
|
Output wrapper object |
get_config(section=None)
Create and return config section parser object for current script.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
section |
str
|
config section name. If not provided, it will default to the command name plus the 'config' suffix. |
None
|
Returns:
Type | Description |
---|---|
PyRevitConfigSectionParser
|
Config section parser object |
Source code in pyrevitlib/pyrevit/script.py
save_config()
Save pyRevit config.
Scripts should call this to save any changes they have done to their config section object received from script.get_config() method.
Source code in pyrevitlib/pyrevit/script.py
reset_config(section=None)
Reset pyRevit config.
Script should call this to reset any save configuration by removing section related to current script.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
section |
str
|
config section name |
None
|
Source code in pyrevitlib/pyrevit/script.py
get_universal_data_file(file_id, file_ext, add_cmd_name=False)
Return filename to be used by a user script to store data.
File name is generated in this format:
pyRevit_{file_id}.{file_ext}
Examples:
'/pyRevit_mydata.data' '/pyRevit_Command Name_mydata.data'Universal data files are not cleaned up at pyRevit startup. Script should manage cleaning up these files.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
file_id |
str
|
unique id for the filename |
required |
file_ext |
str
|
file extension |
required |
add_cmd_name |
bool
|
add command name to file name |
False
|
Returns:
Type | Description |
---|---|
str
|
full file path |
Source code in pyrevitlib/pyrevit/script.py
get_data_file(file_id, file_ext, add_cmd_name=False)
Return filename to be used by a user script to store data.
File name is generated in this format:
pyRevit_{Revit Version}_{file_id}.{file_ext}
Examples:
'/pyRevit_2018_mydata.data' '/pyRevit_2018_Command Name_mydata.data'Data files are not cleaned up at pyRevit startup. Script should manage cleaning up these files.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
file_id |
str
|
unique id for the filename |
required |
file_ext |
str
|
file extension |
required |
add_cmd_name |
bool
|
add command name to file name |
False
|
Returns:
Type | Description |
---|---|
str
|
full file path |
Source code in pyrevitlib/pyrevit/script.py
get_instance_data_file(file_id, add_cmd_name=False)
Return filename to be used by a user script to store data.
File name is generated in this format:
pyRevit_{Revit Version}_{Process Id}_{file_id}.{file_ext}
Examples:
'/pyRevit_2018_6684_mydata.tmp' '/pyRevit_2018_6684_Command Name_mydata.tmp'Instance data files are cleaned up at pyRevit startup.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
file_id |
str
|
unique id for the filename |
required |
add_cmd_name |
bool
|
add command name to file name |
False
|
Returns:
Type | Description |
---|---|
str
|
full file path |
Source code in pyrevitlib/pyrevit/script.py
get_document_data_file(file_id, file_ext, add_cmd_name=False)
Return filename to be used by a user script to store data.
File name is generated in this format:
pyRevit_{Revit Version}_{file_id}_{Project Name}.{file_ext}
Examples:
'/pyRevit_2018_mydata_Project1.data' '/pyRevit_2018_Command Name_mydata_Project1.data'Document data files are not cleaned up at pyRevit startup. Script should manage cleaning up these files.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
file_id |
str
|
unique id for the filename |
required |
file_ext |
str
|
file extension |
required |
add_cmd_name |
bool
|
add command name to file name |
False
|
Returns:
Type | Description |
---|---|
str
|
full file path |
Source code in pyrevitlib/pyrevit/script.py
remove_data_file(filepath)
get_bundle_file(file_name)
Return full path to file under current script bundle.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
file_name |
str
|
bundle file name |
required |
Returns:
Type | Description |
---|---|
str
|
full bundle file path |
Source code in pyrevitlib/pyrevit/script.py
get_bundle_files(sub_path=None)
Return full path to all file under current script bundle.
Returns:
Type | Description |
---|---|
list[str]
|
list of bundle file paths |
Source code in pyrevitlib/pyrevit/script.py
journal_write(data_key, msg)
Write key and value to active Revit journal for current command.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
data_key |
str
|
data key |
required |
msg |
str
|
data value string |
required |
Source code in pyrevitlib/pyrevit/script.py
journal_read(data_key)
Read value for provided key from active Revit journal.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
data_key |
str
|
data key |
required |
Returns:
Type | Description |
---|---|
str
|
data value string |
Source code in pyrevitlib/pyrevit/script.py
get_button()
Find and return current script ui button.
Returns:
Type | Description |
---|---|
_PyRevitRibbonButton
|
ui button object |
Source code in pyrevitlib/pyrevit/script.py
get_all_buttons()
Find and return all ui buttons matching current script command name.
Sometimes tools are duplicated across extensions for user access control so this would help smart buttons to find all the loaded buttons and make icon adjustments.
Returns:
Type | Description |
---|---|
list[_PyRevitRibbonButton]
|
list of ui button objects |
Source code in pyrevitlib/pyrevit/script.py
toggle_icon(new_state, on_icon_path=None, off_icon_path=None, icon_size=ICON_MEDIUM)
Set the state of button icon (on or off).
This method expects on.png and off.png in command bundle for on and off icon states, unless full path of icon states are provided.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
new_state |
bool
|
state of the ui button icon. |
required |
on_icon_path |
str
|
full path of icon for on state. default='on.png' |
None
|
off_icon_path |
str
|
full path of icon for off state. default='off.png' |
None
|
icon_size |
int
|
size of the icon. Defaults to medium (24x24) |
ICON_MEDIUM
|
Source code in pyrevitlib/pyrevit/script.py
exit()
show_file_in_explorer(file_path)
show_folder_in_explorer(folder_path)
open_url(url)
Open url in a new tab in default webbrowser.
clipboard_copy(string_to_copy)
load_index(index_file='index.html')
Load html file into output window.
This method expects index.html file in the current command bundle, unless full path to an html file is provided.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
index_file |
str
|
full path of html file. |
'index.html'
|
Source code in pyrevitlib/pyrevit/script.py
load_ui(ui_instance, ui_file='ui.xaml', handle_esc=True, set_owner=True)
Load xaml file into given window instance.
If window instance defines a method named setup
it
will be called after loading
Parameters:
Name | Type | Description | Default |
---|---|---|---|
ui_instance |
WPFWindow
|
ui form instance |
required |
ui_file |
str
|
name of ui xaml file. defaults to: 'ui.xaml' |
'ui.xaml'
|
handle_esc |
bool
|
handle escape and close window |
True
|
set_owner |
bool
|
set window owner to Revit window |
True
|
Source code in pyrevitlib/pyrevit/script.py
get_envvar(envvar)
Return value of give pyRevit environment variable.
The environment variable system is used to retain small values in memory between script runs (e.g. active/inactive state for toggle tools). Do not store large objects in memory using this method. List of currently set environment variables could be sees in pyRevit settings window.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
envvar |
str
|
name of environment variable |
required |
Returns:
Type | Description |
---|---|
Any
|
object stored in environment variable |
Examples:
TrueSource code in pyrevitlib/pyrevit/script.py
set_envvar(envvar, value)
Set value of give pyRevit environment variable.
The environment variable system is used to retain small values in memory between script runs (e.g. active/inactive state for toggle tools). Do not store large objects in memory using this method. List of currently set environment variables could be sees in pyRevit settings window.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
envvar |
str
|
name of environment variable |
required |
value |
any
|
value of environment variable |
required |
Examples:
FalseSource code in pyrevitlib/pyrevit/script.py
dump_json(data, filepath)
Dumps given data into given json file.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
data |
object
|
serializable data to be dumped |
required |
filepath |
str
|
json file path |
required |
Source code in pyrevitlib/pyrevit/script.py
load_json(filepath)
Loads data from given json file.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
filepath |
str
|
json file path |
required |
Returns:
Type | Description |
---|---|
object
|
deserialized data |
Source code in pyrevitlib/pyrevit/script.py
dump_csv(data, filepath)
Dumps given data into given csv file.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
data |
list[list[str]]
|
data to be dumped |
required |
filepath |
str
|
csv file path |
required |
Source code in pyrevitlib/pyrevit/script.py
load_csv(filepath)
Read lines from given csv file.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
filepath |
str
|
csv file path |
required |
Returns:
Type | Description |
---|---|
list[list[str]]
|
csv data |
Source code in pyrevitlib/pyrevit/script.py
store_data(slot_name, data, this_project=True)
Wraps python pickle.dump() to easily store data to pyRevit data files.
To store native Revit objects, use revit.serialize(). See Example
Parameters:
Name | Type | Description | Default |
---|---|---|---|
slot_name |
type
|
desc |
required |
data |
obj
|
any pickalable data |
required |
this_project |
bool
|
data belongs to this project only |
True
|
Examples:
from pyrevit import revit
from pyrevit import script
class CustomData(object):
def __init__(self, count, element_ids):
self._count = count
# serializes the Revit native objects
self._elmnt_ids = [revit.serialize(x) for x in element_ids]
@property
def count(self):
return self._count
@property
def element_ids(self):
# de-serializes the Revit native objects
return [x.deserialize() for x in self._elmnt_ids]
mydata = CustomData(
count=3,
element_ids=[<DB.ElementId>, <DB.ElementId>, <DB.ElementId>]
)
script.store_data("Selected Elements", mydata)
Source code in pyrevitlib/pyrevit/script.py
load_data(slot_name, this_project=True)
Wraps python pickle.load() to easily load data from pyRevit data files.
To recover native Revit objects, use revit.deserialize(). See Example
Similar to pickle module, the custom data types must be defined in the main scope so the loader can create an instance and return original stored data
Parameters:
Name | Type | Description | Default |
---|---|---|---|
slot_name |
type
|
desc |
required |
this_project |
bool
|
data belongs to this project only |
True
|
Returns:
Type | Description |
---|---|
object
|
stored data |
Examples:
from pyrevit import revit
from pyrevit import script
class CustomData(object):
def __init__(self, count, element_ids):
self._count = count
# serializes the Revit native objects
self._elmnt_ids = [revit.serialize(x) for x in element_ids]
@property
def count(self):
return self._count
@property
def element_ids(self):
# de-serializes the Revit native objects
return [x.deserialize() for x in self._elmnt_ids]
mydata = script.load_data("Selected Elements")
mydata.element_ids
Source code in pyrevitlib/pyrevit/script.py
data_exists(slot_name, this_project=True)
Checks if data file in a specified slot and for certain project exists.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
slot_name |
type
|
desc |
required |
this_project |
bool
|
data belongs to this project only |
True
|
Returns:
Type | Description |
---|---|
bool
|
true if the path exists |