-
-
Notifications
You must be signed in to change notification settings - Fork 873
Create USD Exporter #2009
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Open
servantftransperfect
wants to merge
1
commit into
develop
Choose a base branch
from
dev/UsdExporter
base: develop
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
+762
−314
Open
Create USD Exporter #2009
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,44 @@ | ||
| __version__ = "1.0" | ||
|
|
||
| from meshroom.core import desc | ||
| from meshroom.core.utils import VERBOSE_LEVEL | ||
|
|
||
|
|
||
| class ExportMeshUSD(desc.AVCommandLineNode): | ||
| commandLine = "aliceVision_exportMeshUSD {allParams}" | ||
| size = desc.DynamicNodeSize("input") | ||
|
|
||
| category = "Export" | ||
| documentation = """ Export a mesh (OBJ file) to USD format. """ | ||
|
|
||
| inputs = [ | ||
| desc.File( | ||
| name="input", | ||
| label="Input", | ||
| description="Input mesh file.", | ||
| value="", | ||
| ), | ||
| desc.ChoiceParam( | ||
| name="fileType", | ||
| label="USD File Format", | ||
| description="Output USD file format.", | ||
| value="usda", | ||
| values=["usda", "usdc", "usdz"] | ||
| ), | ||
| desc.ChoiceParam( | ||
| name="verboseLevel", | ||
| label="Verbose Level", | ||
| description="Verbosity level (fatal, error, warning, info, debug, trace).", | ||
| values=VERBOSE_LEVEL, | ||
| value="info", | ||
| ), | ||
| ] | ||
|
|
||
| outputs = [ | ||
| desc.File( | ||
| name="output", | ||
| label="Output", | ||
| description="Path to the output file.", | ||
| value="{nodeCacheFolder}/output.{fileTypeValue}", | ||
| ), | ||
| ] |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,134 @@ | ||
| // This file is part of the AliceVision project. | ||
| // Copyright (c) 2025 AliceVision contributors. | ||
| // This Source Code Form is subject to the terms of the Mozilla Public License, | ||
| // v. 2.0. If a copy of the MPL was not distributed with this file, | ||
| // You can obtain one at https://mozilla.org/MPL/2.0/. | ||
|
|
||
| #include <aliceVision/sfmDataIO/UsdExporter.hpp> | ||
| #include <aliceVision/camera/IntrinsicScaleOffset.hpp> | ||
| #include <aliceVision/system/Logger.hpp> | ||
|
|
||
| #include <pxr/usd/usd/stage.h> | ||
|
|
||
| #include <pxr/usd/usdGeom/xform.h> | ||
| #include <pxr/usd/usdGeom/points.h> | ||
| #include <pxr/usd/usdGeom/camera.h> | ||
|
|
||
| #include <pxr/base/gf/vec3f.h> | ||
| #include <pxr/base/vt/array.h> | ||
|
|
||
|
|
||
| PXR_NAMESPACE_USING_DIRECTIVE | ||
|
|
||
| namespace aliceVision { | ||
| namespace sfmDataIO { | ||
|
|
||
|
|
||
| UsdExporter::UsdExporter(const std::string & filename, double frameRate) | ||
| { | ||
| _stage = UsdStage::CreateNew(filename); | ||
| if (_stage) | ||
| { | ||
| ALICEVISION_THROW_ERROR("UsdStage::CreateNew failed"); | ||
| } | ||
|
|
||
| UsdGeomXform worldPrim = UsdGeomXform::Define(_stage, SdfPath("/World")); | ||
|
|
||
| _stage->SetTimeCodesPerSecond(frameRate); | ||
| _stage->SetFramesPerSecond(frameRate); | ||
| _startTimeCode = std::numeric_limits<IndexT>::max(); | ||
| _endTimeCode = 0; | ||
| } | ||
servantftransperfect marked this conversation as resolved.
Show resolved
Hide resolved
|
||
|
|
||
| void UsdExporter::terminate() | ||
| { | ||
| // If no frames have been exported, _startTimeCode will still be at its sentinel value. | ||
| // In that case, avoid writing invalid start/end time codes to the USD stage. | ||
| if (_startTimeCode == std::numeric_limits<IndexT>::max()) | ||
| { | ||
| ALICEVISION_LOG_WARNING("UsdExporter::terminate called but no frames have been exported; " | ||
| "skipping start/end time code settings."); | ||
| } | ||
| else | ||
| { | ||
| _stage->SetStartTimeCode(_startTimeCode); | ||
| _stage->SetEndTimeCode(_endTimeCode); | ||
| } | ||
|
|
||
| _stage->Save(); | ||
| } | ||
|
|
||
| void UsdExporter::createNewCamera(const std::string & cameraName) | ||
| { | ||
| SdfPath cameraPath("/World/" + cameraName); | ||
| UsdGeomCamera camera = UsdGeomCamera::Define(_stage, cameraPath); | ||
|
|
||
| UsdAttribute projectionAttr = camera.GetProjectionAttr(); | ||
| projectionAttr.Set(UsdGeomTokens->perspective); | ||
|
|
||
| UsdGeomXformable xformable(camera); | ||
| UsdGeomXformOp motion = xformable.MakeMatrixXform(); | ||
| GfMatrix4d identity(1.0); | ||
| motion.Set(identity); | ||
| } | ||
|
|
||
| void UsdExporter::addFrame(const std::string & cameraName, const sfmData::CameraPose & pose, const camera::Pinhole & intrinsic, IndexT frameId) | ||
| { | ||
| SdfPath cameraPath("/World/" + cameraName); | ||
| UsdGeomCamera camera = UsdGeomCamera::Get(_stage, cameraPath); | ||
|
|
||
| _startTimeCode = std::min(_startTimeCode, frameId); | ||
| _endTimeCode = std::max(_endTimeCode, frameId); | ||
|
|
||
| UsdAttribute focalLengthAttr = camera.GetFocalLengthAttr(); | ||
| UsdAttribute horizontalApertureAttr = camera.GetHorizontalApertureAttr(); | ||
| UsdAttribute verticalApertureAttr = camera.GetVerticalApertureAttr(); | ||
| UsdAttribute horizontalApertureOffsetAttr = camera.GetHorizontalApertureOffsetAttr(); | ||
| UsdAttribute verticalApertureOffsetAttr = camera.GetVerticalApertureOffsetAttr(); | ||
|
|
||
| horizontalApertureAttr.Set(static_cast<float>(intrinsic.sensorWidth())); | ||
| verticalApertureAttr.Set(static_cast<float>(intrinsic.sensorHeight())); | ||
|
|
||
| UsdTimeCode t(frameId); | ||
| double pixToMillimeters = intrinsic.sensorWidth() / intrinsic.w(); | ||
|
|
||
| horizontalApertureOffsetAttr.Set(static_cast<float>(intrinsic.getOffset().x() * pixToMillimeters), t); | ||
| verticalApertureOffsetAttr.Set(static_cast<float>(intrinsic.getOffset().y() * pixToMillimeters), t); | ||
| focalLengthAttr.Set(static_cast<float>(intrinsic.getFocalLength()), t); | ||
|
|
||
|
|
||
|
|
||
| //Transform sfmData pose to usd pose | ||
| Eigen::Matrix4d glTransform = Eigen::Matrix4d::Identity(); | ||
| glTransform(1, 1) = -1.0; | ||
| glTransform(2, 2) = -1.0; | ||
|
|
||
| // Inverse the pose and change the geometric frame | ||
| Eigen::Matrix4d camera_T_world = pose.getTransform().getHomogeneous(); | ||
| Eigen::Matrix4d world_T_camera = camera_T_world.inverse(); | ||
| Eigen::Matrix4d world_gl_T_camera_gl = glTransform * world_T_camera * glTransform; | ||
|
|
||
| //Copy element by element while transposing | ||
| GfMatrix4d usdT; | ||
| for (int i = 0; i < 4; i++) | ||
| { | ||
| for (int j = 0; j < 4; j++) | ||
| { | ||
| usdT[j][i] = world_gl_T_camera_gl(i, j); | ||
| } | ||
| } | ||
|
|
||
| //Assign pose to motion | ||
| UsdGeomXformable xformable(camera); | ||
| bool dummy = false; | ||
| std::vector<UsdGeomXformOp> xformOps = xformable.GetOrderedXformOps(&dummy); | ||
|
|
||
|
|
||
| if (!xformOps.empty()) { | ||
| UsdGeomXformOp motion = xformOps[0]; | ||
| motion.Set(usdT, t); | ||
| } | ||
| } | ||
|
|
||
| } // namespace sfmDataIO | ||
| } // namespace aliceVision | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,72 @@ | ||
| // This file is part of the AliceVision project. | ||
| // Copyright (c) 2025 AliceVision contributors. | ||
| // This Source Code Form is subject to the terms of the Mozilla Public License, | ||
| // v. 2.0. If a copy of the MPL was not distributed with this file, | ||
| // You can obtain one at https://mozilla.org/MPL/2.0/. | ||
|
|
||
| #pragma once | ||
|
|
||
| #include <aliceVision/sfmData/SfMData.hpp> | ||
| #include <aliceVision/camera/Pinhole.hpp> | ||
| #include <pxr/usd/usd/stage.h> | ||
|
|
||
| namespace aliceVision { | ||
| namespace sfmDataIO { | ||
|
|
||
| /** | ||
| * @brief Export SfM data to USD (Universal Scene Description) format. | ||
| * | ||
| * The UsdExporter class provides functionality to export camera poses and intrinsics | ||
| * from AliceVision's SfM data to Pixar's USD format, enabling integration with | ||
| * USD-based pipelines and DCC applications. | ||
| */ | ||
| class UsdExporter | ||
| { | ||
| public: | ||
| /** | ||
| * @brief Construct a new USD Exporter. | ||
| * | ||
| * @param filename The output USD file path | ||
| * @param frameRate The frame rate for the animation timeline (e.g., 24.0 for 24 fps) | ||
| */ | ||
| UsdExporter(const std::string & filename, double frameRate); | ||
|
|
||
| /** | ||
| * @brief Create a new camera in the USD stage. | ||
| * | ||
| * Creates a USD camera primitive with the specified name. This camera | ||
| * can then be animated using addFrame(). | ||
| * | ||
| * @param cameraName The unique name for the camera in the USD stage | ||
| */ | ||
| void createNewCamera(const std::string & cameraName); | ||
|
|
||
| /** | ||
| * @brief Add a frame of camera data (pose and intrinsics) to an existing camera. | ||
| * | ||
| * Records the camera's pose and intrinsic parameters at a specific frame, | ||
| * creating keyframe animation data in the USD file. | ||
| * | ||
| * @param cameraName The name of the camera to add the frame to | ||
| * @param pose The camera pose (position and orientation) for this frame | ||
| * @param camera The pinhole camera model containing intrinsic parameters | ||
| * @param frameId The frame index/time code for this keyframe | ||
| */ | ||
| void addFrame(const std::string & cameraName, const sfmData::CameraPose & pose, const camera::Pinhole & camera, IndexT frameId); | ||
|
|
||
| /** | ||
| * @brief Finalize and save the USD file. | ||
| * | ||
| * Completes the USD export process and writes the data to disk. | ||
| * Should be called after all cameras and frames have been added. | ||
| */ | ||
| void terminate(); | ||
|
|
||
| private: | ||
| pxr::UsdStageRefPtr _stage; ///< USD stage containing the scene hierarchy | ||
| IndexT _startTimeCode; ///< First frame index in the animation timeline | ||
| IndexT _endTimeCode; ///< Last frame index in the animation timeline | ||
| }; | ||
|
|
||
| } // namespace sfmDataIO | ||
| } // namespace aliceVision |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.