@@ -30,10 +30,33 @@ def current_file():
3030 return current_filepath
3131
3232
33- def work_root ():
34-
35- # Base the root on the current Maya workspace.
36- return os .path .join (
37- cmds .workspace (query = True , rootDirectory = True ),
38- cmds .workspace (fileRuleEntry = "scene" )
39- )
33+ def work_root (session ):
34+ work_dir = session ["AVALON_WORKDIR" ]
35+ scene_dir = None
36+
37+ # Query scene file rule from workspace.mel if it exists in WORKDIR
38+ # We are parsing the workspace.mel manually as opposed to temporarily
39+ # setting the Workspace in Maya in a context manager since Maya had a
40+ # tendency to crash on frequently changing the workspace when this
41+ # function was called many times as one scrolled through Work Files assets.
42+ workspace_mel = os .path .join (work_dir , "workspace.mel" )
43+ if os .path .exists (workspace_mel ):
44+ scene_rule = 'workspace -fr "scene" '
45+ # We need to use builtins as `open` is overridden by the workio API
46+ open_file = __builtins__ ["open" ]
47+ with open_file (workspace_mel , "r" ) as f :
48+ for line in f :
49+ if line .strip ().startswith (scene_rule ):
50+ # remainder == "rule";
51+ remainder = line [len (scene_rule ):]
52+ # scene_dir == rule
53+ scene_dir = remainder .split ('"' )[1 ]
54+ else :
55+ # We can't query a workspace that does not exist
56+ # so we return similar to what we do in other hosts.
57+ scene_dir = session .get ("AVALON_SCENEDIR" )
58+
59+ if scene_dir :
60+ return os .path .join (work_dir , scene_dir )
61+ else :
62+ return work_dir
0 commit comments