1 //
2 // Copyright 2016 Pixar
3 //
4 // Licensed under the Apache License, Version 2.0 (the "Apache License")
5 // with the following modification; you may not use this file except in
6 // compliance with the Apache License and the following modification to it:
7 // Section 6. Trademarks. is deleted and replaced with:
8 //
9 // 6. Trademarks. This License does not grant permission to use the trade
10 //    names, trademarks, service marks, or product names of the Licensor
11 //    and its affiliates, except as required to comply with Section 4(c) of
12 //    the License and to reproduce the content of the NOTICE file.
13 //
14 // You may obtain a copy of the Apache License at
15 //
16 //     http://www.apache.org/licenses/LICENSE-2.0
17 //
18 // Unless required by applicable law or agreed to in writing, software
19 // distributed under the Apache License with the above modification is
20 // distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
21 // KIND, either express or implied. See the Apache License for the specific
22 // language governing permissions and limitations under the Apache License.
23 //
24 #include "pxr/pxr.h"
25 #include "pxr/usd/plugin/usdAbc/alembicFileFormat.h"
26 
27 #include "pxr/usd/plugin/usdAbc/alembicData.h"
28 #include "pxr/usd/usd/usdaFileFormat.h"
29 
30 #include "pxr/usd/sdf/layer.h"
31 
32 #include "pxr/base/trace/trace.h"
33 
34 #include "pxr/base/tf/fileUtils.h"
35 #include "pxr/base/tf/pathUtils.h"
36 #include "pxr/base/tf/registryManager.h"
37 #include "pxr/base/tf/staticData.h"
38 
39 #include <boost/assign.hpp>
40 #include <ostream>
41 
42 PXR_NAMESPACE_OPEN_SCOPE
43 
44 
45 using std::string;
46 
47 TF_DEFINE_PUBLIC_TOKENS(
48     UsdAbcAlembicFileFormatTokens,
49     USDABC_ALEMBIC_FILE_FORMAT_TOKENS);
50 
TF_REGISTRY_FUNCTION(TfType)51 TF_REGISTRY_FUNCTION(TfType)
52 {
53     SDF_DEFINE_FILE_FORMAT(UsdAbcAlembicFileFormat, SdfFileFormat);
54 }
55 
56 
UsdAbcAlembicFileFormat()57 UsdAbcAlembicFileFormat::UsdAbcAlembicFileFormat()
58     : SdfFileFormat(
59         UsdAbcAlembicFileFormatTokens->Id,
60         UsdAbcAlembicFileFormatTokens->Version,
61         UsdAbcAlembicFileFormatTokens->Target,
62         UsdAbcAlembicFileFormatTokens->Id),
63     _usda(SdfFileFormat::FindById(UsdUsdaFileFormatTokens->Id))
64 {
65 }
66 
~UsdAbcAlembicFileFormat()67 UsdAbcAlembicFileFormat::~UsdAbcAlembicFileFormat()
68 {
69 }
70 
71 SdfAbstractDataRefPtr
InitData(const FileFormatArguments & args) const72 UsdAbcAlembicFileFormat::InitData(const FileFormatArguments& args) const
73 {
74     return UsdAbc_AlembicData::New(args);
75 }
76 
77 bool
CanRead(const string & filePath) const78 UsdAbcAlembicFileFormat::CanRead(const string& filePath) const
79 {
80     // XXX: Add more verification of file header magic
81     auto extension = TfGetExtension(filePath);
82     if (extension.empty()) {
83         return false;
84     }
85 
86     return extension == this->GetFormatId();
87 }
88 
89 bool
Read(SdfLayer * layer,const string & resolvedPath,bool metadataOnly) const90 UsdAbcAlembicFileFormat::Read(
91     SdfLayer* layer,
92     const string& resolvedPath,
93     bool metadataOnly) const
94 {
95     TRACE_FUNCTION();
96 
97     SdfAbstractDataRefPtr data = InitData(layer->GetFileFormatArguments());
98     UsdAbc_AlembicDataRefPtr abcData = TfStatic_cast<UsdAbc_AlembicDataRefPtr>(data);
99     if (!abcData->Open(resolvedPath)) {
100         return false;
101     }
102 
103     _SetLayerData(layer, data);
104     return true;
105 }
106 
107 bool
WriteToFile(const SdfLayer & layer,const std::string & filePath,const std::string & comment,const FileFormatArguments & args) const108 UsdAbcAlembicFileFormat::WriteToFile(
109     const SdfLayer& layer,
110     const std::string& filePath,
111     const std::string& comment,
112     const FileFormatArguments& args) const
113 {
114     // Write.
115     SdfAbstractDataConstPtr data = _GetLayerData(layer);
116     return TF_VERIFY(data) && UsdAbc_AlembicData::Write(data, filePath, comment);
117 }
118 
119 bool
ReadFromString(SdfLayer * layer,const std::string & str) const120 UsdAbcAlembicFileFormat::ReadFromString(
121     SdfLayer* layer,
122     const std::string& str) const
123 {
124     // XXX: For now, defer to the usda file format for this. May need to
125     //      revisit this as the alembic reader gets fully fleshed out.
126     return _usda->ReadFromString(layer, str);
127 }
128 
129 bool
WriteToString(const SdfLayer & layer,std::string * str,const std::string & comment) const130 UsdAbcAlembicFileFormat::WriteToString(
131     const SdfLayer& layer,
132     std::string* str,
133     const std::string& comment) const
134 {
135     // XXX: For now, defer to the usda file format for this. May need to
136     //      revisit this as the alembic reader gets fully fleshed out.
137     return _usda->WriteToString(layer, str, comment);
138 }
139 
140 bool
WriteToStream(const SdfSpecHandle & spec,std::ostream & out,size_t indent) const141 UsdAbcAlembicFileFormat::WriteToStream(
142     const SdfSpecHandle &spec,
143     std::ostream& out,
144     size_t indent) const
145 {
146     // XXX: Because WriteToString() uses the usda file format and because
147     //      a spec will always use it's own file format for writing we'll
148     //      get here trying to write an Alembic layer as usda.  So we
149     //      turn around call usda.
150     return _usda->WriteToStream(spec, out, indent);
151 }
152 
153 PXR_NAMESPACE_CLOSE_SCOPE
154 
155