Arnaud
Arnaud

Reputation: 467

How to share a variable between 2 pyRevit scripts?

I am using the latest version of pyRevit, v45. I'm writing some info in temporary files with

myTempFile = script.get_instance_data_file("id")

This creates a file named pyRevit_2018_xxxx_id.tmp in which I store useful info. If I'm not mistaken, the "xxxx" part is changing every time I reload Revit. Now, I need to get access to this information from another pyRevit script.

How can I retrieve the name of the temp file I need to read? In other words, how do I access "myTempFile" from within the second script, which has no idea of the name of "myTempFile"?

I guess I can share somehow that variable between my script, but what's the proper way to do this? I know this must be a very basic programming question, but I'm indeed not a programmer ;)

Thanks a lot, Arnaud.

Upvotes: 1

Views: 288

Answers (2)

Ehsan Iran-Nejad
Ehsan Iran-Nejad

Reputation: 1807

pyrevit.script module provides 4 different methods for creating temporary files based on their use case:

get_instance_data_file: for data files marked with Revit instance pid. This means that scripts running on another instance will not see this temp file. http://pyrevit.readthedocs.io/en/latest/pyrevit/script.html#pyrevit.script.get_instance_data_file

get_universal_data_file: for temp files accessible to all Revit instances and versions http://pyrevit.readthedocs.io/en/latest/pyrevit/script.html#pyrevit.script.get_universal_data_file

get_data_file: Base method to get a standard temp file for current revit version http://pyrevit.readthedocs.io/en/latest/pyrevit/script.html#pyrevit.script.get_data_file

get_document_data_file: temp file marked with active document (so scripts working on another document will not see this) http://pyrevit.readthedocs.io/en/latest/pyrevit/script.html#pyrevit.script.get_document_data_file

Each method uses a pattern to create the temp file name. So as long as the call to the method is the same of different scripts, the method generates the same file name.

Example:

Script 1:

from pyrevit import script
tfile = script.get_data_file('mydata')

Script 2:

from pyrevit import script
tempfile = script.get_data_file('mydata')

In this example tempfile = tfile since the file id is the same.

There is documentation on each so make sure you take a look at those and pick the flavor that serves your purpose.

Upvotes: 1

Arnaud
Arnaud

Reputation: 467

Ok, I realise now that my variables in the 1st script cease to exist after its execution. So for now I wrote the file name in another file, of which I know the name.. That works.

But if there's a cleaner way to do this, I'd be glad to learn ;)

Arnaud

Upvotes: 0

Related Questions