From 83d2ae540e78681c281628570c67981992c3acdd Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Micha=C5=82=20Pe=C5=82ka?= Date: Tue, 29 Apr 2025 17:03:23 +0200 Subject: [PATCH] Added possibility to load USD from command line MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Signed-off-by: Michał Pełka --- CMakeLists.txt | 2 +- src/main.cpp | 8 +++++++- src/mainwindow.cpp | 27 +++++++++++++++++++++++++-- src/mainwindow.h | 11 ++++++++++- src/views/usdsessionlayer.cpp | 5 +++-- src/views/usdsessionlayer.h | 14 ++++++++------ 6 files changed, 54 insertions(+), 13 deletions(-) diff --git a/CMakeLists.txt b/CMakeLists.txt index 6c1a36d..9fff966 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -6,7 +6,7 @@ include(GNUInstallDirs) set(CMAKE_CXX_STANDARD 17) set(CMAKE_CXX_STANDARD_REQUIRED ON) - +find_package(X11) find_package(pxr REQUIRED) find_package(OpenGL REQUIRED) diff --git a/src/main.cpp b/src/main.cpp index d612ad7..2a43e0c 100644 --- a/src/main.cpp +++ b/src/main.cpp @@ -149,7 +149,13 @@ int main(int argc, char** argv) glEnable(GL_DEPTH_TEST); pxr::Model model; - pxr::MainWindow mainWindow(&model); + std::vector params; + for (int i =0; i < argc; i++) + { + params.emplace_back(argv[i]); + } + + pxr::MainWindow mainWindow(&model, params); while (!glfwWindowShouldClose(window)) { glfwPollEvents(); diff --git a/src/mainwindow.cpp b/src/mainwindow.cpp index b506de0..e4c0ce6 100644 --- a/src/mainwindow.cpp +++ b/src/mainwindow.cpp @@ -7,12 +7,23 @@ #include "views/usdsessionlayer.h" #include "views/view.h" #include "views/viewport.h" - +#include +#include PXR_NAMESPACE_OPEN_SCOPE -MainWindow::MainWindow(Model* model) : _model(model) +MainWindow::MainWindow(Model* model, const std::vector& params) : _model(model) { ResetDefaultViews(); + if (params.size()>1) + { + const auto & usdFileName = params[1]; + auto views = GetViewsOfType(UsdSessionLayer::VIEW_TYPE); + assert(!views.empty()); + UsdSessionLayer* sessionLayerPtr = dynamic_cast(views.front()); + assert(sessionLayerPtr); + std::cout << "Loading "<< usdFileName << std::endl; + sessionLayerPtr->LoadUsdStage(usdFileName); + } }; void MainWindow::Update() @@ -89,4 +100,16 @@ void MainWindow::AddView(const string viewType) } } +vector MainWindow::GetViewsOfType(const string viewType) +{ + vector viewptrs; + for (auto &view:_views){ + if (view->GetViewType() == viewType) + { + viewptrs.emplace_back(view); + } + } + return viewptrs; +} + PXR_NAMESPACE_CLOSE_SCOPE diff --git a/src/mainwindow.h b/src/mainwindow.h index 5d0b4be..a6c7097 100644 --- a/src/mainwindow.h +++ b/src/mainwindow.h @@ -29,8 +29,9 @@ class MainWindow { * @brief Construct a new Main Window object * * @param model the Model of the new Main Window + * @param application arguments */ - MainWindow(Model* model); + MainWindow(Model* model, const std::vector& args); /** * @brief Update the draw call of the main window @@ -53,7 +54,15 @@ class MainWindow { */ void AddView(const string viewType); + /** + * @brief Gets views of given type + * + * @param viewType the type of the retrieved view + */ + vector GetViewsOfType(const string viewType); + private: + vector _views; Model* _model; }; diff --git a/src/views/usdsessionlayer.cpp b/src/views/usdsessionlayer.cpp index f0c58c7..6d1579d 100644 --- a/src/views/usdsessionlayer.cpp +++ b/src/views/usdsessionlayer.cpp @@ -34,6 +34,7 @@ UsdSessionLayer::UsdSessionLayer(Model* model, const string label) GetModel()->AddSceneIndexBase(sceneIndices.finalSceneIndex); _SetEmptyStage(); + } const string UsdSessionLayer::GetViewType() @@ -92,7 +93,7 @@ void UsdSessionLayer::_Draw() if (ImGuiFileDialog::Instance()->Display("LoadFile")) { if (ImGuiFileDialog::Instance()->IsOk()) { string filePath = ImGuiFileDialog::Instance()->GetFilePathName(); - _LoadUsdStage(filePath); + LoadUsdStage(filePath); } ImGuiFileDialog::Instance()->Close(); } @@ -121,7 +122,7 @@ void UsdSessionLayer::_SetEmptyStage() GetModel()->SetStage(_stage); } -void UsdSessionLayer::_LoadUsdStage(const string usdFilePath) +void UsdSessionLayer::LoadUsdStage(const string usdFilePath) { if (!ifstream(usdFilePath)) { TF_RUNTIME_ERROR( diff --git a/src/views/usdsessionlayer.h b/src/views/usdsessionlayer.h index 474c866..97d6d89 100644 --- a/src/views/usdsessionlayer.h +++ b/src/views/usdsessionlayer.h @@ -48,6 +48,13 @@ class UsdSessionLayer : public View { */ ImGuiWindowFlags _GetGizmoWindowFlags() override; + /** + * @brief Load a Usd Stage based on the given Usd file path + * + * @param usdFilePath a string containing a Usd file path + */ + void LoadUsdStage(const string usdFilePath); + private: TextEditor _editor; bool _isEditing; @@ -63,12 +70,7 @@ class UsdSessionLayer : public View { */ void _Draw() override; - /** - * @brief Load a Usd Stage based on the given Usd file path - * - * @param usdFilePath a string containing a Usd file path - */ - void _LoadUsdStage(const string usdFilePath); + /** * @brief Set the model to an empty stage