1+ //
2+ // Copyright 2025 Autodesk
3+ //
4+ // Licensed under the Apache License, Version 2.0 (the "License");
5+ // you may not use this file except in compliance with the License.
6+ // You may obtain a copy of the License at
7+ //
8+ // http://www.apache.org/licenses/LICENSE-2.0
9+ //
10+ // Unless required by applicable law or agreed to in writing, software
11+ // distributed under the License is distributed on an "AS IS" BASIS,
12+ // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+ // See the License for the specific language governing permissions and
14+ // limitations under the License.
15+ //
16+
17+ #include < mayaUsd/utils/utilComponentCreator.h>
18+
19+ #include < pxr/base/tf/diagnostic.h>
20+ #include < pxr/pxr.h>
21+
22+ #include < maya/MGlobal.h>
23+ #include < maya/MString.h>
24+
25+ #include < vector>
26+
27+ PXR_NAMESPACE_USING_DIRECTIVE
28+
29+ namespace MAYAUSD_NS_DEF {
30+ namespace ComponentUtils {
31+
32+ std::vector<std::string> getAdskUsdComponentLayersToSave (const std::string& proxyPath)
33+ {
34+ // Ask via python what layers need to be saved for the component.
35+ // With the maya api we can only return a string, so we concat the ids.
36+ MString getLayersFromComponent;
37+ getLayersFromComponent.format (
38+ " def usd_component_creator_get_layers_to_save():\n "
39+ " import mayaUsd\n "
40+ " import mayaUsd.ufe\n "
41+ " from usd_component_creator_plugin import MayaComponentManager\n "
42+ " stage = mayaUsd.ufe.getStage('^1s')\n "
43+ " if stage is None:\n "
44+ " return ''\n "
45+ " ids = MayaComponentManager.GetInstance().GetSaveInfo(stage)\n "
46+ " result = ''\n "
47+ " first = True\n "
48+ " for id in ids:\n "
49+ " if not first:\n "
50+ " result += '\\ n'\n "
51+ " result += id\n "
52+ " first = False\n "
53+ " return result\n " ,
54+ proxyPath.c_str ());
55+
56+ if (MGlobal::executePythonCommand (getLayersFromComponent)) {
57+ auto resultString = MGlobal::executePythonCommandStringResult (
58+ " usd_component_creator_get_layers_to_save()" );
59+ MStringArray layerIds;
60+ resultString.split (' \n ' , layerIds);
61+ std::vector<std::string> toSave;
62+ for (const auto & id : layerIds) {
63+ toSave.push_back (id.asUTF8 ());
64+ }
65+ return toSave;
66+ }
67+ return {};
68+ }
69+
70+ bool isAdskUsdComponent (const std::string& proxyShapePath)
71+ {
72+ MString defineIsComponentCmd;
73+ defineIsComponentCmd.format (
74+ " def usd_component_creator_is_proxy_shape_a_component():\n "
75+ " from pxr import Sdf, Usd, UsdUtils\n "
76+ " import mayaUsd\n "
77+ " import mayaUsd.ufe\n "
78+ " try:\n "
79+ " from AdskUsdComponentCreator import ComponentDescription\n "
80+ " except ImportError:\n "
81+ " return -1\n "
82+ " proxyStage = mayaUsd.ufe.getStage('^1s')\n "
83+ " component_description = ComponentDescription.CreateFromStageMetadata(proxyStage)\n "
84+ " if component_description:\n "
85+ " return 1\n "
86+ " else:\n "
87+ " return 0" ,
88+ proxyShapePath.c_str ());
89+
90+ int isStageAComponent = 0 ;
91+ MStatus success;
92+ if (MS::kSuccess
93+ == (success = MGlobal::executePythonCommand (defineIsComponentCmd, false , false ))) {
94+ MString runIsComponentCmd = " usd_component_creator_is_proxy_shape_a_component()" ;
95+ success = MGlobal::executePythonCommand (runIsComponentCmd, isStageAComponent);
96+ }
97+
98+ if (success != MS::kSuccess ) {
99+ TF_RUNTIME_ERROR (
100+ " Error occurred when testing stage '%s' for component." , proxyShapePath.c_str ());
101+ }
102+
103+ return isStageAComponent == 1 ;
104+ }
105+
106+ void saveAdskUsdComponent (const std::string& proxyPath)
107+ {
108+ MString saveComponent;
109+ saveComponent.format (
110+ " from pxr import Sdf, Usd, UsdUtils\n "
111+ " import mayaUsd\n "
112+ " import mayaUsd.ufe\n "
113+ " from usd_component_creator_plugin import MayaComponentManager\n "
114+ " proxyStage = mayaUsd.ufe.getStage('^1s')\n "
115+ " MayaComponentManager.GetInstance().SaveComponent(proxyStage)" ,
116+ proxyPath.c_str ());
117+
118+ if (!MGlobal::executePythonCommand (saveComponent)) {
119+ TF_RUNTIME_ERROR (" Error while saving USD component '%s'" , proxyPath.c_str ());
120+ }
121+ }
122+
123+ std::string previewSaveAdskUsdComponent (
124+ const std::string& saveLocation,
125+ const std::string& componentName,
126+ const std::string& proxyPath)
127+ {
128+ MString defMoveComponentPreviewCmd;
129+ defMoveComponentPreviewCmd.format (
130+ " def usd_component_creator_move_component_preview():\n "
131+ " import json\n "
132+ " from pxr import Sdf, Usd, UsdUtils\n "
133+ " import mayaUsd\n "
134+ " import mayaUsd.ufe\n "
135+ " try:\n "
136+ " from AdskUsdComponentCreator import ComponentDescription, "
137+ " PreviewMoveComponentHierarchy\n "
138+ " except ImportError:\n "
139+ " return None\n "
140+ " proxyStage = mayaUsd.ufe.getStage('^1s')\n "
141+ " component_description = "
142+ " ComponentDescription.CreateFromStageMetadata(proxyStage)\n "
143+ " if component_description:\n "
144+ " move_comp_preview = PreviewMoveComponentHierarchy(component_description, '^2s', "
145+ " '^3s')\n "
146+ " return json.dumps(move_comp_preview)\n "
147+ " else:\n "
148+ " return \"\" " ,
149+ proxyPath.c_str (),
150+ saveLocation.c_str (),
151+ componentName.c_str ());
152+
153+ if (MS::kSuccess == MGlobal::executePythonCommand (defMoveComponentPreviewCmd)) {
154+ MString result;
155+ MString runComponentMovePreviewCmd = " usd_component_creator_move_component_preview()" ;
156+ if (MS::kSuccess == MGlobal::executePythonCommand (runComponentMovePreviewCmd, result)) {
157+ return result.asChar ();
158+ }
159+ }
160+ return {};
161+ }
162+
163+ } // namespace ComponentUtils
164+ } // namespace MAYAUSD_NS_DEF
0 commit comments