14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138 | class CreateWorkfile(AutoCreator):
"""Workfile auto-creator."""
settings_category = "flame"
identifier = "io.ayon.creators.flame.workfile"
label = "Workfile"
product_type = "workfile"
icon = "fa5.file"
default_variant = "Main"
@staticmethod
def _get_project_workfile_filepath():
"""
Args:
project_name (str): The project name.
Returns:
str. The path to the expected Json workfile.
"""
project_name = flapi.get_current_project().name
return os.path.join(
os.environ["AYON_WORKDIR"],
f"{project_name}.workfile"
)
def _dump_instance_data(self, data):
""" Dump instance data into a side-car json file.
Args:
data (dict): The data to push to the project metadata.
Returns:
bool. Has the metadata been updated.
"""
out_path = self._get_project_workfile_filepath()
with open(out_path, "w", encoding="utf-8") as out_file:
json.dump(data, out_file)
def _load_instance_data(self):
""" Returns the data stored in side-car json file if exists.
Returns:
dict. The workfile metadata data.
"""
in_path = self._get_project_workfile_filepath()
try:
with open(in_path) as in_file:
return json.load(in_file)
except (FileNotFoundError, json.JSONDecodeError):
return {}
def _create_new_instance(self):
"""Create a new workfile instance.
Returns:
dict. The data of the instance to be created.
"""
variant = self.default_variant
project_name = self.create_context.get_current_project_name()
folder_path = self.create_context.get_current_folder_path()
task_name = self.create_context.get_current_task_name()
host_name = self.create_context.host_name
folder_entity = self.create_context.get_current_folder_entity()
task_entity = self.create_context.get_current_task_entity()
product_name = self.get_product_name(
project_name,
folder_entity,
task_entity,
self.default_variant,
host_name,
)
data = {
"folderPath": folder_path,
"task": task_name,
"variant": variant,
}
data.update(
self.get_dynamic_data(
variant,
task_name,
folder_entity,
project_name,
host_name,
False,
)
)
self.log.info("Auto-creating workfile instance...")
current_instance = CreatedInstance(
self.product_type, product_name, data, self)
self._add_instance_to_context(current_instance)
return current_instance
def create(self, options=None):
"""Auto-create an instance by default."""
instance_data = self._load_instance_data()
if instance_data:
return
self.log.info("Auto-creating workfile instance...")
self._create_new_instance()
def collect_instances(self):
"""Collect from timeline marker or create a new one."""
data = self._load_instance_data()
if not data:
return
instance = CreatedInstance(
self.product_type, data["productName"], data, self
)
self._add_instance_to_context(instance)
def update_instances(self, update_list):
"""Store changes in project metadata so they can be recollected.
Args:
update_list(List[UpdateData]): Gets list of tuples. Each item
contain changed instance and its changes.
"""
for created_inst, _ in update_list:
data = created_inst.data_to_store()
self._dump_instance_data(data)
|