12
13
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 | class ExtractOxRig(plugin.MayaExtractorPlugin):
"""Extract the Ornatrix rig to a Maya Scene and write the Ornatrix rig data."""
label = "Extract Ornatrix Rig"
families = ["oxrig"]
scene_type = "ma"
def process(self, instance):
"""Plugin entry point."""
cmds.loadPlugin("Ornatrix", quiet=True)
maya_settings = instance.context.data["project_settings"]["maya"]
ext_mapping = {
item["name"]: item["value"]
for item in maya_settings["ext_mapping"]
}
if ext_mapping:
self.log.debug("Looking in settings for scene type ...")
# use extension mapping for first family found
for family in self.families:
try:
self.scene_type = ext_mapping[family]
self.log.debug(
"Using {} as scene type".format(self.scene_type))
break
except KeyError:
# no preset found
pass
# Define extract output file path
dirname = self.staging_dir(instance)
settings_path = os.path.join(dirname, "ornatrix.rigsettings")
image_search_path = instance.data["resourcesDir"]
# add textures to transfers
if 'transfers' not in instance.data:
instance.data['transfers'] = []
resources = instance.data.get("resources", [])
for resource in resources:
for file in resource['files']:
src = file
dst = os.path.join(image_search_path, os.path.basename(file))
resource["destination_file"] = dst
instance.data['transfers'].append([src, dst])
self.log.debug("adding transfer {} -> {}". format(src, dst))
self.log.debug("Writing metadata file: {}".format(settings_path))
# The rigsettings contains a list of {"node": node_path} entries.
settings = []
for node in instance.data["setMembers"]:
settings.append({
"node": node
})
with open(settings_path, "w") as fp:
json.dump(settings, fp, ensure_ascii=False)
texture_attributes = {
resource["texture_attribute"]: resource["destination_file"]
for resource in resources
}
# Ornatrix related staging dirs
maya_path = os.path.join(dirname,
"ornatrix_rig.{}".format(self.scene_type))
ox_groom_path = os.path.join(dirname, "ornatrix_rig.oxg.zip")
ox_groom_path = ox_groom_path.replace("\\", "/")
nodes = instance.data["setMembers"]
with lib.maintained_selection():
# Export ornatrix_rig.ma (or .mb)
with lib.attribute_values(texture_attributes):
cmds.select(nodes, noExpand=True)
cmds.file(maya_path,
force=True,
exportSelected=True,
type="mayaAscii" if self.scene_type == "ma" else "mayaBinary", # noqa: E501
preserveReferences=False,
constructionHistory=True,
shader=True)
# Export ornatrix_rig.oxg.zip
cmds.select(nodes, noExpand=True)
cmds.OxSaveGroom(path=ox_groom_path, optional=True)
# Ensure files can be stored
# build representations
if "representations" not in instance.data:
instance.data["representations"] = []
self.log.debug(f"Ornatrix rig file: {maya_path}")
instance.data["representations"].append(
{
'name': self.scene_type,
'ext': self.scene_type,
'files': os.path.basename(maya_path),
'stagingDir': dirname
}
)
self.log.debug(f"OxGroom file: {ox_groom_path}")
instance.data["representations"].append(
{
'name': "oxg.zip",
'ext': "oxg.zip",
'files': os.path.basename(ox_groom_path),
'stagingDir': dirname
}
)
self.log.debug(f"Ornatrix rigsettings file: {settings_path}")
instance.data["representations"].append(
{
'name': "rigsettings",
'ext': "rigsettings",
'files': os.path.basename(settings_path),
'stagingDir': dirname
}
)
self.log.debug("Extracted {} to {}".format(instance, dirname))
|