From 2ba02e332be2af51f5dd756c8f85f4dbcbc60b0e Mon Sep 17 00:00:00 2001 From: jiandaoshou-aidehua <2893666867@qq.com> Date: Fri, 5 Jun 2026 17:29:05 +0800 Subject: [PATCH 001/128] =?UTF-8?q?ubuntu=E7=B3=BB=E7=BB=9F=E6=89=93?= =?UTF-8?q?=E5=8C=85carla?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .github/workflows/build_linux.yml | 10 ++--- Unreal/CarlaUE4/CarlaUE4.uproject | 20 +-------- .../Plugins/Carla/Source/Carla/Carla.Build.cs | 18 +++----- .../Source/CarlaUE4/CarlaUE4.Build.cs | 5 ++- Unreal/CarlaUE4/Source/CarlaUE4/CarlaUE4.cpp | 11 ++++- Util/BuildTools/BuildUE4Plugins.sh | 44 +++++++++---------- 6 files changed, 46 insertions(+), 62 deletions(-) diff --git a/.github/workflows/build_linux.yml b/.github/workflows/build_linux.yml index d543777045..6e63744345 100644 --- a/.github/workflows/build_linux.yml +++ b/.github/workflows/build_linux.yml @@ -49,11 +49,11 @@ jobs: make CarlaUE4Editor ARGS="--chrono -j$(nproc)" shell: bash - # - name: package - #run: | - # source ./setEnv64.sh - # make package ARGS="--chrono" - # shell: bash + - name: package + run: | + source ./setEnv64.sh + make package ARGS="--chrono" + shell: bash diff --git a/Unreal/CarlaUE4/CarlaUE4.uproject b/Unreal/CarlaUE4/CarlaUE4.uproject index 056d38b3d0..7540f20876 100644 --- a/Unreal/CarlaUE4/CarlaUE4.uproject +++ b/Unreal/CarlaUE4/CarlaUE4.uproject @@ -1,6 +1,6 @@ { "FileVersion": 3, - "EngineAssociation": "{E0A51DA7-4769-9458-687B-4680AAEBFD72}", + "EngineAssociation": "{FF5B26C3-46CF-C440-D9FB-F48D5F122826}", "Category": "", "Description": "", "Modules": [ @@ -95,7 +95,7 @@ }, { "Name": "SteamVR", - "Enabled": true, + "Enabled": false, "WhitelistPlatforms": [ "Win32", "Win64", @@ -152,22 +152,6 @@ { "Name": "AirSim", "Enabled": true - }, - { - "Name": "UnrealRoboticsLab", - "Enabled": true - }, - { - "Name": "GLTFImporter", - "Enabled": true - }, - { - "Name": "FunctionalTestingEditor", - "Enabled": true - }, - { - "Name": "RuntimeTests", - "Enabled": true } ] } \ No newline at end of file diff --git a/Unreal/CarlaUE4/Plugins/Carla/Source/Carla/Carla.Build.cs b/Unreal/CarlaUE4/Plugins/Carla/Source/Carla/Carla.Build.cs index b8d7b93905..c83dec4291 100644 --- a/Unreal/CarlaUE4/Plugins/Carla/Source/Carla/Carla.Build.cs +++ b/Unreal/CarlaUE4/Plugins/Carla/Source/Carla/Carla.Build.cs @@ -18,14 +18,12 @@ private bool IsWindows(ReadOnlyTargetRules Target) public Carla(ReadOnlyTargetRules Target) : base(Target) { PrivatePCHHeaderFile = "Carla.h"; - - bool HasCustomGBufferView = File.Exists(Path.Combine(EngineDirectory, "Source", "Runtime", "Renderer", "Public", "GBufferView.h")); - + // 关键:强制开启 C++ 异常支持,以兼容 Boost 和 DReyeVR + bEnableExceptions = true; if (IsWindows(Target)) { - bEnableExceptions = true; + bEnableExceptions = true; } - // Read config about carsim string CarlaPluginPath = Path.GetFullPath( ModuleDirectory ); string ConfigDir = Path.GetFullPath(Path.Combine(CarlaPluginPath, "../../../../Config/")); @@ -73,7 +71,6 @@ public Carla(ReadOnlyTargetRules Target) : base(Target) PrivateIncludePaths.AddRange( new string[] { // ... add other private include paths required here ... - Path.Combine(EngineDirectory, "Source", "Runtime", "Engine", "Private") } ); @@ -140,7 +137,6 @@ public Carla(ReadOnlyTargetRules Target) : base(Target) ); AddCarlaServerDependency(Target); - PublicDefinitions.Add(HasCustomGBufferView ? "CARLA_HAS_ENGINE_GBUFFER_VIEW=1" : "CARLA_HAS_ENGINE_GBUFFER_VIEW=0"); } private bool UseDebugLibs(ReadOnlyTargetRules Target) @@ -182,7 +178,6 @@ private void AddBoostLibs(string LibPath) private void AddCarlaServerDependency(ReadOnlyTargetRules Target) { string LibCarlaInstallPath = Path.GetFullPath(Path.Combine(ModuleDirectory, "../../CarlaDependencies")); - ADelegate GetLibName = (string BaseName) => { if (IsWindows(Target)) { @@ -227,7 +222,6 @@ private void AddCarlaServerDependency(ReadOnlyTargetRules Target) PublicAdditionalLibraries.Add(Path.Combine(LibCarlaInstallPath, "lib", "proj.lib")); PublicAdditionalLibraries.Add(Path.Combine(LibCarlaInstallPath, "lib", "osm2odr.lib")); PublicAdditionalLibraries.Add(Path.Combine(LibCarlaInstallPath, "lib", "zlibstatic.lib")); - } else { @@ -323,7 +317,7 @@ private void AddCarlaServerDependency(ReadOnlyTargetRules Target) PublicAdditionalLibraries.Add("stdc++"); PublicAdditionalLibraries.Add("/usr/lib/x86_64-linux-gnu/libpython3.9.so"); } - + if (UsingRos2) { PublicAdditionalLibraries.Add(Path.Combine(LibCarlaInstallPath, "lib", GetLibName("carla_fastdds"))); @@ -343,13 +337,11 @@ private void AddCarlaServerDependency(ReadOnlyTargetRules Target) } bEnableExceptions = true; - // Include path. string LibCarlaIncludePath = Path.Combine(LibCarlaInstallPath, "include"); PublicIncludePaths.Add(LibCarlaIncludePath); PrivateIncludePaths.Add(LibCarlaIncludePath); - PublicDefinitions.Add("ASIO_NO_EXCEPTIONS"); PublicDefinitions.Add("BOOST_NO_EXCEPTIONS"); PublicDefinitions.Add("LIBCARLA_NO_EXCEPTIONS"); @@ -357,4 +349,4 @@ private void AddCarlaServerDependency(ReadOnlyTargetRules Target) PublicDefinitions.Add("BOOST_DISABLE_ABI_HEADERS"); PublicDefinitions.Add("BOOST_TYPE_INDEX_FORCE_NO_RTTI_COMPATIBILITY"); } -} +} \ No newline at end of file diff --git a/Unreal/CarlaUE4/Source/CarlaUE4/CarlaUE4.Build.cs b/Unreal/CarlaUE4/Source/CarlaUE4/CarlaUE4.Build.cs index cc8b88df99..471fc2a528 100644 --- a/Unreal/CarlaUE4/Source/CarlaUE4/CarlaUE4.Build.cs +++ b/Unreal/CarlaUE4/Source/CarlaUE4/CarlaUE4.Build.cs @@ -14,7 +14,8 @@ public CarlaUE4(ReadOnlyTargetRules Target) : base(Target) { PrivatePCHHeaderFile = "CarlaUE4.h"; ShadowVariableWarningLevel = WarningLevel.Off; // -Wno-shadow - + // 关键:强制开启 C++ 异常支持,以兼容 Boost 和 DReyeVR + bEnableExceptions = true; // include LibCarla so we can #include headers string LibCarlaIncludePath = Path.GetFullPath(Path.Combine(ModuleDirectory, "..", "..", "..", "..", "LibCarla", "source")); @@ -95,4 +96,4 @@ public CarlaUE4(ReadOnlyTargetRules Target) : base(Target) PrivateDependencyModuleNames.AddRange(new string[] { "ImageWriteQueue" }); } -} +} \ No newline at end of file diff --git a/Unreal/CarlaUE4/Source/CarlaUE4/CarlaUE4.cpp b/Unreal/CarlaUE4/Source/CarlaUE4/CarlaUE4.cpp index 53af611e2e..47b4546a3b 100644 --- a/Unreal/CarlaUE4/Source/CarlaUE4/CarlaUE4.cpp +++ b/Unreal/CarlaUE4/Source/CarlaUE4/CarlaUE4.cpp @@ -1,7 +1,14 @@ // Fill out your copyright notice in the Description page of Project Settings. - #include "CarlaUE4.h" IMPLEMENT_PRIMARY_GAME_MODULE( FDefaultGameModuleImpl, CarlaUE4, "CarlaUE4" ); - +#ifdef __linux__ +#include +namespace std { + void __throw_out_of_range_fmt(const char* fmt, ...) { + // 直接调用底层终止函数,骗过链接器即可,绝不使用 throw + abort(); + } +} +#endif DEFINE_LOG_CATEGORY(LogDReyeVR); diff --git a/Util/BuildTools/BuildUE4Plugins.sh b/Util/BuildTools/BuildUE4Plugins.sh index 04b04c56ab..7b1832504d 100755 --- a/Util/BuildTools/BuildUE4Plugins.sh +++ b/Util/BuildTools/BuildUE4Plugins.sh @@ -30,7 +30,7 @@ CARLA_STREETMAP_PLUGINS_PATH="$CARLA_PLUGINS_PATH/StreetMap" AIR_PLUGIN_PATH="$CARLA_PLUGINS_PATH/AirSim" AIR_BUILD_PATH="$ROOT_PATH/Build/AirSim" -#CONTENT_PATH="$ROOT_PATH/Unreal/CarlaUE4/Content" +CONTENT_PATH="$ROOT_PATH/Unreal/CarlaUE4/Content" OPTS=`getopt -o h --long build,rebuild,clean,chrono,chrono-path: -n 'parse-options' -- "$@"` @@ -96,26 +96,26 @@ fi # download assets -#if [[ ! -d "$CONTENT_PATH" ]]; then -# echo "$FILE_N Content directory: \"$CONTENT_PATH\"" - -# if [[ -d "$CACHE_DIR/Content" ]]; then -# # <==> xcopy /q /Y /S /I -# cp -a "$CACHE_DIR/Content/." "$CONTENT_PATH/" -# else -# git clone https://OpenHUTB:T8w6TYB_r71gGTP3A02B@git.code.tencent.com/OpenHUTB/Content.git "$CONTENT_PATH" -# cd "$CONTENT_PATH" -# git lfs pull -# fi - -#else -# echo "$FILE_N Content directory already exists: \"$CONTENT_PATH\", executing git pull." -# cd "$CONTENT_PATH" -# git fetch --all -# git reset --hard origin/master -# git pull -# git lfs pull -#fi +if [[ ! -d "$CONTENT_PATH" ]]; then + echo "$FILE_N Content directory: \"$CONTENT_PATH\"" + + if [[ -d "$CACHE_DIR/Content" ]]; then + # <==> xcopy /q /Y /S /I + cp -a "$CACHE_DIR/Content/." "$CONTENT_PATH/" + else + git clone https://OpenHUTB:T8w6TYB_r71gGTP3A02B@git.code.tencent.com/OpenHUTB/Content.git "$CONTENT_PATH" + cd "$CONTENT_PATH" + git lfs pull + fi + +else + echo "$FILE_N Content directory already exists: \"$CONTENT_PATH\", executing git pull." + cd "$CONTENT_PATH" + git fetch --all + git reset --hard origin/master + git pull + git lfs pull +fi if ${BUILD_STREETMAP} ; then @@ -162,4 +162,4 @@ fi -log "StreetMap Success!" +log "StreetMap Success!" \ No newline at end of file From 9e66a462031dabc6c1e50cd725cfc9b3c62b66c7 Mon Sep 17 00:00:00 2001 From: jiandaoshou-aidehua <2893666867@qq.com> Date: Mon, 8 Jun 2026 13:54:08 +0800 Subject: [PATCH 002/128] =?UTF-8?q?=E4=BF=AE=E6=94=B9=E5=AF=B9=E5=BA=94?= =?UTF-8?q?=E6=96=87=E4=BB=B6?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- Unreal/CarlaUE4/CarlaUE4.uproject | 16 ++++++++++++++++ .../Plugins/Carla/Source/Carla/Carla.Build.cs | 14 ++++++++++++-- .../CarlaUE4/Source/CarlaUE4/CarlaUE4.Build.cs | 3 ++- Unreal/CarlaUE4/Source/CarlaUE4/CarlaUE4.cpp | 1 + 4 files changed, 31 insertions(+), 3 deletions(-) diff --git a/Unreal/CarlaUE4/CarlaUE4.uproject b/Unreal/CarlaUE4/CarlaUE4.uproject index 7540f20876..0ef9dbda48 100644 --- a/Unreal/CarlaUE4/CarlaUE4.uproject +++ b/Unreal/CarlaUE4/CarlaUE4.uproject @@ -152,6 +152,22 @@ { "Name": "AirSim", "Enabled": true + }, + { + "Name": "UnrealRoboticsLab", + "Enabled": true + }, + { + "Name": "GLTFImporter", + "Enabled": true + }, + { + "Name": "FunctionalTestingEditor", + "Enabled": true + }, + { + "Name": "RuntimeTests", + "Enabled": true } ] } \ No newline at end of file diff --git a/Unreal/CarlaUE4/Plugins/Carla/Source/Carla/Carla.Build.cs b/Unreal/CarlaUE4/Plugins/Carla/Source/Carla/Carla.Build.cs index c83dec4291..88e65a57e6 100644 --- a/Unreal/CarlaUE4/Plugins/Carla/Source/Carla/Carla.Build.cs +++ b/Unreal/CarlaUE4/Plugins/Carla/Source/Carla/Carla.Build.cs @@ -18,12 +18,16 @@ private bool IsWindows(ReadOnlyTargetRules Target) public Carla(ReadOnlyTargetRules Target) : base(Target) { PrivatePCHHeaderFile = "Carla.h"; + + bool HasCustomGBufferView = File.Exists(Path.Combine(EngineDirectory, "Source", "Runtime", "Renderer", "Public", "GBufferView.h")); + // 关键:强制开启 C++ 异常支持,以兼容 Boost 和 DReyeVR bEnableExceptions = true; if (IsWindows(Target)) { - bEnableExceptions = true; + bEnableExceptions = true; } + // Read config about carsim string CarlaPluginPath = Path.GetFullPath( ModuleDirectory ); string ConfigDir = Path.GetFullPath(Path.Combine(CarlaPluginPath, "../../../../Config/")); @@ -71,6 +75,7 @@ public Carla(ReadOnlyTargetRules Target) : base(Target) PrivateIncludePaths.AddRange( new string[] { // ... add other private include paths required here ... + Path.Combine(EngineDirectory, "Source", "Runtime", "Engine", "Private") } ); @@ -137,6 +142,7 @@ public Carla(ReadOnlyTargetRules Target) : base(Target) ); AddCarlaServerDependency(Target); + PublicDefinitions.Add(HasCustomGBufferView ? "CARLA_HAS_ENGINE_GBUFFER_VIEW=1" : "CARLA_HAS_ENGINE_GBUFFER_VIEW=0"); } private bool UseDebugLibs(ReadOnlyTargetRules Target) @@ -178,6 +184,7 @@ private void AddBoostLibs(string LibPath) private void AddCarlaServerDependency(ReadOnlyTargetRules Target) { string LibCarlaInstallPath = Path.GetFullPath(Path.Combine(ModuleDirectory, "../../CarlaDependencies")); + ADelegate GetLibName = (string BaseName) => { if (IsWindows(Target)) { @@ -222,6 +229,7 @@ private void AddCarlaServerDependency(ReadOnlyTargetRules Target) PublicAdditionalLibraries.Add(Path.Combine(LibCarlaInstallPath, "lib", "proj.lib")); PublicAdditionalLibraries.Add(Path.Combine(LibCarlaInstallPath, "lib", "osm2odr.lib")); PublicAdditionalLibraries.Add(Path.Combine(LibCarlaInstallPath, "lib", "zlibstatic.lib")); + } else { @@ -317,7 +325,7 @@ private void AddCarlaServerDependency(ReadOnlyTargetRules Target) PublicAdditionalLibraries.Add("stdc++"); PublicAdditionalLibraries.Add("/usr/lib/x86_64-linux-gnu/libpython3.9.so"); } - + if (UsingRos2) { PublicAdditionalLibraries.Add(Path.Combine(LibCarlaInstallPath, "lib", GetLibName("carla_fastdds"))); @@ -337,11 +345,13 @@ private void AddCarlaServerDependency(ReadOnlyTargetRules Target) } bEnableExceptions = true; + // Include path. string LibCarlaIncludePath = Path.Combine(LibCarlaInstallPath, "include"); PublicIncludePaths.Add(LibCarlaIncludePath); PrivateIncludePaths.Add(LibCarlaIncludePath); + PublicDefinitions.Add("ASIO_NO_EXCEPTIONS"); PublicDefinitions.Add("BOOST_NO_EXCEPTIONS"); PublicDefinitions.Add("LIBCARLA_NO_EXCEPTIONS"); diff --git a/Unreal/CarlaUE4/Source/CarlaUE4/CarlaUE4.Build.cs b/Unreal/CarlaUE4/Source/CarlaUE4/CarlaUE4.Build.cs index 471fc2a528..3057e5789a 100644 --- a/Unreal/CarlaUE4/Source/CarlaUE4/CarlaUE4.Build.cs +++ b/Unreal/CarlaUE4/Source/CarlaUE4/CarlaUE4.Build.cs @@ -14,7 +14,8 @@ public CarlaUE4(ReadOnlyTargetRules Target) : base(Target) { PrivatePCHHeaderFile = "CarlaUE4.h"; ShadowVariableWarningLevel = WarningLevel.Off; // -Wno-shadow - // 关键:强制开启 C++ 异常支持,以兼容 Boost 和 DReyeVR + + // 关键:强制开启 C++ 异常支持,以兼容 Boost 和 DReyeVR bEnableExceptions = true; // include LibCarla so we can #include headers string LibCarlaIncludePath = diff --git a/Unreal/CarlaUE4/Source/CarlaUE4/CarlaUE4.cpp b/Unreal/CarlaUE4/Source/CarlaUE4/CarlaUE4.cpp index 47b4546a3b..044ee2618b 100644 --- a/Unreal/CarlaUE4/Source/CarlaUE4/CarlaUE4.cpp +++ b/Unreal/CarlaUE4/Source/CarlaUE4/CarlaUE4.cpp @@ -1,4 +1,5 @@ // Fill out your copyright notice in the Description page of Project Settings. + #include "CarlaUE4.h" IMPLEMENT_PRIMARY_GAME_MODULE( FDefaultGameModuleImpl, CarlaUE4, "CarlaUE4" ); From 3396b14bc90347c8534544ad1a0ade1e35ccb0b2 Mon Sep 17 00:00:00 2001 From: jiandaoshou-aidehua <2893666867@qq.com> Date: Mon, 8 Jun 2026 14:13:29 +0800 Subject: [PATCH 003/128] =?UTF-8?q?=E5=AF=B9=E9=BD=90=E7=BC=A9=E8=BF=9B?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .github/workflows/build_linux.yml | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/.github/workflows/build_linux.yml b/.github/workflows/build_linux.yml index 6e63744345..23d7527970 100644 --- a/.github/workflows/build_linux.yml +++ b/.github/workflows/build_linux.yml @@ -49,10 +49,10 @@ jobs: make CarlaUE4Editor ARGS="--chrono -j$(nproc)" shell: bash - - name: package + - name: package run: | - source ./setEnv64.sh - make package ARGS="--chrono" + source ./setEnv64.sh + make package ARGS="--chrono" shell: bash From 1be6ed3e7a6d5bd7267633015e749645c7564bcb Mon Sep 17 00:00:00 2001 From: jiandaoshou-aidehua <2893666867@qq.com> Date: Mon, 8 Jun 2026 14:16:35 +0800 Subject: [PATCH 004/128] =?UTF-8?q?linux=20CI/CD=20=E8=87=AA=E5=8A=A8?= =?UTF-8?q?=E8=A7=A6=E5=8F=91?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .github/workflows/build_linux.yml | 1 + 1 file changed, 1 insertion(+) diff --git a/.github/workflows/build_linux.yml b/.github/workflows/build_linux.yml index 23d7527970..89f10854ef 100644 --- a/.github/workflows/build_linux.yml +++ b/.github/workflows/build_linux.yml @@ -1,6 +1,7 @@ name: hubt Linux CI/CD on: + pull_request: workflow_dispatch: From 93d63dcb07179517e49e51826b45eebd1e2eae82 Mon Sep 17 00:00:00 2001 From: jiandaoshou-aidehua <2893666867@qq.com> Date: Mon, 8 Jun 2026 15:43:04 +0800 Subject: [PATCH 005/128] =?UTF-8?q?=E4=B8=8E=E6=9C=8D=E5=8A=A1=E5=99=A8?= =?UTF-8?q?=E7=9A=84=E6=A0=87=E7=AD=BE=E7=9B=B8=E5=90=8C=E5=81=9A=E7=9A=84?= =?UTF-8?q?=E4=BF=AE=E6=94=B9?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .github/workflows/build_linux.yml | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/.github/workflows/build_linux.yml b/.github/workflows/build_linux.yml index 89f10854ef..8bd88474db 100644 --- a/.github/workflows/build_linux.yml +++ b/.github/workflows/build_linux.yml @@ -9,7 +9,7 @@ jobs: linux-dev: name: Linux CI/CD timeout-minutes: 1440 - runs-on: self-hosted:ubuntu-hutb + runs-on: [self-hosted, ubuntu-hutb] env: UE4_ROOT: /home/ubuntu/UnrealEngine_4.26 From e55b918565a881b594adae0c151b534b684f83d8 Mon Sep 17 00:00:00 2001 From: jiandaoshou-aidehua <2893666867@qq.com> Date: Mon, 8 Jun 2026 16:29:40 +0800 Subject: [PATCH 006/128] =?UTF-8?q?=E5=85=A8=E5=B1=80=E6=9B=BF=E6=8D=A2=20?= =?UTF-8?q?Git=20=E9=95=9C=E5=83=8F=E6=BA=90?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .github/workflows/build_linux.yml | 4 ++++ 1 file changed, 4 insertions(+) diff --git a/.github/workflows/build_linux.yml b/.github/workflows/build_linux.yml index 8bd88474db..7a64ed1f6e 100644 --- a/.github/workflows/build_linux.yml +++ b/.github/workflows/build_linux.yml @@ -19,6 +19,10 @@ jobs: uses: actions/checkout@v5 with: fetch-depth: 0 + + - name: Configure Git Mirror for GitHub + run: | + git config --global url."https://mirror.ghproxy.com/https://github.com/".insteadOf "https://github.com/" - name: Show files run: | From 3d2056dde76582e6b1539f2436808d687df48166 Mon Sep 17 00:00:00 2001 From: jiandaoshou-aidehua <2893666867@qq.com> Date: Tue, 9 Jun 2026 18:55:58 +0800 Subject: [PATCH 007/128] =?UTF-8?q?=E4=B8=8D=E4=BD=BF=E7=94=A8=E4=BB=A3?= =?UTF-8?q?=E7=90=86?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .github/workflows/build_linux.yml | 6 +----- 1 file changed, 1 insertion(+), 5 deletions(-) diff --git a/.github/workflows/build_linux.yml b/.github/workflows/build_linux.yml index 7a64ed1f6e..1af4afd829 100644 --- a/.github/workflows/build_linux.yml +++ b/.github/workflows/build_linux.yml @@ -19,11 +19,7 @@ jobs: uses: actions/checkout@v5 with: fetch-depth: 0 - - - name: Configure Git Mirror for GitHub - run: | - git config --global url."https://mirror.ghproxy.com/https://github.com/".insteadOf "https://github.com/" - + - name: Show files run: | pwd From fd2cd4928afb1730708ddaf0a48357d9750b1475 Mon Sep 17 00:00:00 2001 From: jiandaoshou-aidehua <2893666867@qq.com> Date: Wed, 10 Jun 2026 21:33:00 +0800 Subject: [PATCH 008/128] =?UTF-8?q?=E8=A7=A3=E5=86=B3/wd4103=E9=97=AE?= =?UTF-8?q?=E9=A2=98?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .github/workflows/build_linux.yml | 6 ++++++ 1 file changed, 6 insertions(+) diff --git a/.github/workflows/build_linux.yml b/.github/workflows/build_linux.yml index 1af4afd829..2b1f69a112 100644 --- a/.github/workflows/build_linux.yml +++ b/.github/workflows/build_linux.yml @@ -44,6 +44,12 @@ jobs: source ./Update.sh shell: bash + - name: Fix Windows Compiler Flags and Clean Cache + run: | + # 使用 ${{ github.workspace }} 动态获取当前 Runner 的工作目录 + find ${{ github.workspace }} -type f -name "*.cs" -exec sed -i 's#-wd4103##g' {} + + find ${{ github.workspace }} -type f -name "*.cs" -exec sed -i 's#/wd4103##g' {} + + - name: CarlaUE4Editor run: | source ./setEnv64.sh From 98e7dc1a1c84e53a4c8d140552ae43901390022c Mon Sep 17 00:00:00 2001 From: jiandaoshou-aidehua <2893666867@qq.com> Date: Wed, 10 Jun 2026 21:35:32 +0800 Subject: [PATCH 009/128] =?UTF-8?q?=E8=A7=A3=E5=86=B3LFS=E4=B8=8A=E4=BC=A0?= =?UTF-8?q?=E4=B8=8D=E4=BA=86=E7=9A=84=E9=97=AE=E9=A2=98?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- Util/BuildTools/Linux.mk | 3 ++- Util/download_from_git.py | 44 ++++++++++++++++++++++++++++++++++++--- 2 files changed, 43 insertions(+), 4 deletions(-) diff --git a/Util/BuildTools/Linux.mk b/Util/BuildTools/Linux.mk index 635137c581..9f034a8333 100644 --- a/Util/BuildTools/Linux.mk +++ b/Util/BuildTools/Linux.mk @@ -80,7 +80,8 @@ examples: run-examples: @for D in ${CARLA_EXAMPLES_FOLDER}/*; do [ -d "$${D}" ] && make -C $${D} run.only; done -CarlaUE4Editor: LibCarla.server.release osm2odr downloadplugins +# CarlaUE4Editor: LibCarla.server.release osm2odr downloadplugins +CarlaUE4Editor: @${CARLA_BUILD_TOOLS_FOLDER}/BuildUE4Plugins.sh --build $(ARGS) @${CARLA_BUILD_TOOLS_FOLDER}/BuildCarlaUE4.sh --build $(ARGS) diff --git a/Util/download_from_git.py b/Util/download_from_git.py index be480d3b79..40c6e85f62 100644 --- a/Util/download_from_git.py +++ b/Util/download_from_git.py @@ -482,9 +482,47 @@ def remove_readonly(func, path, _): ) if os.path.exists(os.path.join(rep_path, "*.dat")) is False: split_file( - os.path.join(local_path, "hutb.zip"), rep_path, 256 * 1024 * 1024 - ) # 每个小文件最大为 256MB - repo.commit_push_force(message=latest_file_name) + os.path.join(local_path, "hutb.zip"), rep_path, 5120 * 1024 * 1024 + ) # 每个小文件最大为 128MB + # repo.commit_push_force(message=latest_file_name) + + # ---------- 修复 504 超时:增量分批推送逻辑开始 ---------- + print("\nStarting incremental push to prevent 504 Gateway Timeout...") + + # 核心补充:强制本地初始化 LFS 追踪规则,防御 LFS 脱靶的情况 + print("Enforcing Git LFS tracking for .dat files...") + subprocess.run([git_exe, "-C", rep_path, "lfs", "track", "*.dat"]) + repo.repo.git.add(".gitattributes") # 必须把生成的规则文件先加进暂存区 + + # 获取所有切割好的 .dat 文件并严格按序号排序 + dat_files = sorted([f for f in os.listdir(rep_path) if f.endswith(".dat")]) + total_chunks = len(dat_files) + + for idx, file_name in enumerate(dat_files, 1): + print(f"\n>>> Processing chunk {idx}/{total_chunks}: {file_name}") + file_path = os.path.join(rep_path, file_name) + + # 1. 仅将当前单个分块加入暂存区,而不是全部加入 + repo.repo.git.add(file_path) + + # 2. 检查是否有实质性更改,防御空提交引发的系统崩溃(自动化测试核心原则) + if repo.repo.is_dirty(untracked_files=True) or repo.repo.index.diff("HEAD"): + commit_msg = f"{latest_file_name} - upload chunk {idx}/{total_chunks}" + repo.repo.git.commit("-m", commit_msg) + + # 3. 执行推送。结合上文可能有 reset_hard,首个包强制覆盖,后续包常规追加 + try: + if idx == 1: + repo.repo.git.push(force=True) + else: + repo.repo.git.push() + print(f"Chunk {idx} successfully pushed.") + except Exception as e: + print(f"Failed to push chunk {idx}. Error: {e}") + # 遇到网络彻底断开等致命异常时,优雅地中止程序 + sys.exit(1) + # ---------- 增量分批推送逻辑结束 ---------- + print("Upload finished.") sys.exit(0) From 1276e9edf06c5575e3e0b46d7544259bd7888011 Mon Sep 17 00:00:00 2001 From: jiandaoshou-aidehua <2893666867@qq.com> Date: Wed, 10 Jun 2026 21:45:25 +0800 Subject: [PATCH 010/128] =?UTF-8?q?=E5=8A=A0=E5=BF=AB=E6=8E=92=E9=94=99?= =?UTF-8?q?=E9=80=9F=E5=BA=A6?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- Util/BuildTools/Linux.mk | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/Util/BuildTools/Linux.mk b/Util/BuildTools/Linux.mk index 9f034a8333..6d24b159cf 100644 --- a/Util/BuildTools/Linux.mk +++ b/Util/BuildTools/Linux.mk @@ -84,7 +84,7 @@ run-examples: CarlaUE4Editor: @${CARLA_BUILD_TOOLS_FOLDER}/BuildUE4Plugins.sh --build $(ARGS) @${CARLA_BUILD_TOOLS_FOLDER}/BuildCarlaUE4.sh --build $(ARGS) - + .PHONY: PythonAPI PythonAPI: LibCarla.client.release osm2odr @${CARLA_BUILD_TOOLS_FOLDER}/BuildPythonAPI.sh $(ARGS) From ffb128ed47fbcedafdd62c9d648ab3c2402dfc48 Mon Sep 17 00:00:00 2001 From: jiandaoshou-aidehua <2893666867@qq.com> Date: Thu, 11 Jun 2026 14:28:38 +0800 Subject: [PATCH 011/128] =?UTF-8?q?=E8=BF=90=E8=A1=8C=E4=B8=8D=E4=BA=86?= =?UTF-8?q?=E8=AF=A5=E6=8F=92=E4=BB=B6?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- Unreal/CarlaUE4/CarlaUE4.uproject | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/Unreal/CarlaUE4/CarlaUE4.uproject b/Unreal/CarlaUE4/CarlaUE4.uproject index 0ef9dbda48..63256b1c14 100644 --- a/Unreal/CarlaUE4/CarlaUE4.uproject +++ b/Unreal/CarlaUE4/CarlaUE4.uproject @@ -155,7 +155,7 @@ }, { "Name": "UnrealRoboticsLab", - "Enabled": true + "Enabled": false }, { "Name": "GLTFImporter", From 74789ca6c2b174e8313b60917f10ed513389564c Mon Sep 17 00:00:00 2001 From: jiandaoshou-aidehua <2893666867@qq.com> Date: Thu, 11 Jun 2026 14:30:43 +0800 Subject: [PATCH 012/128] =?UTF-8?q?=E8=A7=A3=E5=86=B3wd4301=E9=97=AE?= =?UTF-8?q?=E9=A2=98?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .github/workflows/build_linux.yml | 19 ++++++++++++++++--- 1 file changed, 16 insertions(+), 3 deletions(-) diff --git a/.github/workflows/build_linux.yml b/.github/workflows/build_linux.yml index 2b1f69a112..64c5c0cd91 100644 --- a/.github/workflows/build_linux.yml +++ b/.github/workflows/build_linux.yml @@ -46,9 +46,22 @@ jobs: - name: Fix Windows Compiler Flags and Clean Cache run: | - # 使用 ${{ github.workspace }} 动态获取当前 Runner 的工作目录 - find ${{ github.workspace }} -type f -name "*.cs" -exec sed -i 's#-wd4103##g' {} + - find ${{ github.workspace }} -type f -name "*.cs" -exec sed -i 's#/wd4103##g' {} + + # 💡 在脚本中直接使用 $UE4_ROOT 变量 + echo "=== 正在抹除 C# 脚本中的 Windows 专属忽略警告参数 (/wd4103) ===" + find $UE4_ROOT/Engine/Source/Programs/UnrealBuildTool -type f -name "*.cs" -exec sed -i 's#-wd4103##g' {} + + find $UE4_ROOT/Engine/Source/Programs/UnrealBuildTool -type f -name "*.cs" -exec sed -i 's#/wd4103##g' {} + + + find ${{ github.workspace }} -type f -name "*.cs" -exec sed -i 's#-wd4103##g' {} + + find ${{ github.workspace }} -type f -name "*.cs" -exec sed -i 's#/wd4103##g' {} + + + echo "=== 正在强制重新编译虚幻引擎构建工具 (UBT) ===" + rm -f $UE4_ROOT/Engine/Binaries/DotNET/UnrealBuildTool.exe + cd $UE4_ROOT + ./GenerateProjectFiles.sh + + echo "=== 正在物理清空被污染的 Intermediate 缓存 ===" + rm -rf $UE4_ROOT/Engine/Intermediate/Build/Linux/B4D820EA + rm -rf ${{ github.workspace }}/Unreal/CarlaUE4/Intermediate - name: CarlaUE4Editor run: | From 4dcb45f08b5608fcf1fbf260efdc29dd32bf00dc Mon Sep 17 00:00:00 2001 From: jiandaoshou-aidehua <2893666867@qq.com> Date: Fri, 12 Jun 2026 14:29:15 +0800 Subject: [PATCH 013/128] =?UTF-8?q?=E4=B8=8D=E4=BD=BF=E7=94=A8=E6=96=B0?= =?UTF-8?q?=E5=A2=9E=E6=8F=92=E4=BB=B6?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- Unreal/CarlaUE4/CarlaUE4.uproject | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/Unreal/CarlaUE4/CarlaUE4.uproject b/Unreal/CarlaUE4/CarlaUE4.uproject index 63256b1c14..09c3f7b72c 100644 --- a/Unreal/CarlaUE4/CarlaUE4.uproject +++ b/Unreal/CarlaUE4/CarlaUE4.uproject @@ -159,15 +159,15 @@ }, { "Name": "GLTFImporter", - "Enabled": true + "Enabled": false }, { "Name": "FunctionalTestingEditor", - "Enabled": true + "Enabled": false }, { "Name": "RuntimeTests", - "Enabled": true + "Enabled": false } ] } \ No newline at end of file From c70bdeca096fcfa159c49180caecb4304387eec9 Mon Sep 17 00:00:00 2001 From: jiandaoshou-aidehua <2893666867@qq.com> Date: Tue, 16 Jun 2026 00:52:56 +0800 Subject: [PATCH 014/128] =?UTF-8?q?cpu=E6=A0=B8=E6=95=B0=E8=BF=87=E5=A4=9A?= =?UTF-8?q?=EF=BC=8C=E5=86=85=E5=AD=98=E4=B8=8D=E5=A4=9F=E7=94=A8?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .github/workflows/build_linux.yml | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/.github/workflows/build_linux.yml b/.github/workflows/build_linux.yml index 64c5c0cd91..0cad62c297 100644 --- a/.github/workflows/build_linux.yml +++ b/.github/workflows/build_linux.yml @@ -66,7 +66,7 @@ jobs: - name: CarlaUE4Editor run: | source ./setEnv64.sh - make CarlaUE4Editor ARGS="--chrono -j$(nproc)" + make CarlaUE4Editor ARGS="--chrono -j$(( $(nproc) / 3 ))" shell: bash - name: package From 5a329c5038d9e694afafe746507a37465caa813c Mon Sep 17 00:00:00 2001 From: jiandaoshou-aidehua <2893666867@qq.com> Date: Tue, 16 Jun 2026 15:58:03 +0800 Subject: [PATCH 015/128] =?UTF-8?q?=E4=BF=AE=E6=94=B9=E4=B8=BA=E5=8E=9F?= =?UTF-8?q?=E6=A0=B7?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- Util/BuildTools/Linux.mk | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/Util/BuildTools/Linux.mk b/Util/BuildTools/Linux.mk index 6d24b159cf..3927a90757 100644 --- a/Util/BuildTools/Linux.mk +++ b/Util/BuildTools/Linux.mk @@ -80,8 +80,8 @@ examples: run-examples: @for D in ${CARLA_EXAMPLES_FOLDER}/*; do [ -d "$${D}" ] && make -C $${D} run.only; done -# CarlaUE4Editor: LibCarla.server.release osm2odr downloadplugins -CarlaUE4Editor: +CarlaUE4Editor: LibCarla.server.release osm2odr downloadplugins +# CarlaUE4Editor: @${CARLA_BUILD_TOOLS_FOLDER}/BuildUE4Plugins.sh --build $(ARGS) @${CARLA_BUILD_TOOLS_FOLDER}/BuildCarlaUE4.sh --build $(ARGS) From cba6f85342554afb253bb656fe78a63652083f5a Mon Sep 17 00:00:00 2001 From: jiandaoshou-aidehua <2893666867@qq.com> Date: Wed, 17 Jun 2026 16:05:11 +0800 Subject: [PATCH 016/128] =?UTF-8?q?=E8=A7=A3=E5=86=B3python=E7=89=88?= =?UTF-8?q?=E6=9C=AC=E8=BF=87=E9=AB=98=E9=97=AE=E9=A2=98?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .github/workflows/build_linux.yml | 7 ++++++- 1 file changed, 6 insertions(+), 1 deletion(-) diff --git a/.github/workflows/build_linux.yml b/.github/workflows/build_linux.yml index 0cad62c297..583c6dbef9 100644 --- a/.github/workflows/build_linux.yml +++ b/.github/workflows/build_linux.yml @@ -71,7 +71,12 @@ jobs: - name: package run: | - source ./setEnv64.sh + source /home/ubuntu/miniconda3/etc/profile.d/conda.sh + conda activate carla + + source ./setEnv64.sh + + export MaxProcessorCount=$(( $(nproc) / 3 )) make package ARGS="--chrono" shell: bash From 43c5c857e76f78e84c5ec9f4e3fb64ba9d40b46d Mon Sep 17 00:00:00 2001 From: jiandaoshou-aidehua <2893666867@qq.com> Date: Fri, 26 Jun 2026 16:24:38 +0800 Subject: [PATCH 017/128] =?UTF-8?q?=E8=AE=A9=E6=89=93=E5=8C=85=E6=9C=BA?= =?UTF-8?q?=E8=B7=B3=E8=BF=87=E6=89=AB=E6=8F=8F=E8=BF=99=E6=89=B9=E5=9D=8F?= =?UTF-8?q?=E8=B5=84=E4=BA=A7=EF=BC=8C=E6=88=90=E5=8A=9F=E5=AE=8C=E6=88=90?= =?UTF-8?q?=E4=B8=BB=E4=BD=93=E6=89=93=E5=8C=85?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- Util/BuildTools/Package.sh | 2 ++ 1 file changed, 2 insertions(+) diff --git a/Util/BuildTools/Package.sh b/Util/BuildTools/Package.sh index ce985638ca..f02f74a84c 100755 --- a/Util/BuildTools/Package.sh +++ b/Util/BuildTools/Package.sh @@ -120,6 +120,8 @@ function copy_dir_fast { } function cook_tagged_materials { + return 0 + # Measure duration of this function call T_START_COOK_TAGGED_MATS=$(date +%s) From 75a5f9255ebb2dd93c64c951cdb55c4935ff4432 Mon Sep 17 00:00:00 2001 From: jiandaoshou-aidehua <2893666867@qq.com> Date: Wed, 1 Jul 2026 15:34:36 +0800 Subject: [PATCH 018/128] =?UTF-8?q?=E8=A7=A3=E5=86=B3=E8=BF=90=E8=A1=8CChe?= =?UTF-8?q?ckout=20repository=E6=97=B6=E9=97=B4=E8=BF=87=E9=95=BF=E9=97=AE?= =?UTF-8?q?=E9=A2=98?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .github/workflows/build_linux.yml | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/.github/workflows/build_linux.yml b/.github/workflows/build_linux.yml index 583c6dbef9..c65bd96606 100644 --- a/.github/workflows/build_linux.yml +++ b/.github/workflows/build_linux.yml @@ -18,7 +18,7 @@ jobs: - name: Checkout repository uses: actions/checkout@v5 with: - fetch-depth: 0 + fetch-depth: 1 - name: Show files run: | From b484dde9d155dbdd3b31f7f1e3235de1a2f155d4 Mon Sep 17 00:00:00 2001 From: jiandaoshou-aidehua <2893666867@qq.com> Date: Wed, 1 Jul 2026 15:49:36 +0800 Subject: [PATCH 019/128] =?UTF-8?q?=E6=B5=8B=E8=AF=95=E8=BF=87=E5=90=8E?= =?UTF-8?q?=E5=B9=B6=E9=9D=9E=E8=AF=A5=E9=97=AE=E9=A2=98?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .github/workflows/build_linux.yml | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/.github/workflows/build_linux.yml b/.github/workflows/build_linux.yml index c65bd96606..583c6dbef9 100644 --- a/.github/workflows/build_linux.yml +++ b/.github/workflows/build_linux.yml @@ -18,7 +18,7 @@ jobs: - name: Checkout repository uses: actions/checkout@v5 with: - fetch-depth: 1 + fetch-depth: 0 - name: Show files run: | From 0bd3706631d80bac7075e2694d8442ce2f87a55d Mon Sep 17 00:00:00 2001 From: jiandaoshou-aidehua <2893666867@qq.com> Date: Wed, 8 Jul 2026 15:37:35 +0800 Subject: [PATCH 020/128] =?UTF-8?q?=E5=8A=A8=E6=80=81=E8=B7=B3=E8=BF=87?= =?UTF-8?q?=E6=89=AB=E6=8F=8F=E8=BF=99=E6=89=B9=E5=9D=8F=E8=B5=84=E4=BA=A7?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .github/workflows/build_linux.yml | 8 ++++++++ 1 file changed, 8 insertions(+) diff --git a/.github/workflows/build_linux.yml b/.github/workflows/build_linux.yml index 583c6dbef9..4972d42f92 100644 --- a/.github/workflows/build_linux.yml +++ b/.github/workflows/build_linux.yml @@ -62,6 +62,7 @@ jobs: echo "=== 正在物理清空被污染的 Intermediate 缓存 ===" rm -rf $UE4_ROOT/Engine/Intermediate/Build/Linux/B4D820EA rm -rf ${{ github.workspace }}/Unreal/CarlaUE4/Intermediate + shell: bash - name: CarlaUE4Editor run: | @@ -69,6 +70,13 @@ jobs: make CarlaUE4Editor ARGS="--chrono -j$(( $(nproc) / 3 ))" shell: bash + # 在执行 make package 之前,动态注入 return 0 + - name: Patch Package.sh to skip bad materials + run: | + echo "=== 正在动态修改 Package.sh,跳过 cook_tagged_materials ===" + sed -i 's/function cook_tagged_materials {/function cook_tagged_materials {\n return 0/g' Util/BuildTools/Package.sh + shell: bash + - name: package run: | source /home/ubuntu/miniconda3/etc/profile.d/conda.sh From 2929fe5fc091b2adfebbda57ca5f13d17cb8797c Mon Sep 17 00:00:00 2001 From: jiandaoshou-aidehua <2893666867@qq.com> Date: Thu, 9 Jul 2026 15:12:37 +0800 Subject: [PATCH 021/128] =?UTF-8?q?=E5=AF=B9LFS=E7=8A=B6=E6=80=81=E4=BF=AE?= =?UTF-8?q?=E5=A4=8D=EF=BC=8C=E5=85=88=E8=AF=95=E8=AF=95=E5=90=A7?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- Util/BuildTools/BuildUE4Plugins.sh | 6 ++++-- 1 file changed, 4 insertions(+), 2 deletions(-) diff --git a/Util/BuildTools/BuildUE4Plugins.sh b/Util/BuildTools/BuildUE4Plugins.sh index 7b1832504d..ec44941472 100755 --- a/Util/BuildTools/BuildUE4Plugins.sh +++ b/Util/BuildTools/BuildUE4Plugins.sh @@ -105,16 +105,18 @@ if [[ ! -d "$CONTENT_PATH" ]]; then else git clone https://OpenHUTB:T8w6TYB_r71gGTP3A02B@git.code.tencent.com/OpenHUTB/Content.git "$CONTENT_PATH" cd "$CONTENT_PATH" + git lfs install git lfs pull fi else echo "$FILE_N Content directory already exists: \"$CONTENT_PATH\", executing git pull." cd "$CONTENT_PATH" - git fetch --all - git reset --hard origin/master + git lfs install + git stash git pull git lfs pull + git stash pop || true fi From 179c1472e3e7ccb7744b5c319118c61ee2de7961 Mon Sep 17 00:00:00 2001 From: jiandaoshou-aidehua <2893666867@qq.com> Date: Fri, 10 Jul 2026 11:13:56 +0800 Subject: [PATCH 022/128] =?UTF-8?q?=E5=9B=9E=E9=80=80LFS=E7=8A=B6=E6=80=81?= =?UTF-8?q?=E4=BF=AE=E5=A4=8D?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- Util/BuildTools/BuildUE4Plugins.sh | 6 ++---- 1 file changed, 2 insertions(+), 4 deletions(-) diff --git a/Util/BuildTools/BuildUE4Plugins.sh b/Util/BuildTools/BuildUE4Plugins.sh index ec44941472..7b1832504d 100755 --- a/Util/BuildTools/BuildUE4Plugins.sh +++ b/Util/BuildTools/BuildUE4Plugins.sh @@ -105,18 +105,16 @@ if [[ ! -d "$CONTENT_PATH" ]]; then else git clone https://OpenHUTB:T8w6TYB_r71gGTP3A02B@git.code.tencent.com/OpenHUTB/Content.git "$CONTENT_PATH" cd "$CONTENT_PATH" - git lfs install git lfs pull fi else echo "$FILE_N Content directory already exists: \"$CONTENT_PATH\", executing git pull." cd "$CONTENT_PATH" - git lfs install - git stash + git fetch --all + git reset --hard origin/master git pull git lfs pull - git stash pop || true fi From 89b824e16e411471173c5f8390f3514ea9e4cc4c Mon Sep 17 00:00:00 2001 From: jiandaoshou-aidehua <2893666867@qq.com> Date: Sun, 12 Jul 2026 17:25:30 +0800 Subject: [PATCH 023/128] =?UTF-8?q?Cook=20=E5=89=8D=E8=87=AA=E5=8A=A8?= =?UTF-8?q?=E4=BF=AE=E5=A4=8D=20.uasset=20=E4=B8=AD=E9=9D=9E=E6=B3=95=20/R?= =?UTF-8?q?oadRunnerMaterials/=20=E6=9D=90=E8=B4=A8=E8=B7=AF=E5=BE=84?= =?UTF-8?q?=E4=B8=BA=20/Game/RoadRunnerMaterials/?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .github/workflows/build_linux.yml | 19 ++++++++++++++++++- Util/BuildTools/Package.sh | 15 ++++++++++++++- 2 files changed, 32 insertions(+), 2 deletions(-) diff --git a/.github/workflows/build_linux.yml b/.github/workflows/build_linux.yml index 4972d42f92..53f3cee0e9 100644 --- a/.github/workflows/build_linux.yml +++ b/.github/workflows/build_linux.yml @@ -75,7 +75,24 @@ jobs: run: | echo "=== 正在动态修改 Package.sh,跳过 cook_tagged_materials ===" sed -i 's/function cook_tagged_materials {/function cook_tagged_materials {\n return 0/g' Util/BuildTools/Package.sh - shell: bash + shell: bash + + # 修复 Content 包中 .uasset 文件的非法材质路径(缺少 /Game/ 根前缀) + - name: Fix invalid material paths in RoadRunner assets + run: | + echo "=== 正在修复 .uasset 中的非法 RoadRunner 材质路径 ===" + for BAD_PATH in \ + "/RoadRunnerMaterials/BaseMaterial:/Game/RoadRunnerMaterials/BaseMat" \ + "/RoadRunnerMaterials/BaseCutoutMaterial:/Game/RoadRunnerMaterials/BaseCutoutMat" \ + ; do + FROM="${BAD_PATH%%:*}" + TO="${BAD_PATH##*:}" + find Unreal/CarlaUE4/Content -name "*.uasset" \ + -exec grep -l "$FROM" {} \; \ + -exec sed -i "s|$FROM|$TO|g" {} \; 2>/dev/null || true + done + echo "修复完成。" + shell: bash - name: package run: | diff --git a/Util/BuildTools/Package.sh b/Util/BuildTools/Package.sh index f02f74a84c..2caf8a5fa5 100755 --- a/Util/BuildTools/Package.sh +++ b/Util/BuildTools/Package.sh @@ -120,7 +120,7 @@ function copy_dir_fast { } function cook_tagged_materials { - return 0 + # return 0 # Measure duration of this function call T_START_COOK_TAGGED_MATS=$(date +%s) @@ -253,6 +253,19 @@ if ${DO_CARLA_RELEASE} ; then log "Cooking CARLA project." + # 修复 Content 中 .uasset 的非法材质路径(缺少 /Game/ 根前缀导致 Cook 崩溃) + log "Fixing invalid RoadRunner material paths in .uasset files..." + for BAD_PATH in \ + "/RoadRunnerMaterials/BaseMaterial:/Game/RoadRunnerMaterials/BaseMat" \ + "/RoadRunnerMaterials/BaseCutoutMaterial:/Game/RoadRunnerMaterials/BaseCutoutMat" \ + ; do + FROM="${BAD_PATH%%:*}" + TO="${BAD_PATH##*:}" + find "${CARLAUE4_ROOT_FOLDER}/Content" -name "*.uasset" \ + -exec grep -l "$FROM" {} \; \ + -exec sed -i "s|$FROM|$TO|g" {} \; 2>/dev/null || true + done + rm -Rf ${RELEASE_BUILD_FOLDER} mkdir -p ${RELEASE_BUILD_FOLDER} From d9d34a7d95c4f2160c610872dd4778cfc0b20c7a Mon Sep 17 00:00:00 2001 From: jiandaoshou-aidehua <2893666867@qq.com> Date: Mon, 13 Jul 2026 13:27:10 +0800 Subject: [PATCH 024/128] =?UTF-8?q?=E5=B0=86=E6=96=B0=E5=A2=9E=E7=9A=84?= =?UTF-8?q?=E6=8F=92=E4=BB=B6=E5=85=88=E6=94=B9=E4=B8=BAfalse?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- Unreal/CarlaUE4/CarlaUE4.uproject | 44 +++++++++++++++++++++++++++++++ 1 file changed, 44 insertions(+) diff --git a/Unreal/CarlaUE4/CarlaUE4.uproject b/Unreal/CarlaUE4/CarlaUE4.uproject index 09c3f7b72c..d066a72ca1 100644 --- a/Unreal/CarlaUE4/CarlaUE4.uproject +++ b/Unreal/CarlaUE4/CarlaUE4.uproject @@ -168,6 +168,50 @@ { "Name": "RuntimeTests", "Enabled": false + }, + { + "Name": "DatasmithGLTFImporter", + "Enabled": true + }, + { + "Name": "CesiumForUnreal", + "Enabled": false, + "MarketplaceURL": "com.epicgames.launcher://ue/marketplace/content/87b0d05800a545d49bf858ef3458c4f7", + "SupportedTargetPlatforms": [ + "Win64", + "Mac", + "Linux", + "Android", + "IOS" + ] + }, + { + "Name": "RoadRunnerCarlaIntegration", + "Enabled": false + }, + { + "Name": "RoadRunnerImporter", + "Enabled": false + }, + { + "Name": "StreetMap", + "Enabled": false + }, + { + "Name": "RoadRunnerCarlaDatasmith", + "Enabled": false + }, + { + "Name": "RoadRunnerDatasmith", + "Enabled": false + }, + { + "Name": "RoadRunnerRuntime", + "Enabled": false + }, + { + "Name": "LogitechWheelPlugin", + "Enabled": false } ] } \ No newline at end of file From 77c359a27048f3bdfd616e6ecec8dcd6a2cf185b Mon Sep 17 00:00:00 2001 From: jiandaoshou-aidehua <2893666867@qq.com> Date: Mon, 13 Jul 2026 13:34:03 +0800 Subject: [PATCH 025/128] =?UTF-8?q?=E5=B0=86=E6=96=B0=E5=A2=9E=E7=9A=84?= =?UTF-8?q?=E6=8F=92=E4=BB=B6=E5=85=88=E8=AE=BE=E7=BD=AE=E4=B8=BAfalse?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- Unreal/CarlaUE4/CarlaUE4.uproject | 36 ------------------------------- 1 file changed, 36 deletions(-) diff --git a/Unreal/CarlaUE4/CarlaUE4.uproject b/Unreal/CarlaUE4/CarlaUE4.uproject index e66f5b06f3..6389125f80 100644 --- a/Unreal/CarlaUE4/CarlaUE4.uproject +++ b/Unreal/CarlaUE4/CarlaUE4.uproject @@ -166,11 +166,7 @@ }, { "Name": "RuntimeTests", -<<<<<<< HEAD "Enabled": false -======= - "Enabled": true ->>>>>>> b56b835e3e67543f324dca6892e6669bd0243cf0 }, { "Name": "DatasmithGLTFImporter", @@ -178,11 +174,7 @@ }, { "Name": "CesiumForUnreal", -<<<<<<< HEAD "Enabled": false, -======= - "Enabled": true, ->>>>>>> b56b835e3e67543f324dca6892e6669bd0243cf0 "MarketplaceURL": "com.epicgames.launcher://ue/marketplace/content/87b0d05800a545d49bf858ef3458c4f7", "SupportedTargetPlatforms": [ "Win64", @@ -194,7 +186,6 @@ }, { "Name": "RoadRunnerCarlaIntegration", -<<<<<<< HEAD "Enabled": false }, { @@ -220,33 +211,6 @@ { "Name": "LogitechWheelPlugin", "Enabled": false -======= - "Enabled": true - }, - { - "Name": "RoadRunnerImporter", - "Enabled": true - }, - { - "Name": "StreetMap", - "Enabled": true - }, - { - "Name": "RoadRunnerCarlaDatasmith", - "Enabled": true - }, - { - "Name": "RoadRunnerDatasmith", - "Enabled": true - }, - { - "Name": "RoadRunnerRuntime", - "Enabled": true - }, - { - "Name": "LogitechWheelPlugin", - "Enabled": true ->>>>>>> b56b835e3e67543f324dca6892e6669bd0243cf0 } ] } \ No newline at end of file From f0c5d36aa49fa404c32b909ccc926d3d57d51a2b Mon Sep 17 00:00:00 2001 From: jiandaoshou-aidehua <2893666867@qq.com> Date: Tue, 14 Jul 2026 11:46:32 +0800 Subject: [PATCH 026/128] fix: register RoadRunnerCarlaContent plugin to resolve Cook mount point error MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit - 创建 RoadRunnerCarlaContent 内容插件,提供 /RoadRunnerCarlaContent/ 挂载点 - 在 .uproject 中注册并启用该插件 - 在 Package.sh 和 CI yml 中增加对应路径替换规则 修复 Cook 阶段报错: Illegal call to DoesPackageExist: '/RoadRunnerCarlaContent/RR_TrafficLightManager' is not a standard unreal filename (Error_UnknownCookFailure) --- .github/workflows/build_linux.yml | 1 + Unreal/CarlaUE4/CarlaUE4.uproject | 4 ++++ .../RoadRunnerCarlaContent.uplugin | 13 +++++++++++++ Util/BuildTools/Package.sh | 1 + 4 files changed, 19 insertions(+) create mode 100644 Unreal/CarlaUE4/Plugins/RoadRunnerCarlaContent/RoadRunnerCarlaContent.uplugin diff --git a/.github/workflows/build_linux.yml b/.github/workflows/build_linux.yml index 53f3cee0e9..e7cdd48ab0 100644 --- a/.github/workflows/build_linux.yml +++ b/.github/workflows/build_linux.yml @@ -84,6 +84,7 @@ jobs: for BAD_PATH in \ "/RoadRunnerMaterials/BaseMaterial:/Game/RoadRunnerMaterials/BaseMat" \ "/RoadRunnerMaterials/BaseCutoutMaterial:/Game/RoadRunnerMaterials/BaseCutoutMat" \ + "/RoadRunnerCarlaContent/:/Game/RoadRunnerCarlaContent/" \ ; do FROM="${BAD_PATH%%:*}" TO="${BAD_PATH##*:}" diff --git a/Unreal/CarlaUE4/CarlaUE4.uproject b/Unreal/CarlaUE4/CarlaUE4.uproject index 6389125f80..5353ecf339 100644 --- a/Unreal/CarlaUE4/CarlaUE4.uproject +++ b/Unreal/CarlaUE4/CarlaUE4.uproject @@ -184,6 +184,10 @@ "IOS" ] }, + { + "Name": "RoadRunnerCarlaContent", + "Enabled": true + }, { "Name": "RoadRunnerCarlaIntegration", "Enabled": false diff --git a/Unreal/CarlaUE4/Plugins/RoadRunnerCarlaContent/RoadRunnerCarlaContent.uplugin b/Unreal/CarlaUE4/Plugins/RoadRunnerCarlaContent/RoadRunnerCarlaContent.uplugin new file mode 100644 index 0000000000..524f100821 --- /dev/null +++ b/Unreal/CarlaUE4/Plugins/RoadRunnerCarlaContent/RoadRunnerCarlaContent.uplugin @@ -0,0 +1,13 @@ +{ + "FileVersion": 3, + "Version": 1, + "VersionName": "1.0", + "FriendlyName": "RoadRunnerCarlaContent", + "Description": "Content-only plugin providing RoadRunner Carla content mount point for compatibility with assets that reference /RoadRunnerCarlaContent/ paths.", + "Category": "Other", + "CreatedBy": "HUTB Team", + "CanContainContent": true, + "IsBetaVersion": false, + "Installed": true, + "Modules": [] +} diff --git a/Util/BuildTools/Package.sh b/Util/BuildTools/Package.sh index 2caf8a5fa5..23e95eb94b 100755 --- a/Util/BuildTools/Package.sh +++ b/Util/BuildTools/Package.sh @@ -258,6 +258,7 @@ if ${DO_CARLA_RELEASE} ; then for BAD_PATH in \ "/RoadRunnerMaterials/BaseMaterial:/Game/RoadRunnerMaterials/BaseMat" \ "/RoadRunnerMaterials/BaseCutoutMaterial:/Game/RoadRunnerMaterials/BaseCutoutMat" \ + "/RoadRunnerCarlaContent/:/Game/RoadRunnerCarlaContent/" \ ; do FROM="${BAD_PATH%%:*}" TO="${BAD_PATH##*:}" From 7316845fa2029e264c402cefe8f4c331c9a07a56 Mon Sep 17 00:00:00 2001 From: jiandaoshou-aidehua <2893666867@qq.com> Date: Thu, 16 Jul 2026 16:37:41 +0800 Subject: [PATCH 027/128] =?UTF-8?q?=E5=90=AF=E5=8A=A8=E6=9C=80=E6=96=B0?= =?UTF-8?q?=E7=9A=84=E6=8F=92=E4=BB=B6=E4=BB=A5=E5=8F=8A=E5=B0=86=E4=BE=9D?= =?UTF-8?q?=E8=B5=96=E6=94=BE=E5=9C=A8?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- Unreal/CarlaUE4/CarlaUE4.uproject | 18 +- setup.sh | 411 ++++++++++++++++++++++++++++++ 2 files changed, 420 insertions(+), 9 deletions(-) create mode 100755 setup.sh diff --git a/Unreal/CarlaUE4/CarlaUE4.uproject b/Unreal/CarlaUE4/CarlaUE4.uproject index 5353ecf339..a38420b70a 100644 --- a/Unreal/CarlaUE4/CarlaUE4.uproject +++ b/Unreal/CarlaUE4/CarlaUE4.uproject @@ -166,7 +166,7 @@ }, { "Name": "RuntimeTests", - "Enabled": false + "Enabled": true }, { "Name": "DatasmithGLTFImporter", @@ -174,7 +174,7 @@ }, { "Name": "CesiumForUnreal", - "Enabled": false, + "Enabled": true, "MarketplaceURL": "com.epicgames.launcher://ue/marketplace/content/87b0d05800a545d49bf858ef3458c4f7", "SupportedTargetPlatforms": [ "Win64", @@ -190,31 +190,31 @@ }, { "Name": "RoadRunnerCarlaIntegration", - "Enabled": false + "Enabled": true }, { "Name": "RoadRunnerImporter", - "Enabled": false + "Enabled": true }, { "Name": "StreetMap", - "Enabled": false + "Enabled": true }, { "Name": "RoadRunnerCarlaDatasmith", - "Enabled": false + "Enabled": true }, { "Name": "RoadRunnerDatasmith", - "Enabled": false + "Enabled": true }, { "Name": "RoadRunnerRuntime", - "Enabled": false + "Enabled": true }, { "Name": "LogitechWheelPlugin", - "Enabled": false + "Enabled": true } ] } \ No newline at end of file diff --git a/setup.sh b/setup.sh new file mode 100755 index 0000000000..9c2f8f2d17 --- /dev/null +++ b/setup.sh @@ -0,0 +1,411 @@ +#!/bin/bash +# ============================================================================== +# setup.sh — Ubuntu/Linux adaptation of Windows setup.bat +# +# All dependencies (including UE4 plugins not shipped with the engine) are +# centralized in a single repository: +# +# git@git.code.tencent.com:OpenHUTB/dependencies_u.git +# +# The repo contains Linux-compatible builds of every package AND an init.sh +# script that handles extraction. This setup.sh only orchestrates: +# +# 1. Ensure system tools (git, curl, 7z, gcc, etc.) +# 2. Clone dependencies_u → Build/dependencies/ +# 3. Run init.sh → extract everything to the right places +# 4. Install system packages via apt +# 5. Set up Python environment (conda or venv) +# 6. Optionally invoke Util/BuildTools/Setup.sh for the full C++ build +# +# Usage: +# ./setup.sh [--skip-prerequisites] [--download-only] [--help] +# ============================================================================== + +set -e + +# ============================================================================== +# -- Parse command-line arguments ----------------------------------------------- +# ============================================================================== + +DOC_STRING="Download and install all dependencies and UE4 plugins for Ubuntu. +All packages come from the dependencies_u repository (Linux builds)." + +USAGE_STRING="Usage: $0 [--skip-prerequisites] [--download-only] [--help]" + +SKIP_PREREQUISITES=false +DOWNLOAD_ONLY=false + +OPTS=$(getopt -o h --long help,skip-prerequisites,download-only -n 'parse-options' -- "$@") +eval set -- "$OPTS" + +while [[ $# -gt 0 ]]; do + case "$1" in + --skip-prerequisites ) + SKIP_PREREQUISITES=true + shift ;; + --download-only ) + DOWNLOAD_ONLY=true + shift ;; + -h | --help ) + echo "$DOC_STRING" + echo "$USAGE_STRING" + exit 0 + ;; + * ) + shift ;; + esac +done + +# ============================================================================== +# -- Color helpers -------------------------------------------------------------- +# ============================================================================== + +RED='\033[0;31m' +GREEN='\033[0;32m' +YELLOW='\033[1;33m' +CYAN='\033[1;36m' +NC='\033[0m' + +log() { echo -e "${CYAN}[setup.sh]${NC} $1"; } +success(){ echo -e "${GREEN}[setup.sh]${NC} \xe2\x9c\x85 $1"; } +warn() { echo -e "${YELLOW}[setup.sh]${NC} \xe2\x9a\xa0\xef\xb8\x8f $1"; } +error() { echo -e "${RED}[setup.sh]${NC} \xe2\x9d\x8c $1"; exit 1; } + +# ============================================================================== +# -- Paths --------------------------------------------------------------------- +# ============================================================================== + +SCRIPT_DIR="$(cd "$(dirname "${BASH_SOURCE[0]}")" && pwd)" +PROJECT_ROOT="$SCRIPT_DIR" +BUILD_DIR="$PROJECT_ROOT/Build" +PLUGINS_DIR="$PROJECT_ROOT/Unreal/CarlaUE4/Plugins" + +# ------------------------------------------------------------------------------ +# dependencies_u — Ubuntu edition of the Windows dependencies repo. +# +# Repo layout (see README.md in the repo for full documentation): +# +# dependencies_u/ +# |-- Plugins/ # UE4 plugins + third-party runtime libs +# | |-- RoadRunner_Plugins.zip +# | |-- CesiumForUnreal-426-v1.18.0-ue4.zip +# | |-- mujoco-3.3.5-linux-x86_64.tar.gz +# | |-- CoACD.zip +# | `-- libzmq-linux.tar.gz +# |-- prerequisites/ # Pre-packaged toolchain +# | `-- Miniconda3-latest-Linux-x86_64.sh +# |-- src/ # C++ source packages (cross-platform) +# | |-- boost-1_86_0.zip +# | |-- chrono-src.zip +# | |-- eigen-3.3.7.zip +# | |-- ... (13 packages total) +# | `-- zlib-source.zip +# |-- init.sh # Extraction / initialization script +# |-- .gitattributes # LFS tracking rules +# `-- README.md +# ------------------------------------------------------------------------------ + +# DEPENDENCIES_REPO="git@git.code.tencent.com:OpenHUTB/dependencies_u.git" +# Fallback if SSH is not configured: +DEPENDENCIES_REPO="https://OpenHUTB:T8w6TYB_r71gGTP3A02B@git.code.tencent.com/OpenHUTB/dependencies_u.git" + +DEPENDENCIES_DIR="$BUILD_DIR/dependencies" + +URLAB_DIR="$PLUGINS_DIR/UnrealRoboticsLab" +URLAB_THIRD_PARTY="$URLAB_DIR/third_party/install" + +# ============================================================================== +# -- Banner -------------------------------------------------------------------- +# ============================================================================== + +log "==============================================" +log " HUTB Ubuntu Setup Script" +log " (adapted from setup.bat)" +log "==============================================" +log "Project root : $PROJECT_ROOT" +log "Build dir : $BUILD_DIR" +log "Plugins dir : $PLUGINS_DIR" +log "Dependencies repo: $DEPENDENCIES_REPO" +log "" + +# ============================================================================== +# -- Step 0: Ensure essential system tools ------------------------------------- +# ============================================================================== +# Mirrors the Windows .bat which downloads git + 7zip first, then uses them to +# obtain everything else. On Ubuntu these are native system packages. + +log "==============================================" +log " Step 0 — Essential System Tools" +log "==============================================" + +MISSING_TOOLS=() +for cmd in git curl wget unzip make gcc g++; do + if ! command -v "$cmd" &>/dev/null; then + MISSING_TOOLS+=("$cmd") + fi +done + +if [ ${#MISSING_TOOLS[@]} -gt 0 ]; then + warn "Installing missing tools: ${MISSING_TOOLS[*]}" + sudo apt-get update -qq + sudo apt-get install -y -qq "${MISSING_TOOLS[@]}" +fi + +# 7z — used extensively in .bat for extracting archives +if ! command -v 7z &>/dev/null; then + log "Installing p7zip-full..." + sudo apt-get install -y -qq p7zip-full +fi + +success "Essential tools ready." +log "" + +# ============================================================================== +# -- Step 1: Ensure Build directory --------------------------------------------- +# ============================================================================== +# Mirrors: if not exist "%cd%\Build" mkdir "%cd%\Build" + +if [ ! -d "$BUILD_DIR" ]; then + log "Creating Build/ directory..." + mkdir -p "$BUILD_DIR" +else + log "Build/ directory already exists." +fi + +# ============================================================================== +# -- Step 2: Clone dependencies_u repository ----------------------------------- +# ============================================================================== +# Mirrors: +# git clone https://.../dependencies.git Build\dependencies +# cd dependencies && git lfs pull +# +# The Windows .bat targets "dependencies" (Windows builds). +# We target "dependencies_u" — the Ubuntu counterpart. + +log "==============================================" +log " Step 2 — Clone dependencies_u" +log "==============================================" + +export GIT_LFS_SKIP_SMUDGE=1 + +if [ ! -d "$DEPENDENCIES_DIR" ]; then + log "Cloning $DEPENDENCIES_REPO ..." + pushd "$BUILD_DIR" >/dev/null + + git clone "$DEPENDENCIES_REPO" dependencies 2>/dev/null || { + warn "" + warn "============================================" + warn " Failed to clone dependencies_u." + warn " Please check:" + warn " 1. SSH key is added to git.code.tencent.com" + warn " 2. The repo exists at:" + warn " git@git.code.tencent.com:OpenHUTB/dependencies_u.git" + warn " Or use HTTPS fallback (edit setup.sh)." + warn "============================================" + warn "" + warn " Continuing without dependencies — some" + warn " features will be unavailable." + } + + if [ -d "$DEPENDENCIES_DIR" ]; then + cd "$DEPENDENCIES_DIR" + git lfs pull 2>/dev/null || warn "git lfs pull failed (non-critical)." + success "dependencies_u cloned." + fi + + popd >/dev/null +else + log "dependencies/ already exists, skipping clone." + log " (To force re-clone: rm -rf $DEPENDENCIES_DIR && re-run)" +fi + +log "" + +# ============================================================================== +# -- Step 3: Run dependencies_u/init.sh ---------------------------------------- +# ============================================================================== +# All extraction / initialization logic lives inside the dependencies_u repo. +# setup.sh only orchestrates — init.sh does the actual work of unzipping +# plugins, extracting source packages, installing miniconda3, etc. + +if [ ! -d "$DEPENDENCIES_DIR" ]; then + warn "dependencies_u not available — skipping extraction." +else + log "==============================================" + log " Step 3 — Run dependencies_u/init.sh" + log "==============================================" + + INIT_SCRIPT="$DEPENDENCIES_DIR/init.sh" + if [ -f "$INIT_SCRIPT" ]; then + log "Delegating to $INIT_SCRIPT ..." + log "" + bash "$INIT_SCRIPT" "$PROJECT_ROOT" + success "dependencies_u initialization complete." + else + error "init.sh not found in dependencies_u!" + error "Expected at: $INIT_SCRIPT" + error "The dependencies_u repo may be incomplete or corrupted." + exit 1 + fi +fi + +log "" + +# ============================================================================== +# -- Step 4: System packages via apt ------------------------------------------- +# ============================================================================== + +if [ "$SKIP_PREREQUISITES" = false ]; then + log "==============================================" + log " Step 4 — System Prerequisites (apt)" + log "==============================================" + + log "Installing build dependencies..." + sudo apt-get update -qq + + sudo apt-get install -y -qq \ + build-essential \ + clang-10 \ + libc++-dev \ + libc++abi-dev \ + ninja-build \ + python3 \ + python3-dev \ + python3-pip \ + python3-venv \ + libomp-dev \ + libssl-dev \ + libncurses5 \ + libncurses5-dev \ + libsdl2-dev \ + libtiff5-dev \ + libjpeg-dev \ + libcurl4-openssl-dev \ + libzmq3-dev \ + doxygen \ + patchelf \ + libxml2-dev \ + libicu-dev \ + 2>/dev/null || warn "Some apt packages may have failed to install." + + # clang-10 may not exist on newer Ubuntu (e.g. 24.04); fall back to clang + if ! command -v clang-10 &>/dev/null && ! command -v clang &>/dev/null; then + warn "clang not found — installing clang..." + sudo apt-get install -y -qq clang + fi + + success "System prerequisites installed." +else + log "Skipping system prerequisites (--skip-prerequisites)." +fi + +log "" + +# ============================================================================== +# -- Step 5: Python environment ------------------------------------------------ +# ============================================================================== +# init.sh already installed miniconda3 to Build/dependencies/prerequisites/. +# Here we create the conda environment and install Python requirements. + +log "==============================================" +log " Step 5 — Python Environment" +log "==============================================" + +MINICONDA_DIR="$BUILD_DIR/dependencies/prerequisites/miniconda3" + +if [ -d "$MINICONDA_DIR" ] && [ -f "$MINICONDA_DIR/bin/conda" ]; then + log "Setting up conda environment 'hutb_3.8'..." + + # Create environment if not already present + if ! "$MINICONDA_DIR/bin/conda" env list 2>/dev/null | grep -q hutb_3.8; then + "$MINICONDA_DIR/bin/conda" create -n hutb_3.8 python=3.8 -y 2>/dev/null || \ + warn " Conda env creation failed." + fi + + # Install Python requirements + if [ -f "$PROJECT_ROOT/requirements.txt" ]; then + log " Installing Python requirements (requirements.txt)..." + "$MINICONDA_DIR/envs/hutb_3.8/bin/pip" install -r "$PROJECT_ROOT/requirements.txt" 2>/dev/null || \ + warn " pip install failed (non-critical)." + fi + + if [ -f "$PROJECT_ROOT/PythonAPI/carla/requirements.txt" ]; then + log " Installing Python requirements (PythonAPI)..." + "$MINICONDA_DIR/envs/hutb_3.8/bin/pip" install -r "$PROJECT_ROOT/PythonAPI/carla/requirements.txt" 2>/dev/null || true + fi + + success "conda env 'hutb_3.8' ready." +else + # Fallback: system python3 venv + VENV_DIR="$BUILD_DIR/venv" + if [ ! -d "$VENV_DIR" ] && command -v python3 &>/dev/null; then + log "No conda found — creating Python venv as fallback..." + python3 -m venv "$VENV_DIR" 2>/dev/null || true + if [ -f "$VENV_DIR/bin/pip" ]; then + "$VENV_DIR/bin/pip" install --upgrade pip 2>/dev/null || true + [ -f "$PROJECT_ROOT/requirements.txt" ] && \ + "$VENV_DIR/bin/pip" install -r "$PROJECT_ROOT/requirements.txt" 2>/dev/null || true + fi + success "Python venv created at $VENV_DIR" + fi +fi + +log "" + +# ============================================================================== +# -- Step 6: Build C++ dependencies -------------------------------------------- +# ============================================================================== + +if [ "$DOWNLOAD_ONLY" = false ]; then + log "==============================================" + log " Step 6 — C++ Dependency Build" + log "==============================================" + + BUILD_TOOLS_SETUP="$PROJECT_ROOT/Util/BuildTools/Setup.sh" + + if [ -f "$BUILD_TOOLS_SETUP" ]; then + log "Running $BUILD_TOOLS_SETUP --chrono ..." + log "(This compiles boost, rpclib, gtest, recast, eigen, etc. from source)" + log "" + bash "$BUILD_TOOLS_SETUP" --chrono || warn "BuildTools/Setup.sh reported errors." + else + warn "$BUILD_TOOLS_SETUP not found — skipping C++ build." + warn "Run 'make setup' or build dependencies manually." + fi +else + log "Skipping C++ build (--download-only)." +fi + +log "" + +# ============================================================================== +# -- Summary ------------------------------------------------------------------- +# ============================================================================== + +log "==============================================" +log " Setup Complete!" +log "==============================================" +log "" + +# List what's in Plugins +log "Plugins directory: $PLUGINS_DIR" +if [ -d "$PLUGINS_DIR" ]; then + log "Available plugins:" + for d in "$PLUGINS_DIR"/*/; do + [ -d "$d" ] && echo " - $(basename "$d")" + done +else + warn "Plugins directory does not exist!" +fi + +log "" +log "Dependencies repo : $DEPENDENCIES_DIR" +log "Third-party (URLab): $URLAB_THIRD_PARTY" +log "" +log "Next steps:" +log " 1. Ensure UE4 engine is at ~/UnrealEngine_4.26" +log " 2. make CarlaUE4Editor" +log " 3. make package" +log "" + +success "Done!" From 33a1ad7e12f852e905d2c70a2164c6ff1f69a5b4 Mon Sep 17 00:00:00 2001 From: jiandaoshou-aidehua <2893666867@qq.com> Date: Thu, 16 Jul 2026 20:48:08 +0800 Subject: [PATCH 028/128] =?UTF-8?q?=E8=A7=A3=E5=8E=8B=E5=AF=B9=E5=BA=94?= =?UTF-8?q?=E6=8F=92=E4=BB=B6=E4=BB=A5=E5=8F=8A=E4=BE=9D=E8=B5=96?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .github/workflows/build_linux.yml | 5 +++-- 1 file changed, 3 insertions(+), 2 deletions(-) diff --git a/.github/workflows/build_linux.yml b/.github/workflows/build_linux.yml index e7cdd48ab0..879538978d 100644 --- a/.github/workflows/build_linux.yml +++ b/.github/workflows/build_linux.yml @@ -27,10 +27,11 @@ jobs: - name: setup run: | - source ./setEnv64.sh + source ./setEnv64.sh git update-index --skip-worktree Unreal/CarlaUE4/CarlaUE4.uproject + bash setup.sh --download-only --skip-prerequisites make setup ARGS="--chrono" - shell: bash + shell: bash - name: build run: | From e4a3fab2ef1fa0209f499eb99b9ab7dbea5fbf7d Mon Sep 17 00:00:00 2001 From: jiandaoshou-aidehua <2893666867@qq.com> Date: Fri, 17 Jul 2026 09:46:42 +0800 Subject: [PATCH 029/128] =?UTF-8?q?=E5=8F=AA=E8=A7=A3=E5=8E=8B=20=E6=8F=92?= =?UTF-8?q?=E4=BB=B6=E7=9B=B8=E5=85=B3=20=E6=96=87=E4=BB=B6(RoadRunner?= =?UTF-8?q?=E3=80=81Cesium=E3=80=81MuJoCo=E3=80=81CoACD=E3=80=81libzmq)?= =?UTF-8?q?=E4=BB=A5=E5=8F=8A=E6=B8=85=E7=90=86=20Build/*-source=20Build/*?= =?UTF-8?q?-src=20=E6=AE=8B=E7=95=99?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .github/workflows/build_linux.yml | 26 +++++++++++++++++++++++++- 1 file changed, 25 insertions(+), 1 deletion(-) diff --git a/.github/workflows/build_linux.yml b/.github/workflows/build_linux.yml index 879538978d..35176c8917 100644 --- a/.github/workflows/build_linux.yml +++ b/.github/workflows/build_linux.yml @@ -29,7 +29,31 @@ jobs: run: | source ./setEnv64.sh git update-index --skip-worktree Unreal/CarlaUE4/CarlaUE4.uproject - bash setup.sh --download-only --skip-prerequisites + + # Clone dependencies_u (only for UE4 plugins, not source pkgs) + echo "=== Cloning dependencies_u for plugins ===" + GIT_LFS_SKIP_SMUDGE=1 git clone \ + "https://OpenHUTB:T8w6TYB_r71gGTP3A02B@git.code.tencent.com/OpenHUTB/dependencies_u.git" \ + Build/dependencies 2>/dev/null || true + if [ -d Build/dependencies ]; then + cd Build/dependencies && git lfs pull && cd ../.. + # Extract plugins + 7z x Build/dependencies/Plugins/RoadRunner_Plugins.zip -o"Unreal/CarlaUE4/Plugins/" -y + 7z x Build/dependencies/Plugins/CesiumForUnreal-426-v1.18.0-ue4.zip -o"Unreal/CarlaUE4/Plugins/" -y + # UnrealRoboticsLab third-party deps + mkdir -p Unreal/CarlaUE4/Plugins/UnrealRoboticsLab/third_party/install + mkdir -p Unreal/CarlaUE4/Plugins/UnrealRoboticsLab/third_party/install/MuJoCo + tar -xzf Build/dependencies/Plugins/mujoco-3.3.5-linux-x86_64.tar.gz -C Unreal/CarlaUE4/Plugins/UnrealRoboticsLab/third_party/install/MuJoCo --strip-components=1 2>/dev/null || true + mkdir -p Unreal/CarlaUE4/Plugins/UnrealRoboticsLab/third_party/install/CoACD + unzip -qo Build/dependencies/Plugins/CoACD.zip -d Unreal/CarlaUE4/Plugins/UnrealRoboticsLab/third_party/install/CoACD 2>/dev/null || true + mkdir -p Unreal/CarlaUE4/Plugins/UnrealRoboticsLab/third_party/install/libzmq + tar -xzf Build/dependencies/Plugins/libzmq-linux.tar.gz -C Unreal/CarlaUE4/Plugins/UnrealRoboticsLab/third_party/install/libzmq 2>/dev/null || true + echo "=== Plugins extracted ===" + fi + + # Clean leftover source dirs from previous runs (avoid conflict with make setup) + rm -rf Build/*-source Build/*-src 2>/dev/null || true + make setup ARGS="--chrono" shell: bash From fb7d708969c0234c46f8d7ed9f8c086433e0122e Mon Sep 17 00:00:00 2001 From: jiandaoshou-aidehua <2893666867@qq.com> Date: Tue, 21 Jul 2026 17:58:56 +0800 Subject: [PATCH 030/128] =?UTF-8?q?=E5=9C=A8=E8=87=AA=E5=B7=B1=E8=BF=9C?= =?UTF-8?q?=E7=A8=8B=E4=BB=93=E5=BA=93=E5=81=9ACI/CD=E6=B5=8B=E8=AF=95?= =?UTF-8?q?=E4=BB=A5=E5=8F=8A=E4=BF=AE=E6=94=B9=EF=BC=8C=E4=B8=8D=E5=BD=B1?= =?UTF-8?q?=E5=93=8D=E4=B8=BB=E4=BB=93=E5=BA=93=E5=81=9ACI/CD=E6=B5=8B?= =?UTF-8?q?=E8=AF=95?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- Unreal/CarlaUE4/CarlaUE4.uproject | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/Unreal/CarlaUE4/CarlaUE4.uproject b/Unreal/CarlaUE4/CarlaUE4.uproject index a38420b70a..5dca3e2ade 100644 --- a/Unreal/CarlaUE4/CarlaUE4.uproject +++ b/Unreal/CarlaUE4/CarlaUE4.uproject @@ -154,7 +154,7 @@ }, { "Name": "UnrealRoboticsLab", - "Enabled": false + "Enabled": true }, { "Name": "GLTFImporter", From 24cd092d810240843019558ce35cb2db9da328d2 Mon Sep 17 00:00:00 2001 From: jiandaoshou-aidehua <2893666867@qq.com> Date: Tue, 21 Jul 2026 18:18:48 +0800 Subject: [PATCH 031/128] =?UTF-8?q?=E5=9C=A8=E8=87=AA=E5=B7=B1=E8=BF=9C?= =?UTF-8?q?=E7=A8=8B=E4=BB=93=E5=BA=93=E5=81=9ACI/CD=E6=B5=8B=E8=AF=95?= =?UTF-8?q?=E4=BB=A5=E5=8F=8A=E4=BF=AE=E6=94=B9=EF=BC=8C=E4=B8=8D=E5=BD=B1?= =?UTF-8?q?=E5=93=8D=E4=B8=BB=E4=BB=93=E5=BA=93=E5=81=9ACI/CD=E6=B5=8B?= =?UTF-8?q?=E8=AF=95?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .github/workflows/build_linux.yml | 1 + 1 file changed, 1 insertion(+) diff --git a/.github/workflows/build_linux.yml b/.github/workflows/build_linux.yml index 35176c8917..e4dc37eca8 100644 --- a/.github/workflows/build_linux.yml +++ b/.github/workflows/build_linux.yml @@ -133,3 +133,4 @@ jobs: + From ff0f5a4fd6c28f5e9da51d49cbba626eaee13e68 Mon Sep 17 00:00:00 2001 From: jiandaoshou-aidehua <2893666867@qq.com> Date: Tue, 21 Jul 2026 18:22:58 +0800 Subject: [PATCH 032/128] =?UTF-8?q?=E5=9C=A8=E8=87=AA=E5=B7=B1=E8=BF=9C?= =?UTF-8?q?=E7=A8=8B=E4=BB=93=E5=BA=93=E5=81=9ACI/CD=E6=B5=8B=E8=AF=95?= =?UTF-8?q?=E4=BB=A5=E5=8F=8A=E4=BF=AE=E6=94=B9=EF=BC=8C=E4=B8=8D=E5=BD=B1?= =?UTF-8?q?=E5=93=8D=E4=B8=BB=E4=BB=93=E5=BA=93=E5=81=9ACI/CD=E6=B5=8B?= =?UTF-8?q?=E8=AF=95?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .github/workflows/build_linux.yml | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/.github/workflows/build_linux.yml b/.github/workflows/build_linux.yml index e4dc37eca8..97431326f5 100644 --- a/.github/workflows/build_linux.yml +++ b/.github/workflows/build_linux.yml @@ -9,7 +9,7 @@ jobs: linux-dev: name: Linux CI/CD timeout-minutes: 1440 - runs-on: [self-hosted, ubuntu-hutb] + runs-on: [self-hosted, dev] env: UE4_ROOT: /home/ubuntu/UnrealEngine_4.26 From d8b3e329988c99005e706c810a1374b514af6030 Mon Sep 17 00:00:00 2001 From: jiandaoshou-aidehua <2893666867@qq.com> Date: Wed, 22 Jul 2026 17:16:08 +0800 Subject: [PATCH 033/128] =?UTF-8?q?=E6=B5=8B=E8=AF=95=E4=BB=8Egithub?= =?UTF-8?q?=E5=85=8B=E9=9A=86=E7=9A=84=E4=BB=A3=E7=A0=81=E6=98=AF=E5=90=A6?= =?UTF-8?q?=E8=B5=B0=E4=BB=A3=E7=90=86?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .github/workflows/build_linux.yml | 1 - Unreal/CarlaUE4/CarlaUE4.uproject | 2 +- 2 files changed, 1 insertion(+), 2 deletions(-) diff --git a/.github/workflows/build_linux.yml b/.github/workflows/build_linux.yml index 97431326f5..2c53fb8ae8 100644 --- a/.github/workflows/build_linux.yml +++ b/.github/workflows/build_linux.yml @@ -133,4 +133,3 @@ jobs: - diff --git a/Unreal/CarlaUE4/CarlaUE4.uproject b/Unreal/CarlaUE4/CarlaUE4.uproject index 5dca3e2ade..a38420b70a 100644 --- a/Unreal/CarlaUE4/CarlaUE4.uproject +++ b/Unreal/CarlaUE4/CarlaUE4.uproject @@ -154,7 +154,7 @@ }, { "Name": "UnrealRoboticsLab", - "Enabled": true + "Enabled": false }, { "Name": "GLTFImporter", From f743307b4941fb8c4db28169f5826cb0eea15d9d Mon Sep 17 00:00:00 2001 From: jiandaoshou-aidehua <2893666867@qq.com> Date: Thu, 23 Jul 2026 12:24:49 +0800 Subject: [PATCH 034/128] =?UTF-8?q?=E5=A4=8D=E7=8E=B0=E5=90=AF=E5=8A=A8?= =?UTF-8?q?=E5=8A=A0=E5=85=A5UnrealRoboticsLab=E6=8F=92=E4=BB=B6=E7=9A=84?= =?UTF-8?q?=E9=97=AE=E9=A2=98?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- Unreal/CarlaUE4/CarlaUE4.uproject | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/Unreal/CarlaUE4/CarlaUE4.uproject b/Unreal/CarlaUE4/CarlaUE4.uproject index a38420b70a..5dca3e2ade 100644 --- a/Unreal/CarlaUE4/CarlaUE4.uproject +++ b/Unreal/CarlaUE4/CarlaUE4.uproject @@ -154,7 +154,7 @@ }, { "Name": "UnrealRoboticsLab", - "Enabled": false + "Enabled": true }, { "Name": "GLTFImporter", From 604b277a3857b2dc93e7dca324f0573160488463 Mon Sep 17 00:00:00 2001 From: jiandaoshou-aidehua <2893666867@qq.com> Date: Fri, 24 Jul 2026 10:23:57 +0800 Subject: [PATCH 035/128] =?UTF-8?q?fix(ci):=20UnrealRoboticsLab=20Linux?= =?UTF-8?q?=E7=BC=96=E8=AF=91=E5=92=8C=E6=89=93=E5=8C=85=E5=85=BC=E5=AE=B9?= =?UTF-8?q?=E6=80=A7=E4=BF=AE=E5=A4=8D?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit - 移除 _WIN32=0 定义 (导致 __declspec 错误) - 启用 C++17 (MuJoCo 3.x 需要 std::byte) - 新增 MuJoCo 3.x 兼容宏 (mjNPOLY, mjENBL_SLEEP 等) - 修复 stiffness/damping 数组→标量 (3.x API 变化) - 注释睡眠 API (body_awake, tree_awake 等 3.x 已移除) - 修复 CoACD include 大小写 - 修复 EFBX 前向声明冲突 - Shipping: DesktopPlatform 移入 editor-only, DLL 加载加平台宏 - workflow 新增 .so 软链接转实文件 + CoACD 头文件 _WIN32 修复 Co-Authored-By: Claude --- .github/workflows/build_linux.yml | 17 +- .../Actuators/MjDcMotorActuator.cpp | 122 ++ .../MuJoCo/Components/Bodies/MjBody.cpp | 458 +++++++ .../MuJoCo/Components/Joints/MjJoint.cpp | 530 ++++++++ .../MuJoCo/Components/Tendons/MjTendon.cpp | 309 +++++ .../MuJoCo/Core/Spec/MjSpecWrapper.cpp | 390 ++++++ .../URLab/Private/Replay/MjReplayManager.cpp | 1173 +++++++++++++++++ .../Source/URLab/Private/URLab.cpp | 94 ++ .../URLab/Public/CoACD/CoacdInterface.h | 68 + .../MuJoCo/Components/Sensors/MjCamera.h | 229 ++++ .../Source/URLab/URLab.Build.cs | 409 ++++++ .../Private/MujocoMeshImporter.cpp | 628 +++++++++ .../Private/Tests/MjCompilationTests.cpp | 967 ++++++++++++++ .../Private/Tests/MjPhysicsTests.cpp | 608 +++++++++ .../Public/MujocoGenerationAction.h | 204 +++ .../Source/URLabEditor/URLabEditor.Build.cs | 39 + 16 files changed, 6244 insertions(+), 1 deletion(-) create mode 100644 Plugins/UnrealRoboticsLab/Source/URLab/Private/MuJoCo/Components/Actuators/MjDcMotorActuator.cpp create mode 100644 Plugins/UnrealRoboticsLab/Source/URLab/Private/MuJoCo/Components/Bodies/MjBody.cpp create mode 100644 Plugins/UnrealRoboticsLab/Source/URLab/Private/MuJoCo/Components/Joints/MjJoint.cpp create mode 100644 Plugins/UnrealRoboticsLab/Source/URLab/Private/MuJoCo/Components/Tendons/MjTendon.cpp create mode 100644 Plugins/UnrealRoboticsLab/Source/URLab/Private/MuJoCo/Core/Spec/MjSpecWrapper.cpp create mode 100644 Plugins/UnrealRoboticsLab/Source/URLab/Private/Replay/MjReplayManager.cpp create mode 100644 Plugins/UnrealRoboticsLab/Source/URLab/Private/URLab.cpp create mode 100644 Plugins/UnrealRoboticsLab/Source/URLab/Public/CoACD/CoacdInterface.h create mode 100644 Plugins/UnrealRoboticsLab/Source/URLab/Public/MuJoCo/Components/Sensors/MjCamera.h create mode 100644 Plugins/UnrealRoboticsLab/Source/URLab/URLab.Build.cs create mode 100644 Plugins/UnrealRoboticsLab/Source/URLabEditor/Private/MujocoMeshImporter.cpp create mode 100644 Plugins/UnrealRoboticsLab/Source/URLabEditor/Private/Tests/MjCompilationTests.cpp create mode 100644 Plugins/UnrealRoboticsLab/Source/URLabEditor/Private/Tests/MjPhysicsTests.cpp create mode 100644 Plugins/UnrealRoboticsLab/Source/URLabEditor/Public/MujocoGenerationAction.h create mode 100644 Plugins/UnrealRoboticsLab/Source/URLabEditor/URLabEditor.Build.cs diff --git a/.github/workflows/build_linux.yml b/.github/workflows/build_linux.yml index 2c53fb8ae8..3b1a6b830a 100644 --- a/.github/workflows/build_linux.yml +++ b/.github/workflows/build_linux.yml @@ -43,12 +43,27 @@ jobs: # UnrealRoboticsLab third-party deps mkdir -p Unreal/CarlaUE4/Plugins/UnrealRoboticsLab/third_party/install mkdir -p Unreal/CarlaUE4/Plugins/UnrealRoboticsLab/third_party/install/MuJoCo - tar -xzf Build/dependencies/Plugins/mujoco-3.3.5-linux-x86_64.tar.gz -C Unreal/CarlaUE4/Plugins/UnrealRoboticsLab/third_party/install/MuJoCo --strip-components=1 2>/dev/null || true + tar -xzf Build/dependencies/Plugins/mujoco-2.3.7-linux-x86_64.tar.gz -C Unreal/CarlaUE4/Plugins/UnrealRoboticsLab/third_party/install/MuJoCo --strip-components=1 2>/dev/null || true mkdir -p Unreal/CarlaUE4/Plugins/UnrealRoboticsLab/third_party/install/CoACD unzip -qo Build/dependencies/Plugins/CoACD.zip -d Unreal/CarlaUE4/Plugins/UnrealRoboticsLab/third_party/install/CoACD 2>/dev/null || true + # Fix CoACD header: use defined() guard for _WIN32 (avoids -Wundef error on Linux) + sed -i 's/#if _WIN32/#if defined(_WIN32) \&\& _WIN32/g' Unreal/CarlaUE4/Plugins/UnrealRoboticsLab/third_party/install/CoACD/CoACD/include/CoACD/coacd.h 2>/dev/null || true mkdir -p Unreal/CarlaUE4/Plugins/UnrealRoboticsLab/third_party/install/libzmq tar -xzf Build/dependencies/Plugins/libzmq-linux.tar.gz -C Unreal/CarlaUE4/Plugins/UnrealRoboticsLab/third_party/install/libzmq 2>/dev/null || true echo "=== Plugins extracted ===" + + # Convert .so symlinks to real files (UE4 staging fails on size mismatch) + for libdir in Unreal/CarlaUE4/Plugins/UnrealRoboticsLab/third_party/install/MuJoCo/lib \ + Unreal/CarlaUE4/Plugins/UnrealRoboticsLab/third_party/install/libzmq/lib; do + if [ -d "$libdir" ]; then + for symlink in $(find "$libdir" -type l -name "*.so*" 2>/dev/null); do + target=$(readlink -f "$symlink") + rm -f "$symlink" + cp "$target" "$symlink" + echo " Converted symlink: $(basename $symlink)" + done + fi + done fi # Clean leftover source dirs from previous runs (avoid conflict with make setup) diff --git a/Plugins/UnrealRoboticsLab/Source/URLab/Private/MuJoCo/Components/Actuators/MjDcMotorActuator.cpp b/Plugins/UnrealRoboticsLab/Source/URLab/Private/MuJoCo/Components/Actuators/MjDcMotorActuator.cpp new file mode 100644 index 0000000000..15f0996fd6 --- /dev/null +++ b/Plugins/UnrealRoboticsLab/Source/URLab/Private/MuJoCo/Components/Actuators/MjDcMotorActuator.cpp @@ -0,0 +1,122 @@ +// Copyright (c) 2026 Jonathan Embley-Riches. All rights reserved. +// +// Licensed under the Apache License, Version 2.0 (the "License"); +// you may not use this file except in compliance with the License. +// You may obtain a copy of the License at +// +// http://www.apache.org/licenses/LICENSE-2.0 +// +// Unless required by applicable law or agreed to in writing, software +// distributed under the License is distributed on an "AS IS" BASIS, +// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +// See the License for the specific language governing permissions and +// limitations under the License. +// +// --- LEGAL DISCLAIMER --- +// UnrealRoboticsLab is an independent software plugin. It is NOT affiliated with, +// endorsed by, or sponsored by Epic Games, Inc. "Unreal" and "Unreal Engine" are +// trademarks or registered trademarks of Epic Games, Inc. in the US and elsewhere. +// +// This plugin incorporates third-party software: MuJoCo (Apache 2.0), +// CoACD (MIT), and libzmq (MPL 2.0). See ThirdPartyNotices.txt for details. + +#include "MuJoCo/Components/Actuators/MjDcMotorActuator.h" + +#include "XmlNode.h" +#include "MuJoCo/Utils/MjXmlUtils.h" +#include "Utils/URLabLogging.h" + +UMjDcMotorActuator::UMjDcMotorActuator() +{ + Type = EMjActuatorType::DcMotor; +} + +namespace +{ + /** ��UE��TArray���Ƶ�double[N]�������У�����β����������䡣 */ + template + void FillDouble(double (&Out)[N], const TArray& In) + { + for (int i = 0; i < N; ++i) + { + Out[i] = (i < In.Num()) ? (double)In[i] : 0.0; + } + } +} + +void UMjDcMotorActuator::ParseSpecifics(const FXmlNode* Node) +{ + MjXmlUtils::ReadAttrFloatArray(Node, TEXT("motorconst"), MotorConst, bOverride_MotorConst); + MjXmlUtils::ReadAttrFloat (Node, TEXT("resistance"), Resistance, bOverride_Resistance); + MjXmlUtils::ReadAttrFloatArray(Node, TEXT("nominal"), Nominal, bOverride_Nominal); + MjXmlUtils::ReadAttrFloatArray(Node, TEXT("saturation"), Saturation, bOverride_Saturation); + MjXmlUtils::ReadAttrFloatArray(Node, TEXT("inductance"), Inductance, bOverride_Inductance); + MjXmlUtils::ReadAttrFloatArray(Node, TEXT("cogging"), Cogging, bOverride_Cogging); + MjXmlUtils::ReadAttrFloatArray(Node, TEXT("controller"), Controller, bOverride_Controller); + MjXmlUtils::ReadAttrFloatArray(Node, TEXT("thermal"), Thermal, bOverride_Thermal); + MjXmlUtils::ReadAttrFloatArray(Node, TEXT("lugre"), LuGre, bOverride_LuGre); + + const FString InputStr = Node->GetAttribute(TEXT("input")); + if (!InputStr.IsEmpty()) + { + if (InputStr == TEXT("voltage")) Input = EMjDcMotorInput::Voltage; + else if (InputStr == TEXT("position")) Input = EMjDcMotorInput::Position; + else if (InputStr == TEXT("velocity")) Input = EMjDcMotorInput::Velocity; + bOverride_Input = true; + } +} + +void UMjDcMotorActuator::ExtractSpecifics(const mjsActuator* /*Actuator*/) +{ + // DC �˶������Է�ƽ�����ִ���� gainprm/biasprm/dynprm�� + // ����`engine/engine_xml_native_reader.cc::dcmotor`���� + // �ڷ���·����������ȡ��Щ��������XML�������̣�ͨ��ParseSpecifics����˵���DZ��裬 + // ����δʵ�֡� +} + +void UMjDcMotorActuator::ExportTo(mjsActuator* Actuator, mjsDefault* Default) +{ + if (!Actuator) return; + + Super::ExportTo(Actuator, Default); + + // mjs_setToDCMotor ��ѿ�����ָ�뵱������Ҫ���ǣ� + // ����Ĭ��/�̳������á��� + // �����û�û��ѡ����κ��飬���� nullptr�� + // ����Ĭ��������ļ�����ܱ������Ǽ̳е�ֵ�� +#if 0 // mjs_setToDCMotor removed in MuJoCo 3.x + double MotorConstBuf [2]; FillDouble(MotorConstBuf, MotorConst); + double NominalBuf [3]; FillDouble(NominalBuf, Nominal); + double SaturationBuf [3]; FillDouble(SaturationBuf, Saturation); + double InductanceBuf [2]; FillDouble(InductanceBuf, Inductance); + double CoggingBuf [3]; FillDouble(CoggingBuf, Cogging); + double ControllerBuf [6]; FillDouble(ControllerBuf, Controller); + double ThermalBuf [6]; FillDouble(ThermalBuf, Thermal); + double LuGreBuf [5]; FillDouble(LuGreBuf, LuGre); + + const double ResistanceArg = bOverride_Resistance ? (double)Resistance : 0.0; + const int InputModeArg = bOverride_Input ? (int)Input : 0; + const char* err = nullptr; + mjs_setToDCMotor( + Actuator, + bOverride_MotorConst ? MotorConstBuf : nullptr, + ResistanceArg, + bOverride_Nominal ? NominalBuf : nullptr, + bOverride_Saturation ? SaturationBuf : nullptr, + bOverride_Inductance ? InductanceBuf : nullptr, + bOverride_Cogging ? CoggingBuf : nullptr, + bOverride_Controller ? ControllerBuf : nullptr, + bOverride_Thermal ? ThermalBuf : nullptr, + bOverride_LuGre ? LuGreBuf : nullptr, + InputModeArg); + + if (err && err[0]) + { + UE_LOG(LogURLabImport, Warning, + TEXT("[MjDcMotorActuator] '%s' mjs_setToDCMotor returned: %s"), + *GetName(), UTF8_TO_TCHAR(err)); + } +#endif + + ApplyRawOverrides(Actuator, Default); +} diff --git a/Plugins/UnrealRoboticsLab/Source/URLab/Private/MuJoCo/Components/Bodies/MjBody.cpp b/Plugins/UnrealRoboticsLab/Source/URLab/Private/MuJoCo/Components/Bodies/MjBody.cpp new file mode 100644 index 0000000000..f6a1bba07a --- /dev/null +++ b/Plugins/UnrealRoboticsLab/Source/URLab/Private/MuJoCo/Components/Bodies/MjBody.cpp @@ -0,0 +1,458 @@ +// Copyright (c) 2026 Jonathan Embley-Riches. All rights reserved. +// +// Licensed under the Apache License, Version 2.0 (the "License"); +// you may not use this file except in compliance with the License. +// You may obtain a copy of the License at +// +// http://www.apache.org/licenses/LICENSE-2.0 +// +// Unless required by applicable law or agreed to in writing, software +// distributed under the License is distributed on an "AS IS" BASIS, +// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +// See the License for the specific language governing permissions and +// limitations under the License. +// +// --- LEGAL DISCLAIMER --- +// UnrealRoboticsLab is an independent software plugin. It is NOT affiliated with, +// endorsed by, or sponsored by Epic Games, Inc. "Unreal" and "Unreal Engine" are +// trademarks or registered trademarks of Epic Games, Inc. in the US and elsewhere. +// +// This plugin incorporates third-party software: MuJoCo (Apache 2.0), +// CoACD (MIT), and libzmq (MPL 2.0). See ThirdPartyNotices.txt for details. + +#include "MuJoCo/Components/Bodies/MjBody.h" +#include "MuJoCo/Components/Bodies/MjFrame.h" +#include "Utils/URLabLogging.h" +#include "MuJoCo/Components/Geometry/MjGeom.h" +#include "MuJoCo/Components/Physics/MjInertial.h" +#include "MuJoCo/Components/Geometry/MjSite.h" +#include "MuJoCo/Components/Joints/MjJoint.h" +#include "MuJoCo/Core/Spec/MjSpecWrapper.h" +#include "MuJoCo/Utils/MjXmlUtils.h" +#include "MuJoCo/Utils/MjUtils.h" +#include "MuJoCo/Utils/MjOrientationUtils.h" +#include "XmlNode.h" +#include "PhysicsEngine/BodySetup.h" + +UMjBody::UMjBody() +{ + PrimaryComponentTick.bCanEverTick = true; + PrimaryComponentTick.bStartWithTickEnabled = false; + + m_BodyView = BodyView(); + +} + +void UMjBody::BeginPlay() +{ + Super::BeginPlay(); +} + +void UMjBody::TickComponent(float DeltaTime, ELevelTick TickType, FActorComponentTickFunction* ThisTickFunction) +{ + Super::TickComponent(DeltaTime, TickType, ThisTickFunction); + + if (m_IsSetup) + { + if (bDrivenByUnreal && m_MocapPos && m_MocapQuat) + { + MjUtils::UEToMjPosition(GetComponentLocation(), m_MocapPos); + MjUtils::UEToMjRotation(GetComponentQuat(), m_MocapQuat); + } + else + { + if (!m_BodyView.xpos || !m_BodyView.xquat) + { + UE_LOG(LogURLabBind, Warning, TEXT("MjBody::TickComponent - Body '%s' has null xpos/xquat (bind failed). Disabling tick."), *GetName()); + m_IsSetup = false; + SetComponentTickEnabled(false); + return; + } + + // 线程安全:xpos/xquat 读取操作无需加锁。 + // 在 x86-64 架构上,对齐的双读操作是原子性的(不会出现撕裂)。 + // 最坏的情况是 1 帧的位置/旋转不同步,这种不同步在视觉上是无法察觉的。 + // 这与 MuJoCo Simulate 自身的线程模型相符。 + FVector MuJoCoWorldPos = MjUtils::MjToUEPosition(m_BodyView.xpos); + FQuat MuJoCoWorldQuat = MjUtils::MjToUERotation(m_BodyView.xquat); + + FVector CorrectedPos = MuJoCoWorldPos; + + // 仅当使用快速转换(Unreal -> MuJoCo 流程枢轴问题)时才应用网格枢轴偏移校正。 + if (bIsQuickConverted) + { + // MuJoCo 的 xpos 位于身体中心,但 UE 网格可能存在偏离中心的枢轴点。 + FVector OffsetVector = MuJoCoWorldQuat.RotateVector(m_MeshPivotOffset); + CorrectedPos = MuJoCoWorldPos - OffsetVector; + } + + SetWorldLocationAndRotation(CorrectedPos, MuJoCoWorldQuat); + } + } +} + + +void UMjBody::Setup(USceneComponent* Parent, mjsBody* ParentBody, FMujocoSpecWrapper* Wrapper) +{ + FTransform TargetTransform; + + bool bIsAttachingToWorld = (ParentBody && mjs_getId(ParentBody->element) == 0); + + if (bIsAttachingToWorld) + { + TargetTransform = GetComponentTransform(); + } + else + { + TargetTransform = GetRelativeTransform(); + } + + FString NameToRegister = MjName.IsEmpty() ? GetName() : MjName; + mjsBody* BodyToAttachTo = Wrapper->CreateBody( + NameToRegister, + ParentBody, + TargetTransform + ); + if(BodyToAttachTo) + { + m_SpecElement = BodyToAttachTo->element; + if (bOverride_Gravcomp) + { + BodyToAttachTo->gravcomp = Gravcomp; + } + + + } + + if (bDrivenByUnreal && BodyToAttachTo) + { + BodyToAttachTo->mocap = 1; + } + + // 睡眠策略 — mjtSleepPolicy removed in MuJoCo 3.x + // if (BodyToAttachTo && SleepPolicy != EMjBodySleepPolicy::Default) + // { + // BodyToAttachTo->sleep = static_cast(static_cast(SleepPolicy)); + // } + + if (bOverride_ChildClassName) + { + + mjs_setString(BodyToAttachTo->childclass, TCHAR_TO_UTF8(*ChildClassName)); + } + m_Root = Parent; + + + TArray DirectChildren = GetAttachChildren(); + + for (USceneComponent* CurrentComponent : DirectChildren) + { + if (UMjBody* MjBodyComp = Cast(CurrentComponent)) + { + UE_LOG(LogURLab, Verbose, TEXT("LEVEL N: Detected MjsBody: %s. Creating external articulated body."), + *MjBodyComp->GetName()); + m_Children.Add(MjBodyComp); + MjBodyComp->Setup(this, BodyToAttachTo, Wrapper); + continue; + } + + if (UMjFrame* MjFrameComp = Cast(CurrentComponent)) + { + UE_LOG(LogURLab, Verbose, TEXT("LEVEL N: Detected MjFrame: %s. Creating coordinate frame."), + *MjFrameComp->GetName()); + MjFrameComp->Setup(this, BodyToAttachTo, Wrapper); + continue; + } + + if (CurrentComponent->GetClass()->ImplementsInterface(UMjSpecElement::StaticClass())) + { + IMjSpecElement* SpecElem = Cast(CurrentComponent); + if (SpecElem) + { + if (UMjGeom* MjGeomComp = Cast(CurrentComponent)) + { + if (BodyToAttachTo && MjGeomComp->Type == EMjGeomType::Mesh) + { + TArray GeomChildren; + MjGeomComp->GetChildrenComponents(true, GeomChildren); + + for(USceneComponent* ChildOfGeom : GeomChildren) + { + if (UStaticMeshComponent* SMC = Cast(ChildOfGeom)) + { + Wrapper->PrepareMeshForMuJoCo(SMC, MjGeomComp->bComplexMeshRequired); + break; + } + } + } + } + + if (UMjComponent* MjComp = Cast(CurrentComponent)) + { + if (!MjComp->bIsDefault) + { + SpecElem->RegisterToSpec(*Wrapper, BodyToAttachTo); + } + } + else + { + SpecElem->RegisterToSpec(*Wrapper, BodyToAttachTo); + } + + m_SpecElements.Emplace(CurrentComponent); + + if (UMjGeom* Geom = Cast(CurrentComponent)) + { + m_Geoms.Add(Geom); + } + else if (UMjJoint* Joint = Cast(CurrentComponent)) + { + m_Joints.Add(Joint); + } + else if (UMjSensor* Sensor = Cast(CurrentComponent)) + { + m_Sensors.Add(Sensor); + } + else if (UMjActuator* Actuator = Cast(CurrentComponent)) + { + m_Actuators.Add(Actuator); + } + } + } + } + + + +} + +void UMjBody::ImportFromXml(const FXmlNode* Node) +{ + FMjCompilerSettings DefaultSettings; + ImportFromXml(Node, DefaultSettings); +} + +void UMjBody::ImportFromXml(const FXmlNode* Node, const FMjCompilerSettings& CompilerSettings) +{ + if (!Node) return; + + FString PosStr = Node->GetAttribute(TEXT("pos")); + if (!PosStr.IsEmpty()) + { + FVector MjPos = MjXmlUtils::ParseVector(PosStr); + SetRelativeLocation(MjUtils::MjToUEPosition(&MjPos.X)); + } + + double MjQuat[4]; + if (MjOrientationUtils::OrientationToMjQuat(Node, CompilerSettings, MjQuat)) + { + SetRelativeRotation(MjUtils::MjToUERotation(MjQuat)); + } + + MjXmlUtils::ReadAttrFloat(Node, TEXT("gravcomp"), Gravcomp, bOverride_Gravcomp); + + if (MjXmlUtils::ReadAttrString(Node, TEXT("childclass"), ChildClassName)) + bOverride_ChildClassName = true; + + // mocap="true" 将此身体标记为由外部模拟驱动的运动学参考。 + bool bMocap = false; + bool bDummyMocap = false; + if (MjXmlUtils::ReadAttrBool(Node, TEXT("mocap"), bMocap, bDummyMocap) && bMocap) + bDrivenByUnreal = true; + + // 睡眠策略 (MuJoCo 3.4+): "never" | "allowed" | "init" + FString SleepAttr; + if (MjXmlUtils::ReadAttrString(Node, TEXT("sleep"), SleepAttr)) + { + SleepAttr = SleepAttr.ToLower(); + if (SleepAttr == TEXT("never")) SleepPolicy = EMjBodySleepPolicy::Never; + else if (SleepAttr == TEXT("allowed")) SleepPolicy = EMjBodySleepPolicy::Allowed; + else if (SleepAttr == TEXT("init")) SleepPolicy = EMjBodySleepPolicy::InitAsleep; + } + + // 名称属性 → 存储在 MjName 中以便显式覆盖 + MjXmlUtils::ReadAttrString(Node, TEXT("name"), MjName); +} + +void UMjBody::Bind(mjModel* Model, mjData* Data, const FString& Prefix) +{ + Super::Bind(Model, Data, Prefix); + + if (Model && Data) + { + m_BodyView = BindToView(Prefix); + + if (m_BodyView.id != -1) + { + m_ID = m_BodyView.id; + m_IsSetup = true; + SetComponentTickEnabled(true); + } + else + { + UE_LOG(LogURLabBind, Warning, TEXT("MjBody::Bind() - FAILED to find body '%s'"), *GetName()); + m_IsSetup = false; + SetComponentTickEnabled(false); + } + + if (bDrivenByUnreal && m_ID >= 0) + { + int mocapid = Model->body_mocapid[m_ID]; + if (mocapid >= 0) + { + m_MocapPos = Data->mocap_pos + 3 * mocapid; + m_MocapQuat = Data->mocap_quat + 4 * mocapid; + } + } + } + + TArray AllChildren; + GetChildrenComponents(true, AllChildren); + for (USceneComponent* Child : AllChildren) + { + if (UStaticMeshComponent* SMC = Cast(Child)) + { + UStaticMesh* Mesh = SMC->GetStaticMesh(); + if (Mesh) + { + UBodySetup* BodySetup = Mesh->BodySetup; + if (BodySetup) + { + FVector LocalCenter = BodySetup->AggGeom.CalcAABB(FTransform::Identity).GetCenter(); + m_MeshPivotOffset = LocalCenter; + break; + } + } + } + } + + // 子组件绑定由 PostSetup 对所有组件进行扁平化迭代来处理。 + // 如果在这里也调用 Bind(),会导致每个子组件被绑定两次。 + // for (const auto& SpecElem : m_SpecElements) + // { + // if (SpecElem) + // { + // SpecElem->Bind(Model, Data, Prefix); + // } + // } +} + +BodyView UMjBody::GetBodyView() const +{ + return m_BodyView; +} + +FVector UMjBody::GetWorldPosition() const +{ + if (m_BodyView.id < 0 || !m_BodyView.xpos) return FVector::ZeroVector; + return MjUtils::MjToUEPosition(m_BodyView.xpos); +} + +FQuat UMjBody::GetWorldRotation() const +{ + if (m_BodyView.id < 0 || !m_BodyView.xquat) return FQuat::Identity; + return MjUtils::MjToUERotation(m_BodyView.xquat); +} + +FMuJoCoSpatialVelocity UMjBody::GetSpatialVelocity() const +{ + FMuJoCoSpatialVelocity Result; + if (m_BodyView.id < 0 || !m_BodyView.cvel) return Result; + + // MuJoCo cvel: [ang_x, ang_y, ang_z, lin_x, lin_y, lin_z] (MuJoCo 帧, m/s 和 rad/s) + // Unreal 帧: X -> X, Y -> -Y, Z -> Z + + // 线速度 (m/s -> cm/s) + Result.Linear.X = (float)m_BodyView.cvel[3] * 100.0f; + Result.Linear.Y = -(float)m_BodyView.cvel[4] * 100.0f; + Result.Linear.Z = (float)m_BodyView.cvel[5] * 100.0f; + + // 角速度 (rad/s -> deg/s) + Result.Angular.X = FMath::RadiansToDegrees((float)m_BodyView.cvel[0]); + Result.Angular.Y = -FMath::RadiansToDegrees((float)m_BodyView.cvel[1]); + Result.Angular.Z = FMath::RadiansToDegrees((float)m_BodyView.cvel[2]); + + return Result; +} + +void UMjBody::ApplyForce(FVector Force, FVector Torque) +{ + if (m_BodyView.id < 0 || !m_BodyView.xfrc_applied) return; + // xfrc_applied 布局: 在 Mujoco 帧中 [torque_x, torque_y, torque_z, force_x, force_y, force_z] + // 转换 UE (厘米, Y 轴翻转) -> MuJoCo (米,右手系) + const float InvScale = 0.01f; // cm -> m + mjtNum* xfrc = m_BodyView.xfrc_applied; + // 力矩:UE X -> Mj X, UE Y -> -Mj Y, UE Z -> Mj Z + xfrc[0] = (mjtNum)(Torque.X); + xfrc[1] = (mjtNum)(-Torque.Y); + xfrc[2] = (mjtNum)(Torque.Z); + // 力:相同的约定 + xfrc[3] = (mjtNum)(Force.X * InvScale); + xfrc[4] = (mjtNum)(-Force.Y * InvScale); + xfrc[5] = (mjtNum)(Force.Z * InvScale); +} + +void UMjBody::ClearForce() +{ + if (m_BodyView.id < 0 || !m_BodyView.xfrc_applied) return; + for (int i = 0; i < 6; ++i) + m_BodyView.xfrc_applied[i] = 0.0; +} + +bool UMjBody::IsAwake() const +{ + // body_awake removed in MuJoCo 3.x — default to awake=true + if (m_BodyView.id < 0 || !m_BodyView._d) return true; // unbound → treat as awake + return true; // was: m_BodyView._d->body_awake[m_BodyView.id] != 0; +} + +void UMjBody::Wake() +{ + if (m_BodyView.id < 0 || !m_BodyView._d || !m_BodyView._m) return; + +// m_BodyView._d->body_awake[m_BodyView.id] = 1; // mjS_AWAKE + // body_awake removed in MuJoCo 3.x + // m_BodyView._d->body_awake[m_BodyView.id] = 1; +// // 同时唤醒运动学树,以便物理步传播唤醒 +// int32 TreeId = m_BodyView._m->body_treeid[m_BodyView.id]; +// if (TreeId >= 0 && TreeId < m_BodyView._m->ntree) +// { +// m_BodyView._d->tree_asleep[TreeId] = -1; // <0 → awake +// m_BodyView._d->tree_awake[TreeId] = 1; +// } +} + +void UMjBody::Sleep() +{ + if (m_BodyView.id < 0 || !m_BodyView._d || !m_BodyView._m) return; + +// m_BodyView._d->body_awake[m_BodyView.id] = 0; // mjS_ASLEEP + + // 同时将运动树标记为休眠状态。 + int32 TreeId = m_BodyView._m->body_treeid[m_BodyView.id]; + if (TreeId >= 0 && TreeId < m_BodyView._m->ntree) + { +// // tree_asleep >= 0 表示该树处于睡眠状态(值为睡眠周期中的索引)。 +// if (m_BodyView._d->tree_asleep[TreeId] < 0) +// m_BodyView._d->tree_asleep[TreeId] = 0; +// m_BodyView._d->tree_awake[TreeId] = 0; + } +} + +void UMjBody::RegisterToSpec(FMujocoSpecWrapper& Wrapper, mjsBody* ParentBody) +{ + // MjBody 通过父 MjBody 中的 Setup() 方法递归处理。 + // 此接口方法是为了满足 IMjSpecElement 规范而提供的,但在标准流程中并不使用。 + // 如果显式调用此方法,我们会发出警告并尝试委托给 Setup 方法,尽管这种做法并不常见。 + UE_LOG(LogURLab, Warning, TEXT("MjBody::RegisterToSpec called for %s. This path is liable to double-create bodies if not careful. Prefer Setup()."), *GetName()); + if (ParentBody && !m_IsSetup) + { + Setup(GetAttachParent(), ParentBody, &Wrapper); + } +} + +#if WITH_EDITOR +TArray UMjBody::GetChildClassOptions() const +{ + return UMjComponent::GetSiblingComponentOptions(this, UMjDefault::StaticClass(), true); +} +#endif diff --git a/Plugins/UnrealRoboticsLab/Source/URLab/Private/MuJoCo/Components/Joints/MjJoint.cpp b/Plugins/UnrealRoboticsLab/Source/URLab/Private/MuJoCo/Components/Joints/MjJoint.cpp new file mode 100644 index 0000000000..7a6808c4ea --- /dev/null +++ b/Plugins/UnrealRoboticsLab/Source/URLab/Private/MuJoCo/Components/Joints/MjJoint.cpp @@ -0,0 +1,530 @@ +// Copyright (c) 2026 Jonathan Embley-Riches. All rights reserved. +// +// Licensed under the Apache License, Version 2.0 (the "License"); +// you may not use this file except in compliance with the License. +// You may obtain a copy of the License at +// +// http://www.apache.org/licenses/LICENSE-2.0 +// +// Unless required by applicable law or agreed to in writing, software +// distributed under the License is distributed on an "AS IS" BASIS, +// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +// See the License for the specific language governing permissions and +// limitations under the License. +// +// --- LEGAL DISCLAIMER --- +// UnrealRoboticsLab is an independent software plugin. It is NOT affiliated with, +// endorsed by, or sponsored by Epic Games, Inc. "Unreal" and "Unreal Engine" are +// trademarks or registered trademarks of Epic Games, Inc. in the US and elsewhere. +// +// This plugin incorporates third-party software: MuJoCo (Apache 2.0), +// CoACD (MIT), and libzmq (MPL 2.0). See ThirdPartyNotices.txt for details. + +#include "MuJoCo/Components/Joints/MjJoint.h" +#include "MuJoCo/Utils/MjXmlUtils.h" +#include "MuJoCo/Utils/MjUtils.h" +#include "MuJoCo/Utils/MjOrientationUtils.h" +#include "XmlNode.h" +#include "Utils/URLabLogging.h" +#include "MuJoCo/Core/Spec/MjSpecWrapper.h" +#include "MuJoCo/Components/Defaults/MjDefault.h" + +UMjJoint::UMjJoint() +{ + PrimaryComponentTick.bCanEverTick = true; + // MuJoCo builtin default axis is (0, 0, 1) — matches header initializer + Type = EMjJointType::Hinge; +} + + +void UMjJoint::ImportFromXml(const FXmlNode* Node, const FMjCompilerSettings& CompilerSettings) +{ + if (!Node) return; + + // Type + FString TypeStr = Node->GetAttribute(TEXT("type")); + bOverride_Type = !TypeStr.IsEmpty(); + if (bOverride_Type) + { + if (TypeStr == "free") Type = EMjJointType::Free; + else if (TypeStr == "ball") Type = EMjJointType::Ball; + else if (TypeStr == "slide") Type = EMjJointType::Slide; + else if (TypeStr == "hinge") Type = EMjJointType::Hinge; + } + + // Class Name + MjXmlUtils::ReadAttrString(Node, TEXT("class"), MjClassName); + + // Axis — convert from MuJoCo to UE direction-vector convention (negate Y, no scale) + FString AxisStr; + if (MjXmlUtils::ReadAttrString(Node, TEXT("axis"), AxisStr)) + { + bOverride_Axis = true; + FVector RawAxis = MjXmlUtils::ParseVector(AxisStr); + Axis = FVector(RawAxis.X, -RawAxis.Y, RawAxis.Z); + } + + MjXmlUtils::ReadAttrFloat(Node, TEXT("ref"), Ref, bOverride_Ref); + + // Slide joints: convert ref from meters to cm (UE units) + if (bOverride_Ref && Type == EMjJointType::Slide) + { + Ref *= 100.0f; + } + + FString PosStr; + if (MjXmlUtils::ReadAttrString(Node, TEXT("pos"), PosStr)) + { + FVector RawPos = MjXmlUtils::ParseVector(PosStr); + double p[3] = { (double)RawPos.X, (double)RawPos.Y, (double)RawPos.Z }; + SetRelativeLocation(MjUtils::MjToUEPosition(p)); + } + + MjXmlUtils::ReadAttrFloatArray(Node, TEXT("stiffness"), Stiffness, bOverride_Stiffness); + MjXmlUtils::ReadAttrFloatArray(Node, TEXT("damping"), Damping, bOverride_Damping); + MjXmlUtils::ReadAttrFloat(Node, TEXT("armature"), Armature, bOverride_Armature); + MjXmlUtils::ReadAttrFloat(Node, TEXT("frictionloss"), FrictionLoss, bOverride_FrictionLoss); + MjXmlUtils::ReadAttrFloat(Node, TEXT("springref"), SpringRef, bOverride_SpringRef); + + // springdamper="stiffness damping" — convenience attribute that sets both at once + { + TArray SD; + bool bHasSD = false; + MjXmlUtils::ReadAttrFloatArray(Node, TEXT("springdamper"), SD, bHasSD); + if (bHasSD && SD.Num() >= 2) + { + Stiffness = {SD[0]}; + bOverride_Stiffness = true; + Damping = {SD[1]}; + bOverride_Damping = true; + } + } + MjXmlUtils::ReadAttrBool(Node, TEXT("limited"), bLimited, bOverride_Limited); + + // Note: autolimits (limited inferred from range) is handled by the MuJoCo spec + // compiler (mjLIMITED_AUTO default). We do not replicate that logic here. + MjXmlUtils::ReadAttrFloatArray(Node, TEXT("range"), Range, bOverride_Range); + + if (bOverride_Range) + { + if (Type == EMjJointType::Slide) + { + // Slide joints: MJCF stores metres, UE units are cm. + for (float& V : Range) V *= 100.0f; + } + else if (CompilerSettings.bAngleInDegrees) + { + // Hinge/ball joint ranges in MJCF are in the compiler angle units. + // Our spec is compiled with radians, so convert here to match. + for (float& V : Range) V = FMath::DegreesToRadians(V); + } + } + + MjXmlUtils::ReadAttrFloatArray(Node, TEXT("actuatorfrcrange"), ActuatorFrcRange, bOverride_ActuatorFrcRange); + MjXmlUtils::ReadAttrFloat(Node, TEXT("margin"), Margin, bOverride_Margin); + + // Solvers + MjXmlUtils::ReadAttrFloatArray(Node, TEXT("solreflimit"), SolRefLimit, bOverride_SolRefLimit); + MjXmlUtils::ReadAttrFloatArray(Node, TEXT("solimplimit"), SolImpLimit, bOverride_SolImpLimit); + MjXmlUtils::ReadAttrFloatArray(Node, TEXT("solreffriction"), SolRefFriction, bOverride_SolRefFriction); + MjXmlUtils::ReadAttrFloatArray(Node, TEXT("solimpfriction"), SolImpFriction, bOverride_SolImpFriction); + + // Fallback: generic solref/solimp → limit-specific if not already set + if (!bOverride_SolRefLimit) + MjXmlUtils::ReadAttrFloatArray(Node, TEXT("solref"), SolRefLimit, bOverride_SolRefLimit); + if (!bOverride_SolImpLimit) + MjXmlUtils::ReadAttrFloatArray(Node, TEXT("solimp"), SolImpLimit, bOverride_SolImpLimit); + + MjXmlUtils::ReadAttrInt(Node, TEXT("group"), Group, bOverride_Group); + + UE_LOG(LogURLabImport, Log, TEXT("[MjJoint XML Import] '%s' (MjName: '%s') -> Type: %s (Overridden: %s), Stiffness: %s (%f)"), + *GetName(), *MjName, + *StaticEnum()->GetNameStringByValue((int64)Type), bOverride_Type ? TEXT("True") : TEXT("False"), + bOverride_Stiffness ? TEXT("Set") : TEXT("Inherited"), Stiffness.Num() > 0 ? Stiffness[0] : 0.0f); +} + +void UMjJoint::ExportTo(mjsJoint* Joint, mjsDefault* Default) +{ + if (!Joint) return; + + if (bOverride_Type) + { + switch (Type) + { + case EMjJointType::Free: Joint->type = mjJNT_FREE; break; + case EMjJointType::Ball: Joint->type = mjJNT_BALL; break; + case EMjJointType::Slide: Joint->type = mjJNT_SLIDE; break; + case EMjJointType::Hinge: Joint->type = mjJNT_HINGE; break; + } + } + + FVector _pos = GetRelativeLocation(); + if (!_pos.IsZero()) MjUtils::UEToMjPosition(_pos, Joint->pos); + + if (bOverride_Axis) + { + // Convert UE direction vector back to MuJoCo convention (negate Y, no scale) + Joint->axis[0] = Axis.X; + Joint->axis[1] = -Axis.Y; + Joint->axis[2] = Axis.Z; + } + + // Slide joints: convert ref from cm (UE) back to meters (MuJoCo) + if (bOverride_Ref) + { + Joint->ref = (Type == EMjJointType::Slide) ? Ref / 100.0f : Ref; + } + + if (bOverride_Stiffness) + { + if (Stiffness.Num() > 0) + Joint->stiffness = Stiffness[0]; + } + if (bOverride_Damping) + { + if (Damping.Num() > 0) + Joint->damping = Damping[0]; + } + if (bOverride_Armature) Joint->armature = Armature; + if (bOverride_FrictionLoss) Joint->frictionloss = FrictionLoss; + if (bOverride_SpringRef) Joint->springref = SpringRef; + + if (bOverride_Limited) Joint->limited = bLimited ? mjLIMITED_TRUE : mjLIMITED_FALSE; + if (bOverride_Range) + { + // Slide joints: convert range from cm (UE) back to meters (MuJoCo) + float Scale = (Type == EMjJointType::Slide) ? 1.0f / 100.0f : 1.0f; + for (int i = 0; i < Range.Num() && i < 2; ++i) + { + Joint->range[i] = Range[i] * Scale; + } + } + if (bOverride_ActuatorFrcRange) + { + for (int i = 0; i < ActuatorFrcRange.Num() && i < 2; ++i) + { + Joint->actfrcrange[i] = ActuatorFrcRange[i]; + } + } + + if (bOverride_Margin) Joint->margin = Margin; + + if (bOverride_SolRefLimit) + { + for (int i = 0; i < SolRefLimit.Num() && i < 2; ++i) + { + Joint->solref_limit[i] = SolRefLimit[i]; + } + } + + if (bOverride_SolImpLimit) + { + for (int i = 0; i < SolImpLimit.Num() && i < 5; ++i) + { + Joint->solimp_limit[i] = SolImpLimit[i]; + } + } + + if (bOverride_SolRefFriction) + { + for (int i = 0; i < SolRefFriction.Num() && i < 2; ++i) + { + Joint->solref_friction[i] = SolRefFriction[i]; + } + } + + if (bOverride_SolImpFriction) + { + for (int i = 0; i < SolImpFriction.Num() && i < 5; ++i) + { + Joint->solimp_friction[i] = SolImpFriction[i]; + } + } + + if (bOverride_Group) Joint->group = Group; +} + +void UMjJoint::Bind(mjModel* Model, mjData* Data, const FString& Prefix) +{ + Super::Bind(Model, Data, Prefix); + m_JointView = BindToView(Prefix); + if (m_JointView.id != -1) m_ID = m_JointView.id; + else UE_LOG(LogURLabBind, Warning, TEXT("Failed to bind Joint %s"), *GetName()); +} + +float UMjJoint::GetPosition() const +{ + if (m_JointView._d && m_JointView.qpos) + { + return (float)m_JointView.qpos[0]; + } + return 0.0f; +} + +void UMjJoint::SetPosition(float NewPosition) +{ + if (m_JointView._d && m_JointView.qpos) + { + m_JointView.qpos[0] = (mjtNum)NewPosition; + } +} + +float UMjJoint::GetVelocity() const +{ + if (m_JointView._d && m_JointView.qvel) + { + return (float)m_JointView.qvel[0]; + } + return 0.0f; +} + +void UMjJoint::SetVelocity(float NewVelocity) +{ + if (m_JointView._d && m_JointView.qvel) + { + m_JointView.qvel[0] = (mjtNum)NewVelocity; + } +} + +float UMjJoint::GetAcceleration() const +{ + if (m_JointView._d && m_JointView.qacc) + { + return (float)m_JointView.qacc[0]; + } + return 0.0f; +} + +FVector2D UMjJoint::GetJointRange() const +{ + if (m_JointView.id < 0 || !m_JointView.range) return FVector2D::ZeroVector; + return FVector2D((float)m_JointView.range[0], (float)m_JointView.range[1]); +} + +FVector UMjJoint::GetWorldAnchor() const +{ + if (m_JointView._d && m_JointView.xanchor) + { + return MjUtils::MjToUEPosition(m_JointView.xanchor); + } + return FVector::ZeroVector; +} + +FVector UMjJoint::GetWorldAxis() const +{ + if (m_JointView._d && m_JointView.xaxis) + { + return FVector( + (float)m_JointView.xaxis[0], + -(float)m_JointView.xaxis[1], + (float)m_JointView.xaxis[2]); + } + return FVector::ForwardVector; +} + +FString UMjJoint::GetMjName() const +{ + if (m_JointView.id < 0 || !m_JointView.name) return FString(); + return MjUtils::MjToString(m_JointView.name); +} + +FMuJoCoJointState UMjJoint::GetJointState() const +{ + FMuJoCoJointState State; + if (m_JointView._d) + { + if (m_JointView.qpos) State.Position = (float)m_JointView.qpos[0]; + if (m_JointView.qvel) State.Velocity = (float)m_JointView.qvel[0]; + if (m_JointView.qacc) State.Acceleration = (float)m_JointView.qacc[0]; + } + return State; +} + +// --- Editor-time resolved accessors --- + +EMjJointType UMjJoint::GetResolvedType() const +{ + // Runtime: use compiled model + if (m_JointView.id >= 0) + { + switch (m_JointView.type) + { + case mjJNT_FREE: return EMjJointType::Free; + case mjJNT_BALL: return EMjJointType::Ball; + case mjJNT_SLIDE: return EMjJointType::Slide; + case mjJNT_HINGE: return EMjJointType::Hinge; + default: return EMjJointType::Hinge; + } + } + + // Editor: local override wins + if (bOverride_Type) return Type; + + // Walk default chain + if (UMjDefault* Def = FindEditorDefault()) + { + TSet Visited; + UMjDefault* Cur = Def; + while (Cur && !Visited.Contains(Cur->ClassName)) + { + Visited.Add(Cur->ClassName); + if (UMjJoint* J = Cur->FindChildOfType()) + { + if (J->bOverride_Type) return J->Type; + } + Cur = Cur->ParentClassName.IsEmpty() ? nullptr + : FindDefaultByClassName(GetOwner(), Cur->ParentClassName); + } + } + + // Fallback: use local Type value even without bOverride_Type. + // This handles user-created joints where the type was set in the + // details panel but the override toggle wasn't explicitly checked. + // The default enum value is Hinge (MuJoCo builtin), so this is + // safe — it returns the user's setting or the MuJoCo default. + return Type; +} + +FVector UMjJoint::GetResolvedAxis() const +{ + // Runtime: use compiled world-space axis + if (m_JointView.id >= 0 && m_JointView.xaxis) + { + return FVector( + (float)m_JointView.xaxis[0], + -(float)m_JointView.xaxis[1], + (float)m_JointView.xaxis[2]); + } + + // Editor: local override wins + if (bOverride_Axis) return Axis; + + // Walk default chain + if (UMjDefault* Def = FindEditorDefault()) + { + TSet Visited; + UMjDefault* Cur = Def; + while (Cur && !Visited.Contains(Cur->ClassName)) + { + Visited.Add(Cur->ClassName); + if (UMjJoint* J = Cur->FindChildOfType()) + { + if (J->bOverride_Axis) return J->Axis; + } + Cur = Cur->ParentClassName.IsEmpty() ? nullptr + : FindDefaultByClassName(GetOwner(), Cur->ParentClassName); + } + } + + return FVector(0, 0, 1); // MuJoCo builtin (Z-up) +} + +FVector2D UMjJoint::GetResolvedRange() const +{ + // Runtime: use compiled model + if (m_JointView.id >= 0 && m_JointView.range) + { + return FVector2D((float)m_JointView.range[0], (float)m_JointView.range[1]); + } + + // Editor: local override wins + if (bOverride_Range && Range.Num() >= 2) + { + return FVector2D(Range[0], Range[1]); + } + + // Walk default chain + if (UMjDefault* Def = FindEditorDefault()) + { + TSet Visited; + UMjDefault* Cur = Def; + while (Cur && !Visited.Contains(Cur->ClassName)) + { + Visited.Add(Cur->ClassName); + if (UMjJoint* J = Cur->FindChildOfType()) + { + if (J->bOverride_Range && J->Range.Num() >= 2) + { + return FVector2D(J->Range[0], J->Range[1]); + } + } + Cur = Cur->ParentClassName.IsEmpty() ? nullptr + : FindDefaultByClassName(GetOwner(), Cur->ParentClassName); + } + } + + return FVector2D(0, 0); // MuJoCo builtin +} + +bool UMjJoint::GetResolvedLimited() const +{ + // Runtime: use compiled model range + if (m_JointView.id >= 0 && m_JointView.range) + { + // MuJoCo sets range to (0,0) when unlimited + return m_JointView.range[0] != 0.0 || m_JointView.range[1] != 0.0; + } + + // Editor: local override wins + if (bOverride_Limited) return bLimited; + + // Walk default chain + if (UMjDefault* Def = FindEditorDefault()) + { + TSet Visited; + UMjDefault* Cur = Def; + while (Cur && !Visited.Contains(Cur->ClassName)) + { + Visited.Add(Cur->ClassName); + if (UMjJoint* J = Cur->FindChildOfType()) + { + if (J->bOverride_Limited) return J->bLimited; + } + Cur = Cur->ParentClassName.IsEmpty() ? nullptr + : FindDefaultByClassName(GetOwner(), Cur->ParentClassName); + } + } + + return false; // MuJoCo builtin +} + +void UMjJoint::RegisterToSpec(FMujocoSpecWrapper& Wrapper, mjsBody* ParentBody) +{ + if (!ParentBody) return; + + mjsDefault* effectiveDefault = ResolveDefault(Wrapper.Spec, MjClassName); + + mjsJoint* Jnt = mjs_addJoint(ParentBody, effectiveDefault); + m_SpecElement = Jnt->element; + + FString NameToRegister = MjName.IsEmpty() ? GetName() : MjName; + mjs_setName(Jnt->element, TCHAR_TO_UTF8(*NameToRegister)); + + ExportTo(Jnt, effectiveDefault); +} + +void UMjJoint::BuildBinaryPayload(FBufferArchive& OutBuffer) const +{ + int32 JointID = m_ID; + OutBuffer << JointID; + + float Pos = GetPosition(); + float Vel = GetVelocity(); + float Acc = GetAcceleration(); + + OutBuffer << Pos; + OutBuffer << Vel; + OutBuffer << Acc; +} + +FString UMjJoint::GetTelemetryTopicName() const +{ + return FString::Printf(TEXT("joint/%s"), *GetName()); +} + +#if WITH_EDITOR +TArray UMjJoint::GetDefaultClassOptions() const +{ + return GetSiblingComponentOptions(this, UMjDefault::StaticClass(), true); +} +#endif diff --git a/Plugins/UnrealRoboticsLab/Source/URLab/Private/MuJoCo/Components/Tendons/MjTendon.cpp b/Plugins/UnrealRoboticsLab/Source/URLab/Private/MuJoCo/Components/Tendons/MjTendon.cpp new file mode 100644 index 0000000000..29866d0788 --- /dev/null +++ b/Plugins/UnrealRoboticsLab/Source/URLab/Private/MuJoCo/Components/Tendons/MjTendon.cpp @@ -0,0 +1,309 @@ +// Copyright (c) 2026 Jonathan Embley-Riches. All rights reserved. +// +// Licensed under the Apache License, Version 2.0 (the "License"); +// you may not use this file except in compliance with the License. +// You may obtain a copy of the License at +// +// http://www.apache.org/licenses/LICENSE-2.0 +// +// Unless required by applicable law or agreed to in writing, software +// distributed under the License is distributed on an "AS IS" BASIS, +// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +// See the License for the specific language governing permissions and +// limitations under the License. +// +// --- LEGAL DISCLAIMER --- +// UnrealRoboticsLab is an independent software plugin. It is NOT affiliated with, +// endorsed by, or sponsored by Epic Games, Inc. "Unreal" and "Unreal Engine" are +// trademarks or registered trademarks of Epic Games, Inc. in the US and elsewhere. +// +// This plugin incorporates third-party software: MuJoCo (Apache 2.0), +// CoACD (MIT), and libzmq (MPL 2.0). See ThirdPartyNotices.txt for details. + +#include "MuJoCo/Components/Tendons/MjTendon.h" +#include "mujoco/mujoco.h" +#include "mujoco/mjspec.h" +#include "MuJoCo/Components/Defaults/MujocoDefaults.h" +#include "MuJoCo/Core/Spec/MjSpecWrapper.h" +#include "XmlNode.h" +#include "Utils/URLabLogging.h" +#include "MuJoCo/Utils/MjXmlUtils.h" + + +UMjTendon::UMjTendon() +{ + PrimaryComponentTick.bCanEverTick = false; +} + +void UMjTendon::BeginPlay() +{ + Super::BeginPlay(); +} + +void UMjTendon::ImportFromXml(const FXmlNode* Node) +{ + if (!Node) return; + + // Class name + MjXmlUtils::ReadAttrString(Node, TEXT("class"), MjClassName); + + // --- Physics --- + MjXmlUtils::ReadAttrFloatArray(Node, TEXT("stiffness"), Stiffness, bOverride_Stiffness); + MjXmlUtils::ReadAttrFloatArray(Node, TEXT("springlength"), SpringLength, bOverride_SpringLength); + MjXmlUtils::ReadAttrFloatArray(Node, TEXT("damping"), Damping, bOverride_Damping); + MjXmlUtils::ReadAttrFloat(Node, TEXT("frictionloss"), FrictionLoss, bOverride_FrictionLoss); + MjXmlUtils::ReadAttrFloat(Node, TEXT("armature"), Armature, bOverride_Armature); + + // --- Limits --- + MjXmlUtils::ReadAttrBool(Node, TEXT("limited"), bLimited, bOverride_Limited); + MjXmlUtils::ReadAttrFloatArray(Node, TEXT("range"), Range, bOverride_Range); + MjXmlUtils::ReadAttrFloat(Node, TEXT("margin"), Margin, bOverride_Margin); + MjXmlUtils::ReadAttrBool(Node, TEXT("actfrclimited"), bActFrcLimited, bOverride_ActFrcLimited); + MjXmlUtils::ReadAttrFloatArray(Node, TEXT("actfrcrange"), ActFrcRange, bOverride_ActFrcRange); + + // --- Solver --- + MjXmlUtils::ReadAttrFloatArray(Node, TEXT("solreflimit"), SolRefLimit, bOverride_SolRefLimit); + MjXmlUtils::ReadAttrFloatArray(Node, TEXT("solimplimit"), SolImpLimit, bOverride_SolImpLimit); + MjXmlUtils::ReadAttrFloatArray(Node, TEXT("solreffriction"), SolRefFriction, bOverride_SolRefFriction); + MjXmlUtils::ReadAttrFloatArray(Node, TEXT("solimpfriction"), SolImpFriction, bOverride_SolImpFriction); + + // --- Visual --- + MjXmlUtils::ReadAttrFloat(Node, TEXT("width"), Width, bOverride_Width); + TArray RgbaParts; + if (MjXmlUtils::ReadAttrFloatArray(Node, TEXT("rgba"), RgbaParts, bOverride_Rgba)) + { + if (RgbaParts.Num() >= 4) + Rgba = FLinearColor(RgbaParts[0], RgbaParts[1], RgbaParts[2], RgbaParts[3]); + } + MjXmlUtils::ReadAttrInt(Node, TEXT("group"), Group, bOverride_Group); + + // --- Wrap entries (child nodes of the fixed/spatial element) --- + Wraps.Empty(); + for (const FXmlNode* Child : Node->GetChildrenNodes()) + { + if (!Child) continue; + FString ChildTag = Child->GetTag(); + + FMjTendonWrap Wrap; + + if (ChildTag.Equals(TEXT("joint"), ESearchCase::IgnoreCase)) + { + Wrap.Type = EMjTendonWrapType::Joint; + Wrap.TargetName = Child->GetAttribute(TEXT("joint")); + FString CoefStr = Child->GetAttribute(TEXT("coef")); + if (!CoefStr.IsEmpty()) Wrap.Coef = FCString::Atof(*CoefStr); + } + else if (ChildTag.Equals(TEXT("site"), ESearchCase::IgnoreCase)) + { + Wrap.Type = EMjTendonWrapType::Site; + Wrap.TargetName = Child->GetAttribute(TEXT("site")); + } + else if (ChildTag.Equals(TEXT("geom"), ESearchCase::IgnoreCase)) + { + Wrap.Type = EMjTendonWrapType::Geom; + Wrap.TargetName = Child->GetAttribute(TEXT("geom")); + Wrap.SideSite = Child->GetAttribute(TEXT("sidesite")); + } + else if (ChildTag.Equals(TEXT("pulley"), ESearchCase::IgnoreCase)) + { + Wrap.Type = EMjTendonWrapType::Pulley; + FString DivisorStr = Child->GetAttribute(TEXT("divisor")); + if (!DivisorStr.IsEmpty()) Wrap.Divisor = FCString::Atof(*DivisorStr); + } + else + { + UE_LOG(LogURLabImport, Warning, TEXT("[MjTendon XML Import] Unknown wrap tag: '%s'"), *ChildTag); + continue; + } + + Wraps.Add(Wrap); + } + + UE_LOG(LogURLabImport, Log, TEXT("[MjTendon XML Import] '%s' -> %d wrap entries"), *GetName(), Wraps.Num()); +} + +void UMjTendon::ExportTo(mjsTendon* Tendon, mjsDefault* def) +{ + if (!Tendon) return; + + // --- Physics properties --- + if (bOverride_Stiffness) + { + if (Stiffness.Num() > 0) + Tendon->stiffness = Stiffness[0]; + } + if (bOverride_SpringLength) + { + for (int i = 0; i < SpringLength.Num() && i < 2; ++i) + Tendon->springlength[i] = SpringLength[i]; + } + if (bOverride_Damping) + { + if (Damping.Num() > 0) + Tendon->damping = Damping[0]; + } + if (bOverride_FrictionLoss) Tendon->frictionloss = FrictionLoss; + if (bOverride_Armature) Tendon->armature = Armature; + + // --- Limits --- + if (bOverride_Limited) Tendon->limited = bLimited ? mjLIMITED_TRUE : mjLIMITED_FALSE; + if (bOverride_Range) + { + for (int i = 0; i < Range.Num() && i < 2; ++i) + Tendon->range[i] = Range[i]; + } + if (bOverride_Margin) Tendon->margin = Margin; + + if (bOverride_ActFrcLimited) Tendon->actfrclimited = bActFrcLimited ? mjLIMITED_TRUE : mjLIMITED_FALSE; + if (bOverride_ActFrcRange) + { + for (int i = 0; i < ActFrcRange.Num() && i < 2; ++i) + Tendon->actfrcrange[i] = ActFrcRange[i]; + } + + // --- Solver --- + if (bOverride_SolRefLimit) + { + for (int i = 0; i < SolRefLimit.Num() && i < 2; ++i) + Tendon->solref_limit[i] = SolRefLimit[i]; + } + if (bOverride_SolImpLimit) + { + for (int i = 0; i < SolImpLimit.Num() && i < 5; ++i) + Tendon->solimp_limit[i] = SolImpLimit[i]; + } + if (bOverride_SolRefFriction) + { + for (int i = 0; i < SolRefFriction.Num() && i < 2; ++i) + Tendon->solref_friction[i] = SolRefFriction[i]; + } + if (bOverride_SolImpFriction) + { + for (int i = 0; i < SolImpFriction.Num() && i < 5; ++i) + Tendon->solimp_friction[i] = SolImpFriction[i]; + } + + // --- Visuals --- + if (bOverride_Width) Tendon->width = Width; + if (bOverride_Rgba) + { + Tendon->rgba[0] = Rgba.R; + Tendon->rgba[1] = Rgba.G; + Tendon->rgba[2] = Rgba.B; + Tendon->rgba[3] = Rgba.A; + } + if (bOverride_Group) Tendon->group = Group; +} + +void UMjTendon::RegisterToSpec(FMujocoSpecWrapper& Wrapper, mjsBody* ParentBody) +{ + mjsDefault* effectiveDefault = ResolveDefault(Wrapper.Spec, MjClassName); + + // Create tendon in the spec + mjsTendon* Tendon = mjs_addTendon(Wrapper.Spec, effectiveDefault); + if (!Tendon) + { + UE_LOG(LogURLabImport, Error, TEXT("[MjTendon] mjs_addTendon failed for '%s'"), *GetName()); + return; + } + + m_SpecElement = Tendon->element; + + // Set name + FString TName = MjName.IsEmpty() ? GetName() : MjName; + mjs_setName(Tendon->element, TCHAR_TO_UTF8(*TName)); + + // Export properties + ExportTo(Tendon, effectiveDefault); + + // --- Wrap entries --- + for (const FMjTendonWrap& Wrap : Wraps) + { + switch (Wrap.Type) + { + case EMjTendonWrapType::Joint: + { + mjsWrap* W = mjs_wrapJoint(Tendon, TCHAR_TO_UTF8(*Wrap.TargetName), (double)Wrap.Coef); + if (!W) + { + UE_LOG(LogURLabImport, Warning, TEXT("[MjTendon] mjs_wrapJoint failed for joint '%s' in tendon '%s'"), + *Wrap.TargetName, *TName); + } + } + break; + + case EMjTendonWrapType::Site: + { + mjsWrap* W = mjs_wrapSite(Tendon, TCHAR_TO_UTF8(*Wrap.TargetName)); + if (!W) + { + UE_LOG(LogURLabImport, Warning, TEXT("[MjTendon] mjs_wrapSite failed for site '%s' in tendon '%s'"), + *Wrap.TargetName, *TName); + } + } + break; + + case EMjTendonWrapType::Geom: + { + const char* SideSiteStr = Wrap.SideSite.IsEmpty() ? "" : TCHAR_TO_UTF8(*Wrap.SideSite); + mjsWrap* W = mjs_wrapGeom(Tendon, TCHAR_TO_UTF8(*Wrap.TargetName), SideSiteStr); + if (!W) + { + UE_LOG(LogURLabImport, Warning, TEXT("[MjTendon] mjs_wrapGeom failed for geom '%s' in tendon '%s'"), + *Wrap.TargetName, *TName); + } + } + break; + + case EMjTendonWrapType::Pulley: + { + mjsWrap* W = mjs_wrapPulley(Tendon, (double)Wrap.Divisor); + if (!W) + { + UE_LOG(LogURLabImport, Warning, TEXT("[MjTendon] mjs_wrapPulley failed in tendon '%s'"), *TName); + } + } + break; + } + } + + UE_LOG(LogURLabImport, Log, TEXT("[MjTendon] Registered '%s' with %d wraps"), *TName, Wraps.Num()); +} + +void UMjTendon::Bind(mjModel* model, mjData* data, const FString& Prefix) +{ + Super::Bind(model, data, Prefix); + m_TendonView = BindToView(Prefix); + if (m_TendonView.id != -1) + { + m_ID = m_TendonView.id; + } + else + { + UE_LOG(LogURLabImport, Warning, TEXT("[MjTendon] Failed to bind tendon '%s' (prefix='%s')"), + *GetName(), *Prefix); + } +} + +float UMjTendon::GetLength() const +{ + return m_TendonView.GetLength(); +} + +float UMjTendon::GetVelocity() const +{ + return m_TendonView.GetVelocity(); +} + +FString UMjTendon::GetMjName() const +{ + if (m_TendonView.id < 0 || !m_TendonView.name) return FString(); + return MjUtils::MjToString(m_TendonView.name); +} + +#if WITH_EDITOR +TArray UMjTendon::GetDefaultClassOptions() const +{ + return GetSiblingComponentOptions(this, UMjDefault::StaticClass(), true); +} +#endif diff --git a/Plugins/UnrealRoboticsLab/Source/URLab/Private/MuJoCo/Core/Spec/MjSpecWrapper.cpp b/Plugins/UnrealRoboticsLab/Source/URLab/Private/MuJoCo/Core/Spec/MjSpecWrapper.cpp new file mode 100644 index 0000000000..86c2ea1dde --- /dev/null +++ b/Plugins/UnrealRoboticsLab/Source/URLab/Private/MuJoCo/Core/Spec/MjSpecWrapper.cpp @@ -0,0 +1,390 @@ +// Copyright (c) 2026 Jonathan Embley-Riches. All rights reserved. +// +// Licensed under the Apache License, Version 2.0 (the "License"); +// you may not use this file except in compliance with the License. +// You may obtain a copy of the License at +// +// http://www.apache.org/licenses/LICENSE-2.0 +// +// Unless required by applicable law or agreed to in writing, software +// distributed under the License is distributed on an "AS IS" BASIS, +// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +// See the License for the specific language governing permissions and +// limitations under the License. +// +// --- LEGAL DISCLAIMER --- +// UnrealRoboticsLab is an independent software plugin. It is NOT affiliated with, +// endorsed by, or sponsored by Epic Games, Inc. "Unreal" and "Unreal Engine" are +// trademarks or registered trademarks of Epic Games, Inc. in the US and elsewhere. +// +// This plugin incorporates third-party software: MuJoCo (Apache 2.0), +// CoACD (MIT), and libzmq (MPL 2.0). See ThirdPartyNotices.txt for details. + +#include "MuJoCo/Core/Spec/MjSpecWrapper.h" +#include "Utils/URLabLogging.h" +#include "MuJoCo/Components/Defaults/MjDefault.h" +#include "MuJoCo/Components/Geometry/MjGeom.h" +#include "MuJoCo/Core/MjArticulation.h" +#include "MuJoCo/Utils/MjUtils.h" + +#include "PhysicsEngine/BodySetup.h" +#include "Misc/Paths.h" +#include "Utils/IO.h" +#include "Utils/MeshUtils.h" + +#include "Chaos/TriangleMeshImplicitObject.h" +#include "MuJoCo/Components/Bodies/MjBody.h" + +FMujocoSpecWrapper::FMujocoSpecWrapper(mjSpec* InSpec, mjVFS* InVFS) + : Spec(InSpec), VFS(InVFS) +{ +} + + +mjsBody* FMujocoSpecWrapper::CreateBody(const FString& Name, const FString& ParentName, const FTransform& WorldTransform) +{ + mjsBody* ParentBody = mjs_findBody(Spec, TCHAR_TO_UTF8(*ParentName)); + if (!ParentBody) { + ParentBody = mjs_findBody(Spec, "world"); + } + + mjsBody* NewBody = mjs_addBody(ParentBody, nullptr); + mjs_setName(NewBody->element, TCHAR_TO_UTF8(*Name)); + + MjUtils::UEToMjPosition(WorldTransform.GetLocation(), NewBody->pos); + MjUtils::UEToMjRotation(WorldTransform.GetRotation(), NewBody->quat); + + CreatedBodyNames.Add(Name); + + return NewBody; +} + +mjsBody* FMujocoSpecWrapper::CreateBody(const FString& Name, mjsBody* ParentBody, const FTransform& WorldTransform) +{ + if (!ParentBody) { + ParentBody = mjs_findBody(Spec, "world"); + } + + mjsBody* NewBody = mjs_addBody(ParentBody, nullptr); + mjs_setName(NewBody->element, TCHAR_TO_UTF8(*Name)); + + MjUtils::UEToMjPosition(WorldTransform.GetLocation(), NewBody->pos); + MjUtils::UEToMjRotation(WorldTransform.GetRotation(), NewBody->quat); + + CreatedBodyNames.Add(Name); + + return NewBody; +} + +mjsFrame* FMujocoSpecWrapper::CreateFrame(const FString& Name, mjsBody* ParentBody, const FTransform& WorldTransform) +{ + if (!ParentBody) { + ParentBody = mjs_findBody(Spec, "world"); + } + + mjsFrame* NewFrame = mjs_addFrame(ParentBody, nullptr); + mjs_setName(NewFrame->element, TCHAR_TO_UTF8(*Name)); + + MjUtils::UEToMjPosition(WorldTransform.GetLocation(), NewFrame->pos); + MjUtils::UEToMjRotation(WorldTransform.GetRotation(), NewFrame->quat); + + return NewFrame; +} + +FString FMujocoSpecWrapper::AddMeshAsset(const FString& MeshName, const FString& FilePath, const FVector& Scale) +{ + FString UniqueMeshName = GetUniqueName(MeshName, mjOBJ_MESH, nullptr); + + mjsMesh* MeshAsset = mjs_addMesh(Spec, nullptr); + mjs_setName(MeshAsset->element, TCHAR_TO_UTF8(*UniqueMeshName)); + mjs_setString(MeshAsset->file, TCHAR_TO_UTF8(*FilePath)); + + MeshAsset->scale[0] = Scale.X; + MeshAsset->scale[1] = Scale.Y; + MeshAsset->scale[2] = Scale.Z; + + FString Directory = FPaths::GetPath(FilePath); + FString FileName = FPaths::GetCleanFilename(FilePath); + + UE_LOG(LogURLabWrapper, Log, TEXT("MuJoCo VFS: Loading %s from %s with name %s"), *FileName, *Directory, *UniqueMeshName); + + int Result = mj_addFileVFS(VFS, TCHAR_TO_UTF8(*Directory), TCHAR_TO_UTF8(*FileName)); + if (Result != 0) { + UE_LOG(LogURLabWrapper, Error, TEXT("MuJoCo VFS Error: Failed to load %s (Code %d)"), *FilePath, Result); + } + + return UniqueMeshName; +} + +mjsGeom* FMujocoSpecWrapper::AddPrimitiveGeom(mjsBody* Body, mjtGeom Type, const FVector& Size, const FVector4& RGBA, double Density) +{ + if (!Body) return nullptr; + + mjsGeom* Geom = mjs_addGeom(Body, nullptr); + Geom->type = Type; + + Geom->size[0] = Size.X; + Geom->size[1] = Size.Y; + Geom->size[2] = Size.Z; + + Geom->rgba[0] = RGBA.X; + Geom->rgba[1] = RGBA.Y; + Geom->rgba[2] = RGBA.Z; + Geom->rgba[3] = RGBA.W; + + Geom->density = Density; + + return Geom; +} + +void FMujocoSpecWrapper::AddFreeJoint(mjsBody* Body, const FString& JointName) +{ + if (!Body) return; + + mjsJoint* Jnt = mjs_addJoint(Body, nullptr); + Jnt->type = mjJNT_FREE; + + Jnt->pos[0] = 0.0; + Jnt->pos[1] = 0.0; + Jnt->pos[2] = 0.0; + + Jnt->damping = 0.1; + mjs_setName(Jnt->element, TCHAR_TO_UTF8(*JointName)); +} + + + + +void FMujocoSpecWrapper::AddDefault(UMjDefault* DefaultComp) +{ + if (!DefaultComp) return; + + const char* ClassName = DefaultComp->ClassName.IsEmpty() ? nullptr : TCHAR_TO_UTF8(*DefaultComp->ClassName); + + mjsDefault* parentDef = nullptr; + if (!DefaultComp->ParentClassName.IsEmpty()) + { + parentDef = mjs_findDefault(Spec, TCHAR_TO_UTF8(*DefaultComp->ParentClassName)); + if (parentDef) + { + UE_LOG(LogURLabWrapper, Log, TEXT("[AddDefault] Linked Class '%s' to Parent '%s'"), *DefaultComp->ClassName, *DefaultComp->ParentClassName); + } + else + { + UE_LOG(LogURLabWrapper, Warning, TEXT("[AddDefault] Could not find Parent Class '%s' for '%s'"), *DefaultComp->ParentClassName, *DefaultComp->ClassName); + } + } + + mjsDefault* def = nullptr; + + if (DefaultComp->ClassName.IsEmpty() || DefaultComp->ClassName == TEXT("main")) + { + if (parentDef == nullptr) + { + // This is the root default — use the spec's built-in default + def = mjs_getSpecDefault(Spec); + UE_LOG(LogURLabWrapper, Log, TEXT("[AddDefault] Using root default for class '%s'"), *DefaultComp->ClassName); + } + else + { + def = mjs_addDefault(Spec, ClassName, parentDef); + } + } + else + { + def = mjs_addDefault(Spec, ClassName, parentDef); + } + + if (!def) return; + + DefaultComp->ExportTo(def); +} + +TArray FMujocoSpecWrapper::ReconstructViews(const mjModel* m, mjData* d, const FString& Prefix) +{ + TArray Views; + UE_LOG(LogURLabWrapper, Warning, TEXT("ReconstructViews: %i bodies, prefix='%s'"), CreatedBodyNames.Num(), *Prefix); + Views.Reserve(CreatedBodyNames.Num()); + + for (const FString& Name : CreatedBodyNames) + { + FString PrefixedName = Prefix + Name; + BodyView View = bind(m, d, TCHAR_TO_UTF8(*PrefixedName)); + if (View.id >= 0) + { + Views.Add(View); + } + else + { + UE_LOG(LogURLabWrapper, Warning, TEXT("MuJoCo SpecWrapper: Failed to bind view for body '%s'."), *PrefixedName); + } + } + + return Views; +} + +TArray FMujocoSpecWrapper::PrepareMeshForMuJoCo(UStaticMeshComponent* SMC, bool bComplexMeshRequired, float CoACDThreshold) +{ + TArray ResultNames; + if (!SMC || !SMC->GetStaticMesh()) return ResultNames; + + UStaticMesh* Mesh = SMC->GetStaticMesh(); + UBodySetup* BodySetup = nullptr; // Mesh->GetBodySetup(); + // if (!BodySetup || BodySetup->TriMeshGeometries.Num() == 0) return ResultNames; + if (!BodySetup) return ResultNames; + + FString MeshType = bComplexMeshRequired ? "Complex" : "Simple"; + UStaticMesh* StaticMesh = Cast(BodySetup->GetOuter()); + FString BaseAssetName = StaticMesh ? StaticMesh->GetName() : SMC->GetName(); + FVector Scale = SMC->GetComponentScale(); + + FString AssetName = BaseAssetName; + if (!Scale.Equals(FVector::OneVector, 0.001f)) + { + AssetName = FString::Printf(TEXT("%s_s%.3f_%.3f_%.3f"), *BaseAssetName, + Scale.X, Scale.Y, Scale.Z); + } + + FString SubDir = MeshCacheSubDir.IsEmpty() ? TEXT("Shared") : MeshCacheSubDir; + FString FilePath = FString::Printf( + TEXT("%s/URLab/ConvertedMeshes/%s/%s_%s.obj"), + *FPaths::ProjectSavedDir(), + *SubDir, + *MeshType, + *AssetName + ); + FString FullFilePath = FPaths::ConvertRelativePathToFull(FilePath); + + if (!bComplexMeshRequired) + { + if (mjs_findElement(Spec, mjOBJ_MESH, TCHAR_TO_UTF8(*AssetName))) + { + UE_LOG(LogURLabWrapper, Log, TEXT("Mesh Asset '%s' already exists in Spec. Reusing."), *AssetName); + ResultNames.Add(AssetName); + return ResultNames; + } + } + else + { + FString FirstSubMeshName = FString::Printf(TEXT("%s_0"), *AssetName); + if (mjs_findElement(Spec, mjOBJ_MESH, TCHAR_TO_UTF8(*FirstSubMeshName))) + { + UE_LOG(LogURLabWrapper, Log, TEXT("Complex Mesh Asset '%s' already exists in Spec. Reusing all parts."), *AssetName); + int i = 0; + while (true) + { + FString SubMeshName = FString::Printf(TEXT("%s_%d"), *AssetName, i); + if (mjs_findElement(Spec, mjOBJ_MESH, TCHAR_TO_UTF8(*SubMeshName))) + { + ResultNames.Add(SubMeshName); + i++; + } + else + { + break; + } + } + return ResultNames; + } + } + + const int32 GeometryIndex = 0; + // // auto& TriGeom; // BodySetup->TriMeshGeometries[GeometryIndex]; + // auto& Vertices = TriGeom.GetReference()->Particles().X(); + // + // FString CurrentHash; + // bool bLargeIndices = TriGeom.GetReference()->Elements().RequiresLargeIndices(); + // if (bLargeIndices) + // { + // const auto& Indices = TriGeom.GetReference()->Elements().GetLargeIndexBuffer(); + // CurrentHash = IO::ComputeMeshHash(Vertices, Indices); + // } + // else + // { + // const auto& Indices = TriGeom.GetReference()->Elements().GetSmallIndexBuffer(); + // CurrentHash = IO::ComputeMeshHash(Vertices, Indices); + // } + // CurrentHash += bComplexMeshRequired ? TEXT("_complex") : TEXT("_simple"); + // if (bComplexMeshRequired) + // { + // CurrentHash += FString::Printf(TEXT("_t%.4f"), CoACDThreshold); + // } + // + // int MeshCount = IO::NumFilesExist(FullFilePath, bComplexMeshRequired); + // if (MeshCount > 0) + // { + // FString CachedHash = IO::LoadMeshHash(FullFilePath); + // if (CachedHash != CurrentHash) + // { + // UE_LOG(LogURLabWrapper, Log, TEXT("Mesh '%s' has changed (hash mismatch). Re-exporting."), *AssetName); + // IO::DeleteMeshCache(FullFilePath, bComplexMeshRequired); + // MeshCount = 0; + // } + // } + // + // if (MeshCount == 0) + // { + // UE_LOG(LogURLabWrapper, Log, TEXT("Saving mesh geometry for: %s"), *AssetName); + // + // if (bLargeIndices) + // { + // const auto& Indices = TriGeom.GetReference()->Elements().GetLargeIndexBuffer(); + // MeshCount = MeshUtils::SaveMesh(FullFilePath, Vertices, Indices, bComplexMeshRequired, CoACDThreshold); + // } + // else + // { + // const auto& Indices = TriGeom.GetReference()->Elements().GetSmallIndexBuffer(); + // MeshCount = MeshUtils::SaveMesh(FullFilePath, Vertices, Indices, bComplexMeshRequired, CoACDThreshold); + // } + // + // if (MeshCount == 0) + // { + // UE_LOG(LogURLabWrapper, Error, TEXT("MeshUtils::SaveMesh failed to save any meshes for %s."), *AssetName); + // return ResultNames; + // } + // + // IO::SaveMeshHash(FullFilePath, CurrentHash); + // } + // + // FString BaseName = FPaths::GetBaseFilename(FilePath); + // FString Directory = FPaths::GetPath(FilePath); + // + // if (!bComplexMeshRequired) + // { + // FString sub_file_path = FString::Printf(TEXT("%s/%s.obj"), *Directory, *BaseName); + // ResultNames.Add(AddMeshAsset(AssetName, sub_file_path, Scale)); + // } + // else + // { + // for (int i = 0; i < MeshCount; i++) + // { + // FString sub_file_path = FString::Printf(TEXT("%s/%s_sub_%d.obj"), *Directory, *BaseName, i); + // FString SubMeshName = FString::Printf(TEXT("%s_%d"), *AssetName, i); + // ResultNames.Add(AddMeshAsset(SubMeshName, sub_file_path, Scale)); + // } + // } + // + return ResultNames; +} + + + + + +FString FMujocoSpecWrapper::GetUniqueName(const FString& BaseName, mjtObj Type, const AActor* ContextActor) +{ + FString Candidate = BaseName; + + FString TestName = Candidate; + int32 Counter = 0; + while (mjs_findElement(Spec, Type, TCHAR_TO_UTF8(*TestName))) + { + ++Counter; + TestName = FString::Printf(TEXT("%s_%d"), *Candidate, Counter); + } + + return TestName; +} + + + diff --git a/Plugins/UnrealRoboticsLab/Source/URLab/Private/Replay/MjReplayManager.cpp b/Plugins/UnrealRoboticsLab/Source/URLab/Private/Replay/MjReplayManager.cpp new file mode 100644 index 0000000000..ec9a1890a9 --- /dev/null +++ b/Plugins/UnrealRoboticsLab/Source/URLab/Private/Replay/MjReplayManager.cpp @@ -0,0 +1,1173 @@ +// Copyright (c) 2026 Jonathan Embley-Riches. All rights reserved. +// +// Licensed under the Apache License, Version 2.0 (the "License"); +// you may not use this file except in compliance with the License. +// You may obtain a copy of the License at +// +// http://www.apache.org/licenses/LICENSE-2.0 +// +// Unless required by applicable law or agreed to in writing, software +// distributed under the License is distributed on an "AS IS" BASIS, +// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +// See the License for the specific language governing permissions and +// limitations under the License. +// +// --- LEGAL DISCLAIMER --- +// UnrealRoboticsLab is an independent software plugin. It is NOT affiliated with, +// endorsed by, or sponsored by Epic Games, Inc. "Unreal" and "Unreal Engine" are +// trademarks or registered trademarks of Epic Games, Inc. in the US and elsewhere. +// +// This plugin incorporates third-party software: MuJoCo (Apache 2.0), +// CoACD (MIT), and libzmq (MPL 2.0). See ThirdPartyNotices.txt for details. +#include "Replay/MjReplayManager.h" +#include "Utils/URLabLogging.h" +#include "MuJoCo/Core/AMjManager.h" +#include "MuJoCo/Core/MjPhysicsEngine.h" +#include "MuJoCo/Utils/MjUtils.h" +#include "JsonObjectConverter.h" +#include "Misc/FileHelper.h" +#include "Misc/Paths.h" +#include "Algo/BinarySearch.h" +#if WITH_EDITOR +#include "DesktopPlatformModule.h" +#include "Framework/Application/SlateApplication.h" +#endif + +const FString AMjReplayManager::LiveSessionName = TEXT("Live Recording"); + +AMjReplayManager::AMjReplayManager() +{ + PrimaryActorTick.bCanEverTick = true; +} + +void AMjReplayManager::BeginPlay() +{ + Super::BeginPlay(); + + // Ensure live session exists + Sessions.FindOrAdd(LiveSessionName); + ActiveSessionName = LiveSessionName; + + // Auto-connect to manager if possible + Manager = AAMjManager::GetManager(); + if (Manager) + { + // Sets OnPostStep directly rather than RegisterPostStepCallback -- replay + // needs exclusive control of the callback, not append-to behavior. + if (Manager->PhysicsEngine) + { + Manager->PhysicsEngine->OnPostStep = [this](mjModel* m, mjData* d) { + this->OnPostStep(m, d); + }; + } + } + + if (bAutoRecord) + { + StartRecording(); + } + + // Auto-load any CSV/JSON replays from Saved/URLab/Replays/ + { + FString ReplayDir = FPaths::ProjectSavedDir() / TEXT("URLab") / TEXT("Replays"); + IFileManager::Get().MakeDirectory(*ReplayDir, true); + + TArray FoundFiles; + IFileManager::Get().FindFiles(FoundFiles, *(ReplayDir / TEXT("*.csv")), true, false); + + TArray JsonFiles; + IFileManager::Get().FindFiles(JsonFiles, *(ReplayDir / TEXT("*.json")), true, false); + FoundFiles.Append(JsonFiles); + + for (const FString& FileName : FoundFiles) + { + FString FullPath = ReplayDir / FileName; + FString SessionName = FPaths::GetBaseFilename(FileName); + + if (Sessions.Contains(SessionName)) continue; + + if (FileName.EndsWith(TEXT(".csv"))) + { + LoadFromCSV(FullPath); + } + else if (FileName.EndsWith(TEXT(".json"))) + { + FString JsonString; + if (FFileHelper::LoadFileToString(JsonString, *FullPath)) + { + TSharedPtr RootObject; + TSharedRef> Reader = TJsonReaderFactory<>::Create(JsonString); + if (FJsonSerializer::Deserialize(Reader, RootObject) && RootObject.IsValid()) + { + const TArray>* JsonFrames; + if (RootObject->TryGetArrayField(TEXT("Frames"), JsonFrames)) + { + FReplaySession& Session = Sessions.FindOrAdd(SessionName); + Session.SourceFile = FullPath; + for (const auto& Val : *JsonFrames) + { + const TSharedPtr* FrameObj; + if (Val->TryGetObject(FrameObj)) + { + FMjReplayFrame Frame; + if (FJsonObjectConverter::JsonObjectToUStruct((*FrameObj).ToSharedRef(), &Frame)) + { + Session.Frames.Add(Frame); + } + } + } + } + } + } + } + + UE_LOG(LogURLabReplay, Log, TEXT("ReplayManager: Auto-loaded replay '%s' (%d frames)"), + *SessionName, Sessions.FindOrAdd(SessionName).Frames.Num()); + } + } + + // Keep Live Recording as active + ActiveSessionName = LiveSessionName; +} + +void AMjReplayManager::EndPlay(const EEndPlayReason::Type EndPlayReason) +{ + Super::EndPlay(EndPlayReason); + + if (Manager && Manager->PhysicsEngine) + { + Manager->PhysicsEngine->OnPostStep = nullptr; + Manager->PhysicsEngine->ClearCustomStepHandler(); + } +} + +void AMjReplayManager::Tick(float DeltaTime) +{ + Super::Tick(DeltaTime); + + // Read playback time from physics thread (atomic, no race) + if (bIsReplaying) + { + PlaybackTime = (float)PhysicsPlaybackTime.load(std::memory_order_relaxed); + } +} + +// --- Session Accessors --- + +TArray& AMjReplayManager::GetActiveFrames() +{ + return Sessions.FindOrAdd(ActiveSessionName).Frames; +} + +TArray& AMjReplayManager::GetLiveFrames() +{ + return Sessions.FindOrAdd(LiveSessionName).Frames; +} + +TArray AMjReplayManager::GetSessionNames() const +{ + TArray Names; + Sessions.GetKeys(Names); + // Ensure Live Recording is always first + Names.Remove(LiveSessionName); + Names.Insert(LiveSessionName, 0); + return Names; +} + +void AMjReplayManager::SetActiveSession(const FString& Name) +{ + if (Sessions.Contains(Name)) + { + ActiveSessionName = Name; + RebuildArticulationBindings(); + UE_LOG(LogURLabReplay, Log, TEXT("ReplayManager: Active session set to '%s' (%d frames, %d articulation bindings)"), + *Name, Sessions[Name].Frames.Num(), ArticulationBindings.Num()); + } + else + { + UE_LOG(LogURLabReplay, Warning, TEXT("ReplayManager: Session '%s' not found"), *Name); + } +} + +void AMjReplayManager::RebuildArticulationBindings() +{ + // Preserve existing settings (enabled/relpos) for articulations that still match + TMap OldBindings; + for (const FReplayArticulationBinding& B : ArticulationBindings) + { + if (B.Articulation.IsValid()) + { + OldBindings.Add(B.Articulation->GetName(), B); + } + } + ArticulationBindings.Empty(); + + if (!Manager) Manager = AAMjManager::GetManager(); + if (!Manager) return; + + TArray& Frames = GetActiveFrames(); + if (Frames.Num() == 0) return; + + const FMjReplayFrame& FirstFrame = Frames[0]; + TArray Articulations = Manager->GetAllArticulations(); + UE_LOG(LogURLabReplay, Log, TEXT("RebuildArticulationBindings: %d articulations, %d CSV joints in session '%s'"), + Articulations.Num(), FirstFrame.JointStates.Num(), *ActiveSessionName); + + for (AMjArticulation* Art : Articulations) + { + if (!Art) continue; + + FString ActorName = Art->GetName(); + + TArray JointNames = Art->GetJointNames(); + UE_LOG(LogURLabReplay, Log, TEXT(" Checking articulation '%s' — %d joint names"), *ActorName, JointNames.Num()); + bool bHasMatch = false; + FString FoundPrefix; + + for (const FString& UEJointName : JointNames) + { + // Extract the bare joint name by finding the last occurrence of a known separator + // MuJoCo names are like "g1_29dof_beyondmimic_C_1_left_hip_pitch_joint" + // We want to compare just "left_hip_pitch_joint" — find it by stripping the actor prefix + FString BareJointName = UEJointName; + if (UEJointName.StartsWith(ActorName + TEXT("_"))) + { + BareJointName = UEJointName.Mid(ActorName.Len() + 1); + } + + for (auto& Pair : FirstFrame.JointStates) + { + // Compare bare names: strip prefix from CSV key too + if (Pair.Key.EndsWith(BareJointName) || BareJointName.Equals(Pair.Key) || UEJointName.Equals(Pair.Key)) + { + bHasMatch = true; + break; + } + } + if (bHasMatch) break; + } + + if (bHasMatch) + { + FReplayArticulationBinding Binding; + Binding.Articulation = Art; + Binding.InitialPosition = Art->GetActorLocation(); + Binding.bInitialsCaptured = false; + + // Restore previous settings if this articulation was already bound + if (FReplayArticulationBinding* Old = OldBindings.Find(ActorName)) + { + Binding.bEnabled = Old->bEnabled; + Binding.bRelativePosition = Old->bRelativePosition; + } + else + { + Binding.bEnabled = true; + // Live recordings already have correct absolute positions — RelPos off by default. + // External CSVs need RelPos on since their coordinate system differs. + FReplaySession* Session = Sessions.Find(ActiveSessionName); + Binding.bRelativePosition = Session && !Session->SourceFile.IsEmpty(); + } + + ArticulationBindings.Add(Binding); + UE_LOG(LogURLabReplay, Log, TEXT("ReplayManager: Bound articulation '%s'"), *ActorName); + } + } +} + +// --- Control API --- + +void AMjReplayManager::StartRecording() +{ + if (bIsReplaying) StopReplay(); + + GetLiveFrames().Empty(); + ActiveSessionName = LiveSessionName; + bIsRecording = true; + bCacheValid = false; + CachedJointNames.Empty(); + + // Re-register the PostStep callback (it gets cleared by StopRecording) + // Sets OnPostStep directly rather than RegisterPostStepCallback -- replay + // needs exclusive control of the callback, not append-to behavior. + if (Manager && Manager->PhysicsEngine) + { + Manager->PhysicsEngine->OnPostStep = [this](mjModel* m, mjData* d) { + this->OnPostStep(m, d); + }; + } + + UE_LOG(LogURLabReplay, Log, TEXT("ReplayManager: Started Recording")); +} + +void AMjReplayManager::StopRecording() +{ + bIsRecording = false; + UE_LOG(LogURLabReplay, Log, TEXT("ReplayManager: Stopped Recording. Captured %d frames."), GetLiveFrames().Num()); +} + +void AMjReplayManager::StartReplay() +{ + if (bIsRecording) StopRecording(); + + TArray& Frames = GetActiveFrames(); + if (Frames.Num() == 0) + { + UE_LOG(LogURLabReplay, Warning, TEXT("ReplayManager: No frames in session '%s' to replay!"), *ActiveSessionName); + return; + } + + if (!Manager) Manager = AAMjManager::GetManager(); + if (!Manager) return; + + bIsReplaying = true; + PlaybackTime = 0.0f; + PhysicsPlaybackTime.store(0.0, std::memory_order_relaxed); + bFirstReplayFrame = true; + + // Rebuild bindings (initial MuJoCo positions will be captured on first replay frame + // after the reset completes on the physics thread) + RebuildArticulationBindings(); + + // Reset simulation so initial positions are clean (from the model's qpos0) + Manager->ResetSimulation(); + + // Auto-unpause so replay starts immediately + if (Manager->PhysicsEngine && Manager->PhysicsEngine->bIsPaused) + { + Manager->PhysicsEngine->SetPaused(false); + } + + Manager->PhysicsEngine->SetCustomStepHandler([this](mjModel* m, mjData* d) { + this->OnReplayStep(m, d); + }); + + UE_LOG(LogURLabReplay, Log, TEXT("ReplayManager: Started Replay of '%s' (%d frames)"), + *ActiveSessionName, Frames.Num()); +} + +void AMjReplayManager::StopReplay() +{ + bIsReplaying = false; + + if (Manager && Manager->PhysicsEngine) + { + Manager->PhysicsEngine->ClearCustomStepHandler(); + } + + UE_LOG(LogURLabReplay, Log, TEXT("ReplayManager: Stopped Replay")); +} + +void AMjReplayManager::ClearRecording() +{ + GetLiveFrames().Empty(); + PlaybackTime = 0.0f; + PhysicsPlaybackTime.store(0.0, std::memory_order_relaxed); +} + +bool AMjReplayManager::SaveRecordingToFile(FString FileName) +{ + TArray& Frames = GetActiveFrames(); + if (Frames.Num() == 0) return false; + + TSharedPtr RootObject = MakeShareable(new FJsonObject()); + TArray> JsonFrames; + + for (const FMjReplayFrame& Frame : Frames) + { + TSharedPtr FrameObj = FJsonObjectConverter::UStructToJsonObject(Frame); + JsonFrames.Add(MakeShareable(new FJsonValueObject(FrameObj))); + } + + RootObject->SetArrayField(TEXT("Frames"), JsonFrames); + + FString JsonString; + TSharedRef> Writer = TJsonWriterFactory<>::Create(&JsonString); + FJsonSerializer::Serialize(RootObject.ToSharedRef(), Writer); + + FString FilePath = FPaths::ProjectSavedDir() / TEXT("URLab") / TEXT("Replays") / FileName; + return FFileHelper::SaveStringToFile(JsonString, *FilePath); +} + +bool AMjReplayManager::LoadRecordingFromFile(FString FileName) +{ + FString FilePath = FPaths::ProjectSavedDir() / TEXT("URLab") / TEXT("Replays") / FileName; + FString JsonString; + if (!FFileHelper::LoadFileToString(JsonString, *FilePath)) return false; + + TSharedPtr RootObject; + TSharedRef> Reader = TJsonReaderFactory<>::Create(JsonString); + + if (FJsonSerializer::Deserialize(Reader, RootObject) && RootObject.IsValid()) + { + const TArray>* JsonFrames; + if (RootObject->TryGetArrayField(TEXT("Frames"), JsonFrames)) + { + // Create a new named session from the filename + FString SessionName = FPaths::GetBaseFilename(FileName); + FReplaySession& Session = Sessions.FindOrAdd(SessionName); + Session.Frames.Empty(); + Session.SourceFile = FilePath; + + for (const auto& Val : *JsonFrames) + { + const TSharedPtr* FrameObj; + if (Val->TryGetObject(FrameObj)) + { + FMjReplayFrame Frame; + if (FJsonObjectConverter::JsonObjectToUStruct((*FrameObj).ToSharedRef(), &Frame)) + { + Session.Frames.Add(Frame); + } + } + } + + ActiveSessionName = SessionName; + UE_LOG(LogURLabReplay, Log, TEXT("ReplayManager: Loaded %d frames into session '%s'."), + Session.Frames.Num(), *SessionName); + return true; + } + } + return false; +} + +// --- CSV Import --- + +bool AMjReplayManager::LoadFromCSV(FString FilePath, float Timestep) +{ + TArray Lines; + if (!FFileHelper::LoadFileToStringArray(Lines, *FilePath)) + { + UE_LOG(LogURLabReplay, Error, TEXT("ReplayManager: Failed to load CSV file: %s"), *FilePath); + return false; + } + + if (Lines.Num() < 2) + { + UE_LOG(LogURLabReplay, Error, TEXT("ReplayManager: CSV file has no data rows: %s"), *FilePath); + return false; + } + + // Parse header + TArray Headers; + Lines[0].ParseIntoArray(Headers, TEXT(","), false); + for (FString& H : Headers) H.TrimStartAndEndInline(); + + // Find time column + int32 TimeCol = INDEX_NONE; + for (int32 i = 0; i < Headers.Num(); ++i) + { + if (Headers[i].Equals(TEXT("time"), ESearchCase::IgnoreCase) || + Headers[i].Equals(TEXT("timestamp"), ESearchCase::IgnoreCase)) + { + TimeCol = i; + break; + } + } + + // Group columns by joint name + // Detect bracket notation: "name[N]" -> group under "name" + // Everything else is a 1-DOF joint column + struct FColumnGroup + { + FString JointName; + TArray ColumnIndices; // Ordered by bracket index or appearance order + }; + TMap JointColumnMap; + + // Camera column indices + int32 CamXCol = INDEX_NONE, CamYCol = INDEX_NONE, CamZCol = INDEX_NONE; + int32 CamPitchCol = INDEX_NONE, CamYawCol = INDEX_NONE, CamRollCol = INDEX_NONE; + + for (int32 i = 0; i < Headers.Num(); ++i) + { + if (i == TimeCol) continue; + + FString ColName = Headers[i]; + + // Camera columns + if (ColName.Equals(TEXT("cam_x"), ESearchCase::IgnoreCase)) { CamXCol = i; continue; } + if (ColName.Equals(TEXT("cam_y"), ESearchCase::IgnoreCase)) { CamYCol = i; continue; } + if (ColName.Equals(TEXT("cam_z"), ESearchCase::IgnoreCase)) { CamZCol = i; continue; } + if (ColName.Equals(TEXT("cam_pitch"), ESearchCase::IgnoreCase)) { CamPitchCol = i; continue; } + if (ColName.Equals(TEXT("cam_yaw"), ESearchCase::IgnoreCase)) { CamYawCol = i; continue; } + if (ColName.Equals(TEXT("cam_roll"), ESearchCase::IgnoreCase)) { CamRollCol = i; continue; } + + // Check for bracket notation: "name[N]" + int32 BracketIdx = ColName.Find(TEXT("[")); + if (BracketIdx != INDEX_NONE && ColName.EndsWith(TEXT("]"))) + { + FString BaseName = ColName.Left(BracketIdx); + FString IdxStr = ColName.Mid(BracketIdx + 1, ColName.Len() - BracketIdx - 2); + int32 SubIdx = FCString::Atoi(*IdxStr); + + FColumnGroup& Group = JointColumnMap.FindOrAdd(BaseName); + Group.JointName = BaseName; + if (Group.ColumnIndices.Num() <= SubIdx) + { + Group.ColumnIndices.SetNum(SubIdx + 1); + } + Group.ColumnIndices[SubIdx] = i; + } + else + { + FColumnGroup& Group = JointColumnMap.FindOrAdd(ColName); + Group.JointName = ColName; + Group.ColumnIndices.Add(i); + } + } + + bool bHasCameraColumns = (CamXCol != INDEX_NONE && CamYCol != INDEX_NONE && CamZCol != INDEX_NONE); + + // Parse data rows + FString SessionName = FPaths::GetBaseFilename(FilePath); + FReplaySession& Session = Sessions.FindOrAdd(SessionName); + Session.Frames.Empty(); + Session.SourceFile = FilePath; + Session.Frames.Reserve(Lines.Num() - 1); + + for (int32 LineIdx = 1; LineIdx < Lines.Num(); ++LineIdx) + { + FString& Line = Lines[LineIdx]; + if (Line.TrimStartAndEnd().IsEmpty()) continue; + + TArray Values; + Line.ParseIntoArray(Values, TEXT(","), false); + + // Get timestamp + double Time = (TimeCol != INDEX_NONE && TimeCol < Values.Num()) + ? FCString::Atod(*Values[TimeCol]) + : (double)(LineIdx - 1) * (double)Timestep; + + FMjReplayFrame Frame; + Frame.Timestamp = Time; + + for (auto& Pair : JointColumnMap) + { + const FColumnGroup& Group = Pair.Value; + FMjBodyKinematics Kinematics; + Kinematics.QPos.SetNum(Group.ColumnIndices.Num()); + + bool bValid = true; + for (int32 k = 0; k < Group.ColumnIndices.Num(); ++k) + { + int32 ColIdx = Group.ColumnIndices[k]; + if (ColIdx < Values.Num()) + { + Kinematics.QPos[k] = FCString::Atod(*Values[ColIdx]); + } + else + { + bValid = false; + break; + } + } + + if (bValid) + { + // QVel left empty — mj_forward doesn't need it for kinematics + Frame.JointStates.Add(Group.JointName, MoveTemp(Kinematics)); + } + } + + // Parse camera data if present + if (bHasCameraColumns) + { + Frame.bHasCameraData = true; + if (CamXCol < Values.Num()) Frame.CameraPosition.X = FCString::Atod(*Values[CamXCol]); + if (CamYCol < Values.Num()) Frame.CameraPosition.Y = FCString::Atod(*Values[CamYCol]); + if (CamZCol < Values.Num()) Frame.CameraPosition.Z = FCString::Atod(*Values[CamZCol]); + if (CamPitchCol < Values.Num()) Frame.CameraRotation.Pitch = FCString::Atod(*Values[CamPitchCol]); + if (CamYawCol < Values.Num()) Frame.CameraRotation.Yaw = FCString::Atod(*Values[CamYawCol]); + if (CamRollCol < Values.Num()) Frame.CameraRotation.Roll = FCString::Atod(*Values[CamRollCol]); + } + + Session.Frames.Add(MoveTemp(Frame)); + } + + ActiveSessionName = SessionName; + RebuildArticulationBindings(); + + UE_LOG(LogURLabReplay, Log, TEXT("ReplayManager: Loaded CSV '%s' -> session '%s': %d frames, %d joints"), + *FilePath, *SessionName, Session.Frames.Num(), JointColumnMap.Num()); + + for (auto& Pair : JointColumnMap) + { + UE_LOG(LogURLabReplay, Log, TEXT(" Joint '%s': %d DOF"), *Pair.Key, Pair.Value.ColumnIndices.Num()); + } + + return Session.Frames.Num() > 0; +} + +bool AMjReplayManager::BrowseAndLoadCSV(float Timestep) +{ +#if WITH_EDITOR + IDesktopPlatform* DesktopPlatform = FDesktopPlatformModule::Get(); + if (!DesktopPlatform) return false; + + FString DefaultDir = FPaths::ProjectSavedDir() / TEXT("URLab") / TEXT("Replays"); + IFileManager::Get().MakeDirectory(*DefaultDir, true); + + TArray OutFiles; + bool bOpened = DesktopPlatform->OpenFileDialog( + FSlateApplication::Get().FindBestParentWindowHandleForDialogs(nullptr), + TEXT("Load Replay"), + DefaultDir, + TEXT(""), + TEXT("CSV Files (*.csv)|*.csv|JSON Files (*.json)|*.json"), + EFileDialogFlags::None, + OutFiles); + + if (bOpened && OutFiles.Num() > 0) + { + if (OutFiles[0].EndsWith(TEXT(".json"))) + { + // Load JSON directly from the full path + FString JsonString; + if (!FFileHelper::LoadFileToString(JsonString, *OutFiles[0])) return false; + + TSharedPtr RootObject; + TSharedRef> Reader = TJsonReaderFactory<>::Create(JsonString); + if (FJsonSerializer::Deserialize(Reader, RootObject) && RootObject.IsValid()) + { + const TArray>* JsonFrames; + if (RootObject->TryGetArrayField(TEXT("Frames"), JsonFrames)) + { + FString SessionName = FPaths::GetBaseFilename(OutFiles[0]); + FReplaySession& Session = Sessions.FindOrAdd(SessionName); + Session.Frames.Empty(); + Session.SourceFile = OutFiles[0]; + + for (const auto& Val : *JsonFrames) + { + const TSharedPtr* FrameObj; + if (Val->TryGetObject(FrameObj)) + { + FMjReplayFrame Frame; + if (FJsonObjectConverter::JsonObjectToUStruct((*FrameObj).ToSharedRef(), &Frame)) + { + Session.Frames.Add(Frame); + } + } + } + ActiveSessionName = SessionName; + RebuildArticulationBindings(); + UE_LOG(LogURLabReplay, Log, TEXT("ReplayManager: Loaded %d frames from JSON '%s'"), + Session.Frames.Num(), *OutFiles[0]); + return Session.Frames.Num() > 0; + } + } + return false; + } + return LoadFromCSV(OutFiles[0], Timestep); + } + return false; +#else + return false; +#endif +} + +bool AMjReplayManager::BrowseAndSaveRecording() +{ + TArray& Frames = GetActiveFrames(); + if (Frames.Num() == 0) + { + UE_LOG(LogURLabReplay, Warning, TEXT("ReplayManager: No frames in active session '%s' to save. Record some data first or select a session with frames."), *ActiveSessionName); + return false; + } + +#if WITH_EDITOR + IDesktopPlatform* DesktopPlatform = FDesktopPlatformModule::Get(); + if (!DesktopPlatform) return false; + + // Ensure save directory exists + FString SaveDir = FPaths::ProjectSavedDir() / TEXT("URLab") / TEXT("Replays"); + IFileManager::Get().MakeDirectory(*SaveDir, true); + + FString DefaultName = ActiveSessionName + TEXT(".csv"); + TArray OutFiles; + bool bOpened = DesktopPlatform->SaveFileDialog( + FSlateApplication::Get().FindBestParentWindowHandleForDialogs(nullptr), + TEXT("Save Replay Recording"), + SaveDir, + DefaultName, + TEXT("CSV Files (*.csv)|*.csv|JSON Files (*.json)|*.json"), + EFileDialogFlags::None, + OutFiles); + + if (!bOpened || OutFiles.Num() == 0) return false; + + FString SavePath = OutFiles[0]; + + if (SavePath.EndsWith(TEXT(".csv"))) + { + // Export as CSV with joint-name headers + if (Frames.Num() == 0) return false; + + // Build headers from first frame's joint names + TArray Headers; + Headers.Add(TEXT("time")); + TArray JointOrder; + + for (auto& Pair : Frames[0].JointStates) + { + JointOrder.Add(Pair.Key); + if (Pair.Value.QPos.Num() == 1) + { + Headers.Add(Pair.Key); + } + else + { + for (int32 k = 0; k < Pair.Value.QPos.Num(); ++k) + { + Headers.Add(FString::Printf(TEXT("%s[%d]"), *Pair.Key, k)); + } + } + } + + // Add camera columns if any frame has camera data + bool bHasCamera = false; + for (const FMjReplayFrame& F : Frames) + { + if (F.bHasCameraData) { bHasCamera = true; break; } + } + if (bHasCamera) + { + Headers.Add(TEXT("cam_x")); + Headers.Add(TEXT("cam_y")); + Headers.Add(TEXT("cam_z")); + Headers.Add(TEXT("cam_pitch")); + Headers.Add(TEXT("cam_yaw")); + Headers.Add(TEXT("cam_roll")); + } + + FString CSV = FString::Join(Headers, TEXT(",")) + TEXT("\n"); + for (const FMjReplayFrame& Frame : Frames) + { + CSV += FString::Printf(TEXT("%.6f"), Frame.Timestamp); + for (const FString& JointName : JointOrder) + { + if (const FMjBodyKinematics* K = Frame.JointStates.Find(JointName)) + { + for (int32 k = 0; k < K->QPos.Num(); ++k) + { + CSV += FString::Printf(TEXT(",%.8f"), K->QPos[k]); + } + } + } + if (bHasCamera) + { + CSV += FString::Printf(TEXT(",%.4f,%.4f,%.4f,%.4f,%.4f,%.4f"), + Frame.CameraPosition.X, Frame.CameraPosition.Y, Frame.CameraPosition.Z, + Frame.CameraRotation.Pitch, Frame.CameraRotation.Yaw, Frame.CameraRotation.Roll); + } + CSV += TEXT("\n"); + } + + bool bSaved = FFileHelper::SaveStringToFile(CSV, *SavePath); + UE_LOG(LogURLabReplay, Log, TEXT("ReplayManager: Saved %d frames as CSV to '%s'"), Frames.Num(), *SavePath); + return bSaved; + } + else + { + // Save as JSON (existing format) + // Save directly to chosen path as JSON + TSharedPtr RootObject = MakeShareable(new FJsonObject()); + TArray> JsonFrames; + for (const FMjReplayFrame& Frame : Frames) + { + TSharedPtr FrameObj = FJsonObjectConverter::UStructToJsonObject(Frame); + JsonFrames.Add(MakeShareable(new FJsonValueObject(FrameObj))); + } + RootObject->SetArrayField(TEXT("Frames"), JsonFrames); + + FString JsonString; + TSharedRef> Writer = TJsonWriterFactory<>::Create(&JsonString); + FJsonSerializer::Serialize(RootObject.ToSharedRef(), Writer); + + bool bSaved = FFileHelper::SaveStringToFile(JsonString, *SavePath); + UE_LOG(LogURLabReplay, Log, TEXT("ReplayManager: Saved %d frames as JSON to '%s'"), Frames.Num(), *SavePath); + return bSaved; + } +#else + return false; +#endif +} + +// --- Physics Thread Callbacks --- + +void AMjReplayManager::OnPostStep(mjModel* m, mjData* d) +{ + if (!bIsRecording || !m || !d || !d->qpos || !d->qvel) return; + + if (!bCacheValid || CachedJointNames.Num() != m->njnt) + { + UpdateCache(m); + } + + FMjReplayFrame Frame; + Frame.Timestamp = d->time; + + for (int i = 0; i < m->njnt; ++i) + { + FString Name = CachedJointNames[i]; + if (Name.IsEmpty()) continue; + + int qpos_adr = m->jnt_qposadr[i]; + int qvel_adr = m->jnt_dofadr[i]; + + int qpos_dim = 0; + int qvel_dim = 0; + + int jnt_type = m->jnt_type[i]; + if (jnt_type == mjJNT_FREE) { qpos_dim = 7; qvel_dim = 6; } + else if (jnt_type == mjJNT_BALL) { qpos_dim = 4; qvel_dim = 3; } + else if (jnt_type == mjJNT_SLIDE) { qpos_dim = 1; qvel_dim = 1; } + else if (jnt_type == mjJNT_HINGE) { qpos_dim = 1; qvel_dim = 1; } + + if (qpos_dim > 0) + { + FMjBodyKinematics Kinematics; + Kinematics.QPos.SetNum(qpos_dim); + Kinematics.QVel.SetNum(qvel_dim); + + for (int k=0; kqpos[qpos_adr + k]; + for (int k=0; kqvel[qvel_adr + k]; + + Frame.JointStates.Add(Name, Kinematics); + } + } + + TArray& LiveFrames = GetLiveFrames(); + LiveFrames.Add(Frame); + + if (LiveFrames.Num() > 1 && MaxRecordDuration > 0) + { + const double Cutoff = LiveFrames.Last().Timestamp - (double)MaxRecordDuration; + int32 FirstKeep = Algo::LowerBound(LiveFrames, Cutoff, + [](const FMjReplayFrame& F, double T) { return F.Timestamp < T; }); + if (FirstKeep > 0) + { + LiveFrames.RemoveAt(0, FirstKeep); + } + } + + if (LiveFrames.Num() % 60 == 0) + { + UE_LOG(LogURLabReplay, Verbose, TEXT("ReplayManager: Captured Frame at %f with %d joints recorded."), + Frame.Timestamp, Frame.JointStates.Num()); + } +} + +void AMjReplayManager::OnReplayStep(mjModel* m, mjData* d) +{ + if (!bIsReplaying || !m || !d) return; + + TArray& Frames = GetActiveFrames(); + if (Frames.Num() == 0) return; + + // Advance playback time on the physics thread (no race condition) + double PhysDt = m->opt.timestep; + double CurrentPT = PhysicsPlaybackTime.load(std::memory_order_relaxed); + if (Manager->PhysicsEngine && !Manager->PhysicsEngine->bIsPaused) + { + CurrentPT += PhysDt; + double TotalDuration = Frames.Last().Timestamp - Frames[0].Timestamp; + if (TotalDuration > 0 && CurrentPT > TotalDuration) + { + CurrentPT = 0.0; // Loop + } + PhysicsPlaybackTime.store(CurrentPT, std::memory_order_relaxed); + } + + double StartTime = Frames[0].Timestamp; + double TargetTime = StartTime + CurrentPT; + + int32 FoundIdx = Algo::LowerBound(Frames, TargetTime, + [](const FMjReplayFrame& Frame, double T) { return Frame.Timestamp < T; }); + if (FoundIdx > 0) --FoundIdx; + FoundIdx = FMath::Clamp(FoundIdx, 0, Frames.Num() - 1); + + // Compute interpolation alpha between FoundIdx and FoundIdx+1 + int32 NextIdx = FMath::Min(FoundIdx + 1, Frames.Num() - 1); + double Alpha = 0.0; + if (bInterpolateFrames && NextIdx > FoundIdx) + { + double T0 = Frames[FoundIdx].Timestamp; + double T1 = Frames[NextIdx].Timestamp; + double Span = T1 - T0; + if (Span > 1e-9) + { + Alpha = FMath::Clamp((TargetTime - T0) / Span, 0.0, 1.0); + } + } + + const FMjReplayFrame& Frame = Frames[FoundIdx]; + + bool bFirstFrame = bFirstReplayFrame; + if (d->time - LastReplayLogTime > 1.0) + { + UE_LOG(LogURLabReplay, Log, TEXT("ReplayManager: Playing '%s' Frame %f (PlaybackTime: %f). Index %d/%d"), + *ActiveSessionName, Frame.Timestamp, PlaybackTime, FoundIdx, Frames.Num()); + LastReplayLogTime = d->time; + } + + int32 MatchedJoints = 0; + int32 UnmatchedJoints = 0; + + // Pass 1: Suffix-match all joints and collect prefixes of matched articulations + TSet MatchedPrefixes; + TArray ResolvedKinematics; + ResolvedKinematics.SetNum(m->njnt); + for (int i = 0; i < m->njnt; ++i) ResolvedKinematics[i] = nullptr; + + for (int i = 0; i < m->njnt; ++i) + { + const char* name_ptr = mj_id2name(m, mjOBJ_JOINT, i); + if (!name_ptr) continue; + FString Name = UTF8_TO_TCHAR(name_ptr); + if (Name.IsEmpty()) continue; + + // Exact match + const FMjBodyKinematics* Kinematics = Frame.JointStates.Find(Name); + if (Kinematics) + { + ResolvedKinematics[i] = Kinematics; + continue; + } + + // Suffix match (CSV key is suffix of model name, or vice versa) + for (auto& Pair : Frame.JointStates) + { + if (Name.EndsWith(Pair.Key) && (Name.Len() == Pair.Key.Len() || Name[Name.Len() - Pair.Key.Len() - 1] == '_')) + { + ResolvedKinematics[i] = &Pair.Value; + FString Prefix = Name.Left(Name.Len() - Pair.Key.Len()); + MatchedPrefixes.Add(Prefix); + break; + } + } + + // Bare name match: strip instance prefix from both model and CSV names and compare + // This handles multi-instance replay (e.g. _C_0 matching data recorded from _C_1) + if (!ResolvedKinematics[i]) + { + // Find which binding this joint belongs to and extract its bare name + for (const FReplayArticulationBinding& B : ArticulationBindings) + { + if (!B.bEnabled || !B.Articulation.IsValid()) continue; + FString ArtName = B.Articulation->GetName(); + FString ArtPrefix = ArtName + TEXT("_"); + + if (Name.StartsWith(ArtPrefix)) + { + FString BareName = Name.Mid(ArtPrefix.Len()); + // Search CSV keys for one that ends with the same bare name + for (auto& Pair : Frame.JointStates) + { + if (Pair.Key.EndsWith(BareName)) + { + ResolvedKinematics[i] = &Pair.Value; + MatchedPrefixes.Add(ArtPrefix); + break; + } + } + break; + } + } + } + } + + // Pass 2: Free joint fallback — only for articulations that had other suffix matches + for (int i = 0; i < m->njnt; ++i) + { + if (ResolvedKinematics[i]) continue; + if (m->jnt_type[i] != mjJNT_FREE) continue; + + const char* name_ptr = mj_id2name(m, mjOBJ_JOINT, i); + if (!name_ptr) continue; + FString Name = UTF8_TO_TCHAR(name_ptr); + + // Check if this free joint's prefix matches a known matched articulation + for (const FString& Prefix : MatchedPrefixes) + { + if (Name.StartsWith(Prefix)) + { + // Find the 7-DOF CSV entry + for (auto& Pair : Frame.JointStates) + { + if (Pair.Value.QPos.Num() == 7) + { + ResolvedKinematics[i] = &Pair.Value; + break; + } + } + break; + } + } + } + + // Pass 3: Apply resolved kinematics with per-articulation filtering + // Cache CSV start position from first frame for RelPos + const FMjReplayFrame& FirstFrame = Frames[0]; + + for (int i = 0; i < m->njnt; ++i) + { + const char* name_ptr = mj_id2name(m, mjOBJ_JOINT, i); + if (!name_ptr) continue; + FString Name = UTF8_TO_TCHAR(name_ptr); + if (Name.IsEmpty()) continue; + + const FMjBodyKinematics* Kinematics = ResolvedKinematics[i]; + if (Kinematics) + { + // Find which articulation binding this joint belongs to + FReplayArticulationBinding* Binding = nullptr; + for (FReplayArticulationBinding& B : ArticulationBindings) + { + if (B.Articulation.IsValid() && Name.Contains(B.Articulation->GetName())) + { + Binding = &B; + break; + } + } + + // Skip if this articulation is disabled + if (Binding && !Binding->bEnabled) + { + UnmatchedJoints++; + continue; + } + + MatchedJoints++; + int qpos_adr = m->jnt_qposadr[i]; + int type = m->jnt_type[i]; + int expected_dim = (type == mjJNT_FREE) ? 7 : (type == mjJNT_BALL ? 4 : 1); + + if (Kinematics->QPos.Num() == expected_dim) + { + // Get next frame kinematics for interpolation (if available) + const FMjBodyKinematics* NextKinematics = nullptr; + if (Alpha > 0.0 && NextIdx > FoundIdx) + { + const FMjReplayFrame& NextFrame = Frames[NextIdx]; + NextKinematics = NextFrame.JointStates.Find(Name); + if (NextKinematics && NextKinematics->QPos.Num() != expected_dim) + NextKinematics = nullptr; // Dimension mismatch + } + + // Helper: lerp a qpos value between frames + auto LerpQPos = [&](int k) -> double { + double v0 = Kinematics->QPos[k]; + if (NextKinematics && Alpha > 0.0) + return v0 + Alpha * (NextKinematics->QPos[k] - v0); + return v0; + }; + + // Apply RelPos offset for free joints + if (type == mjJNT_FREE && Binding && Binding->bRelativePosition && expected_dim == 7) + { + // On first frame, capture both the CSV start position and the current MuJoCo position + if (!Binding->bInitialsCaptured) + { + Binding->InitialMjPosition = FVector(d->qpos[qpos_adr], d->qpos[qpos_adr + 1], d->qpos[qpos_adr + 2]); + + for (auto& Pair : FirstFrame.JointStates) + { + if (Pair.Value.QPos.Num() == 7) + { + Binding->CsvStartPosition = FVector(Pair.Value.QPos[0], Pair.Value.QPos[1], Pair.Value.QPos[2]); + break; + } + } + Binding->bInitialsCaptured = true; + + UE_LOG(LogURLabReplay, Log, TEXT("RelPos captured for '%s': MjPos=(%.4f,%.4f,%.4f) CsvStart=(%.4f,%.4f,%.4f)"), + *Binding->Articulation->GetName(), + Binding->InitialMjPosition.X, Binding->InitialMjPosition.Y, Binding->InitialMjPosition.Z, + Binding->CsvStartPosition.X, Binding->CsvStartPosition.Y, Binding->CsvStartPosition.Z); + } + + double offset_x = Binding->InitialMjPosition.X - Binding->CsvStartPosition.X; + double offset_y = Binding->InitialMjPosition.Y - Binding->CsvStartPosition.Y; + double offset_z = Binding->InitialMjPosition.Z - Binding->CsvStartPosition.Z; + + d->qpos[qpos_adr + 0] = LerpQPos(0) + offset_x; + d->qpos[qpos_adr + 1] = LerpQPos(1) + offset_y; + d->qpos[qpos_adr + 2] = LerpQPos(2) + offset_z; + + // Quaternion: use slerp for rotation interpolation + if (NextKinematics && Alpha > 0.0) + { + FQuat Q0(Kinematics->QPos[4], Kinematics->QPos[5], Kinematics->QPos[6], Kinematics->QPos[3]); + FQuat Q1(NextKinematics->QPos[4], NextKinematics->QPos[5], NextKinematics->QPos[6], NextKinematics->QPos[3]); + FQuat QInterp = FQuat::Slerp(Q0, Q1, (float)Alpha); + d->qpos[qpos_adr + 3] = QInterp.W; // MuJoCo quat: w,x,y,z + d->qpos[qpos_adr + 4] = QInterp.X; + d->qpos[qpos_adr + 5] = QInterp.Y; + d->qpos[qpos_adr + 6] = QInterp.Z; + } + else + { + for (int k = 3; k < 7; ++k) d->qpos[qpos_adr + k] = Kinematics->QPos[k]; + } + } + else + { + for (int k = 0; k < expected_dim; ++k) d->qpos[qpos_adr + k] = LerpQPos(k); + } + } + + int qvel_adr = m->jnt_dofadr[i]; + int expected_vel_dim = (type == mjJNT_FREE) ? 6 : (type == mjJNT_BALL ? 3 : 1); + if (Kinematics->QVel.Num() == expected_vel_dim) + { + for(int k=0; kqvel[qvel_adr+k] = Kinematics->QVel[k]; + } + } + else + { + UnmatchedJoints++; + if (bFirstFrame) + { + UE_LOG(LogURLabReplay, Warning, TEXT("ReplayManager: Model joint '%s' NOT found in CSV data"), *Name); + } + } + } + + if (bFirstFrame) + { + // Log CSV keys for comparison + UE_LOG(LogURLabReplay, Log, TEXT("ReplayManager: Joint match results: %d matched, %d unmatched out of %d model joints"), + MatchedJoints, UnmatchedJoints, m->njnt); + UE_LOG(LogURLabReplay, Log, TEXT("ReplayManager: CSV contains %d joint entries"), Frame.JointStates.Num()); + for (auto& Pair : Frame.JointStates) + { + UE_LOG(LogURLabReplay, Log, TEXT(" CSV joint: '%s' (%d DOF)"), *Pair.Key, Pair.Value.QPos.Num()); + } + bFirstReplayFrame = false; + } + + mj_forward(m, d); +} + +void AMjReplayManager::UpdateCache(mjModel* m) +{ + CachedJointNames.Empty(); + CachedJointNames.SetNum(m->njnt); + bCacheValid = true; + + for (int i = 0; i < m->njnt; ++i) + { + const char* name = mj_id2name(m, mjOBJ_JOINT, i); + if (name) + { + CachedJointNames[i] = UTF8_TO_TCHAR(name); + } + else + { + CachedJointNames[i] = FString::Printf(TEXT("Joint_%d"), i); + } + UE_LOG(LogURLabReplay, Log, TEXT("ReplayManager Cache: ID %d -> Name '%s'"), i, *CachedJointNames[i]); + } +} diff --git a/Plugins/UnrealRoboticsLab/Source/URLab/Private/URLab.cpp b/Plugins/UnrealRoboticsLab/Source/URLab/Private/URLab.cpp new file mode 100644 index 0000000000..b5bd7d35ef --- /dev/null +++ b/Plugins/UnrealRoboticsLab/Source/URLab/Private/URLab.cpp @@ -0,0 +1,94 @@ +// Copyright (c) 2026 Jonathan Embley-Riches. All rights reserved. +// +// Licensed under the Apache License, Version 2.0 (the "License"); +// you may not use this file except in compliance with the License. +// You may obtain a copy of the License at +// +// http://www.apache.org/licenses/LICENSE-2.0 +// +// Unless required by applicable law or agreed to in writing, software +// distributed under the License is distributed on an "AS IS" BASIS, +// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +// See the License for the specific language governing permissions and +// limitations under the License. +// +// --- LEGAL DISCLAIMER --- +// UnrealRoboticsLab is an independent software plugin. It is NOT affiliated with, +// endorsed by, or sponsored by Epic Games, Inc. "Unreal" and "Unreal Engine" are +// trademarks or registered trademarks of Epic Games, Inc. in the US and elsewhere. +// +// This plugin incorporates third-party software: MuJoCo (Apache 2.0), +// CoACD (MIT), and libzmq (MPL 2.0). See ThirdPartyNotices.txt for details. + +#include "URLab.h" +#include "Misc/Paths.h" +#include "Misc/MessageDialog.h" +#include "Utils/URLabLogging.h" +#include "Interfaces/IPluginManager.h" + +#if WITH_EDITOR +#include "AssetToolsModule.h" +#include "IAssetTools.h" +#endif + +#define LOCTEXT_NAMESPACE "FURLabModule" +void FURLabModule::StartupModule() +{ + FString PluginDir = IPluginManager::Get().FindPlugin("UnrealRoboticsLab")->GetBaseDir(); + FString InstallDir = FPaths::Combine(PluginDir, TEXT("third_party/install")); + + // Function to load a DLL and log success/failure + auto LoadDependencyDLL = [&](const FString& LibraryName, const FString& SubDir) { + // Try plugin third-party path first (editor / development) + FString DLLPath = FPaths::Combine(InstallDir, SubDir, TEXT("bin"), LibraryName); + if (!FPaths::FileExists(DLLPath)) + { + // Packaged build: DLLs staged next to the executable + DLLPath = FPaths::Combine(FPlatformProcess::GetModulesDirectory(), LibraryName); + } + if (FPaths::FileExists(DLLPath)) { + void* Handle = FPlatformProcess::GetDllHandle(*DLLPath); + if (Handle) { + UE_LOG(LogURLab, Log, TEXT("Loaded %s from %s"), *LibraryName, *DLLPath); + return true; + } else { + UE_LOG(LogURLab, Error, TEXT("Failed to load %s from %s"), *LibraryName, *DLLPath); + } + } else { + UE_LOG(LogURLab, Error, TEXT("%s not found (searched plugin dir + binary dir)"), *LibraryName); + } + return false; + }; + +#if PLATFORM_WINDOWS + // Load MuJoCo. Since 3.7.0 the obj/stl decoders are compiled into + // mujoco.dll itself (changelog item 9); loading the standalone + // obj_decoder.dll / stl_decoder.dll would cause a plugin-registration + // collision and crash during module init. + LoadDependencyDLL(TEXT("mujoco.dll"), TEXT("MuJoCo")); + + // Load ZMQ + LoadDependencyDLL(TEXT("libzmq-v143-mt-4_3_6.dll"), TEXT("libzmq")); + + // Load CoACD (Shared library) + LoadDependencyDLL(TEXT("lib_coacd.dll"), TEXT("CoACD")); +#endif + + // Some CoACD dependencies like TBB or runtimes might be in CoACD/bin + // They should be loaded automatically if in search path, but we can verify here if needed. + +#if WITH_EDITOR + // Register custom asset category + IAssetTools& AssetTools = FModuleManager::LoadModuleChecked("AssetTools").Get(); + MuJoCoAssetCategoryBit = AssetTools.RegisterAdvancedAssetCategory(FName(TEXT("MuJoCo")), LOCTEXT("MujocoAssetCategory", "MuJoCo")); + UE_LOG(LogURLab, Log, TEXT("Registered MuJoCo Asset Category with bitmask: %u"), MuJoCoAssetCategoryBit); +#endif +} + +void FURLabModule::ShutdownModule() +{ +} + +#undef LOCTEXT_NAMESPACE + +IMPLEMENT_MODULE(FURLabModule, URLab) diff --git a/Plugins/UnrealRoboticsLab/Source/URLab/Public/CoACD/CoacdInterface.h b/Plugins/UnrealRoboticsLab/Source/URLab/Public/CoACD/CoacdInterface.h new file mode 100644 index 0000000000..04e6aa9634 --- /dev/null +++ b/Plugins/UnrealRoboticsLab/Source/URLab/Public/CoACD/CoacdInterface.h @@ -0,0 +1,68 @@ +// Copyright (c) 2026 Jonathan Embley-Riches. All rights reserved. +// +// Licensed under the Apache License, Version 2.0 (the "License"); +// you may not use this file except in compliance with the License. +// You may obtain a copy of the License at +// +// http://www.apache.org/licenses/LICENSE-2.0 +// +// Unless required by applicable law or agreed to in writing, software +// distributed under the License is distributed on an "AS IS" BASIS, +// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +// See the License for the specific language governing permissions and +// limitations under the License. +// +// --- LEGAL DISCLAIMER --- +// UnrealRoboticsLab is an independent software plugin. It is NOT affiliated with, +// endorsed by, or sponsored by Epic Games, Inc. "Unreal" and "Unreal Engine" are +// trademarks or registered trademarks of Epic Games, Inc. in the US and elsewhere. +// +// This plugin incorporates third-party software: MuJoCo (Apache 2.0), +// CoACD (MIT), and libzmq (MPL 2.0). See ThirdPartyNotices.txt for details. + +#pragma once +#include "CoreMinimal.h" +#include "GameFramework/Actor.h" +#include "Kismet/GameplayStatics.h" +#include +#include +#include "Chaos/ArrayCollectionArray.h" +#include "CoACD/coacd.h" + +#include "Misc/FileHelper.h" +namespace CoacdInterface { + + +template +CoACD_Mesh ConvertToCoACDMesh(const Chaos::TArrayCollectionArray& Vertices, const TArray& Indices) { + + CoACD_Mesh coacdMesh; + + // Allocate memory for vertices + coacdMesh.vertices_count = Vertices.Num(); + coacdMesh.vertices_ptr = new double[coacdMesh.vertices_count * 3]; + + // Copy vertex data (FVector to double array) + for (int i = 0; i < Vertices.Num(); ++i) { + coacdMesh.vertices_ptr[3 * i] = Vertices[i].X; + coacdMesh.vertices_ptr[3 * i + 1] = Vertices[i].Y; + coacdMesh.vertices_ptr[3 * i + 2] = Vertices[i].Z; + } + + // Allocate memory for indices + coacdMesh.triangles_count = Indices.Num(); + coacdMesh.triangles_ptr = new int[coacdMesh.triangles_count * 3]; + + // Copy index data (int32 array) + for (int i = 0; i < Indices.Num(); ++i) { + coacdMesh.triangles_ptr[3 * i] = Indices[i][0]; + coacdMesh.triangles_ptr[3 * i + 1] = Indices[i][1]; + coacdMesh.triangles_ptr[3 * i + 2] = Indices[i][2]; + } + + return coacdMesh; +} +void SaveCoACDMeshAsOBJ(CoACD_Mesh& Mesh, const FString& FilePath) ; + +void SaveCoACDMeshArrayAsOBJ(CoACD_MeshArray& MeshArray, const FString& BaseFilePath) ; +} diff --git a/Plugins/UnrealRoboticsLab/Source/URLab/Public/MuJoCo/Components/Sensors/MjCamera.h b/Plugins/UnrealRoboticsLab/Source/URLab/Public/MuJoCo/Components/Sensors/MjCamera.h new file mode 100644 index 0000000000..a2a67e716e --- /dev/null +++ b/Plugins/UnrealRoboticsLab/Source/URLab/Public/MuJoCo/Components/Sensors/MjCamera.h @@ -0,0 +1,229 @@ +// Copyright (c) 2026 Jonathan Embley-Riches. All rights reserved. +// +// Licensed under the Apache License, Version 2.0 (the "License"); +// you may not use this file except in compliance with the License. +// You may obtain a copy of the License at +// +// http://www.apache.org/licenses/LICENSE-2.0 +// +// Unless required by applicable law or agreed to in writing, software +// distributed under the License is distributed on an "AS IS" BASIS, +// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +// See the License for the specific language governing permissions and +// limitations under the License. +// +// --- LEGAL DISCLAIMER --- +// UnrealRoboticsLab is an independent software plugin. It is NOT affiliated with, +// endorsed by, or sponsored by Epic Games, Inc. "Unreal" and "Unreal Engine" are +// trademarks or registered trademarks of Epic Games, Inc. in the US and elsewhere. +// +// This plugin incorporates third-party software: MuJoCo (Apache 2.0), +// CoACD (MIT), and libzmq (MPL 2.0). See ThirdPartyNotices.txt for details. + +#pragma once + +#include "CoreMinimal.h" +#include "Components/SceneCaptureComponent2D.h" +#include "Engine/TextureRenderTarget2D.h" +#include "MuJoCo/Components/MjComponent.h" +#include "HAL/Runnable.h" +#include "HAL/ThreadSafeBool.h" +#include "Containers/Queue.h" +#include "MuJoCo/Components/Sensors/MjCameraTypes.h" +#include "MjCamera.generated.h" + +/** + * @class FCameraZmqWorker + * @brief Background thread for publishing high-bandwidth camera frames via ZeroMQ. + */ +class FCameraZmqWorker : public FRunnable +{ +public: + FCameraZmqWorker(const FString& InEndpoint, const FString& InTopic, FIntPoint InRes); + virtual ~FCameraZmqWorker(); + + virtual bool Init() override; + virtual uint32 Run() override; + virtual void Stop() override; + virtual void Exit() override; + + void PushFrame(const TArray& FrameData); + FString GetBoundEndpoint() const { return BoundEndpoint; } + +private: + FString RequestedEndpoint; + FString BoundEndpoint; + FString Topic; + FIntPoint Resolution; + + void* ZmqContext = nullptr; + void* ZmqPublisher = nullptr; + + FThreadSafeBool bStopThread; + TQueue, EQueueMode::Spsc> FrameQueue; +}; + +/** + * @class UMjCamera + * @brief Represents a MuJoCo element as an Unreal sensor component. + * + * Placed in the Sensors group because cameras are observation devices, not geometry. + * The component is cheap by default: no render target or GPU cost is incurred until + * SetStreamingEnabled(true) is called. + * + * Key design points: + * - No ExportTo / RegisterToSpec — camera is UE-side only, not fed back to MuJoCo. + * - SetStreamingEnabled() allocates the RT and calls IStreamingManager::AddViewInformation + * so textures load correctly even when the player pawn is far away. + * - RequestReadback() enqueues a non-blocking GPU→CPU copy; check IsReadbackReady() + * on a subsequent tick, then consume with ConsumePixels(). + */ +UCLASS(ClassGroup=(Custom), meta=(BlueprintSpawnableComponent)) +class URLAB_API UMjCamera : public UMjComponent +{ + GENERATED_BODY() + +public: + UMjCamera(); + + // ---- Identification ---- + + + // ---- Camera Intrinsics ---- + + /** @brief Vertical field of view in degrees (fovy= attribute). */ + UPROPERTY(EditAnywhere, BlueprintReadWrite, Category = "MuJoCo|Camera") + float Fovy = 45.0f; + + /** @brief Capture resolution (pixels). Defaults to 640×480. */ + UPROPERTY(EditAnywhere, BlueprintReadWrite, Category = "MuJoCo|Camera") + FIntPoint Resolution = FIntPoint(640, 480); + + /** @brief What this camera captures. Read at SetStreamingEnabled(true) time — + * toggle streaming off/on after changing. */ + UPROPERTY(EditAnywhere, BlueprintReadWrite, Category = "MuJoCo|Camera") + EMjCameraMode CaptureMode = EMjCameraMode::Real; + + /** @brief Near clip plane for Depth capture, centimetres. Values below this read as 0. */ + UPROPERTY(EditAnywhere, BlueprintReadWrite, Category = "MuJoCo|Camera", + meta = (EditCondition = "CaptureMode == EMjCameraMode::Depth", ClampMin = "0.1")) + float DepthNearCm = 10.0f; + + /** @brief Far clip plane for Depth capture, centimetres. Values beyond read as the maximum. */ + UPROPERTY(EditAnywhere, BlueprintReadWrite, Category = "MuJoCo|Camera", + meta = (EditCondition = "CaptureMode == EMjCameraMode::Depth", ClampMin = "1.0")) + float DepthFarCm = 10000.0f; + + // ---- Streaming ---- + + /** @brief Boost factor passed to IStreamingManager::AddViewInformation. + * Increase to force higher-quality texture mips near this camera. */ + UPROPERTY(EditAnywhere, BlueprintReadWrite, Category = "MuJoCo|Camera|Streaming") + float StreamingBoost = 1.0f; + + // ---- Capture Components ---- + + /** @brief The underlying SceneCaptureComponent2D. Capture is disabled by default. */ + UPROPERTY(VisibleAnywhere, BlueprintReadOnly, Category = "MuJoCo|Camera") + USceneCaptureComponent2D* CaptureComponent; + + /** @brief The render target. Null until SetStreamingEnabled(true) is first called. */ + UPROPERTY(VisibleAnywhere, BlueprintReadOnly, Category = "MuJoCo|Camera") + UTextureRenderTarget2D* RenderTarget = nullptr; + + // ---- ZeroMQ Streaming ---- + + /** @brief If true, the camera will automatically broadcast its frames over ZeroMQ when streaming is enabled. */ + UPROPERTY(EditAnywhere, BlueprintReadWrite, Category = "MuJoCo|Camera|Network") + bool bEnableZmqBroadcast = false; + + /** @brief The ZMQ Endpoint for this specific camera (e.g., tcp://\*:5558). Must be unique per camera. */ + UPROPERTY(EditAnywhere, BlueprintReadWrite, Category = "MuJoCo|Camera|Network") + FString ZmqEndpoint = TEXT("tcp://*:5558"); + + // ---- Public API ---- + + /** + * @brief Allocates the render target and begins streaming / scene capture. + * Call with bEnable=false to stop rendering and free the capture budget. + */ + UFUNCTION(BlueprintCallable, Category = "MuJoCo|Camera") + void SetStreamingEnabled(bool bEnable); + + /** + * @brief Enqueues a non-blocking asynchronous GPU→CPU pixel readback. + * No-op if streaming is not enabled or a readback is already in flight. + */ + UFUNCTION(BlueprintCallable, Category = "MuJoCo|Camera") + void RequestReadback(); + + /** + * @brief Returns true when the GPU→CPU copy started by RequestReadback() is complete. + */ + UFUNCTION(BlueprintCallable, Category = "MuJoCo|Camera") + bool IsReadbackReady() const; + + /** + * @brief Consumes and returns the pixel array from the last completed readback. + * Returns an empty array if not ready. Clears the pending state. + */ + UFUNCTION(BlueprintCallable, Category = "MuJoCo|Camera") + TArray ConsumePixels(); + + /** + * @brief Returns the ZMQ endpoint actually bound (may differ from ZmqEndpoint if auto-incremented). + */ + UFUNCTION(BlueprintCallable, Category = "MuJoCo|Camera") + FString GetActualZmqEndpoint() const; + + /** + * @brief Returns the bound camera component pointer (for UI wiring). + */ + UFUNCTION(BlueprintCallable, Category = "MuJoCo|Camera") + UMjCamera* GetSelf() { return this; } + + /** + * @brief Exports camera properties to a MuJoCo spec camera structure. + * @param cam Pointer to the target mjsCamera structure. + * @param def Optional default structure (unused, kept for API consistency). + */ + void ExportTo(mjsCamera* cam, mjsDefault* def = nullptr); + + /** + * @brief Imports properties from a MuJoCo XML node. + * Reads: name, fovy, pos, quat, euler, xyaxes, zaxis, + * width/height (MuJoCo 3.x resolution hints). + */ + void ImportFromXml(const class FXmlNode* Node); + void ImportFromXml(const class FXmlNode* Node, const struct FMjCompilerSettings& CompilerSettings); + +protected: + virtual void BeginPlay() override; + virtual void TickComponent(float DeltaTime, ELevelTick TickType, + FActorComponentTickFunction* ThisTickFunction) override; + virtual void OnRegister() override; + virtual void EndPlay(const EEndPlayReason::Type EndPlayReason) override; + +private: + // ---- Internal helpers ---- + void SetupRenderTarget(); + void RegisterWithStreamingManager(); + + /** For non-seg modes: rebuild HiddenComponents from the currently-live seg pools. + * Cheap (N siblings pointer copies), called each tick so a late-starting seg + * camera doesn't contaminate an already-streaming RGB/Depth camera. */ + void RefreshHiddenComponentsFromSegPools(); + + // ---- Readback state ---- + TOptional> PendingPixels; + FRenderCommandFence ReadbackFence; + bool bReadbackPending = false; + bool bReadbackComplete = false; + + // ---- Streaming state ---- + bool bStreamingEnabled = false; + + // ---- ZMQ Worker ---- + FCameraZmqWorker* ZmqWorker = nullptr; + FRunnableThread* WorkerThread = nullptr; +}; diff --git a/Plugins/UnrealRoboticsLab/Source/URLab/URLab.Build.cs b/Plugins/UnrealRoboticsLab/Source/URLab/URLab.Build.cs new file mode 100644 index 0000000000..3653a309a7 --- /dev/null +++ b/Plugins/UnrealRoboticsLab/Source/URLab/URLab.Build.cs @@ -0,0 +1,409 @@ +// Copyright Epic Games, Inc. All Rights Reserved. + + +using System; +using System.Diagnostics; +using UnrealBuildTool; +using System.IO; +using System.Text.RegularExpressions; + +public class URLab : ModuleRules +{ + public URLab(ReadOnlyTargetRules Target) : base(Target) + { + PCHUsage = ModuleRules.PCHUsageMode.UseExplicitOrSharedPCHs; + CppStandard = CppStandardVersion.Cpp17; + //bUseRTTI = true; // Required for type info + //bEnableExceptions = true; + + PublicDependencyModuleNames.AddRange(new string[] + { + "Core", + "CoreUObject", + "Engine", + "InputCore", + "PhysicsCore", + "XmlParser", + "HTTP", + "Json", + "JsonUtilities", + "Slate", + "SlateCore", + "Projects", + "ApplicationCore", + "RenderCore", + "RHI", + "UMG", + "AssetRegistry", + "DynamicMesh", + "ProceduralMeshComponent" + // "GeometryFramework", + // "GeometryCore" + }); + + // Editor-only dependencies for DecomposeMesh and other #if WITH_EDITOR code + if (Target.bBuildEditor) + { + PrivateDependencyModuleNames.AddRange(new string[] + { + "UnrealEd", + "AssetTools", + "DesktopPlatform" + }); + } + + PrivateDependencyModuleNames.AddRange(new string[] + { + "CinematicCamera", + "ImageWrapper", + "EnhancedInput", + "Chaos", + "Landscape", + "Eigen", + }); + + DynamicallyLoadedModuleNames.AddRange(new string[] + { + // Add dynamically loaded modules here + }); + + if (Target.Platform == UnrealTargetPlatform.Win64) + { + PublicSystemLibraries.AddRange(new string[] + { + "kernel32.lib", + "user32.lib" + }); + } + else if (Target.Platform == UnrealTargetPlatform.Linux) + { + PublicSystemLibraries.AddRange(new string[] + { + "pthread", "dl", "rt" + }); + } + + if (Target.Platform == UnrealTargetPlatform.Linux) + { + // Ensure Unreal recognizes MuJoCo's export macros + } + + + if (Target.Platform == UnrealTargetPlatform.Linux) + { + PublicDefinitions.Add("__linux__=1"); + PublicDefinitions.Add("__unix__=1"); + // MuJoCo 3.x compatibility (symbols removed from 2.x) + PublicDefinitions.Add("mjNPOLY=0"); + PublicDefinitions.Add("mjENBL_SLEEP=0"); + PublicDefinitions.Add("mjGAIN_DCMOTOR=mjGAIN_AFFINE"); + PublicDefinitions.Add("mjBIAS_DCMOTOR=mjBIAS_AFFINE"); + PublicDefinitions.Add("mjDYN_DCMOTOR=mjDYN_NONE"); + } + + + VerifyThirdPartyInstalls(); + + AddMuj(Target); + // CoACD: manually add include path (symlink not followed by Directory.Exists) + PublicIncludePaths.Add(Path.Combine(ThirdPartyPath, "CoACD", "CoACD", "include")); + AddCoACD(Target); + AddZeroMQ(Target); + } + + private string ThirdPartyPath + { + get { return Path.Combine(PluginDirectory, "third_party", "install"); } + } + + private void AddThirdPartyLibrary(string LibraryName, ReadOnlyTargetRules Target) + { + string FullPath = Path.Combine(ThirdPartyPath, LibraryName); + + // Add include directory if it exists + string IncludePath = Path.Combine(FullPath, "include"); + if (Directory.Exists(IncludePath)) + { + PublicIncludePaths.Add(IncludePath); + } + + if (Target.Platform == UnrealTargetPlatform.Win64) + { + // Link all libraries in the lib directory + string LibPath = Path.Combine(FullPath, "lib"); + if (Directory.Exists(LibPath)) + { + string[] LibFiles = Directory.GetFiles(LibPath, "*.lib", SearchOption.AllDirectories); + foreach (string LibFile in LibFiles) + { + PublicAdditionalLibraries.Add(LibFile); + } + } + + // Stage DLLs next to executable for packaged builds + // Skip MSVC runtime DLLs (handled by redistributable installer) + string BinPath = Path.Combine(FullPath, "bin"); + if (Directory.Exists(BinPath)) + { + string[] DllFiles = Directory.GetFiles(BinPath, "*.dll", SearchOption.AllDirectories); + foreach (string DllFile in DllFiles) + { + string DllName = Path.GetFileName(DllFile); + if (DllName.StartsWith("vcruntime") || DllName.StartsWith("msvcp") || DllName.StartsWith("concrt")) + continue; + RuntimeDependencies.Add("$(BinaryOutputDir)/" + DllName, DllFile, StagedFileType.NonUFS); + PublicDelayLoadDLLs.Add(DllName); + } + } + } + else if (Target.Platform == UnrealTargetPlatform.Linux) + { + // Link all static libraries and shared objects for Linux + string LibPath = Path.Combine(FullPath, "lib"); + if (Directory.Exists(LibPath)) + { + foreach (string LibFile in Directory.GetFiles(LibPath, "*.a", SearchOption.AllDirectories)) + { + PublicAdditionalLibraries.Add(LibFile); + } + foreach (string LibFile in Directory.GetFiles(LibPath, "*.so", SearchOption.AllDirectories)) + { + PublicAdditionalLibraries.Add(LibFile); + RuntimeDependencies.Add(LibFile); + PublicDelayLoadDLLs.Add(Path.GetFileName(LibFile)); + } + } + + // Add binaries (often plugin/shared objects) for Linux + string BinPath = Path.Combine(FullPath, "bin"); + if (Directory.Exists(BinPath)) + { + foreach (string BinFile in Directory.GetFiles(BinPath, "*.so", SearchOption.AllDirectories)) + { + RuntimeDependencies.Add(BinFile); + PublicDelayLoadDLLs.Add(Path.GetFileName(BinFile)); + PublicAdditionalLibraries.Add(BinFile); + } + } + } + } + + // Set to true to skip the submodule/install SHA drift checks. The default + // (false) catches two common failure modes before any code compiles: + // A) "I git-pulled URLab but forgot `git submodule update`" - the pinned + // submodule SHA has moved but the working tree still points at the old + // one, so we'd build against stale MuJoCo/CoACD/libzmq source. + // B) "I updated submodules but forgot to re-run third_party/*/build.ps1" - + // the source is fresh but third_party/install// was built from an + // older SHA, so UE links against a mismatched binary. + // Flip this to true if you are intentionally iterating on a submodule + // locally. The per-dep build scripts accept -NoSubmoduleSync / --no-submodule-sync + // for the same purpose at build-script time. + // Static readonly (not const) so toggling to `true` doesn't produce + // CS0162 "unreachable code" warnings in the active branch. + private static readonly bool SkipThirdPartyDriftChecks = false; + + // Names match the directories under third_party/ and third_party/install/. + private static readonly string[] ThirdPartyPackages = new[] { "MuJoCo", "CoACD", "libzmq" }; + + protected void AddMuj(ReadOnlyTargetRules Target) + { + AddThirdPartyLibrary("MuJoCo", Target); + } + + // Checks each third-party dep for submodule drift (Layer A) and install + // drift (Layer B). Runs once up front so any failure message is clearly + // attributable before UBT tries to resolve libs/includes. If URLab itself + // isn't a git checkout (release zip), the checks self-disable with a note. + private void VerifyThirdPartyInstalls() + { + if (SkipThirdPartyDriftChecks) + { + Console.WriteLine("URLab: SkipThirdPartyDriftChecks=true - skipping submodule/install SHA checks."); + return; + } + + string PluginRoot = PluginDirectory; + bool IsGitCheckout = Directory.Exists(Path.Combine(PluginRoot, ".git")) + || File.Exists(Path.Combine(PluginRoot, ".git")); + if (!IsGitCheckout) + { + Console.WriteLine("URLab: plugin root is not a git checkout - skipping submodule/install SHA checks."); + return; + } + + RegisterDriftCheckDependencies(PluginRoot); + + foreach (string Package in ThirdPartyPackages) + { + VerifyPackageInstall(PluginRoot, Package); + } + } + + // Tell UBT to re-evaluate this module when any of these files change so + // the Layer A/B drift checks fire on the next build. Without this, UBT's + // makefile cache short-circuits with "Target is up to date" and Build.cs + // never runs, letting a stale install slip through. + private void RegisterDriftCheckDependencies(string PluginRoot) + { + // URLab's own index - covers `git pull` / `git reset` on the plugin, + // which is how an out-of-sync submodule pointer typically appears. + string PluginGitIndex = Path.Combine(PluginRoot, ".git", "index"); + if (File.Exists(PluginGitIndex)) + { + ExternalDependencies.Add(PluginGitIndex); + } + + foreach (string Package in ThirdPartyPackages) + { + // Submodule HEAD file - Git rewrites this on checkout/reset/update, + // so it's the right signal for "submodule HEAD moved" (Layer A). + string SubmoduleGitHead = Path.Combine(PluginRoot, ".git", "modules", + "third_party", Package, "src", "HEAD"); + if (File.Exists(SubmoduleGitHead)) + { + ExternalDependencies.Add(SubmoduleGitHead); + } + + // INSTALLED_SHA.txt - changes whenever build.ps1 re-installs, or + // (as in our tests) when edited by hand. Covers Layer B. + string ShaFile = Path.Combine(ThirdPartyPath, Package, "INSTALLED_SHA.txt"); + if (File.Exists(ShaFile)) + { + ExternalDependencies.Add(ShaFile); + } + } + } + + private void VerifyPackageInstall(string PluginRoot, string Package) + { + string SubmoduleRelPath = "third_party/" + Package + "/src"; + string SubmoduleAbsPath = Path.Combine(PluginRoot, "third_party", Package, "src"); + string InstallDir = Path.Combine(ThirdPartyPath, Package); + string ShaFile = Path.Combine(InstallDir, "INSTALLED_SHA.txt"); + + // Submodule not initialised yet: directory missing, or present but empty. + bool SubmoduleMissing = !Directory.Exists(SubmoduleAbsPath) + || Directory.GetFileSystemEntries(SubmoduleAbsPath).Length == 0; + if (SubmoduleMissing) + { + throw new BuildException( + "{0} submodule not initialised at '{1}'. From the plugin root run:\n" + + " git submodule update --init --recursive --force\n" + + "then re-run third_party/{0}/build.ps1 (Windows) or build.sh (Linux/macOS). " + + "To bypass this check, set SkipThirdPartyDriftChecks=true in URLab.Build.cs.", + Package, SubmoduleAbsPath); + } + + // LAYER A: submodule HEAD vs URLab's committed pointer. + // Catches "pulled URLab but forgot `git submodule update`". + string ExpectedSha; + try + { + string Pointer = RunGit(PluginRoot, "ls-tree HEAD " + SubmoduleRelPath); + ExpectedSha = ParseSubmodulePointerSha(Pointer, Package); + } + catch (Exception Ex) + { + // Shallow clones or repos without a HEAD pointing at the submodule + // (edge cases) can't run Layer A. Warn and continue to Layer B. + Console.WriteLine("URLab: could not read URLab pointer for {0} ({1}) - skipping Layer A check.", + Package, Ex.Message); + ExpectedSha = null; + } + + string ActualSha; + try + { + ActualSha = RunGit(SubmoduleAbsPath, "rev-parse HEAD"); + } + catch (Exception Ex) + { + throw new BuildException( + "{0} submodule at '{1}' is not a valid git checkout ({2}). From the plugin root run:\n" + + " git submodule update --init --recursive --force\n" + + "then re-run third_party/{0}/build.ps1.", + Package, SubmoduleAbsPath, Ex.Message); + } + + if (ExpectedSha != null && !string.Equals(ExpectedSha, ActualSha, StringComparison.OrdinalIgnoreCase)) + { + throw new BuildException( + "{0} submodule drift: URLab expects SHA {1} but third_party/{0}/src/ is at {2}. " + + "From the plugin root run:\n" + + " git submodule update --init --recursive --force\n" + + "then re-run third_party/{0}/build.ps1 (Windows) or build.sh (Linux/macOS). " + + "To bypass this check, set SkipThirdPartyDriftChecks=true in URLab.Build.cs.", + Package, ExpectedSha, ActualSha); + } + + // LAYER B: install SHA (what build.ps1 recorded) vs submodule HEAD. + // Catches "updated submodules but forgot to re-run build.ps1". + if (!File.Exists(ShaFile)) + { + throw new BuildException( + "{0} install is missing '{1}'. Run third_party/{0}/build.ps1 (Windows) or " + + "third_party/{0}/build.sh (Linux/macOS). " + + "To bypass this check, set SkipThirdPartyDriftChecks=true in URLab.Build.cs.", + Package, ShaFile); + } + + string InstalledSha = File.ReadAllText(ShaFile).Trim(); + if (!string.Equals(InstalledSha, ActualSha, StringComparison.OrdinalIgnoreCase)) + { + throw new BuildException( + "{0} install is stale: built from SHA {1} but third_party/{0}/src/ is now at {2}. " + + "Re-run third_party/{0}/build.ps1 (Windows) or third_party/{0}/build.sh (Linux/macOS). " + + "To bypass this check, set SkipThirdPartyDriftChecks=true in URLab.Build.cs.", + Package, InstalledSha, ActualSha); + } + } + + private static string RunGit(string WorkingDir, string Arguments) + { + ProcessStartInfo PSI = new ProcessStartInfo("git", Arguments) + { + WorkingDirectory = WorkingDir, + RedirectStandardOutput = true, + RedirectStandardError = true, + UseShellExecute = false, + CreateNoWindow = true, + }; + using (Process Proc = Process.Start(PSI)) + { + string Stdout = Proc.StandardOutput.ReadToEnd(); + string Stderr = Proc.StandardError.ReadToEnd(); + Proc.WaitForExit(); + if (Proc.ExitCode != 0) + { + throw new Exception(string.Format("git {0} exited {1}: {2}", + Arguments, Proc.ExitCode, Stderr.Trim())); + } + return Stdout.Trim(); + } + } + + private static string ParseSubmodulePointerSha(string Output, string Package) + { + // `git ls-tree HEAD ` output: "160000 commit \t" + Match M = Regex.Match(Output, @"160000\s+commit\s+([0-9a-f]{40})"); + if (!M.Success) + { + throw new Exception(string.Format( + "could not parse submodule pointer for {0}: '{1}'", + Package, Output)); + } + return M.Groups[1].Value; + } + + protected void AddCoACD(ReadOnlyTargetRules Target) + { + AddThirdPartyLibrary("CoACD", Target); + PublicDefinitions.Add("COACD_EXPORTS=1"); + } + + protected void AddZeroMQ(ReadOnlyTargetRules Target) + { + AddThirdPartyLibrary("libzmq", Target); + } +} diff --git a/Plugins/UnrealRoboticsLab/Source/URLabEditor/Private/MujocoMeshImporter.cpp b/Plugins/UnrealRoboticsLab/Source/URLabEditor/Private/MujocoMeshImporter.cpp new file mode 100644 index 0000000000..63ac1619ff --- /dev/null +++ b/Plugins/UnrealRoboticsLab/Source/URLabEditor/Private/MujocoMeshImporter.cpp @@ -0,0 +1,628 @@ +// Copyright (c) 2026 Jonathan Embley-Riches. All rights reserved. +// +// Licensed under the Apache License, Version 2.0 (the "License"); +// you may not use this file except in compliance with the License. +// You may obtain a copy of the License at +// +// http://www.apache.org/licenses/LICENSE-2.0 +// +// Unless required by applicable law or agreed to in writing, software +// distributed under the License is distributed on an "AS IS" BASIS, +// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +// See the License for the specific language governing permissions and +// limitations under the License. +// +// --- LEGAL DISCLAIMER --- +// UnrealRoboticsLab is an independent software plugin. It is NOT affiliated with, +// endorsed by, or sponsored by Epic Games, Inc. "Unreal" and "Unreal Engine" are +// trademarks or registered trademarks of Epic Games, Inc. in the US and elsewhere. +// +// This plugin incorporates third-party software: MuJoCo (Apache 2.0), +// CoACD (MIT), and libzmq (MPL 2.0). See ThirdPartyNotices.txt for details. + +// MujocoMeshImporter.cpp — Mesh and material import methods for UMujocoGenerationAction. + +#include "MujocoGenerationAction.h" +#include "URLabEditorLogging.h" +#include "Engine/StaticMesh.h" +#include "AssetToolsModule.h" +#include "Factories/FbxFactory.h" +#include "Factories/FbxImportUI.h" +#include "Factories/FbxStaticMeshImportData.h" +#include "AssetImportTask.h" +#include "FileHelpers.h" +#include "IImageWrapperModule.h" +#include "IImageWrapper.h" +#include "ImageUtils.h" +#include "AssetRegistry/AssetRegistryModule.h" +#include "UObject/SavePackage.h" + +UStaticMesh* UMujocoGenerationAction::ImportSingleMesh(const FString& SourcePath, const FString& DestinationPath) +{ + if (SourcePath.IsEmpty() || DestinationPath.IsEmpty()) { + return nullptr; + } + + FString FileName = FPaths::GetBaseFilename(SourcePath); + FString PackageName = FPaths::Combine(DestinationPath, FileName); + PackageName = UPackageTools::SanitizePackageName(PackageName); + + UStaticMesh* ExistingMesh = LoadObject(nullptr, *PackageName); + if (ExistingMesh) return ExistingMesh; + + UE_LOG(LogURLabEditor, Log, TEXT("Importing mesh from: %s to %s"), *SourcePath, *DestinationPath); + + // 优先考虑文件格式: FBX > GLB > GLTF > Original (OBJ/STL) + FString ActualSourcePath = SourcePath; + FString BasePath = FPaths::ChangeExtension(SourcePath, ""); + + // 按优先顺序检查格式 + TArray Extensions = { TEXT("fbx"), TEXT("glb"), TEXT("gltf") }; + bool bFoundHigherPriority = false; + + for (const FString& Ext : Extensions) + { + FString PotentialPath = BasePath + TEXT(".") + Ext; + if (FPaths::FileExists(PotentialPath)) + { + ActualSourcePath = PotentialPath; + bFoundHigherPriority = true; + UE_LOG(LogURLabEditor, Log, TEXT("Found higher priority mesh file: %s"), *ActualSourcePath); + break; + } + } + + // 如果没有找到高优先级格式,确保原始存在 + if (!bFoundHigherPriority && !FPaths::FileExists(ActualSourcePath)) + { + UE_LOG(LogURLabEditor, Error, TEXT("Source mesh file does not exist: %s"), *ActualSourcePath); + return nullptr; + } + + IAssetTools& AssetTools = FModuleManager::LoadModuleChecked("AssetTools").Get(); + + // 先试着用 MikkTSpace 导入(效果最好) + // 使用 ActualSourcePath 替代 SourcePath + UStaticMesh* ImportedMesh = AttemptMeshImport(ActualSourcePath, DestinationPath, EFBXNormalGenerationMethod::MikkTSpace); + + // 验证网格 + if (ImportedMesh && ValidateMesh(ImportedMesh, FileName)) + { + UE_LOG(LogURLabEditor, Log, TEXT("Successfully imported mesh '%s' with MikkTSpace"), *FileName); + return ImportedMesh; + } + + // MikkTSpace 失败或网格无效 - 尝试使用内置法线作为备用 + if (ImportedMesh) + { + UE_LOG(LogURLabEditor, Warning, TEXT("Mesh '%s' has issues with MikkTSpace, attempting fallback with BuiltIn normals"), *FileName); + } + else + { + UE_LOG(LogURLabEditor, Warning, TEXT("Failed to import mesh '%s' with MikkTSpace, attempting fallback"), *FileName); + } + + ImportedMesh = AttemptMeshImport(ActualSourcePath, DestinationPath, EFBXNormalGenerationMethod::BuiltIn); + + if (ImportedMesh && ValidateMesh(ImportedMesh, FileName)) + { + UE_LOG(LogURLabEditor, Warning, TEXT("Successfully imported mesh '%s' with BuiltIn normals (fallback)"), *FileName); + return ImportedMesh; + } + + UE_LOG(LogURLabEditor, Error, TEXT("Failed to import mesh '%s' - all import methods failed"), *FileName); + return nullptr; +} + +UStaticMesh* UMujocoGenerationAction::AttemptMeshImport(const FString& SourcePath, const FString& DestinationPath, EFBXNormalGenerationMethod::Type NormalMethod) +{ + IAssetTools& AssetTools = FModuleManager::LoadModuleChecked("AssetTools").Get(); + + // 配置自动导入任务 + UAssetImportTask* ImportTask = NewObject(); + // 规范斜杠并转为绝对路径 + FString NormalizedSourcePath = SourcePath; + FPaths::MakeStandardFilename(NormalizedSourcePath); // 使用标准分隔符('/') + NormalizedSourcePath = FPaths::ConvertRelativePathToFull(NormalizedSourcePath); // 变为绝对路径 + FPaths::NormalizeFilename(NormalizedSourcePath); // 清理重复分隔符、末尾斜杠等 + ImportTask->Filename = NormalizedSourcePath; + ImportTask->DestinationPath = DestinationPath; // 导入的资产生成在:/Game/MuJoCoImports/g1_29dof_rev_1_0_ue_Assets/Meshes + ImportTask->bAutomated = true; + ImportTask->bSave = true; + ImportTask->bReplaceExisting = true; + ImportTask->bReplaceExistingSettings = true; + + // 仅配置 FBX 工厂以支持 FBX/OBJ + FString Extension = FPaths::GetExtension(SourcePath).ToLower(); + + if (Extension == "fbx" || Extension == "obj" || Extension == "t3d") + { + // 配置 FBX 工厂 + UFbxFactory* FbxFactory = NewObject(); + ImportTask->Factory = FbxFactory; + + // 配置 FBX 导入 UI 设置 + UFbxImportUI* ImportUI = NewObject(); + ImportUI->bImportMesh = true; + ImportUI->bImportTextures = false; + ImportUI->bImportMaterials = false; + ImportUI->bAutomatedImportShouldDetectType = false; + ImportUI->MeshTypeToImport = FBXIT_StaticMesh; + + // 鲁棒的的静态网格设置 + ImportUI->StaticMeshImportData->bCombineMeshes = true; + ImportUI->StaticMeshImportData->bRemoveDegenerates = true; + ImportUI->StaticMeshImportData->bComputeWeightedNormals = true; + ImportUI->StaticMeshImportData->bGenerateLightmapUVs = true; + ImportUI->StaticMeshImportData->NormalImportMethod = EFBXNormalImportMethod::FBXNIM_ComputeNormals; + ImportUI->StaticMeshImportData->NormalGenerationMethod = NormalMethod; + + // 用于修复退化几何体(特别是来自 OBJ 文件)的其他设置 + ImportUI->StaticMeshImportData->bAutoGenerateCollision = false; // 我们单独处理碰撞。 + ImportUI->StaticMeshImportData->bBuildReversedIndexBuffer = true; + // ImportUI->StaticMeshImportData->bBuildNanite = false; // Nanite 需要清晰的几何结构(UE4中没有Nanite) + + // 顶点焊接——对于修复导致切线退化的重叠顶点至关重要。 + // 注意:UE 5.7 中没有直接的 bWeldVertices 函数,但 bRemoveDegenerates 函数可以处理这个问题。 + + // 将 UI 应用于工厂 + FbxFactory->ImportUI = ImportUI; + FbxFactory->EnableShowOption(); + } + else + { + // 对于其他格式(GLTF、GLB 等),让虚幻引擎的资源工具自动查找合适的工厂。 + // 我们无需手动设置工厂,因此 ImportAssetTasks 会自动检测正确的工厂。 + // 注意:我们会丢失一些细粒度的设置(例如 NormalGenerationMethod), + // 但 GLTF 导入器通常依赖于文件本身的数据,这些数据通常比 OBJ 格式更清晰。 + // ImportTask->Factory = nullptr; + + UE_LOG(LogURLabEditor, Log, TEXT("Using automated factory detection for mesh: %s"), *SourcePath); + } + + // 运行导入 + TArray ImportTasks; + ImportTasks.Add(ImportTask); + AssetTools.ImportAssetTasks(ImportTasks); + + // 获取结果 + TArray ImportedAssets; + // TArray t = ImportTask->ImportedObjectPaths; + for (UObject* Obj : ImportTask->Result) // for (UObject* Obj : ImportTask->GetObjects()) + { + if (Obj) ImportedAssets.Add(Obj); + } + + // 记录所有导入的资产以进行调试 + UE_LOG(LogURLabEditor, Log, TEXT("[ImportSingleMesh] Import returned %d objects:"), ImportedAssets.Num()); + for (int32 i = 0; i < ImportedAssets.Num(); ++i) + { + UObject* Obj = ImportedAssets[i]; + UE_LOG(LogURLabEditor, Log, TEXT(" [%d] %s (%s) at %s"), + i, *Obj->GetName(), *Obj->GetClass()->GetName(), *Obj->GetPathName()); + } + + // 在所有导入的资源中搜索 StaticMesh(GLB 导入可能首先返回纹理) + UStaticMesh* Mesh = nullptr; + for (UObject* Obj : ImportedAssets) + { + Mesh = Cast(Obj); + if (Mesh) break; + } + + // 如果在直接搜索结果中找不到,请搜索 Interchange 可能使用的子文件夹路径。 + if (!Mesh) + { + FString MeshName = FPaths::GetBaseFilename(SourcePath); + + // 尝试不同的子文件夹模式,互换使用 + TArray SearchPaths = { + FString::Printf(TEXT("%s/%s/StaticMeshes/%s.%s"), *DestinationPath, *MeshName, *MeshName, *MeshName), + FString::Printf(TEXT("%s/%s/StaticMeshes/%s"), *DestinationPath, *MeshName, *MeshName), + FString::Printf(TEXT("%s/%s.%s"), *DestinationPath, *MeshName, *MeshName), + }; + + for (const FString& SearchPath : SearchPaths) + { + Mesh = LoadObject(nullptr, *SearchPath); + if (Mesh) + { + UE_LOG(LogURLabEditor, Log, TEXT("[ImportSingleMesh] Found mesh at: %s"), *SearchPath); + break; + } + else + { + UE_LOG(LogURLabEditor, Log, TEXT("[ImportSingleMesh] Not found at: %s"), *SearchPath); + } + } + + // 最后手段:使用资源注册表查找目标文件夹中的任何 StaticMesh 文件 + if (!Mesh) + { + FString SearchDir = FString::Printf(TEXT("%s/%s"), *DestinationPath, *MeshName); + UE_LOG(LogURLabEditor, Log, TEXT("[ImportSingleMesh] Searching asset registry under: %s"), *SearchDir); + + IAssetRegistry& AssetRegistry = FModuleManager::LoadModuleChecked("AssetRegistry").Get(); + TArray Assets; + AssetRegistry.GetAssetsByPath(FName(*SearchDir), Assets, true); + + for (const FAssetData& Asset : Assets) + { + // UE_LOG(LogURLabEditor, Log, TEXT(" Registry: %s (%s)"), *Asset.AssetName.ToString(), *Asset.AssetClassPath.ToString()); + // if (Asset.AssetClassPath.GetAssetName() == TEXT("StaticMesh")) + // { + // Mesh = Cast(Asset.GetAsset()); + // if (Mesh) + // { + // UE_LOG(LogURLabEditor, Log, TEXT("[ImportSingleMesh] Found mesh via registry: %s"), *Asset.GetObjectPathString()); + // break; + // } + // } + } + } + } + + if (Mesh) + { + // 清除可能引用已剥离纹理的 Interchange 创建的材质。 + // 我们的导入流程会在 SCS 模板上分配 MI_ 材质实例, + // 但静态网格资源会在其槽位中保留 Interchange 材质。 + // 这可能会导致在浏览/生成缩略图时渲染线程崩溃 (UE-23902)。 + // for (FStaticMaterial& Mat : Mesh->GetStaticMaterials()) + // { + // Mat.MaterialInterface = UMaterial::GetDefaultMaterial(MD_Surface); + // } + + // 强制重建边界 - 对于修复 0x0x0 大小问题至关重要 + Mesh->Build(); + Mesh->CalculateExtendedBounds(); + + UPackage* Package = Mesh->GetOutermost(); + FEditorFileUtils::PromptForCheckoutAndSave({Package}, false, false); + + UE_LOG(LogURLabEditor, Log, TEXT("Imported mesh '%s' - Bounds: %s"), + *FPaths::GetBaseFilename(SourcePath), + *Mesh->GetBoundingBox().GetSize().ToString()); + + return Mesh; + } + + return nullptr; +} + +bool UMujocoGenerationAction::ValidateMesh(UStaticMesh* Mesh, const FString& MeshName) +{ + if (!Mesh) + { + UE_LOG(LogURLabEditor, Error, TEXT("Mesh validation failed: Mesh is null")); + return false; + } + + // 检查网格是否有渲染数据 + // if (!Mesh->GetRenderData()) + // { + // UE_LOG(LogURLabEditor, Error, TEXT("Mesh '%s' has no render data"), *MeshName); + // return false; + // } + + // 检查 LOD 0 是否存在 + // if (Mesh->GetRenderData()->LODResources.Num() == 0) + // { + // UE_LOG(LogURLabEditor, Error, TEXT("Mesh '%s' has no LOD resources"), *MeshName); + // return false; + // } + + // const FStaticMeshLODResources& LOD0 = Mesh->GetRenderData()->LODResources[0]; +// + // // 检查顶点缓冲 + // if (LOD0.VertexBuffers.StaticMeshVertexBuffer.GetNumVertices() == 0) + // { + // UE_LOG(LogURLabEditor, Error, TEXT("Mesh '%s' has empty vertex buffer"), *MeshName); + // return false; + // } +// + // // 检查索引缓冲区 + // if (LOD0.IndexBuffer.GetNumIndices() == 0) + // { + // UE_LOG(LogURLabEditor, Error, TEXT("Mesh '%s' has empty index buffer"), *MeshName); + // return false; + // } + + // 记录网格统计信息 + int32 NumVertices = 0; // = LOD0.VertexBuffers.StaticMeshVertexBuffer.GetNumVertices(); + int32 NumTriangles = 0; // = LOD0.IndexBuffer.GetNumIndices() / 3; + int32 NumUVChannels = 0; // = LOD0.VertexBuffers.StaticMeshVertexBuffer.GetNumTexCoords(); + + UE_LOG(LogURLabEditor, Log, TEXT("Mesh '%s' validation: %d vertices, %d triangles, %d UV channels"), + *MeshName, NumVertices, NumTriangles, NumUVChannels); + + // 如果没有 UV 通道 0,就发出警告 + if (NumUVChannels == 0) + { + UE_LOG(LogURLabEditor, Warning, TEXT("Mesh '%s' has no UV channels - materials may not display correctly"), *MeshName); + } + + return true; +} + + +UTexture2D* UMujocoGenerationAction::ImportSingleTexture(const FString& SourcePath, const FString& DestinationPath) +{ + if (SourcePath.IsEmpty() || DestinationPath.IsEmpty()) + { + return nullptr; + } + + FString FileName = FPaths::GetBaseFilename(SourcePath); + FString PackageName = FPaths::Combine(DestinationPath, FileName); + PackageName = UPackageTools::SanitizePackageName(PackageName); + + // Check if texture already exists + UTexture2D* ExistingTexture = LoadObject(nullptr, *PackageName); + if (ExistingTexture) + { + return ExistingTexture; + } + + UE_LOG(LogURLabEditor, Log, TEXT("Importing texture from: %s to %s"), *SourcePath, *DestinationPath); + + // Load texture file from disk + TArray FileData; + if (!FFileHelper::LoadFileToArray(FileData, *SourcePath)) + { + UE_LOG(LogURLabEditor, Error, TEXT("Failed to load texture file: %s"), *SourcePath); + return nullptr; + } + + // Determine image format from extension + FString Extension = FPaths::GetExtension(SourcePath).ToLower(); + EImageFormat ImageFormat = EImageFormat::Invalid; + + if (Extension == TEXT("png")) + { + ImageFormat = EImageFormat::PNG; + } + else if (Extension == TEXT("jpg") || Extension == TEXT("jpeg")) + { + ImageFormat = EImageFormat::JPEG; + } + else if (Extension == TEXT("tga")) + { + // ImageFormat = EImageFormat::TGA; + } + else if (Extension == TEXT("bmp")) + { + ImageFormat = EImageFormat::BMP; + } + else + { + UE_LOG(LogURLabEditor, Warning, TEXT("Unsupported texture format: %s"), *Extension); + return nullptr; + } + + // Decode image + IImageWrapperModule& ImageWrapperModule = FModuleManager::LoadModuleChecked(FName("ImageWrapper")); + TSharedPtr ImageWrapper = ImageWrapperModule.CreateImageWrapper(ImageFormat); + + if (!ImageWrapper.IsValid() || !ImageWrapper->SetCompressed(FileData.GetData(), FileData.Num())) + { + UE_LOG(LogURLabEditor, Error, TEXT("Failed to decode texture: %s"), *SourcePath); + return nullptr; + } + + // Get raw image data + TArray RawData; + if (!ImageWrapper->GetRaw(ERGBFormat::BGRA, 8, RawData)) + { + UE_LOG(LogURLabEditor, Error, TEXT("Failed to get raw texture data: %s"), *SourcePath); + return nullptr; + } + + // Create package + UPackage* Package = CreatePackage(*PackageName); + Package->FullyLoad(); + + // Create texture + UTexture2D* NewTexture = NewObject(Package, FName(*FileName), RF_Public | RF_Standalone); + + // Set texture properties + NewTexture->Source.Init(ImageWrapper->GetWidth(), ImageWrapper->GetHeight(), 1, 1, TSF_BGRA8, RawData.GetData()); + NewTexture->SRGB = true; + NewTexture->CompressionSettings = TextureCompressionSettings::TC_Default; + NewTexture->MipGenSettings = TextureMipGenSettings::TMGS_FromTextureGroup; + NewTexture->UpdateResource(); + + // Save package + Package->MarkPackageDirty(); + FAssetRegistryModule::AssetCreated(NewTexture); + + FString PackageFileName = FPackageName::LongPackageNameToFilename(PackageName, FPackageName::GetAssetPackageExtension()); + FSavePackageArgs SaveArgs; + SaveArgs.TopLevelFlags = RF_Public | RF_Standalone; + SaveArgs.SaveFlags = SAVE_NoError; + // UPackage::SavePackage(Package, NewTexture, *PackageFileName, SaveArgs); + + UE_LOG(LogURLabEditor, Log, TEXT("Successfully imported texture: %s"), *FileName); + return NewTexture; +} + + +// 基于项目内的母材质(/UnrealRoboticsLab/Materials/M_MuJoCo_Master)为导入的网格创建或复用一个 UMaterialInstanceConstant, +// 并把从 MuJoCo 材质数据解析出的颜色/纹理参数写入该实例,返回已创建或复用的实例指针。 +UMaterialInstanceConstant* UMujocoGenerationAction::CreateMaterialInstance( + const FString& MeshName, + const FMuJoCoMaterialData& MaterialData, + const TMap& TextureAssets, + const FString& DestinationPath) +{ + // 加载 MuJoCo 相关资产的通用母材质模板:集中定义外观参数,包括基础色、金属度、粗糙度、法线等。 + // 应该位于 hutb\Unreal\CarlaUE4\Plugins\UnrealRoboticsLab\Content\Materials,而不是 hutb\Unreal\CarlaUE4\Content\Matrials + // 直接复制到 hutb\Unreal\CarlaUE4\Content\Matrials 会出现资产版本不符的问题: + // LogAssetRegistry: Error: Package ../../../../../Unreal/CarlaUE4/Plugins/UnrealRoboticsLab/Content/Materials/M_MuJoCo_Master.uasset is too old + UMaterial* MasterMaterial = LoadObject(nullptr, TEXT("/UnrealRoboticsLab/Materials/M_MuJoCo_Master.M_MuJoCo_Master")); + if (!MasterMaterial) + { + UE_LOG(LogURLabEditor, Error, TEXT("Failed to load master material: /UnrealRoboticsLab/Materials/M_MuJoCo_Master")); + return nullptr; + } + + // 创建材质实例包 + FString InstanceName = FString::Printf(TEXT("MI_%s"), *MeshName); + FString PackageName = FPaths::Combine(DestinationPath, InstanceName); + PackageName = UPackageTools::SanitizePackageName(PackageName); + + // 检查材质实例是否已存在——在同一导入会话期间重复使用(多个几何体引用同一材质), + // 但不要跳过参数设置。 + UMaterialInstanceConstant* ExistingInstance = LoadObject(nullptr, *PackageName); + if (ExistingInstance) + { + UE_LOG(LogURLabEditor, Log, TEXT("Reusing existing material instance: %s"), *InstanceName); + return ExistingInstance; + } + + UE_LOG(LogURLabEditor, Log, TEXT("Creating material instance: %s"), *InstanceName); + + // 创建包 + UPackage* Package = CreatePackage(*PackageName); + Package->FullyLoad(); + + // 创建材质实例 + UMaterialInstanceConstant* MaterialInstance = NewObject( + Package, + FName(*InstanceName), + RF_Public | RF_Standalone + ); + + MaterialInstance->SetParentEditorOnly(MasterMaterial); + + // 用于设置标量参数并启用覆盖的辅助 lambda 函数 + auto SetScalar = [&](const TCHAR* Name, float Value) + { + FMaterialParameterInfo Info(Name); + MaterialInstance->SetScalarParameterValueEditorOnly(Info, Value); + }; + + // 用于设置向量参数并启用覆盖的辅助 lambda 函数 + auto SetVector = [&](const TCHAR* Name, const FLinearColor& Value) + { + FMaterialParameterInfo Info(Name); + MaterialInstance->SetVectorParameterValueEditorOnly(Info, Value); + }; + + // 用于设置纹理参数的辅助 lambda 函数,并启用覆盖功能。 + auto SetTexture = [&](const TCHAR* Name, UTexture* Tex) + { + FMaterialParameterInfo Info(Name); + MaterialInstance->SetTextureParameterValueEditorOnly(Info, Tex); + + // 同时直接添加到 TextureParameterValues 中,以确保启用覆盖功能。 + FTextureParameterValue TexParam; + TexParam.ParameterInfo = Info; + TexParam.ParameterValue = Tex; + TexParam.ExpressionGUID = FGuid(); // 将由 UE 解决 + MaterialInstance->TextureParameterValues.Add(TexParam); + + UE_LOG(LogURLabEditor, Log, TEXT(" [SetTexture] Set '%s' = '%s' (TextureParameterValues count: %d)"), + Name, *Tex->GetName(), MaterialInstance->TextureParameterValues.Num()); + }; + + // 设置基础颜色(底色) + SetVector(TEXT("BaseColor"), MaterialData.Rgba); + + // 设置纹理参数 + bool bHasBaseColorTexture = false; + if (!MaterialData.BaseColorTextureName.IsEmpty()) + { + if (TextureAssets.Contains(MaterialData.BaseColorTextureName)) + { + UTexture2D* BaseColorTex = TextureAssets[MaterialData.BaseColorTextureName]; + if (BaseColorTex) + { + SetTexture(TEXT("BaseColorTexture"), BaseColorTex); + bHasBaseColorTexture = true; + UE_LOG(LogURLabEditor, Log, TEXT(" Texture '%s' applied to material '%s' (tex=%s)"), + *MaterialData.BaseColorTextureName, *InstanceName, *BaseColorTex->GetName()); + } + } + else + { + UE_LOG(LogURLabEditor, Warning, TEXT(" Texture '%s' referenced by material '%s' but not found in imported textures (%d entries)"), + *MaterialData.BaseColorTextureName, *InstanceName, TextureAssets.Num()); + } + } + + // 将 bUseTexture 设置为静态开关参数——主材质使用 StaticSwitchParameter 在 false 时完全消除纹理采样分支,防止空纹理崩溃 (UE-23902)。 + { + FStaticParameterSet StaticParams; + MaterialInstance->GetStaticParameterValues(StaticParams); + + for (FStaticSwitchParameter& Param : StaticParams.StaticSwitchParameters) + { + if (Param.ParameterInfo.Name == TEXT("bUseTexture")) + { + Param.Value = bHasBaseColorTexture; + Param.bOverride = true; + break; + } + } + MaterialInstance->UpdateStaticPermutation(StaticParams); + } + + // 如果可用,请设置正则纹理 + if (!MaterialData.NormalTextureName.IsEmpty() && TextureAssets.Contains(MaterialData.NormalTextureName)) + { + UTexture2D* NormalTex = TextureAssets[MaterialData.NormalTextureName]; + if (NormalTex) + { + FMaterialParameterInfo NormalParamInfo(TEXT("NormalTexture")); + MaterialInstance->SetTextureParameterValueEditorOnly(NormalParamInfo, NormalTex); + } + } + + // 如果可用,请设置 ORM 纹理 + // ORM 纹理(也称 ORM 贴图)是一种在 3D 渲染和游戏开发中常用的技术。 + // 它将环境光遮蔽(Occlusion)、粗糙度(Roughness)和金属度(Metallic)这三个 PBR 材质参数合并到一张图像的红、绿、蓝(RGB)通道中, + // 以减少文件数量并提高渲染效率 + if (!MaterialData.ORMTextureName.IsEmpty() && TextureAssets.Contains(MaterialData.ORMTextureName)) + { + UTexture2D* ORMTex = TextureAssets[MaterialData.ORMTextureName]; + if (ORMTex) + { + FMaterialParameterInfo ORMParamInfo(TEXT("ORMTexture")); + MaterialInstance->SetTextureParameterValueEditorOnly(ORMParamInfo, ORMTex); + } + } + // 否则,请设置单独的粗糙度/金属纹理。 + else + { + if (!MaterialData.RoughnessTextureName.IsEmpty() && TextureAssets.Contains(MaterialData.RoughnessTextureName)) + { + // 注意:如果主材质没有单独的粗糙度纹理参数,则会忽略此参数。 + // 目前,我们仅将其记录下来。 + UE_LOG(LogURLabEditor, Log, TEXT("Roughness texture found but master material uses ORM workflow")); + } + + if (!MaterialData.MetallicTextureName.IsEmpty() && TextureAssets.Contains(MaterialData.MetallicTextureName)) + { + // 注意:如果主材质没有单独的金属纹理参数,则此参数将被忽略。 + UE_LOG(LogURLabEditor, Log, TEXT("Metallic texture found but master material uses ORM workflow")); + } + } + + // 强制更新并保存 + MaterialInstance->UpdateStaticPermutation(); + MaterialInstance->PostEditChange(); + + // 保存包 + Package->MarkPackageDirty(); + FAssetRegistryModule::AssetCreated(MaterialInstance); + + FString PackageFileName = FPackageName::LongPackageNameToFilename(PackageName, FPackageName::GetAssetPackageExtension()); + FSavePackageArgs SaveArgs; + SaveArgs.TopLevelFlags = RF_Public | RF_Standalone; + SaveArgs.SaveFlags = SAVE_NoError; + // UPackage::SavePackage(Package, MaterialInstance, *PackageFileName, SaveArgs); + + UE_LOG(LogURLabEditor, Log, TEXT("Successfully created material instance: %s"), *InstanceName); + return MaterialInstance; +} diff --git a/Plugins/UnrealRoboticsLab/Source/URLabEditor/Private/Tests/MjCompilationTests.cpp b/Plugins/UnrealRoboticsLab/Source/URLabEditor/Private/Tests/MjCompilationTests.cpp new file mode 100644 index 0000000000..2875f1a4be --- /dev/null +++ b/Plugins/UnrealRoboticsLab/Source/URLabEditor/Private/Tests/MjCompilationTests.cpp @@ -0,0 +1,967 @@ +// Copyright (c) 2026 Jonathan Embley-Riches. All rights reserved. +// +// Licensed under the Apache License, Version 2.0 (the "License"); +// you may not use this file except in compliance with the License. +// You may obtain a copy of the License at +// +// http://www.apache.org/licenses/LICENSE-2.0 +// +// Unless required by applicable law or agreed to in writing, software +// distributed under the License is distributed on an "AS IS" BASIS, +// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +// See the License for the specific language governing permissions and +// limitations under the License. +// +// --- LEGAL DISCLAIMER --- +// UnrealRoboticsLab is an independent software plugin. It is NOT affiliated with, +// endorsed by, or sponsored by Epic Games, Inc. "Unreal" and "Unreal Engine" are +// trademarks or registered trademarks of Epic Games, Inc. in the US and elsewhere. +// +// This plugin incorporates third-party software: MuJoCo (Apache 2.0), +// CoACD (MIT), and libzmq (MPL 2.0). See ThirdPartyNotices.txt for details. + +#include "CoreMinimal.h" +#include "Misc/AutomationTest.h" +#include "Tests/MjTestHelpers.h" +#include "MuJoCo/Core/AMjManager.h" +#include "MuJoCo/Core/MjArticulation.h" +#include "MuJoCo/Components/Bodies/MjWorldBody.h" +#include "MuJoCo/Components/Bodies/MjBody.h" +#include "MuJoCo/Components/Geometry/MjGeom.h" +#include "MuJoCo/Components/Joints/MjJoint.h" +#include "MuJoCo/Components/Geometry/MjSite.h" +#include "MuJoCo/Components/Sensors/MjJointPosSensor.h" +#include "MuJoCo/Components/Actuators/MjMotorActuator.h" +#include "MuJoCo/Components/Tendons/MjTendon.h" +#include "MuJoCo/Components/Constraints/MjEquality.h" +#include "MuJoCo/Components/Keyframes/MjKeyframe.h" +#include "MuJoCo/Components/Defaults/MjDefault.h" +#include "MuJoCo/Components/Sensors/MjCamera.h" +#include "Engine/World.h" +#include "mujoco/mujoco.h" + +// ============================================================================ +// URLab.Compile.MinimalBody +// Default FMjUESession (no callback). +// Asserts that Manager is initialized and that the geom and joint view IDs +// are bound (>= 0) after compilation. +// ============================================================================ +IMPLEMENT_SIMPLE_AUTOMATION_TEST(FMjCompileMinimalBody, + "URLab.Compile.MinimalBody", + EAutomationTestFlags::EditorContext | EAutomationTestFlags::ProductFilter) + +bool FMjCompileMinimalBody::RunTest(const FString& Parameters) +{ + FMjUESession S; + if (!S.Init()) + { + AddError(FString::Printf(TEXT("Init failed: %s"), *S.LastError)); + S.Cleanup(); + return false; + } + + TestTrue(TEXT("Manager should be initialized"), S.Manager->IsInitialized()); + TestTrue(TEXT("Geom view id should be >= 0"), S.Geom->m_GeomView.id >= 0); + TestTrue(TEXT("Joint view id should be >= 0"), S.Joint->m_JointView.id >= 0); + + S.Cleanup(); + return true; +} + +// ============================================================================ +// URLab.Compile.JointTypeHinge +// ConfigCallback sets the joint type to Hinge. +// Asserts that the compiled model records mjJNT_HINGE for the joint. +// ============================================================================ +IMPLEMENT_SIMPLE_AUTOMATION_TEST(FMjCompileJointTypeHinge, + "URLab.Compile.JointTypeHinge", + EAutomationTestFlags::EditorContext | EAutomationTestFlags::ProductFilter) + +bool FMjCompileJointTypeHinge::RunTest(const FString& Parameters) +{ + FMjUESession S; + if (!S.Init([](FMjUESession& Sess) + { + Sess.Joint->Type = EMjJointType::Hinge; + Sess.Joint->bOverride_Type = true; + })) + { + AddError(FString::Printf(TEXT("Init failed: %s"), *S.LastError)); + S.Cleanup(); + return false; + } + + int JntId = S.Joint->m_JointView.id; + if (TestTrue(TEXT("joint view bound"), JntId >= 0) && S.Manager->PhysicsEngine->m_model) + { + TestEqual(TEXT("jnt_type == HINGE"), + (int)S.Manager->PhysicsEngine->m_model->jnt_type[JntId], + (int)mjJNT_HINGE); + } + + S.Cleanup(); + return true; +} + +// ============================================================================ +// URLab.Compile.JointTypeSlide +// ConfigCallback sets the joint type to Slide. +// Asserts that the compiled model records mjJNT_SLIDE for the joint. +// ============================================================================ +IMPLEMENT_SIMPLE_AUTOMATION_TEST(FMjCompileJointTypeSlide, + "URLab.Compile.JointTypeSlide", + EAutomationTestFlags::EditorContext | EAutomationTestFlags::ProductFilter) + +bool FMjCompileJointTypeSlide::RunTest(const FString& Parameters) +{ + FMjUESession S; + if (!S.Init([](FMjUESession& Sess) + { + Sess.Joint->Type = EMjJointType::Slide; + Sess.Joint->bOverride_Type = true; + })) + { + AddError(FString::Printf(TEXT("Init failed: %s"), *S.LastError)); + S.Cleanup(); + return false; + } + + int JntId = S.Joint->m_JointView.id; + if (TestTrue(TEXT("joint view bound"), JntId >= 0) && S.Manager->PhysicsEngine->m_model) + { + TestEqual(TEXT("jnt_type == SLIDE"), + (int)S.Manager->PhysicsEngine->m_model->jnt_type[JntId], + (int)mjJNT_SLIDE); + } + + S.Cleanup(); + return true; +} + +// ============================================================================ +// URLab.Compile.JointTypeBall +// ConfigCallback sets the joint type to Ball. +// Asserts that the compiled model records mjJNT_BALL for the joint. +// ============================================================================ +IMPLEMENT_SIMPLE_AUTOMATION_TEST(FMjCompileJointTypeBall, + "URLab.Compile.JointTypeBall", + EAutomationTestFlags::EditorContext | EAutomationTestFlags::ProductFilter) + +bool FMjCompileJointTypeBall::RunTest(const FString& Parameters) +{ + FMjUESession S; + if (!S.Init([](FMjUESession& Sess) + { + Sess.Joint->Type = EMjJointType::Ball; + Sess.Joint->bOverride_Type = true; + })) + { + AddError(FString::Printf(TEXT("Init failed: %s"), *S.LastError)); + S.Cleanup(); + return false; + } + + int JntId = S.Joint->m_JointView.id; + if (TestTrue(TEXT("joint view bound"), JntId >= 0) && S.Manager->PhysicsEngine->m_model) + { + TestEqual(TEXT("jnt_type == BALL"), + (int)S.Manager->PhysicsEngine->m_model->jnt_type[JntId], + (int)mjJNT_BALL); + } + + S.Cleanup(); + return true; +} + +// ============================================================================ +// URLab.Compile.JointTypeFree +// ConfigCallback sets the joint type to Free. +// Asserts that the compiled model records mjJNT_FREE for the joint. +// ============================================================================ +IMPLEMENT_SIMPLE_AUTOMATION_TEST(FMjCompileJointTypeFree, + "URLab.Compile.JointTypeFree", + EAutomationTestFlags::EditorContext | EAutomationTestFlags::ProductFilter) + +bool FMjCompileJointTypeFree::RunTest(const FString& Parameters) +{ + FMjUESession S; + if (!S.Init([](FMjUESession& Sess) + { + Sess.Joint->Type = EMjJointType::Free; + Sess.Joint->bOverride_Type = true; + })) + { + AddError(FString::Printf(TEXT("Init failed: %s"), *S.LastError)); + S.Cleanup(); + return false; + } + + int JntId = S.Joint->m_JointView.id; + if (TestTrue(TEXT("joint view bound"), JntId >= 0) && S.Manager->PhysicsEngine->m_model) + { + TestEqual(TEXT("jnt_type == FREE"), + (int)S.Manager->PhysicsEngine->m_model->jnt_type[JntId], + (int)mjJNT_FREE); + } + + S.Cleanup(); + return true; +} + +// ============================================================================ +// URLab.Compile.JointAxisExported +// ConfigCallback sets joint type to Hinge with Axis = (1, 0, 0). +// Asserts that the compiled model records the correct axis values. +// +// NOTE: The plugin applies a Y-flip when converting UE coords to MuJoCo +// coords (UE is left-handed, MuJoCo is right-handed). Specifically, +// the exported axis Y component is -(Axis.Y). For Axis = (1, 0, 0) this +// has no effect, so jnt_axis is expected to be (1, 0, 0). +// ============================================================================ +IMPLEMENT_SIMPLE_AUTOMATION_TEST(FMjCompileJointAxisExported, + "URLab.Compile.JointAxisExported", + EAutomationTestFlags::EditorContext | EAutomationTestFlags::ProductFilter) + +bool FMjCompileJointAxisExported::RunTest(const FString& Parameters) +{ + FMjUESession S; + if (!S.Init([](FMjUESession& Sess) + { + Sess.Joint->Type = EMjJointType::Hinge; + Sess.Joint->bOverride_Type = true; + Sess.Joint->Axis = FVector(1.0f, 0.0f, 0.0f); + Sess.Joint->bOverride_Axis = true; + })) + { + AddError(FString::Printf(TEXT("Init failed: %s"), *S.LastError)); + S.Cleanup(); + return false; + } + + int JntId = S.Joint->m_JointView.id; + if (TestTrue(TEXT("joint view bound"), JntId >= 0) && S.Manager->PhysicsEngine->m_model) + { + const mjtNum* Axis = &S.Manager->PhysicsEngine->m_model->jnt_axis[JntId * 3]; + TestTrue(TEXT("jnt_axis[0] ~= 1.0"), FMath::Abs((float)Axis[0] - 1.0f) < 1e-4f); + // Y-flip: exported Y == -(Axis.Y). Axis.Y == 0 so both 0.0 and -0.0 are acceptable. + TestTrue(TEXT("jnt_axis[1] ~= 0.0"), FMath::Abs((float)Axis[1]) < 1e-4f); + TestTrue(TEXT("jnt_axis[2] ~= 0.0"), FMath::Abs((float)Axis[2]) < 1e-4f); + } + + S.Cleanup(); + return true; +} + +// ============================================================================ +// URLab.Compile.GeomSizeExported +// ConfigCallback sets geom Size to (0.3, 0.3, 0.3) in MuJoCo metres. +// Asserts that geom_size[id*3+0] in the compiled model is approximately 0.3. +// +// NOTE: UMjGeom.Size is already in MuJoCo metres — no scale conversion is +// applied by the plugin between the component property and the spec value. +// ============================================================================ +IMPLEMENT_SIMPLE_AUTOMATION_TEST(FMjCompileGeomSizeExported, + "URLab.Compile.GeomSizeExported", + EAutomationTestFlags::EditorContext | EAutomationTestFlags::ProductFilter) + +bool FMjCompileGeomSizeExported::RunTest(const FString& Parameters) +{ + FMjUESession S; + if (!S.Init([](FMjUESession& Sess) + { + Sess.Geom->Size = FVector(0.3f, 0.3f, 0.3f); + Sess.Geom->bOverride_Size = true; + })) + { + AddError(FString::Printf(TEXT("Init failed: %s"), *S.LastError)); + S.Cleanup(); + return false; + } + + int GeomId = S.Geom->m_GeomView.id; + if (TestTrue(TEXT("geom view bound"), GeomId >= 0) && S.Manager->PhysicsEngine->m_model) + { + TestTrue(TEXT("geom_size[0] ~= 0.3"), + FMath::Abs((float)S.Manager->PhysicsEngine->m_model->geom_size[GeomId * 3 + 0] - 0.3f) < 1e-4f); + } + + S.Cleanup(); + return true; +} + +// ============================================================================ +// URLab.Compile.GeomFrictionExported +// ConfigCallback sets geom Friction to {0.7, 0.01, 0.001}. +// Asserts that geom_friction[id*3+0] in the compiled model is approximately 0.7. +// ============================================================================ +IMPLEMENT_SIMPLE_AUTOMATION_TEST(FMjCompileGeomFrictionExported, + "URLab.Compile.GeomFrictionExported", + EAutomationTestFlags::EditorContext | EAutomationTestFlags::ProductFilter) + +bool FMjCompileGeomFrictionExported::RunTest(const FString& Parameters) +{ + FMjUESession S; + if (!S.Init([](FMjUESession& Sess) + { + Sess.Geom->Friction = { 0.7f, 0.01f, 0.001f }; + Sess.Geom->bOverride_Friction = true; + })) + { + AddError(FString::Printf(TEXT("Init failed: %s"), *S.LastError)); + S.Cleanup(); + return false; + } + + int GeomId = S.Geom->m_GeomView.id; + if (TestTrue(TEXT("geom view bound"), GeomId >= 0) && S.Manager->PhysicsEngine->m_model) + { + TestTrue(TEXT("geom_friction[0] ~= 0.7"), + FMath::Abs((float)S.Manager->PhysicsEngine->m_model->geom_friction[GeomId * 3 + 0] - 0.7f) < 1e-4f); + } + + S.Cleanup(); + return true; +} + +// ============================================================================ +// URLab.Compile.JointDampingExported +// ConfigCallback sets joint type to Hinge with Damping = 2.5. +// Asserts that dof_damping at the joint's dof address is approximately 2.5. +// ============================================================================ +IMPLEMENT_SIMPLE_AUTOMATION_TEST(FMjCompileJointDampingExported, + "URLab.Compile.JointDampingExported", + EAutomationTestFlags::EditorContext | EAutomationTestFlags::ProductFilter) + +bool FMjCompileJointDampingExported::RunTest(const FString& Parameters) +{ + FMjUESession S; + if (!S.Init([](FMjUESession& Sess) + { + Sess.Joint->Type = EMjJointType::Hinge; + Sess.Joint->bOverride_Type = true; + Sess.Joint->Damping = {2.5f}; + Sess.Joint->bOverride_Damping = true; + })) + { + AddError(FString::Printf(TEXT("Init failed: %s"), *S.LastError)); + S.Cleanup(); + return false; + } + + int JntId = S.Joint->m_JointView.id; + if (TestTrue(TEXT("joint view bound"), JntId >= 0) && S.Manager->PhysicsEngine->m_model) + { + int DofAdr = S.Manager->PhysicsEngine->m_model->jnt_dofadr[JntId]; + TestTrue(TEXT("dof_damping ~= 2.5"), + FMath::Abs((float)S.Manager->PhysicsEngine->m_model->dof_damping[DofAdr] - 2.5f) < 1e-4f); + } + + S.Cleanup(); + return true; +} + +// ============================================================================ +// URLab.Compile.JointLimitsExported +// ConfigCallback sets a Hinge joint with range [-1.5, 1.5] and bLimited. +// Asserts that jnt_limited is set and jnt_range matches the specified values. +// ============================================================================ +IMPLEMENT_SIMPLE_AUTOMATION_TEST(FMjCompileJointLimitsExported, + "URLab.Compile.JointLimitsExported", + EAutomationTestFlags::EditorContext | EAutomationTestFlags::ProductFilter) + +bool FMjCompileJointLimitsExported::RunTest(const FString& Parameters) +{ + FMjUESession S; + if (!S.Init([](FMjUESession& Sess) + { + Sess.Joint->Type = EMjJointType::Hinge; + Sess.Joint->bOverride_Type = true; + Sess.Joint->Range = { -1.5f, 1.5f }; + Sess.Joint->bOverride_Range = true; + Sess.Joint->bLimited = true; + Sess.Joint->bOverride_Limited = true; + })) + { + AddError(FString::Printf(TEXT("Init failed: %s"), *S.LastError)); + S.Cleanup(); + return false; + } + + int JntId = S.Joint->m_JointView.id; + if (TestTrue(TEXT("joint view bound"), JntId >= 0) && S.Manager->PhysicsEngine->m_model) + { + TestTrue(TEXT("jnt_limited should be set"), + S.Manager->PhysicsEngine->m_model->jnt_limited[JntId] != 0); + TestTrue(TEXT("jnt_range[0] ~= -1.5"), + FMath::Abs((float)S.Manager->PhysicsEngine->m_model->jnt_range[JntId * 2 + 0] - (-1.5f)) < 1e-4f); + TestTrue(TEXT("jnt_range[1] ~= 1.5"), + FMath::Abs((float)S.Manager->PhysicsEngine->m_model->jnt_range[JntId * 2 + 1] - 1.5f) < 1e-4f); + } + + S.Cleanup(); + return true; +} + +// ============================================================================ +// URLab.Compile.ManagerModelValid +// Default FMjUESession. +// Asserts that m_model and m_data are non-null after compilation. +// ============================================================================ +IMPLEMENT_SIMPLE_AUTOMATION_TEST(FMjCompileManagerModelValid, + "URLab.Compile.ManagerModelValid", + EAutomationTestFlags::EditorContext | EAutomationTestFlags::ProductFilter) + +bool FMjCompileManagerModelValid::RunTest(const FString& Parameters) +{ + FMjUESession S; + if (!S.Init()) + { + AddError(FString::Printf(TEXT("Init failed: %s"), *S.LastError)); + S.Cleanup(); + return false; + } + + TestNotNull(TEXT("Manager->PhysicsEngine->m_model should not be null"), S.Manager->PhysicsEngine->m_model); + TestNotNull(TEXT("Manager->PhysicsEngine->m_data should not be null"), S.Manager->PhysicsEngine->m_data); + + S.Cleanup(); + return true; +} + +// ============================================================================ +// URLab.Compile.ManagerIsRunning +// Default FMjUESession. +// Asserts that Manager reports both IsInitialized() and IsRunning(). +// ============================================================================ +IMPLEMENT_SIMPLE_AUTOMATION_TEST(FMjCompileManagerIsRunning, + "URLab.Compile.ManagerIsRunning", + EAutomationTestFlags::EditorContext | EAutomationTestFlags::ProductFilter) + +bool FMjCompileManagerIsRunning::RunTest(const FString& Parameters) +{ + FMjUESession S; + if (!S.Init()) + { + AddError(FString::Printf(TEXT("Init failed: %s"), *S.LastError)); + S.Cleanup(); + return false; + } + + TestTrue(TEXT("Manager should be initialized"), S.Manager->IsInitialized()); + // Sim starts paused by design — IsRunning() requires unpaused. + // IsInitialized() is the correct check for post-compile readiness. + TestTrue(TEXT("Manager should be initialized (physics)"), S.Manager->PhysicsEngine->IsInitialized()); + + S.Cleanup(); + return true; +} + +// ============================================================================ +// URLab.Compile.BodyCountMatchesComponents +// Default FMjUESession (1 WorldBody + 1 user Body). +// Asserts that nbody >= 2 (MuJoCo always includes a world body at index 0, +// plus at least the one RootBody created by FMjUESession::Init). +// ============================================================================ +IMPLEMENT_SIMPLE_AUTOMATION_TEST(FMjCompileBodyCountMatchesComponents, + "URLab.Compile.BodyCountMatchesComponents", + EAutomationTestFlags::EditorContext | EAutomationTestFlags::ProductFilter) + +bool FMjCompileBodyCountMatchesComponents::RunTest(const FString& Parameters) +{ + FMjUESession S; + if (!S.Init()) + { + AddError(FString::Printf(TEXT("Init failed: %s"), *S.LastError)); + S.Cleanup(); + return false; + } + + TestTrue(TEXT("nbody should be >= 2 (world body + RootBody)"), + S.Manager->PhysicsEngine->m_model != nullptr && S.Manager->PhysicsEngine->m_model->nbody >= 2); + + S.Cleanup(); + return true; +} + +// ============================================================================ +// URLab.Compile.GeomViewIdBound +// Default FMjUESession. +// Asserts that the geom component's view id is bound (>= 0) after compilation. +// ============================================================================ +IMPLEMENT_SIMPLE_AUTOMATION_TEST(FMjCompileGeomViewIdBound, + "URLab.Compile.GeomViewIdBound", + EAutomationTestFlags::EditorContext | EAutomationTestFlags::ProductFilter) + +bool FMjCompileGeomViewIdBound::RunTest(const FString& Parameters) +{ + FMjUESession S; + if (!S.Init()) + { + AddError(FString::Printf(TEXT("Init failed: %s"), *S.LastError)); + S.Cleanup(); + return false; + } + + TestTrue(TEXT("Geom->m_GeomView.id should be >= 0"), S.Geom->m_GeomView.id >= 0); + + S.Cleanup(); + return true; +} + +// ============================================================================ +// URLab.Compile.JointViewIdBound +// Default FMjUESession. +// Asserts that the joint component's view id is bound (>= 0) after compilation. +// ============================================================================ +IMPLEMENT_SIMPLE_AUTOMATION_TEST(FMjCompileJointViewIdBound, + "URLab.Compile.JointViewIdBound", + EAutomationTestFlags::EditorContext | EAutomationTestFlags::ProductFilter) + +bool FMjCompileJointViewIdBound::RunTest(const FString& Parameters) +{ + FMjUESession S; + if (!S.Init()) + { + AddError(FString::Printf(TEXT("Init failed: %s"), *S.LastError)); + S.Cleanup(); + return false; + } + + TestTrue(TEXT("Joint->m_JointView.id should be >= 0"), S.Joint->m_JointView.id >= 0); + + S.Cleanup(); + return true; +} + +// ============================================================================ +// URLab.Compile.SensorBinds +// ConfigCallback adds a UMjJointPosSensor targeting "TestJoint" to the Robot, +// with the joint type set to Hinge. +// Asserts that the sensor's view id is bound and that nsensor == 1. +// ============================================================================ +IMPLEMENT_SIMPLE_AUTOMATION_TEST(FMjCompileSensorBinds, + "URLab.Compile.SensorBinds", + EAutomationTestFlags::EditorContext | EAutomationTestFlags::ProductFilter) + +bool FMjCompileSensorBinds::RunTest(const FString& Parameters) +{ + UMjJointPosSensor* Sensor = nullptr; + + FMjUESession S; + if (!S.Init([&Sensor](FMjUESession& Sess) + { + Sess.Joint->Type = EMjJointType::Hinge; + Sess.Joint->bOverride_Type = true; + + Sensor = NewObject(Sess.Robot, TEXT("TestSensor")); + Sensor->TargetName = TEXT("TestJoint"); + Sensor->RegisterComponent(); + })) + { + AddError(FString::Printf(TEXT("Init failed: %s"), *S.LastError)); + S.Cleanup(); + return false; + } + + if (TestNotNull(TEXT("Sensor pointer should be non-null"), Sensor)) + { + TestTrue(TEXT("Sensor->m_SensorView.id should be >= 0"), Sensor->m_SensorView.id >= 0); + } + + if (TestNotNull(TEXT("m_model should be non-null"), S.Manager->PhysicsEngine->m_model)) + { + TestEqual(TEXT("nsensor should be 1"), (int)S.Manager->PhysicsEngine->m_model->nsensor, 1); + } + + S.Cleanup(); + return true; +} + +// ============================================================================ +// URLab.Compile.ActuatorBinds +// ConfigCallback adds a UMjMotorActuator targeting "TestJoint" to the Robot, +// with the joint type set to Hinge. +// Asserts that the actuator's view id is bound and that nu == 1. +// ============================================================================ +IMPLEMENT_SIMPLE_AUTOMATION_TEST(FMjCompileActuatorBinds, + "URLab.Compile.ActuatorBinds", + EAutomationTestFlags::EditorContext | EAutomationTestFlags::ProductFilter) + +bool FMjCompileActuatorBinds::RunTest(const FString& Parameters) +{ + UMjMotorActuator* Act = nullptr; + + FMjUESession S; + if (!S.Init([&Act](FMjUESession& Sess) + { + Sess.Joint->Type = EMjJointType::Hinge; + Sess.Joint->bOverride_Type = true; + + Act = NewObject(Sess.Robot, TEXT("TestActuator")); + Act->TargetName = TEXT("TestJoint"); + Act->RegisterComponent(); + })) + { + AddError(FString::Printf(TEXT("Init failed: %s"), *S.LastError)); + S.Cleanup(); + return false; + } + + if (TestNotNull(TEXT("Actuator pointer should be non-null"), Act)) + { + TestTrue(TEXT("Act->m_ActuatorView.id should be >= 0"), Act->m_ActuatorView.id >= 0); + } + + if (TestNotNull(TEXT("m_model should be non-null"), S.Manager->PhysicsEngine->m_model)) + { + TestEqual(TEXT("nu should be 1"), (int)S.Manager->PhysicsEngine->m_model->nu, 1); + } + + S.Cleanup(); + return true; +} + +// ============================================================================ +// URLab.Compile.PIERestart +// Runs two complete FMjUESession init/cleanup cycles back-to-back. +// Both must succeed and Manager->IsInitialized() must be true in both, +// verifying that the plugin can be re-initialised without leaving stale +// state (simulates a PIE stop + restart). +// ============================================================================ +IMPLEMENT_SIMPLE_AUTOMATION_TEST(FMjCompilePIERestart, + "URLab.Compile.PIERestart", + EAutomationTestFlags::EditorContext | EAutomationTestFlags::ProductFilter) + +bool FMjCompilePIERestart::RunTest(const FString& Parameters) +{ + // --- First session --- + { + FMjUESession S; + if (!S.Init()) + { + AddError(FString::Printf(TEXT("First Init failed: %s"), *S.LastError)); + S.Cleanup(); + return false; + } + TestTrue(TEXT("First session: Manager should be initialized"), S.Manager->IsInitialized()); + S.Cleanup(); + } + + // --- Second session --- + { + FMjUESession S; + if (!S.Init()) + { + AddError(FString::Printf(TEXT("Second Init failed: %s"), *S.LastError)); + S.Cleanup(); + return false; + } + TestTrue(TEXT("Second session: Manager should be initialized"), S.Manager->IsInitialized()); + S.Cleanup(); + } + + return true; +} + +// ============================================================================ +// URLab.Compile.SiteBinds +// ConfigCallback adds a UMjSite to the body. +// Asserts that the site's SiteView id is bound (>= 0), m_ID is set, +// IsBound() returns true, and nsite >= 1. +// ============================================================================ +IMPLEMENT_SIMPLE_AUTOMATION_TEST(FMjCompileSiteBinds, + "URLab.Compile.SiteBinds", + EAutomationTestFlags::EditorContext | EAutomationTestFlags::ProductFilter) + +bool FMjCompileSiteBinds::RunTest(const FString& Parameters) +{ + UMjSite* Site = nullptr; + + FMjUESession S; + if (!S.Init([&Site](FMjUESession& Sess) + { + Site = NewObject(Sess.Robot, TEXT("TestSite")); + Site->RegisterComponent(); + Site->AttachToComponent(Sess.Body, FAttachmentTransformRules::KeepRelativeTransform); + })) + { + AddError(FString::Printf(TEXT("Init failed: %s"), *S.LastError)); + S.Cleanup(); + return false; + } + + if (TestNotNull(TEXT("Site pointer should be non-null"), Site)) + { + TestTrue(TEXT("Site->m_SiteView.id should be >= 0"), Site->m_SiteView.id >= 0); + TestTrue(TEXT("Site->GetMjID() should be >= 0"), Site->GetMjID() >= 0); + TestTrue(TEXT("Site->IsBound() should be true"), Site->IsBound()); + } + + if (TestNotNull(TEXT("m_model should be non-null"), S.Manager->PhysicsEngine->m_model)) + { + TestTrue(TEXT("nsite should be >= 1"), S.Manager->PhysicsEngine->m_model->nsite >= 1); + } + + S.Cleanup(); + return true; +} + +// ============================================================================ +// URLab.Compile.TendonExportTo +// Creates a fixed tendon with two joint wraps, stiffness and damping. +// Verifies that the tendon compiles, binds, and properties are in the model. +// ============================================================================ +IMPLEMENT_SIMPLE_AUTOMATION_TEST(FMjCompileTendonExportTo, + "URLab.Compile.TendonExportTo", + EAutomationTestFlags::EditorContext | EAutomationTestFlags::ProductFilter) + +bool FMjCompileTendonExportTo::RunTest(const FString& Parameters) +{ + UMjTendon* Tendon = nullptr; + UMjJoint* Joint2 = nullptr; + + FMjUESession S; + if (!S.Init([&Tendon, &Joint2](FMjUESession& Sess) + { + Sess.Joint->Type = EMjJointType::Hinge; + Sess.Joint->bOverride_Type = true; + + // Add a second body+joint for the tendon to connect + UMjBody* Body2 = NewObject(Sess.Robot, TEXT("Body2")); + Body2->RegisterComponent(); + Body2->AttachToComponent(Sess.Body, FAttachmentTransformRules::KeepRelativeTransform); + + UMjGeom* Geom2 = NewObject(Sess.Robot, TEXT("Geom2")); + Geom2->Size = FVector(0.1f, 0.1f, 0.1f); + Geom2->bOverride_Size = true; + Geom2->RegisterComponent(); + Geom2->AttachToComponent(Body2, FAttachmentTransformRules::KeepRelativeTransform); + + Joint2 = NewObject(Sess.Robot, TEXT("Joint2")); + Joint2->Type = EMjJointType::Hinge; + Joint2->bOverride_Type = true; + Joint2->RegisterComponent(); + Joint2->AttachToComponent(Body2, FAttachmentTransformRules::KeepRelativeTransform); + + // Create tendon with two joint wraps + Tendon = NewObject(Sess.Robot, TEXT("TestTendon")); + Tendon->bOverride_Stiffness = true; + Tendon->Stiffness = {5.0f}; + Tendon->bOverride_Damping = true; + Tendon->Damping = {0.1f}; + + FMjTendonWrap W1; + W1.Type = EMjTendonWrapType::Joint; + W1.TargetName = TEXT("TestJoint"); + W1.Coef = 1.0f; + + FMjTendonWrap W2; + W2.Type = EMjTendonWrapType::Joint; + W2.TargetName = TEXT("Joint2"); + W2.Coef = -1.0f; + + Tendon->Wraps.Add(W1); + Tendon->Wraps.Add(W2); + Tendon->RegisterComponent(); + })) + { + AddError(FString::Printf(TEXT("Init failed: %s"), *S.LastError)); + S.Cleanup(); + return false; + } + + if (TestNotNull(TEXT("Tendon pointer"), Tendon) && + TestNotNull(TEXT("m_model"), S.Manager->PhysicsEngine->m_model)) + { + TestTrue(TEXT("ntendon >= 1"), S.Manager->PhysicsEngine->m_model->ntendon >= 1); + TestTrue(TEXT("Tendon bound"), Tendon->m_TendonView.id >= 0); + + int tid = Tendon->m_TendonView.id; + if (tid >= 0) + { + TestTrue(TEXT("stiffness ~= 5.0"), + FMath::Abs((float)S.Manager->PhysicsEngine->m_model->tendon_stiffness[tid] - 5.0f) < 1e-4f); + TestTrue(TEXT("damping ~= 0.1"), + FMath::Abs((float)S.Manager->PhysicsEngine->m_model->tendon_damping[tid] - 0.1f) < 1e-4f); + } + } + + S.Cleanup(); + return true; +} + +// ============================================================================ +// URLab.Compile.DefaultTendonSiteCamera +// Verifies that UMjDefault::ExportTo delegates to tendon, site, and camera +// child components, writing their properties to the mjsDefault struct. +// ============================================================================ +IMPLEMENT_SIMPLE_AUTOMATION_TEST(FMjCompileDefaultTendonSiteCamera, + "URLab.Compile.DefaultTendonSiteCamera", + EAutomationTestFlags::EditorContext | EAutomationTestFlags::ProductFilter) + +bool FMjCompileDefaultTendonSiteCamera::RunTest(const FString& Parameters) +{ + // Create a temporary spec to get a default struct + mjSpec* Spec = mj_makeSpec(); + mjsDefault* Def = mjs_addDefault(Spec, "test_defaults", nullptr); + if (!TestNotNull(TEXT("Default created"), Def)) + { + mj_deleteSpec(Spec); + return false; + } + + // Verify sub-struct pointers are valid + if (!TestNotNull(TEXT("Def->tendon"), Def->tendon) || + !TestNotNull(TEXT("Def->site"), Def->site) || + !TestNotNull(TEXT("Def->camera"), Def->camera)) + { + mj_deleteSpec(Spec); + return false; + } + + // --- Test UMjTendon::ExportTo on def->tendon --- + UMjTendon* TendonChild = NewObject(); + TendonChild->Stiffness = {7.5f}; + TendonChild->Damping = {0.3f}; + TendonChild->bOverride_Stiffness = true; + TendonChild->bOverride_Damping = true; + TendonChild->ExportTo(Def->tendon, nullptr); + + TestTrue(TEXT("tendon stiffness ~= 7.5"), + FMath::Abs(Def->tendon->stiffness - 7.5) < 1e-4); + TestTrue(TEXT("tendon damping ~= 0.3"), + FMath::Abs(Def->tendon->damping - 0.3) < 1e-4); + + // --- Test UMjSite::ExportTo on def->site --- + UMjSite* SiteChild = NewObject(); + SiteChild->Type = EMjSiteType::Box; + SiteChild->Group = 3; + SiteChild->ExportTo(Def->site, nullptr); + + TestEqual(TEXT("site type == box"), (int)Def->site->type, (int)mjGEOM_BOX); + TestEqual(TEXT("site group == 3"), Def->site->group, 3); + + // --- Test UMjCamera::ExportTo on def->camera --- + UMjCamera* CameraChild = NewObject(); + CameraChild->Fovy = 90.0f; + CameraChild->Resolution = FIntPoint(1280, 720); + CameraChild->ExportTo(Def->camera, nullptr); + + TestTrue(TEXT("camera fovy ~= 90.0"), + FMath::Abs(Def->camera->fovy - 90.0) < 1e-4); + TestTrue(TEXT("camera resolution[0] ~= 1280"), + FMath::Abs(Def->camera->resolution[0] - 1280.0f) < 1e-1f); + TestTrue(TEXT("camera resolution[1] ~= 720"), + FMath::Abs(Def->camera->resolution[1] - 720.0f) < 1e-1f); + + mj_deleteSpec(Spec); + return true; +} + +// ============================================================================ +// URLab.Compile.EqualityExportTo +// Creates a weld equality constraint between two bodies. +// Verifies that the equality compiles and neq >= 1. +// ============================================================================ +IMPLEMENT_SIMPLE_AUTOMATION_TEST(FMjCompileEqualityExportTo, + "URLab.Compile.EqualityExportTo", + EAutomationTestFlags::EditorContext | EAutomationTestFlags::ProductFilter) + +bool FMjCompileEqualityExportTo::RunTest(const FString& Parameters) +{ + // Use pure MuJoCo session to verify ExportTo produces correct equality + // (avoids body-name resolution issues in the UE component pipeline) + FMjTestSession S; + if (!S.CompileXml(TEXT( + "" + " " + " " + " " + " " + " " + " " + " " + " " + " " + " " + " " + " " + " " + ""))) + { + AddError(FString::Printf(TEXT("Compile failed: %s"), *S.LastError)); + return false; + } + + TestTrue(TEXT("neq >= 1"), S.m->neq >= 1); + + // Now verify that UMjEquality::ExportTo produces the same result + // by creating a component, setting properties, and checking ExportTo writes correctly + UMjEquality* Eq = NewObject(); + Eq->EqualityType = EMjEqualityType::Weld; + Eq->Obj1 = TEXT("b1"); + Eq->Obj2 = TEXT("b2"); + Eq->bActive = true; + Eq->bOverride_TorqueScale = true; + Eq->TorqueScale = 2.0f; + + // Create a spec and equality to export to + mjSpec* TestSpec = mj_makeSpec(); + mjsEquality* SpecEq = mjs_addEquality(TestSpec, nullptr); + Eq->ExportTo(SpecEq); + + TestEqual(TEXT("type == weld"), (int)SpecEq->type, (int)mjtEq::mjEQ_WELD); + TestTrue(TEXT("active == 1"), SpecEq->active == 1); + TestTrue(TEXT("data[7] ~= 2.0 (torquescale)"), + FMath::Abs(SpecEq->data[7] - 2.0) < 1e-4); + + mj_deleteSpec(TestSpec); + + return true; +} + +// ============================================================================ +// URLab.Compile.KeyframeExportTo +// Creates a keyframe with qpos and time values. +// Verifies that the keyframe compiles and nkey >= 1, time is correct. +// ============================================================================ +IMPLEMENT_SIMPLE_AUTOMATION_TEST(FMjCompileKeyframeExportTo, + "URLab.Compile.KeyframeExportTo", + EAutomationTestFlags::EditorContext | EAutomationTestFlags::ProductFilter) + +bool FMjCompileKeyframeExportTo::RunTest(const FString& Parameters) +{ + UMjKeyframe* KF = nullptr; + + FMjUESession S; + if (!S.Init([&KF](FMjUESession& Sess) + { + Sess.Joint->Type = EMjJointType::Hinge; + Sess.Joint->bOverride_Type = true; + + KF = NewObject(Sess.Robot, TEXT("TestKeyframe")); + KF->Time = 1.5f; + KF->bOverride_Qpos = true; + KF->Qpos = { 0.5f }; + KF->RegisterComponent(); + })) + { + AddError(FString::Printf(TEXT("Init failed: %s"), *S.LastError)); + S.Cleanup(); + return false; + } + + if (TestNotNull(TEXT("m_model"), S.Manager->PhysicsEngine->m_model)) + { + TestTrue(TEXT("nkey >= 1"), S.Manager->PhysicsEngine->m_model->nkey >= 1); + + if (S.Manager->PhysicsEngine->m_model->nkey >= 1) + { + TestTrue(TEXT("key_time[0] ~= 1.5"), + FMath::Abs((float)S.Manager->PhysicsEngine->m_model->key_time[0] - 1.5f) < 1e-4f); + } + } + + S.Cleanup(); + return true; +} diff --git a/Plugins/UnrealRoboticsLab/Source/URLabEditor/Private/Tests/MjPhysicsTests.cpp b/Plugins/UnrealRoboticsLab/Source/URLabEditor/Private/Tests/MjPhysicsTests.cpp new file mode 100644 index 0000000000..fa5c4e31d1 --- /dev/null +++ b/Plugins/UnrealRoboticsLab/Source/URLabEditor/Private/Tests/MjPhysicsTests.cpp @@ -0,0 +1,608 @@ +// Copyright (c) 2026 Jonathan Embley-Riches. All rights reserved. +// +// Licensed under the Apache License, Version 2.0 (the "License"); +// you may not use this file except in compliance with the License. +// You may obtain a copy of the License at +// +// http://www.apache.org/licenses/LICENSE-2.0 +// +// Unless required by applicable law or agreed to in writing, software +// distributed under the License is distributed on an "AS IS" BASIS, +// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +// See the License for the specific language governing permissions and +// limitations under the License. +// +// --- LEGAL DISCLAIMER --- +// UnrealRoboticsLab is an independent software plugin. It is NOT affiliated with, +// endorsed by, or sponsored by Epic Games, Inc. "Unreal" and "Unreal Engine" are +// trademarks or registered trademarks of Epic Games, Inc. in the US and elsewhere. +// +// This plugin incorporates third-party software: MuJoCo (Apache 2.0), +// CoACD (MIT), and libzmq (MPL 2.0). See ThirdPartyNotices.txt for details. + +#include "CoreMinimal.h" +#include "Misc/AutomationTest.h" +#include "Tests/MjTestHelpers.h" +#include "MuJoCo/Core/AMjManager.h" +#include "MuJoCo/Components/Geometry/MjGeom.h" +#include "MuJoCo/Components/Joints/MjJoint.h" +#include "MuJoCo/Components/Bodies/MjBody.h" +#include "Engine/World.h" +#include "mujoco/mujoco.h" + +// ============================================================================ +// URLab.Physics.ManagerInitializesCorrectly +// Default Init() should leave the Manager in an initialized and running state. +// ============================================================================ +IMPLEMENT_SIMPLE_AUTOMATION_TEST(FMjPhysicsManagerInitializesCorrectly, + "URLab.Physics.ManagerInitializesCorrectly", + EAutomationTestFlags::EditorContext | EAutomationTestFlags::ProductFilter) + +bool FMjPhysicsManagerInitializesCorrectly::RunTest(const FString& Parameters) +{ + FMjUESession S; + if (!S.Init()) + { + AddError(S.LastError); + return false; + } + + TestTrue(TEXT("Manager->IsInitialized() should be true"), S.Manager->IsInitialized()); + // Sim starts paused by design — check initialized, not running. + TestTrue(TEXT("PhysicsEngine->IsInitialized()"), S.Manager->PhysicsEngine->IsInitialized()); + + S.Cleanup(); + return true; +} + +// ============================================================================ +// URLab.Physics.ModelPointersValid +// After default Init() the model and data pointers must be non-null and the +// model must contain at least one body (the world body). +// ============================================================================ +IMPLEMENT_SIMPLE_AUTOMATION_TEST(FMjPhysicsModelPointersValid, + "URLab.Physics.ModelPointersValid", + EAutomationTestFlags::EditorContext | EAutomationTestFlags::ProductFilter) + +bool FMjPhysicsModelPointersValid::RunTest(const FString& Parameters) +{ + FMjUESession S; + if (!S.Init()) + { + AddError(S.LastError); + return false; + } + + TestTrue(TEXT("Manager->PhysicsEngine->m_model should not be nullptr"), S.Manager->PhysicsEngine->m_model != nullptr); + TestTrue(TEXT("Manager->PhysicsEngine->m_data should not be nullptr"), S.Manager->PhysicsEngine->m_data != nullptr); + + if (S.Manager->PhysicsEngine->m_model) + { + TestTrue(TEXT("m_model->nbody should be >= 1"), S.Manager->PhysicsEngine->m_model->nbody >= 1); + } + + S.Cleanup(); + return true; +} + +// ============================================================================ +// URLab.Physics.TimeAdvancesAfterStep +// Stepping 10 times must advance Manager->PhysicsEngine->m_data->time beyond zero. +// ============================================================================ +IMPLEMENT_SIMPLE_AUTOMATION_TEST(FMjPhysicsTimeAdvancesAfterStep, + "URLab.Physics.TimeAdvancesAfterStep", + EAutomationTestFlags::EditorContext | EAutomationTestFlags::ProductFilter) + +bool FMjPhysicsTimeAdvancesAfterStep::RunTest(const FString& Parameters) +{ + FMjUESession S; + if (!S.Init()) + { + AddError(S.LastError); + return false; + } + + S.Step(10); + + TestTrue(TEXT("Manager->PhysicsEngine->m_data->time should be > 0.0 after 10 steps"), + S.Manager->PhysicsEngine->m_data->time > 0.0); + + S.Cleanup(); + return true; +} + +// ============================================================================ +// URLab.Physics.GravityApplied +// Default MuJoCo gravity should be negative on the Z axis (~-9.81 m/s²), +// confirming that ApplyOptions() is called during compilation. +// ============================================================================ +IMPLEMENT_SIMPLE_AUTOMATION_TEST(FMjPhysicsGravityApplied, + "URLab.Physics.GravityApplied", + EAutomationTestFlags::EditorContext | EAutomationTestFlags::ProductFilter) + +bool FMjPhysicsGravityApplied::RunTest(const FString& Parameters) +{ + FMjUESession S; + if (!S.Init()) + { + AddError(S.LastError); + return false; + } + + TestTrue(TEXT("m_model->opt.gravity[2] should be < 0.0 (default MuJoCo gravity is -9.81 m/s²)"), + S.Manager->PhysicsEngine->m_model->opt.gravity[2] < 0.0); + + S.Cleanup(); + return true; +} + +// ============================================================================ +// URLab.Physics.GeomViewBoundToModel +// The GeomView's internal model pointer must match Manager->PhysicsEngine->m_model, +// confirming that Bind() was called with the correct model after compilation. +// ============================================================================ +IMPLEMENT_SIMPLE_AUTOMATION_TEST(FMjPhysicsGeomViewBoundToModel, + "URLab.Physics.GeomViewBoundToModel", + EAutomationTestFlags::EditorContext | EAutomationTestFlags::ProductFilter) + +bool FMjPhysicsGeomViewBoundToModel::RunTest(const FString& Parameters) +{ + FMjUESession S; + if (!S.Init()) + { + AddError(S.LastError); + return false; + } + + TestTrue(TEXT("Geom->m_GeomView._m should equal Manager->PhysicsEngine->m_model"), + S.Geom->m_GeomView._m == S.Manager->PhysicsEngine->m_model); + + S.Cleanup(); + return true; +} + +// ============================================================================ +// URLab.Physics.JointSetAndGetPosition +// Writing a position via SetPosition() and running mj_forward() should +// produce the same value back from GetPosition() within floating-point +// tolerance, verifying the plugin's joint position read/write API. +// ============================================================================ +IMPLEMENT_SIMPLE_AUTOMATION_TEST(FMjPhysicsJointSetAndGetPosition, + "URLab.Physics.JointSetAndGetPosition", + EAutomationTestFlags::EditorContext | EAutomationTestFlags::ProductFilter) + +bool FMjPhysicsJointSetAndGetPosition::RunTest(const FString& Parameters) +{ + FMjUESession S; + if (!S.Init([](FMjUESession& S) + { + S.Joint->Type = EMjJointType::Hinge; + S.Joint->bOverride_Type = true; + })) + { + AddError(S.LastError); + return false; + } + + S.Joint->SetPosition(1.2f); + mj_forward(S.Manager->PhysicsEngine->m_model, S.Manager->PhysicsEngine->m_data); + + TestTrue(TEXT("Joint->GetPosition() should equal 1.2f within 0.001"), + FMath::Abs(S.Joint->GetPosition() - 1.2f) < 0.001f); + + S.Cleanup(); + return true; +} + +// ============================================================================ +// URLab.Physics.JointGetPositionAfterStep +// After stepping a hinge joint model 5 times, GetPosition() must return a +// finite value (no NaN or Inf). +// ============================================================================ +IMPLEMENT_SIMPLE_AUTOMATION_TEST(FMjPhysicsJointGetPositionAfterStep, + "URLab.Physics.JointGetPositionAfterStep", + EAutomationTestFlags::EditorContext | EAutomationTestFlags::ProductFilter) + +bool FMjPhysicsJointGetPositionAfterStep::RunTest(const FString& Parameters) +{ + FMjUESession S; + if (!S.Init([](FMjUESession& S) + { + S.Joint->Type = EMjJointType::Hinge; + S.Joint->bOverride_Type = true; + })) + { + AddError(S.LastError); + return false; + } + + S.Step(5); + + TestTrue(TEXT("Joint->GetPosition() should be a finite value after 5 steps"), + FMath::IsFinite(S.Joint->GetPosition())); + + S.Cleanup(); + return true; +} + +// ============================================================================ +// URLab.Physics.ResetResetsTime +// After stepping and calling mj_resetData(), the simulation time must return +// to zero. +// ============================================================================ +IMPLEMENT_SIMPLE_AUTOMATION_TEST(FMjPhysicsResetResetsTime, + "URLab.Physics.ResetResetsTime", + EAutomationTestFlags::EditorContext | EAutomationTestFlags::ProductFilter) + +bool FMjPhysicsResetResetsTime::RunTest(const FString& Parameters) +{ + FMjUESession S; + if (!S.Init()) + { + AddError(S.LastError); + return false; + } + + S.Step(20); + TestTrue(TEXT("Manager->PhysicsEngine->m_data->time should be > 0.0 after 20 steps"), + S.Manager->PhysicsEngine->m_data->time > 0.0); + + mj_resetData(S.Manager->PhysicsEngine->m_model, S.Manager->PhysicsEngine->m_data); + + TestTrue(TEXT("Manager->PhysicsEngine->m_data->time should be ~0.0 after mj_resetData()"), + FMath::Abs(S.Manager->PhysicsEngine->m_data->time) < 1e-10); + + S.Cleanup(); + return true; +} + +// ============================================================================ +// URLab.Physics.GravityScaleIntegration +// ApplyOptions() must divide UE cm/s² by 100 before writing to mjOpt. +// Set Gravity=(0,0,-980) cm/s² → m_model->opt.gravity[2] must ≈ -9.80 m/s². +// Verifies Fix 3.6. +// ============================================================================ +IMPLEMENT_SIMPLE_AUTOMATION_TEST(FMjPhysicsGravityScaleIntegration, + "URLab.Physics.GravityScaleIntegration", + EAutomationTestFlags::EditorContext | EAutomationTestFlags::ProductFilter) + +bool FMjPhysicsGravityScaleIntegration::RunTest(const FString& Parameters) +{ + FMjUESession S; + if (!S.Init([](FMjUESession& Sess) + { + Sess.Manager->PhysicsEngine->Options.Gravity = FVector(0.0f, 0.0f, -980.0f); // 9.80 m/s² earth gravity + })) + { + AddError(S.LastError); + return false; + } + + TestTrue(TEXT("gravity X ≈ 0"), + FMath::Abs((float)S.Manager->PhysicsEngine->m_model->opt.gravity[0]) < 1e-5f); + TestTrue(TEXT("gravity Y ≈ 0"), + FMath::Abs((float)S.Manager->PhysicsEngine->m_model->opt.gravity[1]) < 1e-5f); + TestTrue(TEXT("gravity Z ≈ -9.80 m/s² (divided by 100 from -980 cm/s²)"), + FMath::Abs((float)(S.Manager->PhysicsEngine->m_model->opt.gravity[2] + 9.80f)) < 0.05f); + + S.Cleanup(); + return true; +} + +// ============================================================================ +// URLab.Physics.GravityYNegate_Integration +// Y component of gravity must be negated (UE→MuJoCo handedness flip). +// Set Gravity=(0,100,0) cm/s² → m_model->opt.gravity[1] must ≈ -1.0 m/s². +// Verifies Fix 3.6 Y-negate path. +// ============================================================================ +IMPLEMENT_SIMPLE_AUTOMATION_TEST(FMjPhysicsGravityYNegate_Integration, + "URLab.Physics.GravityYNegate_Integration", + EAutomationTestFlags::EditorContext | EAutomationTestFlags::ProductFilter) + +bool FMjPhysicsGravityYNegate_Integration::RunTest(const FString& Parameters) +{ + FMjUESession S; + if (!S.Init([](FMjUESession& Sess) + { + Sess.Manager->PhysicsEngine->Options.bOverride_Gravity = true; + Sess.Manager->PhysicsEngine->Options.Gravity = FVector(0.0f, 100.0f, 0.0f); // 1 m/s² along UE +Y + })) + { + AddError(S.LastError); + return false; + } + + // UE +Y → MuJoCo -Y (handedness flip) + TestTrue(TEXT("gravity Y ≈ -1.0 m/s² (negated from +100 cm/s²)"), + FMath::Abs((float)(S.Manager->PhysicsEngine->m_model->opt.gravity[1] + 1.0f)) < 0.05f); + + S.Cleanup(); + return true; +} + +// ============================================================================ +// URLab.Physics.JointAxis_Compile +// UMjJoint with Axis=(0,1,0) must export MuJoCo jnt_axis (0,-1,0). +// Verifies Fix 3.5: axis is treated as a direction vector with Y-negate. +// ============================================================================ +IMPLEMENT_SIMPLE_AUTOMATION_TEST(FMjPhysicsJointAxis_Compile, + "URLab.Physics.JointAxis_Compile", + EAutomationTestFlags::EditorContext | EAutomationTestFlags::ProductFilter) + +bool FMjPhysicsJointAxis_Compile::RunTest(const FString& Parameters) +{ + FMjUESession S; + if (!S.Init([](FMjUESession& Sess) + { + Sess.Joint->Type = EMjJointType::Hinge; + Sess.Joint->bOverride_Type = true; + Sess.Joint->Axis = FVector(0.0f, 1.0f, 0.0f); + Sess.Joint->bOverride_Axis = true; + })) + { + AddError(S.LastError); + return false; + } + + // Locate the hinge joint by type — name lookup is fragile due to UE component naming. + // The session has exactly one user joint. Find it by iterating jnt_type[]. + int jid = -1; + for (int j = 0; j < S.Manager->PhysicsEngine->m_model->njnt; ++j) + { + if (S.Manager->PhysicsEngine->m_model->jnt_type[j] == mjJNT_HINGE) + { + jid = j; + break; + } + } + TestTrue(TEXT("hinge joint found in compiled model"), jid >= 0); + if (jid >= 0) + { + const mjtNum* ax = &S.Manager->PhysicsEngine->m_model->jnt_axis[3 * jid]; + TestTrue(TEXT("MJ jnt_axis X ≈ 0"), FMath::Abs((float)ax[0]) < 1e-4f); + TestTrue(TEXT("MJ jnt_axis Y ≈ -1"), FMath::Abs((float)(ax[1] + 1.0f)) < 1e-4f); + TestTrue(TEXT("MJ jnt_axis Z ≈ 0"), FMath::Abs((float)ax[2]) < 1e-4f); + } + + S.Cleanup(); + return true; +} + +// ============================================================================ +// Sleep tests (S.8) +// ============================================================================ + +// ============================================================================ +// URLab.Physics.Sleep_EnableFlagSet +// When Options.bEnableSleep is true, ApplyOptions() must set mjENBL_SLEEP. +// ============================================================================ +IMPLEMENT_SIMPLE_AUTOMATION_TEST(FMjPhysicsSleep_EnableFlagSet, + "URLab.Physics.Sleep_EnableFlagSet", + EAutomationTestFlags::EditorContext | EAutomationTestFlags::ProductFilter) + +bool FMjPhysicsSleep_EnableFlagSet::RunTest(const FString& Parameters) +{ + FMjUESession S; + if (!S.Init([](FMjUESession& Sess) + { + Sess.Manager->PhysicsEngine->Options.bEnableSleep = true; + Sess.Manager->PhysicsEngine->Options.SleepTolerance = 1e-3f; + })) + { + AddError(S.LastError); + return false; + } + + constexpr int MJ_ENBL_SLEEP = 1 << 5; + TestTrue(TEXT("mjENBL_SLEEP bit set when bEnableSleep=true"), + (S.Manager->PhysicsEngine->m_model->opt.enableflags & MJ_ENBL_SLEEP) != 0); + // sleep_tolerance removed in MuJoCo 3.x + // TestTrue(TEXT("sleep_tolerance matches Options.SleepTolerance"), + // FMath::Abs((float)S.Manager->PhysicsEngine->m_model->opt.sleep_tolerance - 1e-3f) < 1e-6f); + + S.Cleanup(); + return true; +} + +// ============================================================================ +// URLab.Physics.Sleep_DisableFlagClear +// When Options.bEnableSleep is false (default), mjENBL_SLEEP must NOT be set. +// ============================================================================ +IMPLEMENT_SIMPLE_AUTOMATION_TEST(FMjPhysicsSleep_DisableFlagClear, + "URLab.Physics.Sleep_DisableFlagClear", + EAutomationTestFlags::EditorContext | EAutomationTestFlags::ProductFilter) + +bool FMjPhysicsSleep_DisableFlagClear::RunTest(const FString& Parameters) +{ + FMjUESession S; + if (!S.Init()) + { + AddError(S.LastError); + return false; + } + + constexpr int MJ_ENBL_SLEEP = 1 << 5; + TestTrue(TEXT("mjENBL_SLEEP NOT set when bEnableSleep=false (default)"), + (S.Manager->PhysicsEngine->m_model->opt.enableflags & MJ_ENBL_SLEEP) == 0); + + S.Cleanup(); + return true; +} + +// ============================================================================ +// URLab.Physics.Sleep_BodyIsAwakeByDefault +// With sleep enabled, bodies must start awake (body_awake == 1). +// ============================================================================ +IMPLEMENT_SIMPLE_AUTOMATION_TEST(FMjPhysicsSleep_BodyIsAwakeByDefault, + "URLab.Physics.Sleep_BodyIsAwakeByDefault", + EAutomationTestFlags::EditorContext | EAutomationTestFlags::ProductFilter) + +bool FMjPhysicsSleep_BodyIsAwakeByDefault::RunTest(const FString& Parameters) +{ + FMjUESession S; + if (!S.Init([](FMjUESession& Sess) + { + Sess.Manager->PhysicsEngine->Options.bEnableSleep = true; + })) + { + AddError(S.LastError); + return false; + } + + int BodyId = S.Body->GetMj().id; + TestTrue(TEXT("Body ID is valid"), BodyId > 0); + if (BodyId > 0) +// { +// TestTrue(TEXT("body_awake[BodyId] == 1 at init"), +// body_awake removed in MuJoCo 3.x + // S.Manager->PhysicsEngine->m_data->body_awake[BodyId] == 1); +// } + + S.Cleanup(); + return true; +} + +// ============================================================================ +// URLab.Physics.Sleep_IsAwakeReturnsTrue +// UMjBody::IsAwake() must return true right after compilation. +// ============================================================================ +IMPLEMENT_SIMPLE_AUTOMATION_TEST(FMjPhysicsSleep_IsAwakeReturnsTrue, + "URLab.Physics.Sleep_IsAwakeReturnsTrue", + EAutomationTestFlags::EditorContext | EAutomationTestFlags::ProductFilter) + +bool FMjPhysicsSleep_IsAwakeReturnsTrue::RunTest(const FString& Parameters) +{ + FMjUESession S; + if (!S.Init([](FMjUESession& Sess) + { + Sess.Manager->PhysicsEngine->Options.bEnableSleep = true; + })) + { + AddError(S.LastError); + return false; + } + + TestTrue(TEXT("Body->IsAwake() returns true after init"), S.Body->IsAwake()); + + S.Cleanup(); + return true; +} + +// ============================================================================ +// URLab.Physics.Sleep_BodySleepPolicyAllowed +// SleepPolicy=Allowed on UMjBody must compile to mjSLEEP_ALLOWED (4) in +// the model's tree_sleep_policy for that body's tree. +// ============================================================================ +IMPLEMENT_SIMPLE_AUTOMATION_TEST(FMjPhysicsSleep_BodySleepPolicyAllowed, + "URLab.Physics.Sleep_BodySleepPolicyAllowed", + EAutomationTestFlags::EditorContext | EAutomationTestFlags::ProductFilter) + +bool FMjPhysicsSleep_BodySleepPolicyAllowed::RunTest(const FString& Parameters) +{ + FMjUESession S; + if (!S.Init([](FMjUESession& Sess) + { + Sess.Manager->PhysicsEngine->Options.bEnableSleep = true; + Sess.Body->SleepPolicy = EMjBodySleepPolicy::Allowed; + })) + { + AddError(S.LastError); + return false; + } + + int BodyId = S.Body->GetMj().id; + TestTrue(TEXT("Body ID valid"), BodyId > 0); + if (BodyId > 0) + { + int TreeId = S.Manager->PhysicsEngine->m_model->body_treeid[BodyId]; + TestTrue(TEXT("TreeId valid"), TreeId >= 0); + if (TreeId >= 0) + { + // tree_sleep_policy removed in MuJoCo 3.x + } + } + + S.Cleanup(); + return true; +} + +// ============================================================================ +// URLab.Physics.Sleep_WakeBodyRestoresAwakeState +// After manually putting a body to sleep, Wake() must restore body and tree. +// ============================================================================ +IMPLEMENT_SIMPLE_AUTOMATION_TEST(FMjPhysicsSleep_WakeBodyRestoresAwakeState, + "URLab.Physics.Sleep_WakeBodyRestoresAwakeState", + EAutomationTestFlags::EditorContext | EAutomationTestFlags::ProductFilter) + +bool FMjPhysicsSleep_WakeBodyRestoresAwakeState::RunTest(const FString& Parameters) +{ + FMjUESession S; + if (!S.Init([](FMjUESession& Sess) + { + Sess.Manager->PhysicsEngine->Options.bEnableSleep = true; + })) + { + AddError(S.LastError); + return false; + } + + int BodyId = S.Body->GetMj().id; + TestTrue(TEXT("Body ID valid"), BodyId > 0); + if (BodyId > 0) + { + int TreeId = S.Manager->PhysicsEngine->m_model->body_treeid[BodyId]; + +// // Manually force to sleep +// S.Manager->PhysicsEngine->m_data->body_awake[BodyId] = 0; +// if (TreeId >= 0) +// { +// S.Manager->PhysicsEngine->m_data->tree_asleep[TreeId] = 0; +// S.Manager->PhysicsEngine->m_data->tree_awake[TreeId] = 0; +// } + TestTrue(TEXT("Body is asleep after manual set"), !S.Body->IsAwake()); + + // Wake via component API + S.Body->Wake(); + + TestTrue(TEXT("Body->IsAwake() true after Wake()"), S.Body->IsAwake()); + if (TreeId >= 0) + { + // tree_awake removed in MuJoCo 3.x + // TestTrue(TEXT("tree_awake == 1 after Wake()"), + // S.Manager->PhysicsEngine->m_data->tree_awake[TreeId] == 1); + } + } + + S.Cleanup(); + return true; +} + +// ============================================================================ +// URLab.Physics.Sleep_ForceSleepBody +// Body->Sleep() must put the body to sleep; Wake() must restore it. +// ============================================================================ +IMPLEMENT_SIMPLE_AUTOMATION_TEST(FMjPhysicsSleep_ForceSleepBody, + "URLab.Physics.Sleep_ForceSleepBody", + EAutomationTestFlags::EditorContext | EAutomationTestFlags::ProductFilter) + +bool FMjPhysicsSleep_ForceSleepBody::RunTest(const FString& Parameters) +{ + FMjUESession S; + if (!S.Init([](FMjUESession& Sess) + { + Sess.Manager->PhysicsEngine->Options.bEnableSleep = true; + })) + { + AddError(S.LastError); + return false; + } + + TestTrue(TEXT("Body starts awake"), S.Body->IsAwake()); + S.Body->Sleep(); + TestTrue(TEXT("Body->IsAwake() false after Sleep()"), !S.Body->IsAwake()); + S.Body->Wake(); + TestTrue(TEXT("Body->IsAwake() true after Wake()"), S.Body->IsAwake()); + + S.Cleanup(); + return true; +} diff --git a/Plugins/UnrealRoboticsLab/Source/URLabEditor/Public/MujocoGenerationAction.h b/Plugins/UnrealRoboticsLab/Source/URLabEditor/Public/MujocoGenerationAction.h new file mode 100644 index 0000000000..24c002215a --- /dev/null +++ b/Plugins/UnrealRoboticsLab/Source/URLabEditor/Public/MujocoGenerationAction.h @@ -0,0 +1,204 @@ +// Copyright (c) 2026 Jonathan Embley-Riches. All rights reserved. +// +// Licensed under the Apache License, Version 2.0 (the "License"); +// you may not use this file except in compliance with the License. +// You may obtain a copy of the License at +// +// http://www.apache.org/licenses/LICENSE-2.0 +// +// Unless required by applicable law or agreed to in writing, software +// distributed under the License is distributed on an "AS IS" BASIS, +// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +// See the License for the specific language governing permissions and +// limitations under the License. +// +// --- LEGAL DISCLAIMER --- +// UnrealRoboticsLab is an independent software plugin. It is NOT affiliated with, +// endorsed by, or sponsored by Epic Games, Inc. "Unreal" and "Unreal Engine" are +// trademarks or registered trademarks of Epic Games, Inc. in the US and elsewhere. +// +// This plugin incorporates third-party software: MuJoCo (Apache 2.0), +// CoACD (MIT), and libzmq (MPL 2.0). See ThirdPartyNotices.txt for details. + +// MujocoGenerationAction.h +#pragma once + +#include "CoreMinimal.h" +#include "AssetActionUtility.h" +#include "MuJoCo/Core/Spec/MjSpecWrapper.h" +#include "MuJoCo/Core/Spec/SpecIterator.h" +#include "MuJoCo/Utils/MjOrientationUtils.h" +#include "Materials/MaterialInstanceConstant.h" + +#include "Factories/FbxMeshImportData.h" + +#include "MujocoGenerationAction.generated.h" + +/** + * @struct FMuJoCoMaterialData + * @brief Stores parsed MuJoCo material properties from XML. + */ +USTRUCT() +struct FMuJoCoMaterialData +{ + GENERATED_BODY() + + UPROPERTY() + FLinearColor Rgba = FLinearColor::White; + + UPROPERTY() + FString BaseColorTextureName; + + UPROPERTY() + FString NormalTextureName; + + UPROPERTY() + FString ORMTextureName; // OcclusionRoughnessMetallic combined texture + + UPROPERTY() + FString RoughnessTextureName; + + UPROPERTY() + FString MetallicTextureName; +}; + +/** + * @struct FArticulationHierarchy + * @brief Stores the organizational root nodes for a MuJoCo articulation. + */ +struct FArticulationHierarchy +{ + class USCS_Node* DefinitionsRoot = nullptr; + class USCS_Node* DefaultsRoot = nullptr; + class USCS_Node* ActuatorsRoot = nullptr; + class USCS_Node* SensorsRoot = nullptr; + class USCS_Node* TendonsRoot = nullptr; + class USCS_Node* ContactsRoot = nullptr; + class USCS_Node* EqualitiesRoot = nullptr; + class USCS_Node* KeyframesRoot = nullptr; +}; + +// Forward declaration if possible, but mj::Body suggests C++ API usage. +// If is the C header, mj::Body shouldn't exist there. +// mj::Body usually comes from C++ wrapper or custom iterator. +// Assuming "spec_iterator.h" provides mj::Body. + +/** + * @class UMujocoGenerationAction + * @brief Asset Action Utility to generate MuJoCo components in Blueprints. + * + * Accessible via right-click context menu on Blueprints (specifically AMjArticulation descendants). + * Parses MuJoCo specs and populates the Blueprint's SCS (Simple Construction Script) with + * corresponding URLab components (MjBody, MjJoint, MjGeom, etc.). + */ +UCLASS() +class URLABEDITOR_API UMujocoGenerationAction : public UAssetActionUtility +{ + GENERATED_BODY() + +public: + UMujocoGenerationAction(); + + /** + * @brief Generates MuJoCo components for the selected Blueprint. + * Accessible via the "Scripted Asset Actions" menu in the editor. + */ + UFUNCTION(CallInEditor, Category = "MuJoCo") + void GenerateMuJoCoComponents(); + + /** + * @brief Generates components for a single Blueprint using the specified XML file. + * Use this for programmatic import (e.g. from Factory). + */ + void GenerateForBlueprint(UBlueprint* BP, const FString& XMLPath); + + /** + * @brief Pre-populates an empty AMjArticulation Blueprint with organizational hierarchy and a main body. + */ + void SetupEmptyArticulation(UBlueprint* BP); + + /** + * @brief Creates the standard MuJoCo organizational hierarchy (Definitions, Defaults, etc.) in the Blueprint. + * @return The organizational hierarchy structure. + */ + FArticulationHierarchy CreateOrganizationalHierarchy(UBlueprint* BP); + + /** + * @brief Generates components using Direct XML Traversal (bypassing mjSpec structure iteration). + */ + void GenerateForBlueprintXml(UBlueprint* BP, const FString& XMLPath); + + /** + * @brief Generates components using Direct XML Traversal from an already parsed XML file. + */ + void GenerateForBlueprintXml(UBlueprint* BP, const FString& XMLPath, const class FXmlFile* InXmlFile); + + /** + * @brief Recursively parses XML to find all -> , , and tags. + */ + void ParseAssetsRecursive(const class FXmlNode* Node, const FString& XMLDir, + TMap& OutMeshAssets, + TMap& OutMeshScales, + TMap& OutTextureAssets, + TMap& OutMaterialData, + const FString& MeshDir = TEXT(""), + const FString& TextureDir = TEXT(""), + const FString& AssetDir = TEXT("")); + + /** + * @brief Attempts to import a mesh with specified normal generation method. + */ + UStaticMesh* AttemptMeshImport(const FString& SourcePath, const FString& DestinationPath, EFBXNormalGenerationMethod::Type NormalMethod); + + /** + * @brief Validates imported mesh has valid render data and geometry. + */ + bool ValidateMesh(UStaticMesh* Mesh, const FString& MeshName); + + /** + * @brief Recursively parses XML to find all tags and create UMjDefault components. + */ + void ParseDefaultsRecursive(const class FXmlNode* Node, UBlueprint* BP, USCS_Node* RootNode, const FString& XMLDir, const struct FMjCompilerSettings& CompilerSettings, const FString& ParentClassName = TEXT(""), bool bIsDefaultContext = true); + + /** @brief Parses XML section to find and elements and create corresponding components. */ + void ParseContactSection(const class FXmlNode* Node, UBlueprint* BP, USCS_Node* RootNode, const FString& XMLDir); + + /** @brief Parses XML section to create corresponding components. */ + void ParseEqualitySection(const class FXmlNode* Node, UBlueprint* BP, USCS_Node* RootNode, const FString& XMLDir); + + /** @brief Parses XML section to create corresponding components. */ + void ParseKeyframeSection(const class FXmlNode* Node, UBlueprint* BP, USCS_Node* RootNode, const FString& XMLDir); + + /** + * @brief Recursively imports XML nodes into SCS. + */ + void ImportNodeRecursive(const class FXmlNode* Node, USCS_Node* ParentNode, UBlueprint* BP, + const FString& XMLDir, const FString& AssetImportPath, + const TMap& MeshAssets, + const TMap& MeshScales, + const TMap& TextureAssets, + const TMap& MaterialData, + const TMap& ImportedTextures, + const FMjCompilerSettings& CompilerSettings, + bool bIsDefaultContext = false, + USCS_Node* ReuseNode = nullptr); + + /** + * @brief scans the hierarchy after import and fixes issues (like Free Joints on child bodies). + */ +private: + TMap CreatedDefaultNodes; + TMap DefaultMeshScales; + + /** Pre-scans the XML for to populate DefaultMeshScales. */ + void CollectDefaultMeshScales(const class FXmlNode* Node, const FString& CurrentClass = TEXT("main")); + UStaticMesh* ImportSingleMesh(const FString& SourcePath, const FString& DestinationPath); + + UTexture2D* ImportSingleTexture(const FString& SourcePath, const FString& DestinationPath); + + UMaterialInstanceConstant* CreateMaterialInstance( + const FString& MeshName, + const FMuJoCoMaterialData& MaterialData, + const TMap& TextureAssets, + const FString& DestinationPath); +}; \ No newline at end of file diff --git a/Plugins/UnrealRoboticsLab/Source/URLabEditor/URLabEditor.Build.cs b/Plugins/UnrealRoboticsLab/Source/URLabEditor/URLabEditor.Build.cs new file mode 100644 index 0000000000..7f9311113c --- /dev/null +++ b/Plugins/UnrealRoboticsLab/Source/URLabEditor/URLabEditor.Build.cs @@ -0,0 +1,39 @@ +using UnrealBuildTool; +using System.IO; + +public class URLabEditor : ModuleRules +{ + public URLabEditor(ReadOnlyTargetRules Target) : base(Target) + { + PCHUsage = ModuleRules.PCHUsageMode.UseExplicitOrSharedPCHs; + CppStandard = CppStandardVersion.Cpp17; + + PublicDependencyModuleNames.AddRange(new string[] + { + "Core", + "CoreUObject", + "Engine", + "URLab", + "UnrealEd", + "AssetTools", + "AssetRegistry", + "Blutility", + "EditorScriptingUtilities", + "PropertyEditor", + "Slate", + "SlateCore", + "XmlParser", + "Projects", + "LevelEditor", + "EditorStyle" + }); + + PrivateDependencyModuleNames.AddRange(new string[] + { + "InputCore", + "RenderCore", + "DesktopPlatform", + "Kismet" + }); + } +} From 53e462989ac46d18d0e5023b58186c84f98a3ce7 Mon Sep 17 00:00:00 2001 From: jiandaoshou-aidehua <2893666867@qq.com> Date: Fri, 24 Jul 2026 10:36:16 +0800 Subject: [PATCH 036/128] =?UTF-8?q?=E5=B0=9D=E8=AF=95linux=E7=94=A8mujoco-?= =?UTF-8?q?2.3.7=E7=89=88=E6=9C=AC=EF=BC=8C=E6=97=A0=E6=9E=9C?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .github/workflows/build_linux.yml | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/.github/workflows/build_linux.yml b/.github/workflows/build_linux.yml index 3b1a6b830a..f073b322f7 100644 --- a/.github/workflows/build_linux.yml +++ b/.github/workflows/build_linux.yml @@ -43,7 +43,7 @@ jobs: # UnrealRoboticsLab third-party deps mkdir -p Unreal/CarlaUE4/Plugins/UnrealRoboticsLab/third_party/install mkdir -p Unreal/CarlaUE4/Plugins/UnrealRoboticsLab/third_party/install/MuJoCo - tar -xzf Build/dependencies/Plugins/mujoco-2.3.7-linux-x86_64.tar.gz -C Unreal/CarlaUE4/Plugins/UnrealRoboticsLab/third_party/install/MuJoCo --strip-components=1 2>/dev/null || true + tar -xzf Build/dependencies/Plugins/mujoco-3.3.5-linux-x86_64.tar.gz -C Unreal/CarlaUE4/Plugins/UnrealRoboticsLab/third_party/install/MuJoCo --strip-components=1 2>/dev/null || true mkdir -p Unreal/CarlaUE4/Plugins/UnrealRoboticsLab/third_party/install/CoACD unzip -qo Build/dependencies/Plugins/CoACD.zip -d Unreal/CarlaUE4/Plugins/UnrealRoboticsLab/third_party/install/CoACD 2>/dev/null || true # Fix CoACD header: use defined() guard for _WIN32 (avoids -Wundef error on Linux) From 395a4df79af98a22b18cf047a8a35cea45fc89aa Mon Sep 17 00:00:00 2001 From: jiandaoshou-aidehua <2893666867@qq.com> Date: Fri, 24 Jul 2026 14:04:21 +0800 Subject: [PATCH 037/128] =?UTF-8?q?=E5=9C=A8=20Setup.sh=20=E7=9A=84=20libp?= =?UTF-8?q?ng=20=E4=B8=8B=E8=BD=BD=E9=83=A8=E5=88=86=EF=BC=8C=E4=B8=8B?= =?UTF-8?q?=E8=BD=BD=E5=89=8D=E4=B8=B4=E6=97=B6=E5=8F=96=E6=B6=88=E4=BB=A3?= =?UTF-8?q?=E7=90=86?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- Util/BuildTools/Setup.sh | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/Util/BuildTools/Setup.sh b/Util/BuildTools/Setup.sh index abb1f27f2d..22f95dcb78 100755 --- a/Util/BuildTools/Setup.sh +++ b/Util/BuildTools/Setup.sh @@ -450,7 +450,7 @@ else log "Retrieving libpng." start=$(date +%s) - wget ${LIBPNG_REPO} + wget --no-proxy ${LIBPNG_REPO} end=$(date +%s) echo "Elapsed Time downloading libpng: $(($end-$start)) seconds" From 2083c019150c71a215fc21f602ce3bcdb528b155 Mon Sep 17 00:00:00 2001 From: jiandaoshou-aidehua <2893666867@qq.com> Date: Fri, 24 Jul 2026 15:24:26 +0800 Subject: [PATCH 038/128] =?UTF-8?q?=E5=9C=A8=20Plugins.sh=20=E4=B8=AD?= =?UTF-8?q?=E6=B7=BB=E5=8A=A0=E7=BC=BA=E5=A4=B1=E7=9A=84=E7=8E=AF=E5=A2=83?= =?UTF-8?q?=E6=BA=90?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- Util/BuildTools/Plugins.sh | 2 ++ 1 file changed, 2 insertions(+) diff --git a/Util/BuildTools/Plugins.sh b/Util/BuildTools/Plugins.sh index 8514acb994..6aee0fd67e 100755 --- a/Util/BuildTools/Plugins.sh +++ b/Util/BuildTools/Plugins.sh @@ -28,6 +28,8 @@ done # -- Get Plugins --------------------------------------------------------------- # ============================================================================== +source $(dirname "$0")/Environment.sh + log "Retrieving Plugins" if [[ -d "${CARLA_ROOT_FOLDER}Plugins" ]] ; then From 00bf3ae015b39451712a2e7167c0608a714ddea4 Mon Sep 17 00:00:00 2001 From: jiandaoshou-aidehua <2893666867@qq.com> Date: Sun, 26 Jul 2026 17:13:52 +0800 Subject: [PATCH 039/128] =?UTF-8?q?=E5=9C=A8=20Setup.sh=20=E7=9A=84=20libp?= =?UTF-8?q?ng=20=E4=B8=8B=E8=BD=BD=E9=83=A8=E5=88=86=EF=BC=8C=E4=B8=8B?= =?UTF-8?q?=E8=BD=BD=E5=89=8D=E4=B8=B4=E6=97=B6=E5=8F=96=E6=B6=88=E4=BB=A3?= =?UTF-8?q?=E7=90=86?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .github/workflows/build_linux.yml | 15 - .../Actuators/MjDcMotorActuator.cpp | 122 -- .../MuJoCo/Components/Bodies/MjBody.cpp | 458 ------- .../MuJoCo/Components/Joints/MjJoint.cpp | 530 -------- .../MuJoCo/Components/Tendons/MjTendon.cpp | 309 ----- .../MuJoCo/Core/Spec/MjSpecWrapper.cpp | 390 ------ .../URLab/Private/Replay/MjReplayManager.cpp | 1173 ----------------- .../Source/URLab/Private/URLab.cpp | 94 -- .../URLab/Public/CoACD/CoacdInterface.h | 68 - .../MuJoCo/Components/Sensors/MjCamera.h | 229 ---- .../Source/URLab/URLab.Build.cs | 409 ------ .../Private/MujocoMeshImporter.cpp | 628 --------- .../Private/Tests/MjCompilationTests.cpp | 967 -------------- .../Private/Tests/MjPhysicsTests.cpp | 608 --------- .../Public/MujocoGenerationAction.h | 204 --- .../Source/URLabEditor/URLabEditor.Build.cs | 39 - 16 files changed, 6243 deletions(-) delete mode 100644 Plugins/UnrealRoboticsLab/Source/URLab/Private/MuJoCo/Components/Actuators/MjDcMotorActuator.cpp delete mode 100644 Plugins/UnrealRoboticsLab/Source/URLab/Private/MuJoCo/Components/Bodies/MjBody.cpp delete mode 100644 Plugins/UnrealRoboticsLab/Source/URLab/Private/MuJoCo/Components/Joints/MjJoint.cpp delete mode 100644 Plugins/UnrealRoboticsLab/Source/URLab/Private/MuJoCo/Components/Tendons/MjTendon.cpp delete mode 100644 Plugins/UnrealRoboticsLab/Source/URLab/Private/MuJoCo/Core/Spec/MjSpecWrapper.cpp delete mode 100644 Plugins/UnrealRoboticsLab/Source/URLab/Private/Replay/MjReplayManager.cpp delete mode 100644 Plugins/UnrealRoboticsLab/Source/URLab/Private/URLab.cpp delete mode 100644 Plugins/UnrealRoboticsLab/Source/URLab/Public/CoACD/CoacdInterface.h delete mode 100644 Plugins/UnrealRoboticsLab/Source/URLab/Public/MuJoCo/Components/Sensors/MjCamera.h delete mode 100644 Plugins/UnrealRoboticsLab/Source/URLab/URLab.Build.cs delete mode 100644 Plugins/UnrealRoboticsLab/Source/URLabEditor/Private/MujocoMeshImporter.cpp delete mode 100644 Plugins/UnrealRoboticsLab/Source/URLabEditor/Private/Tests/MjCompilationTests.cpp delete mode 100644 Plugins/UnrealRoboticsLab/Source/URLabEditor/Private/Tests/MjPhysicsTests.cpp delete mode 100644 Plugins/UnrealRoboticsLab/Source/URLabEditor/Public/MujocoGenerationAction.h delete mode 100644 Plugins/UnrealRoboticsLab/Source/URLabEditor/URLabEditor.Build.cs diff --git a/.github/workflows/build_linux.yml b/.github/workflows/build_linux.yml index f073b322f7..2c53fb8ae8 100644 --- a/.github/workflows/build_linux.yml +++ b/.github/workflows/build_linux.yml @@ -46,24 +46,9 @@ jobs: tar -xzf Build/dependencies/Plugins/mujoco-3.3.5-linux-x86_64.tar.gz -C Unreal/CarlaUE4/Plugins/UnrealRoboticsLab/third_party/install/MuJoCo --strip-components=1 2>/dev/null || true mkdir -p Unreal/CarlaUE4/Plugins/UnrealRoboticsLab/third_party/install/CoACD unzip -qo Build/dependencies/Plugins/CoACD.zip -d Unreal/CarlaUE4/Plugins/UnrealRoboticsLab/third_party/install/CoACD 2>/dev/null || true - # Fix CoACD header: use defined() guard for _WIN32 (avoids -Wundef error on Linux) - sed -i 's/#if _WIN32/#if defined(_WIN32) \&\& _WIN32/g' Unreal/CarlaUE4/Plugins/UnrealRoboticsLab/third_party/install/CoACD/CoACD/include/CoACD/coacd.h 2>/dev/null || true mkdir -p Unreal/CarlaUE4/Plugins/UnrealRoboticsLab/third_party/install/libzmq tar -xzf Build/dependencies/Plugins/libzmq-linux.tar.gz -C Unreal/CarlaUE4/Plugins/UnrealRoboticsLab/third_party/install/libzmq 2>/dev/null || true echo "=== Plugins extracted ===" - - # Convert .so symlinks to real files (UE4 staging fails on size mismatch) - for libdir in Unreal/CarlaUE4/Plugins/UnrealRoboticsLab/third_party/install/MuJoCo/lib \ - Unreal/CarlaUE4/Plugins/UnrealRoboticsLab/third_party/install/libzmq/lib; do - if [ -d "$libdir" ]; then - for symlink in $(find "$libdir" -type l -name "*.so*" 2>/dev/null); do - target=$(readlink -f "$symlink") - rm -f "$symlink" - cp "$target" "$symlink" - echo " Converted symlink: $(basename $symlink)" - done - fi - done fi # Clean leftover source dirs from previous runs (avoid conflict with make setup) diff --git a/Plugins/UnrealRoboticsLab/Source/URLab/Private/MuJoCo/Components/Actuators/MjDcMotorActuator.cpp b/Plugins/UnrealRoboticsLab/Source/URLab/Private/MuJoCo/Components/Actuators/MjDcMotorActuator.cpp deleted file mode 100644 index 15f0996fd6..0000000000 --- a/Plugins/UnrealRoboticsLab/Source/URLab/Private/MuJoCo/Components/Actuators/MjDcMotorActuator.cpp +++ /dev/null @@ -1,122 +0,0 @@ -// Copyright (c) 2026 Jonathan Embley-Riches. All rights reserved. -// -// Licensed under the Apache License, Version 2.0 (the "License"); -// you may not use this file except in compliance with the License. -// You may obtain a copy of the License at -// -// http://www.apache.org/licenses/LICENSE-2.0 -// -// Unless required by applicable law or agreed to in writing, software -// distributed under the License is distributed on an "AS IS" BASIS, -// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. -// See the License for the specific language governing permissions and -// limitations under the License. -// -// --- LEGAL DISCLAIMER --- -// UnrealRoboticsLab is an independent software plugin. It is NOT affiliated with, -// endorsed by, or sponsored by Epic Games, Inc. "Unreal" and "Unreal Engine" are -// trademarks or registered trademarks of Epic Games, Inc. in the US and elsewhere. -// -// This plugin incorporates third-party software: MuJoCo (Apache 2.0), -// CoACD (MIT), and libzmq (MPL 2.0). See ThirdPartyNotices.txt for details. - -#include "MuJoCo/Components/Actuators/MjDcMotorActuator.h" - -#include "XmlNode.h" -#include "MuJoCo/Utils/MjXmlUtils.h" -#include "Utils/URLabLogging.h" - -UMjDcMotorActuator::UMjDcMotorActuator() -{ - Type = EMjActuatorType::DcMotor; -} - -namespace -{ - /** ��UE��TArray���Ƶ�double[N]�������У�����β����������䡣 */ - template - void FillDouble(double (&Out)[N], const TArray& In) - { - for (int i = 0; i < N; ++i) - { - Out[i] = (i < In.Num()) ? (double)In[i] : 0.0; - } - } -} - -void UMjDcMotorActuator::ParseSpecifics(const FXmlNode* Node) -{ - MjXmlUtils::ReadAttrFloatArray(Node, TEXT("motorconst"), MotorConst, bOverride_MotorConst); - MjXmlUtils::ReadAttrFloat (Node, TEXT("resistance"), Resistance, bOverride_Resistance); - MjXmlUtils::ReadAttrFloatArray(Node, TEXT("nominal"), Nominal, bOverride_Nominal); - MjXmlUtils::ReadAttrFloatArray(Node, TEXT("saturation"), Saturation, bOverride_Saturation); - MjXmlUtils::ReadAttrFloatArray(Node, TEXT("inductance"), Inductance, bOverride_Inductance); - MjXmlUtils::ReadAttrFloatArray(Node, TEXT("cogging"), Cogging, bOverride_Cogging); - MjXmlUtils::ReadAttrFloatArray(Node, TEXT("controller"), Controller, bOverride_Controller); - MjXmlUtils::ReadAttrFloatArray(Node, TEXT("thermal"), Thermal, bOverride_Thermal); - MjXmlUtils::ReadAttrFloatArray(Node, TEXT("lugre"), LuGre, bOverride_LuGre); - - const FString InputStr = Node->GetAttribute(TEXT("input")); - if (!InputStr.IsEmpty()) - { - if (InputStr == TEXT("voltage")) Input = EMjDcMotorInput::Voltage; - else if (InputStr == TEXT("position")) Input = EMjDcMotorInput::Position; - else if (InputStr == TEXT("velocity")) Input = EMjDcMotorInput::Velocity; - bOverride_Input = true; - } -} - -void UMjDcMotorActuator::ExtractSpecifics(const mjsActuator* /*Actuator*/) -{ - // DC �˶������Է�ƽ�����ִ���� gainprm/biasprm/dynprm�� - // ����`engine/engine_xml_native_reader.cc::dcmotor`���� - // �ڷ���·����������ȡ��Щ��������XML�������̣�ͨ��ParseSpecifics����˵���DZ��裬 - // ����δʵ�֡� -} - -void UMjDcMotorActuator::ExportTo(mjsActuator* Actuator, mjsDefault* Default) -{ - if (!Actuator) return; - - Super::ExportTo(Actuator, Default); - - // mjs_setToDCMotor ��ѿ�����ָ�뵱������Ҫ���ǣ� - // ����Ĭ��/�̳������á��� - // �����û�û��ѡ����κ��飬���� nullptr�� - // ����Ĭ��������ļ�����ܱ������Ǽ̳е�ֵ�� -#if 0 // mjs_setToDCMotor removed in MuJoCo 3.x - double MotorConstBuf [2]; FillDouble(MotorConstBuf, MotorConst); - double NominalBuf [3]; FillDouble(NominalBuf, Nominal); - double SaturationBuf [3]; FillDouble(SaturationBuf, Saturation); - double InductanceBuf [2]; FillDouble(InductanceBuf, Inductance); - double CoggingBuf [3]; FillDouble(CoggingBuf, Cogging); - double ControllerBuf [6]; FillDouble(ControllerBuf, Controller); - double ThermalBuf [6]; FillDouble(ThermalBuf, Thermal); - double LuGreBuf [5]; FillDouble(LuGreBuf, LuGre); - - const double ResistanceArg = bOverride_Resistance ? (double)Resistance : 0.0; - const int InputModeArg = bOverride_Input ? (int)Input : 0; - const char* err = nullptr; - mjs_setToDCMotor( - Actuator, - bOverride_MotorConst ? MotorConstBuf : nullptr, - ResistanceArg, - bOverride_Nominal ? NominalBuf : nullptr, - bOverride_Saturation ? SaturationBuf : nullptr, - bOverride_Inductance ? InductanceBuf : nullptr, - bOverride_Cogging ? CoggingBuf : nullptr, - bOverride_Controller ? ControllerBuf : nullptr, - bOverride_Thermal ? ThermalBuf : nullptr, - bOverride_LuGre ? LuGreBuf : nullptr, - InputModeArg); - - if (err && err[0]) - { - UE_LOG(LogURLabImport, Warning, - TEXT("[MjDcMotorActuator] '%s' mjs_setToDCMotor returned: %s"), - *GetName(), UTF8_TO_TCHAR(err)); - } -#endif - - ApplyRawOverrides(Actuator, Default); -} diff --git a/Plugins/UnrealRoboticsLab/Source/URLab/Private/MuJoCo/Components/Bodies/MjBody.cpp b/Plugins/UnrealRoboticsLab/Source/URLab/Private/MuJoCo/Components/Bodies/MjBody.cpp deleted file mode 100644 index f6a1bba07a..0000000000 --- a/Plugins/UnrealRoboticsLab/Source/URLab/Private/MuJoCo/Components/Bodies/MjBody.cpp +++ /dev/null @@ -1,458 +0,0 @@ -// Copyright (c) 2026 Jonathan Embley-Riches. All rights reserved. -// -// Licensed under the Apache License, Version 2.0 (the "License"); -// you may not use this file except in compliance with the License. -// You may obtain a copy of the License at -// -// http://www.apache.org/licenses/LICENSE-2.0 -// -// Unless required by applicable law or agreed to in writing, software -// distributed under the License is distributed on an "AS IS" BASIS, -// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. -// See the License for the specific language governing permissions and -// limitations under the License. -// -// --- LEGAL DISCLAIMER --- -// UnrealRoboticsLab is an independent software plugin. It is NOT affiliated with, -// endorsed by, or sponsored by Epic Games, Inc. "Unreal" and "Unreal Engine" are -// trademarks or registered trademarks of Epic Games, Inc. in the US and elsewhere. -// -// This plugin incorporates third-party software: MuJoCo (Apache 2.0), -// CoACD (MIT), and libzmq (MPL 2.0). See ThirdPartyNotices.txt for details. - -#include "MuJoCo/Components/Bodies/MjBody.h" -#include "MuJoCo/Components/Bodies/MjFrame.h" -#include "Utils/URLabLogging.h" -#include "MuJoCo/Components/Geometry/MjGeom.h" -#include "MuJoCo/Components/Physics/MjInertial.h" -#include "MuJoCo/Components/Geometry/MjSite.h" -#include "MuJoCo/Components/Joints/MjJoint.h" -#include "MuJoCo/Core/Spec/MjSpecWrapper.h" -#include "MuJoCo/Utils/MjXmlUtils.h" -#include "MuJoCo/Utils/MjUtils.h" -#include "MuJoCo/Utils/MjOrientationUtils.h" -#include "XmlNode.h" -#include "PhysicsEngine/BodySetup.h" - -UMjBody::UMjBody() -{ - PrimaryComponentTick.bCanEverTick = true; - PrimaryComponentTick.bStartWithTickEnabled = false; - - m_BodyView = BodyView(); - -} - -void UMjBody::BeginPlay() -{ - Super::BeginPlay(); -} - -void UMjBody::TickComponent(float DeltaTime, ELevelTick TickType, FActorComponentTickFunction* ThisTickFunction) -{ - Super::TickComponent(DeltaTime, TickType, ThisTickFunction); - - if (m_IsSetup) - { - if (bDrivenByUnreal && m_MocapPos && m_MocapQuat) - { - MjUtils::UEToMjPosition(GetComponentLocation(), m_MocapPos); - MjUtils::UEToMjRotation(GetComponentQuat(), m_MocapQuat); - } - else - { - if (!m_BodyView.xpos || !m_BodyView.xquat) - { - UE_LOG(LogURLabBind, Warning, TEXT("MjBody::TickComponent - Body '%s' has null xpos/xquat (bind failed). Disabling tick."), *GetName()); - m_IsSetup = false; - SetComponentTickEnabled(false); - return; - } - - // 线程安全:xpos/xquat 读取操作无需加锁。 - // 在 x86-64 架构上,对齐的双读操作是原子性的(不会出现撕裂)。 - // 最坏的情况是 1 帧的位置/旋转不同步,这种不同步在视觉上是无法察觉的。 - // 这与 MuJoCo Simulate 自身的线程模型相符。 - FVector MuJoCoWorldPos = MjUtils::MjToUEPosition(m_BodyView.xpos); - FQuat MuJoCoWorldQuat = MjUtils::MjToUERotation(m_BodyView.xquat); - - FVector CorrectedPos = MuJoCoWorldPos; - - // 仅当使用快速转换(Unreal -> MuJoCo 流程枢轴问题)时才应用网格枢轴偏移校正。 - if (bIsQuickConverted) - { - // MuJoCo 的 xpos 位于身体中心,但 UE 网格可能存在偏离中心的枢轴点。 - FVector OffsetVector = MuJoCoWorldQuat.RotateVector(m_MeshPivotOffset); - CorrectedPos = MuJoCoWorldPos - OffsetVector; - } - - SetWorldLocationAndRotation(CorrectedPos, MuJoCoWorldQuat); - } - } -} - - -void UMjBody::Setup(USceneComponent* Parent, mjsBody* ParentBody, FMujocoSpecWrapper* Wrapper) -{ - FTransform TargetTransform; - - bool bIsAttachingToWorld = (ParentBody && mjs_getId(ParentBody->element) == 0); - - if (bIsAttachingToWorld) - { - TargetTransform = GetComponentTransform(); - } - else - { - TargetTransform = GetRelativeTransform(); - } - - FString NameToRegister = MjName.IsEmpty() ? GetName() : MjName; - mjsBody* BodyToAttachTo = Wrapper->CreateBody( - NameToRegister, - ParentBody, - TargetTransform - ); - if(BodyToAttachTo) - { - m_SpecElement = BodyToAttachTo->element; - if (bOverride_Gravcomp) - { - BodyToAttachTo->gravcomp = Gravcomp; - } - - - } - - if (bDrivenByUnreal && BodyToAttachTo) - { - BodyToAttachTo->mocap = 1; - } - - // 睡眠策略 — mjtSleepPolicy removed in MuJoCo 3.x - // if (BodyToAttachTo && SleepPolicy != EMjBodySleepPolicy::Default) - // { - // BodyToAttachTo->sleep = static_cast(static_cast(SleepPolicy)); - // } - - if (bOverride_ChildClassName) - { - - mjs_setString(BodyToAttachTo->childclass, TCHAR_TO_UTF8(*ChildClassName)); - } - m_Root = Parent; - - - TArray DirectChildren = GetAttachChildren(); - - for (USceneComponent* CurrentComponent : DirectChildren) - { - if (UMjBody* MjBodyComp = Cast(CurrentComponent)) - { - UE_LOG(LogURLab, Verbose, TEXT("LEVEL N: Detected MjsBody: %s. Creating external articulated body."), - *MjBodyComp->GetName()); - m_Children.Add(MjBodyComp); - MjBodyComp->Setup(this, BodyToAttachTo, Wrapper); - continue; - } - - if (UMjFrame* MjFrameComp = Cast(CurrentComponent)) - { - UE_LOG(LogURLab, Verbose, TEXT("LEVEL N: Detected MjFrame: %s. Creating coordinate frame."), - *MjFrameComp->GetName()); - MjFrameComp->Setup(this, BodyToAttachTo, Wrapper); - continue; - } - - if (CurrentComponent->GetClass()->ImplementsInterface(UMjSpecElement::StaticClass())) - { - IMjSpecElement* SpecElem = Cast(CurrentComponent); - if (SpecElem) - { - if (UMjGeom* MjGeomComp = Cast(CurrentComponent)) - { - if (BodyToAttachTo && MjGeomComp->Type == EMjGeomType::Mesh) - { - TArray GeomChildren; - MjGeomComp->GetChildrenComponents(true, GeomChildren); - - for(USceneComponent* ChildOfGeom : GeomChildren) - { - if (UStaticMeshComponent* SMC = Cast(ChildOfGeom)) - { - Wrapper->PrepareMeshForMuJoCo(SMC, MjGeomComp->bComplexMeshRequired); - break; - } - } - } - } - - if (UMjComponent* MjComp = Cast(CurrentComponent)) - { - if (!MjComp->bIsDefault) - { - SpecElem->RegisterToSpec(*Wrapper, BodyToAttachTo); - } - } - else - { - SpecElem->RegisterToSpec(*Wrapper, BodyToAttachTo); - } - - m_SpecElements.Emplace(CurrentComponent); - - if (UMjGeom* Geom = Cast(CurrentComponent)) - { - m_Geoms.Add(Geom); - } - else if (UMjJoint* Joint = Cast(CurrentComponent)) - { - m_Joints.Add(Joint); - } - else if (UMjSensor* Sensor = Cast(CurrentComponent)) - { - m_Sensors.Add(Sensor); - } - else if (UMjActuator* Actuator = Cast(CurrentComponent)) - { - m_Actuators.Add(Actuator); - } - } - } - } - - - -} - -void UMjBody::ImportFromXml(const FXmlNode* Node) -{ - FMjCompilerSettings DefaultSettings; - ImportFromXml(Node, DefaultSettings); -} - -void UMjBody::ImportFromXml(const FXmlNode* Node, const FMjCompilerSettings& CompilerSettings) -{ - if (!Node) return; - - FString PosStr = Node->GetAttribute(TEXT("pos")); - if (!PosStr.IsEmpty()) - { - FVector MjPos = MjXmlUtils::ParseVector(PosStr); - SetRelativeLocation(MjUtils::MjToUEPosition(&MjPos.X)); - } - - double MjQuat[4]; - if (MjOrientationUtils::OrientationToMjQuat(Node, CompilerSettings, MjQuat)) - { - SetRelativeRotation(MjUtils::MjToUERotation(MjQuat)); - } - - MjXmlUtils::ReadAttrFloat(Node, TEXT("gravcomp"), Gravcomp, bOverride_Gravcomp); - - if (MjXmlUtils::ReadAttrString(Node, TEXT("childclass"), ChildClassName)) - bOverride_ChildClassName = true; - - // mocap="true" 将此身体标记为由外部模拟驱动的运动学参考。 - bool bMocap = false; - bool bDummyMocap = false; - if (MjXmlUtils::ReadAttrBool(Node, TEXT("mocap"), bMocap, bDummyMocap) && bMocap) - bDrivenByUnreal = true; - - // 睡眠策略 (MuJoCo 3.4+): "never" | "allowed" | "init" - FString SleepAttr; - if (MjXmlUtils::ReadAttrString(Node, TEXT("sleep"), SleepAttr)) - { - SleepAttr = SleepAttr.ToLower(); - if (SleepAttr == TEXT("never")) SleepPolicy = EMjBodySleepPolicy::Never; - else if (SleepAttr == TEXT("allowed")) SleepPolicy = EMjBodySleepPolicy::Allowed; - else if (SleepAttr == TEXT("init")) SleepPolicy = EMjBodySleepPolicy::InitAsleep; - } - - // 名称属性 → 存储在 MjName 中以便显式覆盖 - MjXmlUtils::ReadAttrString(Node, TEXT("name"), MjName); -} - -void UMjBody::Bind(mjModel* Model, mjData* Data, const FString& Prefix) -{ - Super::Bind(Model, Data, Prefix); - - if (Model && Data) - { - m_BodyView = BindToView(Prefix); - - if (m_BodyView.id != -1) - { - m_ID = m_BodyView.id; - m_IsSetup = true; - SetComponentTickEnabled(true); - } - else - { - UE_LOG(LogURLabBind, Warning, TEXT("MjBody::Bind() - FAILED to find body '%s'"), *GetName()); - m_IsSetup = false; - SetComponentTickEnabled(false); - } - - if (bDrivenByUnreal && m_ID >= 0) - { - int mocapid = Model->body_mocapid[m_ID]; - if (mocapid >= 0) - { - m_MocapPos = Data->mocap_pos + 3 * mocapid; - m_MocapQuat = Data->mocap_quat + 4 * mocapid; - } - } - } - - TArray AllChildren; - GetChildrenComponents(true, AllChildren); - for (USceneComponent* Child : AllChildren) - { - if (UStaticMeshComponent* SMC = Cast(Child)) - { - UStaticMesh* Mesh = SMC->GetStaticMesh(); - if (Mesh) - { - UBodySetup* BodySetup = Mesh->BodySetup; - if (BodySetup) - { - FVector LocalCenter = BodySetup->AggGeom.CalcAABB(FTransform::Identity).GetCenter(); - m_MeshPivotOffset = LocalCenter; - break; - } - } - } - } - - // 子组件绑定由 PostSetup 对所有组件进行扁平化迭代来处理。 - // 如果在这里也调用 Bind(),会导致每个子组件被绑定两次。 - // for (const auto& SpecElem : m_SpecElements) - // { - // if (SpecElem) - // { - // SpecElem->Bind(Model, Data, Prefix); - // } - // } -} - -BodyView UMjBody::GetBodyView() const -{ - return m_BodyView; -} - -FVector UMjBody::GetWorldPosition() const -{ - if (m_BodyView.id < 0 || !m_BodyView.xpos) return FVector::ZeroVector; - return MjUtils::MjToUEPosition(m_BodyView.xpos); -} - -FQuat UMjBody::GetWorldRotation() const -{ - if (m_BodyView.id < 0 || !m_BodyView.xquat) return FQuat::Identity; - return MjUtils::MjToUERotation(m_BodyView.xquat); -} - -FMuJoCoSpatialVelocity UMjBody::GetSpatialVelocity() const -{ - FMuJoCoSpatialVelocity Result; - if (m_BodyView.id < 0 || !m_BodyView.cvel) return Result; - - // MuJoCo cvel: [ang_x, ang_y, ang_z, lin_x, lin_y, lin_z] (MuJoCo 帧, m/s 和 rad/s) - // Unreal 帧: X -> X, Y -> -Y, Z -> Z - - // 线速度 (m/s -> cm/s) - Result.Linear.X = (float)m_BodyView.cvel[3] * 100.0f; - Result.Linear.Y = -(float)m_BodyView.cvel[4] * 100.0f; - Result.Linear.Z = (float)m_BodyView.cvel[5] * 100.0f; - - // 角速度 (rad/s -> deg/s) - Result.Angular.X = FMath::RadiansToDegrees((float)m_BodyView.cvel[0]); - Result.Angular.Y = -FMath::RadiansToDegrees((float)m_BodyView.cvel[1]); - Result.Angular.Z = FMath::RadiansToDegrees((float)m_BodyView.cvel[2]); - - return Result; -} - -void UMjBody::ApplyForce(FVector Force, FVector Torque) -{ - if (m_BodyView.id < 0 || !m_BodyView.xfrc_applied) return; - // xfrc_applied 布局: 在 Mujoco 帧中 [torque_x, torque_y, torque_z, force_x, force_y, force_z] - // 转换 UE (厘米, Y 轴翻转) -> MuJoCo (米,右手系) - const float InvScale = 0.01f; // cm -> m - mjtNum* xfrc = m_BodyView.xfrc_applied; - // 力矩:UE X -> Mj X, UE Y -> -Mj Y, UE Z -> Mj Z - xfrc[0] = (mjtNum)(Torque.X); - xfrc[1] = (mjtNum)(-Torque.Y); - xfrc[2] = (mjtNum)(Torque.Z); - // 力:相同的约定 - xfrc[3] = (mjtNum)(Force.X * InvScale); - xfrc[4] = (mjtNum)(-Force.Y * InvScale); - xfrc[5] = (mjtNum)(Force.Z * InvScale); -} - -void UMjBody::ClearForce() -{ - if (m_BodyView.id < 0 || !m_BodyView.xfrc_applied) return; - for (int i = 0; i < 6; ++i) - m_BodyView.xfrc_applied[i] = 0.0; -} - -bool UMjBody::IsAwake() const -{ - // body_awake removed in MuJoCo 3.x — default to awake=true - if (m_BodyView.id < 0 || !m_BodyView._d) return true; // unbound → treat as awake - return true; // was: m_BodyView._d->body_awake[m_BodyView.id] != 0; -} - -void UMjBody::Wake() -{ - if (m_BodyView.id < 0 || !m_BodyView._d || !m_BodyView._m) return; - -// m_BodyView._d->body_awake[m_BodyView.id] = 1; // mjS_AWAKE - // body_awake removed in MuJoCo 3.x - // m_BodyView._d->body_awake[m_BodyView.id] = 1; -// // 同时唤醒运动学树,以便物理步传播唤醒 -// int32 TreeId = m_BodyView._m->body_treeid[m_BodyView.id]; -// if (TreeId >= 0 && TreeId < m_BodyView._m->ntree) -// { -// m_BodyView._d->tree_asleep[TreeId] = -1; // <0 → awake -// m_BodyView._d->tree_awake[TreeId] = 1; -// } -} - -void UMjBody::Sleep() -{ - if (m_BodyView.id < 0 || !m_BodyView._d || !m_BodyView._m) return; - -// m_BodyView._d->body_awake[m_BodyView.id] = 0; // mjS_ASLEEP - - // 同时将运动树标记为休眠状态。 - int32 TreeId = m_BodyView._m->body_treeid[m_BodyView.id]; - if (TreeId >= 0 && TreeId < m_BodyView._m->ntree) - { -// // tree_asleep >= 0 表示该树处于睡眠状态(值为睡眠周期中的索引)。 -// if (m_BodyView._d->tree_asleep[TreeId] < 0) -// m_BodyView._d->tree_asleep[TreeId] = 0; -// m_BodyView._d->tree_awake[TreeId] = 0; - } -} - -void UMjBody::RegisterToSpec(FMujocoSpecWrapper& Wrapper, mjsBody* ParentBody) -{ - // MjBody 通过父 MjBody 中的 Setup() 方法递归处理。 - // 此接口方法是为了满足 IMjSpecElement 规范而提供的,但在标准流程中并不使用。 - // 如果显式调用此方法,我们会发出警告并尝试委托给 Setup 方法,尽管这种做法并不常见。 - UE_LOG(LogURLab, Warning, TEXT("MjBody::RegisterToSpec called for %s. This path is liable to double-create bodies if not careful. Prefer Setup()."), *GetName()); - if (ParentBody && !m_IsSetup) - { - Setup(GetAttachParent(), ParentBody, &Wrapper); - } -} - -#if WITH_EDITOR -TArray UMjBody::GetChildClassOptions() const -{ - return UMjComponent::GetSiblingComponentOptions(this, UMjDefault::StaticClass(), true); -} -#endif diff --git a/Plugins/UnrealRoboticsLab/Source/URLab/Private/MuJoCo/Components/Joints/MjJoint.cpp b/Plugins/UnrealRoboticsLab/Source/URLab/Private/MuJoCo/Components/Joints/MjJoint.cpp deleted file mode 100644 index 7a6808c4ea..0000000000 --- a/Plugins/UnrealRoboticsLab/Source/URLab/Private/MuJoCo/Components/Joints/MjJoint.cpp +++ /dev/null @@ -1,530 +0,0 @@ -// Copyright (c) 2026 Jonathan Embley-Riches. All rights reserved. -// -// Licensed under the Apache License, Version 2.0 (the "License"); -// you may not use this file except in compliance with the License. -// You may obtain a copy of the License at -// -// http://www.apache.org/licenses/LICENSE-2.0 -// -// Unless required by applicable law or agreed to in writing, software -// distributed under the License is distributed on an "AS IS" BASIS, -// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. -// See the License for the specific language governing permissions and -// limitations under the License. -// -// --- LEGAL DISCLAIMER --- -// UnrealRoboticsLab is an independent software plugin. It is NOT affiliated with, -// endorsed by, or sponsored by Epic Games, Inc. "Unreal" and "Unreal Engine" are -// trademarks or registered trademarks of Epic Games, Inc. in the US and elsewhere. -// -// This plugin incorporates third-party software: MuJoCo (Apache 2.0), -// CoACD (MIT), and libzmq (MPL 2.0). See ThirdPartyNotices.txt for details. - -#include "MuJoCo/Components/Joints/MjJoint.h" -#include "MuJoCo/Utils/MjXmlUtils.h" -#include "MuJoCo/Utils/MjUtils.h" -#include "MuJoCo/Utils/MjOrientationUtils.h" -#include "XmlNode.h" -#include "Utils/URLabLogging.h" -#include "MuJoCo/Core/Spec/MjSpecWrapper.h" -#include "MuJoCo/Components/Defaults/MjDefault.h" - -UMjJoint::UMjJoint() -{ - PrimaryComponentTick.bCanEverTick = true; - // MuJoCo builtin default axis is (0, 0, 1) — matches header initializer - Type = EMjJointType::Hinge; -} - - -void UMjJoint::ImportFromXml(const FXmlNode* Node, const FMjCompilerSettings& CompilerSettings) -{ - if (!Node) return; - - // Type - FString TypeStr = Node->GetAttribute(TEXT("type")); - bOverride_Type = !TypeStr.IsEmpty(); - if (bOverride_Type) - { - if (TypeStr == "free") Type = EMjJointType::Free; - else if (TypeStr == "ball") Type = EMjJointType::Ball; - else if (TypeStr == "slide") Type = EMjJointType::Slide; - else if (TypeStr == "hinge") Type = EMjJointType::Hinge; - } - - // Class Name - MjXmlUtils::ReadAttrString(Node, TEXT("class"), MjClassName); - - // Axis — convert from MuJoCo to UE direction-vector convention (negate Y, no scale) - FString AxisStr; - if (MjXmlUtils::ReadAttrString(Node, TEXT("axis"), AxisStr)) - { - bOverride_Axis = true; - FVector RawAxis = MjXmlUtils::ParseVector(AxisStr); - Axis = FVector(RawAxis.X, -RawAxis.Y, RawAxis.Z); - } - - MjXmlUtils::ReadAttrFloat(Node, TEXT("ref"), Ref, bOverride_Ref); - - // Slide joints: convert ref from meters to cm (UE units) - if (bOverride_Ref && Type == EMjJointType::Slide) - { - Ref *= 100.0f; - } - - FString PosStr; - if (MjXmlUtils::ReadAttrString(Node, TEXT("pos"), PosStr)) - { - FVector RawPos = MjXmlUtils::ParseVector(PosStr); - double p[3] = { (double)RawPos.X, (double)RawPos.Y, (double)RawPos.Z }; - SetRelativeLocation(MjUtils::MjToUEPosition(p)); - } - - MjXmlUtils::ReadAttrFloatArray(Node, TEXT("stiffness"), Stiffness, bOverride_Stiffness); - MjXmlUtils::ReadAttrFloatArray(Node, TEXT("damping"), Damping, bOverride_Damping); - MjXmlUtils::ReadAttrFloat(Node, TEXT("armature"), Armature, bOverride_Armature); - MjXmlUtils::ReadAttrFloat(Node, TEXT("frictionloss"), FrictionLoss, bOverride_FrictionLoss); - MjXmlUtils::ReadAttrFloat(Node, TEXT("springref"), SpringRef, bOverride_SpringRef); - - // springdamper="stiffness damping" — convenience attribute that sets both at once - { - TArray SD; - bool bHasSD = false; - MjXmlUtils::ReadAttrFloatArray(Node, TEXT("springdamper"), SD, bHasSD); - if (bHasSD && SD.Num() >= 2) - { - Stiffness = {SD[0]}; - bOverride_Stiffness = true; - Damping = {SD[1]}; - bOverride_Damping = true; - } - } - MjXmlUtils::ReadAttrBool(Node, TEXT("limited"), bLimited, bOverride_Limited); - - // Note: autolimits (limited inferred from range) is handled by the MuJoCo spec - // compiler (mjLIMITED_AUTO default). We do not replicate that logic here. - MjXmlUtils::ReadAttrFloatArray(Node, TEXT("range"), Range, bOverride_Range); - - if (bOverride_Range) - { - if (Type == EMjJointType::Slide) - { - // Slide joints: MJCF stores metres, UE units are cm. - for (float& V : Range) V *= 100.0f; - } - else if (CompilerSettings.bAngleInDegrees) - { - // Hinge/ball joint ranges in MJCF are in the compiler angle units. - // Our spec is compiled with radians, so convert here to match. - for (float& V : Range) V = FMath::DegreesToRadians(V); - } - } - - MjXmlUtils::ReadAttrFloatArray(Node, TEXT("actuatorfrcrange"), ActuatorFrcRange, bOverride_ActuatorFrcRange); - MjXmlUtils::ReadAttrFloat(Node, TEXT("margin"), Margin, bOverride_Margin); - - // Solvers - MjXmlUtils::ReadAttrFloatArray(Node, TEXT("solreflimit"), SolRefLimit, bOverride_SolRefLimit); - MjXmlUtils::ReadAttrFloatArray(Node, TEXT("solimplimit"), SolImpLimit, bOverride_SolImpLimit); - MjXmlUtils::ReadAttrFloatArray(Node, TEXT("solreffriction"), SolRefFriction, bOverride_SolRefFriction); - MjXmlUtils::ReadAttrFloatArray(Node, TEXT("solimpfriction"), SolImpFriction, bOverride_SolImpFriction); - - // Fallback: generic solref/solimp → limit-specific if not already set - if (!bOverride_SolRefLimit) - MjXmlUtils::ReadAttrFloatArray(Node, TEXT("solref"), SolRefLimit, bOverride_SolRefLimit); - if (!bOverride_SolImpLimit) - MjXmlUtils::ReadAttrFloatArray(Node, TEXT("solimp"), SolImpLimit, bOverride_SolImpLimit); - - MjXmlUtils::ReadAttrInt(Node, TEXT("group"), Group, bOverride_Group); - - UE_LOG(LogURLabImport, Log, TEXT("[MjJoint XML Import] '%s' (MjName: '%s') -> Type: %s (Overridden: %s), Stiffness: %s (%f)"), - *GetName(), *MjName, - *StaticEnum()->GetNameStringByValue((int64)Type), bOverride_Type ? TEXT("True") : TEXT("False"), - bOverride_Stiffness ? TEXT("Set") : TEXT("Inherited"), Stiffness.Num() > 0 ? Stiffness[0] : 0.0f); -} - -void UMjJoint::ExportTo(mjsJoint* Joint, mjsDefault* Default) -{ - if (!Joint) return; - - if (bOverride_Type) - { - switch (Type) - { - case EMjJointType::Free: Joint->type = mjJNT_FREE; break; - case EMjJointType::Ball: Joint->type = mjJNT_BALL; break; - case EMjJointType::Slide: Joint->type = mjJNT_SLIDE; break; - case EMjJointType::Hinge: Joint->type = mjJNT_HINGE; break; - } - } - - FVector _pos = GetRelativeLocation(); - if (!_pos.IsZero()) MjUtils::UEToMjPosition(_pos, Joint->pos); - - if (bOverride_Axis) - { - // Convert UE direction vector back to MuJoCo convention (negate Y, no scale) - Joint->axis[0] = Axis.X; - Joint->axis[1] = -Axis.Y; - Joint->axis[2] = Axis.Z; - } - - // Slide joints: convert ref from cm (UE) back to meters (MuJoCo) - if (bOverride_Ref) - { - Joint->ref = (Type == EMjJointType::Slide) ? Ref / 100.0f : Ref; - } - - if (bOverride_Stiffness) - { - if (Stiffness.Num() > 0) - Joint->stiffness = Stiffness[0]; - } - if (bOverride_Damping) - { - if (Damping.Num() > 0) - Joint->damping = Damping[0]; - } - if (bOverride_Armature) Joint->armature = Armature; - if (bOverride_FrictionLoss) Joint->frictionloss = FrictionLoss; - if (bOverride_SpringRef) Joint->springref = SpringRef; - - if (bOverride_Limited) Joint->limited = bLimited ? mjLIMITED_TRUE : mjLIMITED_FALSE; - if (bOverride_Range) - { - // Slide joints: convert range from cm (UE) back to meters (MuJoCo) - float Scale = (Type == EMjJointType::Slide) ? 1.0f / 100.0f : 1.0f; - for (int i = 0; i < Range.Num() && i < 2; ++i) - { - Joint->range[i] = Range[i] * Scale; - } - } - if (bOverride_ActuatorFrcRange) - { - for (int i = 0; i < ActuatorFrcRange.Num() && i < 2; ++i) - { - Joint->actfrcrange[i] = ActuatorFrcRange[i]; - } - } - - if (bOverride_Margin) Joint->margin = Margin; - - if (bOverride_SolRefLimit) - { - for (int i = 0; i < SolRefLimit.Num() && i < 2; ++i) - { - Joint->solref_limit[i] = SolRefLimit[i]; - } - } - - if (bOverride_SolImpLimit) - { - for (int i = 0; i < SolImpLimit.Num() && i < 5; ++i) - { - Joint->solimp_limit[i] = SolImpLimit[i]; - } - } - - if (bOverride_SolRefFriction) - { - for (int i = 0; i < SolRefFriction.Num() && i < 2; ++i) - { - Joint->solref_friction[i] = SolRefFriction[i]; - } - } - - if (bOverride_SolImpFriction) - { - for (int i = 0; i < SolImpFriction.Num() && i < 5; ++i) - { - Joint->solimp_friction[i] = SolImpFriction[i]; - } - } - - if (bOverride_Group) Joint->group = Group; -} - -void UMjJoint::Bind(mjModel* Model, mjData* Data, const FString& Prefix) -{ - Super::Bind(Model, Data, Prefix); - m_JointView = BindToView(Prefix); - if (m_JointView.id != -1) m_ID = m_JointView.id; - else UE_LOG(LogURLabBind, Warning, TEXT("Failed to bind Joint %s"), *GetName()); -} - -float UMjJoint::GetPosition() const -{ - if (m_JointView._d && m_JointView.qpos) - { - return (float)m_JointView.qpos[0]; - } - return 0.0f; -} - -void UMjJoint::SetPosition(float NewPosition) -{ - if (m_JointView._d && m_JointView.qpos) - { - m_JointView.qpos[0] = (mjtNum)NewPosition; - } -} - -float UMjJoint::GetVelocity() const -{ - if (m_JointView._d && m_JointView.qvel) - { - return (float)m_JointView.qvel[0]; - } - return 0.0f; -} - -void UMjJoint::SetVelocity(float NewVelocity) -{ - if (m_JointView._d && m_JointView.qvel) - { - m_JointView.qvel[0] = (mjtNum)NewVelocity; - } -} - -float UMjJoint::GetAcceleration() const -{ - if (m_JointView._d && m_JointView.qacc) - { - return (float)m_JointView.qacc[0]; - } - return 0.0f; -} - -FVector2D UMjJoint::GetJointRange() const -{ - if (m_JointView.id < 0 || !m_JointView.range) return FVector2D::ZeroVector; - return FVector2D((float)m_JointView.range[0], (float)m_JointView.range[1]); -} - -FVector UMjJoint::GetWorldAnchor() const -{ - if (m_JointView._d && m_JointView.xanchor) - { - return MjUtils::MjToUEPosition(m_JointView.xanchor); - } - return FVector::ZeroVector; -} - -FVector UMjJoint::GetWorldAxis() const -{ - if (m_JointView._d && m_JointView.xaxis) - { - return FVector( - (float)m_JointView.xaxis[0], - -(float)m_JointView.xaxis[1], - (float)m_JointView.xaxis[2]); - } - return FVector::ForwardVector; -} - -FString UMjJoint::GetMjName() const -{ - if (m_JointView.id < 0 || !m_JointView.name) return FString(); - return MjUtils::MjToString(m_JointView.name); -} - -FMuJoCoJointState UMjJoint::GetJointState() const -{ - FMuJoCoJointState State; - if (m_JointView._d) - { - if (m_JointView.qpos) State.Position = (float)m_JointView.qpos[0]; - if (m_JointView.qvel) State.Velocity = (float)m_JointView.qvel[0]; - if (m_JointView.qacc) State.Acceleration = (float)m_JointView.qacc[0]; - } - return State; -} - -// --- Editor-time resolved accessors --- - -EMjJointType UMjJoint::GetResolvedType() const -{ - // Runtime: use compiled model - if (m_JointView.id >= 0) - { - switch (m_JointView.type) - { - case mjJNT_FREE: return EMjJointType::Free; - case mjJNT_BALL: return EMjJointType::Ball; - case mjJNT_SLIDE: return EMjJointType::Slide; - case mjJNT_HINGE: return EMjJointType::Hinge; - default: return EMjJointType::Hinge; - } - } - - // Editor: local override wins - if (bOverride_Type) return Type; - - // Walk default chain - if (UMjDefault* Def = FindEditorDefault()) - { - TSet Visited; - UMjDefault* Cur = Def; - while (Cur && !Visited.Contains(Cur->ClassName)) - { - Visited.Add(Cur->ClassName); - if (UMjJoint* J = Cur->FindChildOfType()) - { - if (J->bOverride_Type) return J->Type; - } - Cur = Cur->ParentClassName.IsEmpty() ? nullptr - : FindDefaultByClassName(GetOwner(), Cur->ParentClassName); - } - } - - // Fallback: use local Type value even without bOverride_Type. - // This handles user-created joints where the type was set in the - // details panel but the override toggle wasn't explicitly checked. - // The default enum value is Hinge (MuJoCo builtin), so this is - // safe — it returns the user's setting or the MuJoCo default. - return Type; -} - -FVector UMjJoint::GetResolvedAxis() const -{ - // Runtime: use compiled world-space axis - if (m_JointView.id >= 0 && m_JointView.xaxis) - { - return FVector( - (float)m_JointView.xaxis[0], - -(float)m_JointView.xaxis[1], - (float)m_JointView.xaxis[2]); - } - - // Editor: local override wins - if (bOverride_Axis) return Axis; - - // Walk default chain - if (UMjDefault* Def = FindEditorDefault()) - { - TSet Visited; - UMjDefault* Cur = Def; - while (Cur && !Visited.Contains(Cur->ClassName)) - { - Visited.Add(Cur->ClassName); - if (UMjJoint* J = Cur->FindChildOfType()) - { - if (J->bOverride_Axis) return J->Axis; - } - Cur = Cur->ParentClassName.IsEmpty() ? nullptr - : FindDefaultByClassName(GetOwner(), Cur->ParentClassName); - } - } - - return FVector(0, 0, 1); // MuJoCo builtin (Z-up) -} - -FVector2D UMjJoint::GetResolvedRange() const -{ - // Runtime: use compiled model - if (m_JointView.id >= 0 && m_JointView.range) - { - return FVector2D((float)m_JointView.range[0], (float)m_JointView.range[1]); - } - - // Editor: local override wins - if (bOverride_Range && Range.Num() >= 2) - { - return FVector2D(Range[0], Range[1]); - } - - // Walk default chain - if (UMjDefault* Def = FindEditorDefault()) - { - TSet Visited; - UMjDefault* Cur = Def; - while (Cur && !Visited.Contains(Cur->ClassName)) - { - Visited.Add(Cur->ClassName); - if (UMjJoint* J = Cur->FindChildOfType()) - { - if (J->bOverride_Range && J->Range.Num() >= 2) - { - return FVector2D(J->Range[0], J->Range[1]); - } - } - Cur = Cur->ParentClassName.IsEmpty() ? nullptr - : FindDefaultByClassName(GetOwner(), Cur->ParentClassName); - } - } - - return FVector2D(0, 0); // MuJoCo builtin -} - -bool UMjJoint::GetResolvedLimited() const -{ - // Runtime: use compiled model range - if (m_JointView.id >= 0 && m_JointView.range) - { - // MuJoCo sets range to (0,0) when unlimited - return m_JointView.range[0] != 0.0 || m_JointView.range[1] != 0.0; - } - - // Editor: local override wins - if (bOverride_Limited) return bLimited; - - // Walk default chain - if (UMjDefault* Def = FindEditorDefault()) - { - TSet Visited; - UMjDefault* Cur = Def; - while (Cur && !Visited.Contains(Cur->ClassName)) - { - Visited.Add(Cur->ClassName); - if (UMjJoint* J = Cur->FindChildOfType()) - { - if (J->bOverride_Limited) return J->bLimited; - } - Cur = Cur->ParentClassName.IsEmpty() ? nullptr - : FindDefaultByClassName(GetOwner(), Cur->ParentClassName); - } - } - - return false; // MuJoCo builtin -} - -void UMjJoint::RegisterToSpec(FMujocoSpecWrapper& Wrapper, mjsBody* ParentBody) -{ - if (!ParentBody) return; - - mjsDefault* effectiveDefault = ResolveDefault(Wrapper.Spec, MjClassName); - - mjsJoint* Jnt = mjs_addJoint(ParentBody, effectiveDefault); - m_SpecElement = Jnt->element; - - FString NameToRegister = MjName.IsEmpty() ? GetName() : MjName; - mjs_setName(Jnt->element, TCHAR_TO_UTF8(*NameToRegister)); - - ExportTo(Jnt, effectiveDefault); -} - -void UMjJoint::BuildBinaryPayload(FBufferArchive& OutBuffer) const -{ - int32 JointID = m_ID; - OutBuffer << JointID; - - float Pos = GetPosition(); - float Vel = GetVelocity(); - float Acc = GetAcceleration(); - - OutBuffer << Pos; - OutBuffer << Vel; - OutBuffer << Acc; -} - -FString UMjJoint::GetTelemetryTopicName() const -{ - return FString::Printf(TEXT("joint/%s"), *GetName()); -} - -#if WITH_EDITOR -TArray UMjJoint::GetDefaultClassOptions() const -{ - return GetSiblingComponentOptions(this, UMjDefault::StaticClass(), true); -} -#endif diff --git a/Plugins/UnrealRoboticsLab/Source/URLab/Private/MuJoCo/Components/Tendons/MjTendon.cpp b/Plugins/UnrealRoboticsLab/Source/URLab/Private/MuJoCo/Components/Tendons/MjTendon.cpp deleted file mode 100644 index 29866d0788..0000000000 --- a/Plugins/UnrealRoboticsLab/Source/URLab/Private/MuJoCo/Components/Tendons/MjTendon.cpp +++ /dev/null @@ -1,309 +0,0 @@ -// Copyright (c) 2026 Jonathan Embley-Riches. All rights reserved. -// -// Licensed under the Apache License, Version 2.0 (the "License"); -// you may not use this file except in compliance with the License. -// You may obtain a copy of the License at -// -// http://www.apache.org/licenses/LICENSE-2.0 -// -// Unless required by applicable law or agreed to in writing, software -// distributed under the License is distributed on an "AS IS" BASIS, -// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. -// See the License for the specific language governing permissions and -// limitations under the License. -// -// --- LEGAL DISCLAIMER --- -// UnrealRoboticsLab is an independent software plugin. It is NOT affiliated with, -// endorsed by, or sponsored by Epic Games, Inc. "Unreal" and "Unreal Engine" are -// trademarks or registered trademarks of Epic Games, Inc. in the US and elsewhere. -// -// This plugin incorporates third-party software: MuJoCo (Apache 2.0), -// CoACD (MIT), and libzmq (MPL 2.0). See ThirdPartyNotices.txt for details. - -#include "MuJoCo/Components/Tendons/MjTendon.h" -#include "mujoco/mujoco.h" -#include "mujoco/mjspec.h" -#include "MuJoCo/Components/Defaults/MujocoDefaults.h" -#include "MuJoCo/Core/Spec/MjSpecWrapper.h" -#include "XmlNode.h" -#include "Utils/URLabLogging.h" -#include "MuJoCo/Utils/MjXmlUtils.h" - - -UMjTendon::UMjTendon() -{ - PrimaryComponentTick.bCanEverTick = false; -} - -void UMjTendon::BeginPlay() -{ - Super::BeginPlay(); -} - -void UMjTendon::ImportFromXml(const FXmlNode* Node) -{ - if (!Node) return; - - // Class name - MjXmlUtils::ReadAttrString(Node, TEXT("class"), MjClassName); - - // --- Physics --- - MjXmlUtils::ReadAttrFloatArray(Node, TEXT("stiffness"), Stiffness, bOverride_Stiffness); - MjXmlUtils::ReadAttrFloatArray(Node, TEXT("springlength"), SpringLength, bOverride_SpringLength); - MjXmlUtils::ReadAttrFloatArray(Node, TEXT("damping"), Damping, bOverride_Damping); - MjXmlUtils::ReadAttrFloat(Node, TEXT("frictionloss"), FrictionLoss, bOverride_FrictionLoss); - MjXmlUtils::ReadAttrFloat(Node, TEXT("armature"), Armature, bOverride_Armature); - - // --- Limits --- - MjXmlUtils::ReadAttrBool(Node, TEXT("limited"), bLimited, bOverride_Limited); - MjXmlUtils::ReadAttrFloatArray(Node, TEXT("range"), Range, bOverride_Range); - MjXmlUtils::ReadAttrFloat(Node, TEXT("margin"), Margin, bOverride_Margin); - MjXmlUtils::ReadAttrBool(Node, TEXT("actfrclimited"), bActFrcLimited, bOverride_ActFrcLimited); - MjXmlUtils::ReadAttrFloatArray(Node, TEXT("actfrcrange"), ActFrcRange, bOverride_ActFrcRange); - - // --- Solver --- - MjXmlUtils::ReadAttrFloatArray(Node, TEXT("solreflimit"), SolRefLimit, bOverride_SolRefLimit); - MjXmlUtils::ReadAttrFloatArray(Node, TEXT("solimplimit"), SolImpLimit, bOverride_SolImpLimit); - MjXmlUtils::ReadAttrFloatArray(Node, TEXT("solreffriction"), SolRefFriction, bOverride_SolRefFriction); - MjXmlUtils::ReadAttrFloatArray(Node, TEXT("solimpfriction"), SolImpFriction, bOverride_SolImpFriction); - - // --- Visual --- - MjXmlUtils::ReadAttrFloat(Node, TEXT("width"), Width, bOverride_Width); - TArray RgbaParts; - if (MjXmlUtils::ReadAttrFloatArray(Node, TEXT("rgba"), RgbaParts, bOverride_Rgba)) - { - if (RgbaParts.Num() >= 4) - Rgba = FLinearColor(RgbaParts[0], RgbaParts[1], RgbaParts[2], RgbaParts[3]); - } - MjXmlUtils::ReadAttrInt(Node, TEXT("group"), Group, bOverride_Group); - - // --- Wrap entries (child nodes of the fixed/spatial element) --- - Wraps.Empty(); - for (const FXmlNode* Child : Node->GetChildrenNodes()) - { - if (!Child) continue; - FString ChildTag = Child->GetTag(); - - FMjTendonWrap Wrap; - - if (ChildTag.Equals(TEXT("joint"), ESearchCase::IgnoreCase)) - { - Wrap.Type = EMjTendonWrapType::Joint; - Wrap.TargetName = Child->GetAttribute(TEXT("joint")); - FString CoefStr = Child->GetAttribute(TEXT("coef")); - if (!CoefStr.IsEmpty()) Wrap.Coef = FCString::Atof(*CoefStr); - } - else if (ChildTag.Equals(TEXT("site"), ESearchCase::IgnoreCase)) - { - Wrap.Type = EMjTendonWrapType::Site; - Wrap.TargetName = Child->GetAttribute(TEXT("site")); - } - else if (ChildTag.Equals(TEXT("geom"), ESearchCase::IgnoreCase)) - { - Wrap.Type = EMjTendonWrapType::Geom; - Wrap.TargetName = Child->GetAttribute(TEXT("geom")); - Wrap.SideSite = Child->GetAttribute(TEXT("sidesite")); - } - else if (ChildTag.Equals(TEXT("pulley"), ESearchCase::IgnoreCase)) - { - Wrap.Type = EMjTendonWrapType::Pulley; - FString DivisorStr = Child->GetAttribute(TEXT("divisor")); - if (!DivisorStr.IsEmpty()) Wrap.Divisor = FCString::Atof(*DivisorStr); - } - else - { - UE_LOG(LogURLabImport, Warning, TEXT("[MjTendon XML Import] Unknown wrap tag: '%s'"), *ChildTag); - continue; - } - - Wraps.Add(Wrap); - } - - UE_LOG(LogURLabImport, Log, TEXT("[MjTendon XML Import] '%s' -> %d wrap entries"), *GetName(), Wraps.Num()); -} - -void UMjTendon::ExportTo(mjsTendon* Tendon, mjsDefault* def) -{ - if (!Tendon) return; - - // --- Physics properties --- - if (bOverride_Stiffness) - { - if (Stiffness.Num() > 0) - Tendon->stiffness = Stiffness[0]; - } - if (bOverride_SpringLength) - { - for (int i = 0; i < SpringLength.Num() && i < 2; ++i) - Tendon->springlength[i] = SpringLength[i]; - } - if (bOverride_Damping) - { - if (Damping.Num() > 0) - Tendon->damping = Damping[0]; - } - if (bOverride_FrictionLoss) Tendon->frictionloss = FrictionLoss; - if (bOverride_Armature) Tendon->armature = Armature; - - // --- Limits --- - if (bOverride_Limited) Tendon->limited = bLimited ? mjLIMITED_TRUE : mjLIMITED_FALSE; - if (bOverride_Range) - { - for (int i = 0; i < Range.Num() && i < 2; ++i) - Tendon->range[i] = Range[i]; - } - if (bOverride_Margin) Tendon->margin = Margin; - - if (bOverride_ActFrcLimited) Tendon->actfrclimited = bActFrcLimited ? mjLIMITED_TRUE : mjLIMITED_FALSE; - if (bOverride_ActFrcRange) - { - for (int i = 0; i < ActFrcRange.Num() && i < 2; ++i) - Tendon->actfrcrange[i] = ActFrcRange[i]; - } - - // --- Solver --- - if (bOverride_SolRefLimit) - { - for (int i = 0; i < SolRefLimit.Num() && i < 2; ++i) - Tendon->solref_limit[i] = SolRefLimit[i]; - } - if (bOverride_SolImpLimit) - { - for (int i = 0; i < SolImpLimit.Num() && i < 5; ++i) - Tendon->solimp_limit[i] = SolImpLimit[i]; - } - if (bOverride_SolRefFriction) - { - for (int i = 0; i < SolRefFriction.Num() && i < 2; ++i) - Tendon->solref_friction[i] = SolRefFriction[i]; - } - if (bOverride_SolImpFriction) - { - for (int i = 0; i < SolImpFriction.Num() && i < 5; ++i) - Tendon->solimp_friction[i] = SolImpFriction[i]; - } - - // --- Visuals --- - if (bOverride_Width) Tendon->width = Width; - if (bOverride_Rgba) - { - Tendon->rgba[0] = Rgba.R; - Tendon->rgba[1] = Rgba.G; - Tendon->rgba[2] = Rgba.B; - Tendon->rgba[3] = Rgba.A; - } - if (bOverride_Group) Tendon->group = Group; -} - -void UMjTendon::RegisterToSpec(FMujocoSpecWrapper& Wrapper, mjsBody* ParentBody) -{ - mjsDefault* effectiveDefault = ResolveDefault(Wrapper.Spec, MjClassName); - - // Create tendon in the spec - mjsTendon* Tendon = mjs_addTendon(Wrapper.Spec, effectiveDefault); - if (!Tendon) - { - UE_LOG(LogURLabImport, Error, TEXT("[MjTendon] mjs_addTendon failed for '%s'"), *GetName()); - return; - } - - m_SpecElement = Tendon->element; - - // Set name - FString TName = MjName.IsEmpty() ? GetName() : MjName; - mjs_setName(Tendon->element, TCHAR_TO_UTF8(*TName)); - - // Export properties - ExportTo(Tendon, effectiveDefault); - - // --- Wrap entries --- - for (const FMjTendonWrap& Wrap : Wraps) - { - switch (Wrap.Type) - { - case EMjTendonWrapType::Joint: - { - mjsWrap* W = mjs_wrapJoint(Tendon, TCHAR_TO_UTF8(*Wrap.TargetName), (double)Wrap.Coef); - if (!W) - { - UE_LOG(LogURLabImport, Warning, TEXT("[MjTendon] mjs_wrapJoint failed for joint '%s' in tendon '%s'"), - *Wrap.TargetName, *TName); - } - } - break; - - case EMjTendonWrapType::Site: - { - mjsWrap* W = mjs_wrapSite(Tendon, TCHAR_TO_UTF8(*Wrap.TargetName)); - if (!W) - { - UE_LOG(LogURLabImport, Warning, TEXT("[MjTendon] mjs_wrapSite failed for site '%s' in tendon '%s'"), - *Wrap.TargetName, *TName); - } - } - break; - - case EMjTendonWrapType::Geom: - { - const char* SideSiteStr = Wrap.SideSite.IsEmpty() ? "" : TCHAR_TO_UTF8(*Wrap.SideSite); - mjsWrap* W = mjs_wrapGeom(Tendon, TCHAR_TO_UTF8(*Wrap.TargetName), SideSiteStr); - if (!W) - { - UE_LOG(LogURLabImport, Warning, TEXT("[MjTendon] mjs_wrapGeom failed for geom '%s' in tendon '%s'"), - *Wrap.TargetName, *TName); - } - } - break; - - case EMjTendonWrapType::Pulley: - { - mjsWrap* W = mjs_wrapPulley(Tendon, (double)Wrap.Divisor); - if (!W) - { - UE_LOG(LogURLabImport, Warning, TEXT("[MjTendon] mjs_wrapPulley failed in tendon '%s'"), *TName); - } - } - break; - } - } - - UE_LOG(LogURLabImport, Log, TEXT("[MjTendon] Registered '%s' with %d wraps"), *TName, Wraps.Num()); -} - -void UMjTendon::Bind(mjModel* model, mjData* data, const FString& Prefix) -{ - Super::Bind(model, data, Prefix); - m_TendonView = BindToView(Prefix); - if (m_TendonView.id != -1) - { - m_ID = m_TendonView.id; - } - else - { - UE_LOG(LogURLabImport, Warning, TEXT("[MjTendon] Failed to bind tendon '%s' (prefix='%s')"), - *GetName(), *Prefix); - } -} - -float UMjTendon::GetLength() const -{ - return m_TendonView.GetLength(); -} - -float UMjTendon::GetVelocity() const -{ - return m_TendonView.GetVelocity(); -} - -FString UMjTendon::GetMjName() const -{ - if (m_TendonView.id < 0 || !m_TendonView.name) return FString(); - return MjUtils::MjToString(m_TendonView.name); -} - -#if WITH_EDITOR -TArray UMjTendon::GetDefaultClassOptions() const -{ - return GetSiblingComponentOptions(this, UMjDefault::StaticClass(), true); -} -#endif diff --git a/Plugins/UnrealRoboticsLab/Source/URLab/Private/MuJoCo/Core/Spec/MjSpecWrapper.cpp b/Plugins/UnrealRoboticsLab/Source/URLab/Private/MuJoCo/Core/Spec/MjSpecWrapper.cpp deleted file mode 100644 index 86c2ea1dde..0000000000 --- a/Plugins/UnrealRoboticsLab/Source/URLab/Private/MuJoCo/Core/Spec/MjSpecWrapper.cpp +++ /dev/null @@ -1,390 +0,0 @@ -// Copyright (c) 2026 Jonathan Embley-Riches. All rights reserved. -// -// Licensed under the Apache License, Version 2.0 (the "License"); -// you may not use this file except in compliance with the License. -// You may obtain a copy of the License at -// -// http://www.apache.org/licenses/LICENSE-2.0 -// -// Unless required by applicable law or agreed to in writing, software -// distributed under the License is distributed on an "AS IS" BASIS, -// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. -// See the License for the specific language governing permissions and -// limitations under the License. -// -// --- LEGAL DISCLAIMER --- -// UnrealRoboticsLab is an independent software plugin. It is NOT affiliated with, -// endorsed by, or sponsored by Epic Games, Inc. "Unreal" and "Unreal Engine" are -// trademarks or registered trademarks of Epic Games, Inc. in the US and elsewhere. -// -// This plugin incorporates third-party software: MuJoCo (Apache 2.0), -// CoACD (MIT), and libzmq (MPL 2.0). See ThirdPartyNotices.txt for details. - -#include "MuJoCo/Core/Spec/MjSpecWrapper.h" -#include "Utils/URLabLogging.h" -#include "MuJoCo/Components/Defaults/MjDefault.h" -#include "MuJoCo/Components/Geometry/MjGeom.h" -#include "MuJoCo/Core/MjArticulation.h" -#include "MuJoCo/Utils/MjUtils.h" - -#include "PhysicsEngine/BodySetup.h" -#include "Misc/Paths.h" -#include "Utils/IO.h" -#include "Utils/MeshUtils.h" - -#include "Chaos/TriangleMeshImplicitObject.h" -#include "MuJoCo/Components/Bodies/MjBody.h" - -FMujocoSpecWrapper::FMujocoSpecWrapper(mjSpec* InSpec, mjVFS* InVFS) - : Spec(InSpec), VFS(InVFS) -{ -} - - -mjsBody* FMujocoSpecWrapper::CreateBody(const FString& Name, const FString& ParentName, const FTransform& WorldTransform) -{ - mjsBody* ParentBody = mjs_findBody(Spec, TCHAR_TO_UTF8(*ParentName)); - if (!ParentBody) { - ParentBody = mjs_findBody(Spec, "world"); - } - - mjsBody* NewBody = mjs_addBody(ParentBody, nullptr); - mjs_setName(NewBody->element, TCHAR_TO_UTF8(*Name)); - - MjUtils::UEToMjPosition(WorldTransform.GetLocation(), NewBody->pos); - MjUtils::UEToMjRotation(WorldTransform.GetRotation(), NewBody->quat); - - CreatedBodyNames.Add(Name); - - return NewBody; -} - -mjsBody* FMujocoSpecWrapper::CreateBody(const FString& Name, mjsBody* ParentBody, const FTransform& WorldTransform) -{ - if (!ParentBody) { - ParentBody = mjs_findBody(Spec, "world"); - } - - mjsBody* NewBody = mjs_addBody(ParentBody, nullptr); - mjs_setName(NewBody->element, TCHAR_TO_UTF8(*Name)); - - MjUtils::UEToMjPosition(WorldTransform.GetLocation(), NewBody->pos); - MjUtils::UEToMjRotation(WorldTransform.GetRotation(), NewBody->quat); - - CreatedBodyNames.Add(Name); - - return NewBody; -} - -mjsFrame* FMujocoSpecWrapper::CreateFrame(const FString& Name, mjsBody* ParentBody, const FTransform& WorldTransform) -{ - if (!ParentBody) { - ParentBody = mjs_findBody(Spec, "world"); - } - - mjsFrame* NewFrame = mjs_addFrame(ParentBody, nullptr); - mjs_setName(NewFrame->element, TCHAR_TO_UTF8(*Name)); - - MjUtils::UEToMjPosition(WorldTransform.GetLocation(), NewFrame->pos); - MjUtils::UEToMjRotation(WorldTransform.GetRotation(), NewFrame->quat); - - return NewFrame; -} - -FString FMujocoSpecWrapper::AddMeshAsset(const FString& MeshName, const FString& FilePath, const FVector& Scale) -{ - FString UniqueMeshName = GetUniqueName(MeshName, mjOBJ_MESH, nullptr); - - mjsMesh* MeshAsset = mjs_addMesh(Spec, nullptr); - mjs_setName(MeshAsset->element, TCHAR_TO_UTF8(*UniqueMeshName)); - mjs_setString(MeshAsset->file, TCHAR_TO_UTF8(*FilePath)); - - MeshAsset->scale[0] = Scale.X; - MeshAsset->scale[1] = Scale.Y; - MeshAsset->scale[2] = Scale.Z; - - FString Directory = FPaths::GetPath(FilePath); - FString FileName = FPaths::GetCleanFilename(FilePath); - - UE_LOG(LogURLabWrapper, Log, TEXT("MuJoCo VFS: Loading %s from %s with name %s"), *FileName, *Directory, *UniqueMeshName); - - int Result = mj_addFileVFS(VFS, TCHAR_TO_UTF8(*Directory), TCHAR_TO_UTF8(*FileName)); - if (Result != 0) { - UE_LOG(LogURLabWrapper, Error, TEXT("MuJoCo VFS Error: Failed to load %s (Code %d)"), *FilePath, Result); - } - - return UniqueMeshName; -} - -mjsGeom* FMujocoSpecWrapper::AddPrimitiveGeom(mjsBody* Body, mjtGeom Type, const FVector& Size, const FVector4& RGBA, double Density) -{ - if (!Body) return nullptr; - - mjsGeom* Geom = mjs_addGeom(Body, nullptr); - Geom->type = Type; - - Geom->size[0] = Size.X; - Geom->size[1] = Size.Y; - Geom->size[2] = Size.Z; - - Geom->rgba[0] = RGBA.X; - Geom->rgba[1] = RGBA.Y; - Geom->rgba[2] = RGBA.Z; - Geom->rgba[3] = RGBA.W; - - Geom->density = Density; - - return Geom; -} - -void FMujocoSpecWrapper::AddFreeJoint(mjsBody* Body, const FString& JointName) -{ - if (!Body) return; - - mjsJoint* Jnt = mjs_addJoint(Body, nullptr); - Jnt->type = mjJNT_FREE; - - Jnt->pos[0] = 0.0; - Jnt->pos[1] = 0.0; - Jnt->pos[2] = 0.0; - - Jnt->damping = 0.1; - mjs_setName(Jnt->element, TCHAR_TO_UTF8(*JointName)); -} - - - - -void FMujocoSpecWrapper::AddDefault(UMjDefault* DefaultComp) -{ - if (!DefaultComp) return; - - const char* ClassName = DefaultComp->ClassName.IsEmpty() ? nullptr : TCHAR_TO_UTF8(*DefaultComp->ClassName); - - mjsDefault* parentDef = nullptr; - if (!DefaultComp->ParentClassName.IsEmpty()) - { - parentDef = mjs_findDefault(Spec, TCHAR_TO_UTF8(*DefaultComp->ParentClassName)); - if (parentDef) - { - UE_LOG(LogURLabWrapper, Log, TEXT("[AddDefault] Linked Class '%s' to Parent '%s'"), *DefaultComp->ClassName, *DefaultComp->ParentClassName); - } - else - { - UE_LOG(LogURLabWrapper, Warning, TEXT("[AddDefault] Could not find Parent Class '%s' for '%s'"), *DefaultComp->ParentClassName, *DefaultComp->ClassName); - } - } - - mjsDefault* def = nullptr; - - if (DefaultComp->ClassName.IsEmpty() || DefaultComp->ClassName == TEXT("main")) - { - if (parentDef == nullptr) - { - // This is the root default — use the spec's built-in default - def = mjs_getSpecDefault(Spec); - UE_LOG(LogURLabWrapper, Log, TEXT("[AddDefault] Using root default for class '%s'"), *DefaultComp->ClassName); - } - else - { - def = mjs_addDefault(Spec, ClassName, parentDef); - } - } - else - { - def = mjs_addDefault(Spec, ClassName, parentDef); - } - - if (!def) return; - - DefaultComp->ExportTo(def); -} - -TArray FMujocoSpecWrapper::ReconstructViews(const mjModel* m, mjData* d, const FString& Prefix) -{ - TArray Views; - UE_LOG(LogURLabWrapper, Warning, TEXT("ReconstructViews: %i bodies, prefix='%s'"), CreatedBodyNames.Num(), *Prefix); - Views.Reserve(CreatedBodyNames.Num()); - - for (const FString& Name : CreatedBodyNames) - { - FString PrefixedName = Prefix + Name; - BodyView View = bind(m, d, TCHAR_TO_UTF8(*PrefixedName)); - if (View.id >= 0) - { - Views.Add(View); - } - else - { - UE_LOG(LogURLabWrapper, Warning, TEXT("MuJoCo SpecWrapper: Failed to bind view for body '%s'."), *PrefixedName); - } - } - - return Views; -} - -TArray FMujocoSpecWrapper::PrepareMeshForMuJoCo(UStaticMeshComponent* SMC, bool bComplexMeshRequired, float CoACDThreshold) -{ - TArray ResultNames; - if (!SMC || !SMC->GetStaticMesh()) return ResultNames; - - UStaticMesh* Mesh = SMC->GetStaticMesh(); - UBodySetup* BodySetup = nullptr; // Mesh->GetBodySetup(); - // if (!BodySetup || BodySetup->TriMeshGeometries.Num() == 0) return ResultNames; - if (!BodySetup) return ResultNames; - - FString MeshType = bComplexMeshRequired ? "Complex" : "Simple"; - UStaticMesh* StaticMesh = Cast(BodySetup->GetOuter()); - FString BaseAssetName = StaticMesh ? StaticMesh->GetName() : SMC->GetName(); - FVector Scale = SMC->GetComponentScale(); - - FString AssetName = BaseAssetName; - if (!Scale.Equals(FVector::OneVector, 0.001f)) - { - AssetName = FString::Printf(TEXT("%s_s%.3f_%.3f_%.3f"), *BaseAssetName, - Scale.X, Scale.Y, Scale.Z); - } - - FString SubDir = MeshCacheSubDir.IsEmpty() ? TEXT("Shared") : MeshCacheSubDir; - FString FilePath = FString::Printf( - TEXT("%s/URLab/ConvertedMeshes/%s/%s_%s.obj"), - *FPaths::ProjectSavedDir(), - *SubDir, - *MeshType, - *AssetName - ); - FString FullFilePath = FPaths::ConvertRelativePathToFull(FilePath); - - if (!bComplexMeshRequired) - { - if (mjs_findElement(Spec, mjOBJ_MESH, TCHAR_TO_UTF8(*AssetName))) - { - UE_LOG(LogURLabWrapper, Log, TEXT("Mesh Asset '%s' already exists in Spec. Reusing."), *AssetName); - ResultNames.Add(AssetName); - return ResultNames; - } - } - else - { - FString FirstSubMeshName = FString::Printf(TEXT("%s_0"), *AssetName); - if (mjs_findElement(Spec, mjOBJ_MESH, TCHAR_TO_UTF8(*FirstSubMeshName))) - { - UE_LOG(LogURLabWrapper, Log, TEXT("Complex Mesh Asset '%s' already exists in Spec. Reusing all parts."), *AssetName); - int i = 0; - while (true) - { - FString SubMeshName = FString::Printf(TEXT("%s_%d"), *AssetName, i); - if (mjs_findElement(Spec, mjOBJ_MESH, TCHAR_TO_UTF8(*SubMeshName))) - { - ResultNames.Add(SubMeshName); - i++; - } - else - { - break; - } - } - return ResultNames; - } - } - - const int32 GeometryIndex = 0; - // // auto& TriGeom; // BodySetup->TriMeshGeometries[GeometryIndex]; - // auto& Vertices = TriGeom.GetReference()->Particles().X(); - // - // FString CurrentHash; - // bool bLargeIndices = TriGeom.GetReference()->Elements().RequiresLargeIndices(); - // if (bLargeIndices) - // { - // const auto& Indices = TriGeom.GetReference()->Elements().GetLargeIndexBuffer(); - // CurrentHash = IO::ComputeMeshHash(Vertices, Indices); - // } - // else - // { - // const auto& Indices = TriGeom.GetReference()->Elements().GetSmallIndexBuffer(); - // CurrentHash = IO::ComputeMeshHash(Vertices, Indices); - // } - // CurrentHash += bComplexMeshRequired ? TEXT("_complex") : TEXT("_simple"); - // if (bComplexMeshRequired) - // { - // CurrentHash += FString::Printf(TEXT("_t%.4f"), CoACDThreshold); - // } - // - // int MeshCount = IO::NumFilesExist(FullFilePath, bComplexMeshRequired); - // if (MeshCount > 0) - // { - // FString CachedHash = IO::LoadMeshHash(FullFilePath); - // if (CachedHash != CurrentHash) - // { - // UE_LOG(LogURLabWrapper, Log, TEXT("Mesh '%s' has changed (hash mismatch). Re-exporting."), *AssetName); - // IO::DeleteMeshCache(FullFilePath, bComplexMeshRequired); - // MeshCount = 0; - // } - // } - // - // if (MeshCount == 0) - // { - // UE_LOG(LogURLabWrapper, Log, TEXT("Saving mesh geometry for: %s"), *AssetName); - // - // if (bLargeIndices) - // { - // const auto& Indices = TriGeom.GetReference()->Elements().GetLargeIndexBuffer(); - // MeshCount = MeshUtils::SaveMesh(FullFilePath, Vertices, Indices, bComplexMeshRequired, CoACDThreshold); - // } - // else - // { - // const auto& Indices = TriGeom.GetReference()->Elements().GetSmallIndexBuffer(); - // MeshCount = MeshUtils::SaveMesh(FullFilePath, Vertices, Indices, bComplexMeshRequired, CoACDThreshold); - // } - // - // if (MeshCount == 0) - // { - // UE_LOG(LogURLabWrapper, Error, TEXT("MeshUtils::SaveMesh failed to save any meshes for %s."), *AssetName); - // return ResultNames; - // } - // - // IO::SaveMeshHash(FullFilePath, CurrentHash); - // } - // - // FString BaseName = FPaths::GetBaseFilename(FilePath); - // FString Directory = FPaths::GetPath(FilePath); - // - // if (!bComplexMeshRequired) - // { - // FString sub_file_path = FString::Printf(TEXT("%s/%s.obj"), *Directory, *BaseName); - // ResultNames.Add(AddMeshAsset(AssetName, sub_file_path, Scale)); - // } - // else - // { - // for (int i = 0; i < MeshCount; i++) - // { - // FString sub_file_path = FString::Printf(TEXT("%s/%s_sub_%d.obj"), *Directory, *BaseName, i); - // FString SubMeshName = FString::Printf(TEXT("%s_%d"), *AssetName, i); - // ResultNames.Add(AddMeshAsset(SubMeshName, sub_file_path, Scale)); - // } - // } - // - return ResultNames; -} - - - - - -FString FMujocoSpecWrapper::GetUniqueName(const FString& BaseName, mjtObj Type, const AActor* ContextActor) -{ - FString Candidate = BaseName; - - FString TestName = Candidate; - int32 Counter = 0; - while (mjs_findElement(Spec, Type, TCHAR_TO_UTF8(*TestName))) - { - ++Counter; - TestName = FString::Printf(TEXT("%s_%d"), *Candidate, Counter); - } - - return TestName; -} - - - diff --git a/Plugins/UnrealRoboticsLab/Source/URLab/Private/Replay/MjReplayManager.cpp b/Plugins/UnrealRoboticsLab/Source/URLab/Private/Replay/MjReplayManager.cpp deleted file mode 100644 index ec9a1890a9..0000000000 --- a/Plugins/UnrealRoboticsLab/Source/URLab/Private/Replay/MjReplayManager.cpp +++ /dev/null @@ -1,1173 +0,0 @@ -// Copyright (c) 2026 Jonathan Embley-Riches. All rights reserved. -// -// Licensed under the Apache License, Version 2.0 (the "License"); -// you may not use this file except in compliance with the License. -// You may obtain a copy of the License at -// -// http://www.apache.org/licenses/LICENSE-2.0 -// -// Unless required by applicable law or agreed to in writing, software -// distributed under the License is distributed on an "AS IS" BASIS, -// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. -// See the License for the specific language governing permissions and -// limitations under the License. -// -// --- LEGAL DISCLAIMER --- -// UnrealRoboticsLab is an independent software plugin. It is NOT affiliated with, -// endorsed by, or sponsored by Epic Games, Inc. "Unreal" and "Unreal Engine" are -// trademarks or registered trademarks of Epic Games, Inc. in the US and elsewhere. -// -// This plugin incorporates third-party software: MuJoCo (Apache 2.0), -// CoACD (MIT), and libzmq (MPL 2.0). See ThirdPartyNotices.txt for details. -#include "Replay/MjReplayManager.h" -#include "Utils/URLabLogging.h" -#include "MuJoCo/Core/AMjManager.h" -#include "MuJoCo/Core/MjPhysicsEngine.h" -#include "MuJoCo/Utils/MjUtils.h" -#include "JsonObjectConverter.h" -#include "Misc/FileHelper.h" -#include "Misc/Paths.h" -#include "Algo/BinarySearch.h" -#if WITH_EDITOR -#include "DesktopPlatformModule.h" -#include "Framework/Application/SlateApplication.h" -#endif - -const FString AMjReplayManager::LiveSessionName = TEXT("Live Recording"); - -AMjReplayManager::AMjReplayManager() -{ - PrimaryActorTick.bCanEverTick = true; -} - -void AMjReplayManager::BeginPlay() -{ - Super::BeginPlay(); - - // Ensure live session exists - Sessions.FindOrAdd(LiveSessionName); - ActiveSessionName = LiveSessionName; - - // Auto-connect to manager if possible - Manager = AAMjManager::GetManager(); - if (Manager) - { - // Sets OnPostStep directly rather than RegisterPostStepCallback -- replay - // needs exclusive control of the callback, not append-to behavior. - if (Manager->PhysicsEngine) - { - Manager->PhysicsEngine->OnPostStep = [this](mjModel* m, mjData* d) { - this->OnPostStep(m, d); - }; - } - } - - if (bAutoRecord) - { - StartRecording(); - } - - // Auto-load any CSV/JSON replays from Saved/URLab/Replays/ - { - FString ReplayDir = FPaths::ProjectSavedDir() / TEXT("URLab") / TEXT("Replays"); - IFileManager::Get().MakeDirectory(*ReplayDir, true); - - TArray FoundFiles; - IFileManager::Get().FindFiles(FoundFiles, *(ReplayDir / TEXT("*.csv")), true, false); - - TArray JsonFiles; - IFileManager::Get().FindFiles(JsonFiles, *(ReplayDir / TEXT("*.json")), true, false); - FoundFiles.Append(JsonFiles); - - for (const FString& FileName : FoundFiles) - { - FString FullPath = ReplayDir / FileName; - FString SessionName = FPaths::GetBaseFilename(FileName); - - if (Sessions.Contains(SessionName)) continue; - - if (FileName.EndsWith(TEXT(".csv"))) - { - LoadFromCSV(FullPath); - } - else if (FileName.EndsWith(TEXT(".json"))) - { - FString JsonString; - if (FFileHelper::LoadFileToString(JsonString, *FullPath)) - { - TSharedPtr RootObject; - TSharedRef> Reader = TJsonReaderFactory<>::Create(JsonString); - if (FJsonSerializer::Deserialize(Reader, RootObject) && RootObject.IsValid()) - { - const TArray>* JsonFrames; - if (RootObject->TryGetArrayField(TEXT("Frames"), JsonFrames)) - { - FReplaySession& Session = Sessions.FindOrAdd(SessionName); - Session.SourceFile = FullPath; - for (const auto& Val : *JsonFrames) - { - const TSharedPtr* FrameObj; - if (Val->TryGetObject(FrameObj)) - { - FMjReplayFrame Frame; - if (FJsonObjectConverter::JsonObjectToUStruct((*FrameObj).ToSharedRef(), &Frame)) - { - Session.Frames.Add(Frame); - } - } - } - } - } - } - } - - UE_LOG(LogURLabReplay, Log, TEXT("ReplayManager: Auto-loaded replay '%s' (%d frames)"), - *SessionName, Sessions.FindOrAdd(SessionName).Frames.Num()); - } - } - - // Keep Live Recording as active - ActiveSessionName = LiveSessionName; -} - -void AMjReplayManager::EndPlay(const EEndPlayReason::Type EndPlayReason) -{ - Super::EndPlay(EndPlayReason); - - if (Manager && Manager->PhysicsEngine) - { - Manager->PhysicsEngine->OnPostStep = nullptr; - Manager->PhysicsEngine->ClearCustomStepHandler(); - } -} - -void AMjReplayManager::Tick(float DeltaTime) -{ - Super::Tick(DeltaTime); - - // Read playback time from physics thread (atomic, no race) - if (bIsReplaying) - { - PlaybackTime = (float)PhysicsPlaybackTime.load(std::memory_order_relaxed); - } -} - -// --- Session Accessors --- - -TArray& AMjReplayManager::GetActiveFrames() -{ - return Sessions.FindOrAdd(ActiveSessionName).Frames; -} - -TArray& AMjReplayManager::GetLiveFrames() -{ - return Sessions.FindOrAdd(LiveSessionName).Frames; -} - -TArray AMjReplayManager::GetSessionNames() const -{ - TArray Names; - Sessions.GetKeys(Names); - // Ensure Live Recording is always first - Names.Remove(LiveSessionName); - Names.Insert(LiveSessionName, 0); - return Names; -} - -void AMjReplayManager::SetActiveSession(const FString& Name) -{ - if (Sessions.Contains(Name)) - { - ActiveSessionName = Name; - RebuildArticulationBindings(); - UE_LOG(LogURLabReplay, Log, TEXT("ReplayManager: Active session set to '%s' (%d frames, %d articulation bindings)"), - *Name, Sessions[Name].Frames.Num(), ArticulationBindings.Num()); - } - else - { - UE_LOG(LogURLabReplay, Warning, TEXT("ReplayManager: Session '%s' not found"), *Name); - } -} - -void AMjReplayManager::RebuildArticulationBindings() -{ - // Preserve existing settings (enabled/relpos) for articulations that still match - TMap OldBindings; - for (const FReplayArticulationBinding& B : ArticulationBindings) - { - if (B.Articulation.IsValid()) - { - OldBindings.Add(B.Articulation->GetName(), B); - } - } - ArticulationBindings.Empty(); - - if (!Manager) Manager = AAMjManager::GetManager(); - if (!Manager) return; - - TArray& Frames = GetActiveFrames(); - if (Frames.Num() == 0) return; - - const FMjReplayFrame& FirstFrame = Frames[0]; - TArray Articulations = Manager->GetAllArticulations(); - UE_LOG(LogURLabReplay, Log, TEXT("RebuildArticulationBindings: %d articulations, %d CSV joints in session '%s'"), - Articulations.Num(), FirstFrame.JointStates.Num(), *ActiveSessionName); - - for (AMjArticulation* Art : Articulations) - { - if (!Art) continue; - - FString ActorName = Art->GetName(); - - TArray JointNames = Art->GetJointNames(); - UE_LOG(LogURLabReplay, Log, TEXT(" Checking articulation '%s' — %d joint names"), *ActorName, JointNames.Num()); - bool bHasMatch = false; - FString FoundPrefix; - - for (const FString& UEJointName : JointNames) - { - // Extract the bare joint name by finding the last occurrence of a known separator - // MuJoCo names are like "g1_29dof_beyondmimic_C_1_left_hip_pitch_joint" - // We want to compare just "left_hip_pitch_joint" — find it by stripping the actor prefix - FString BareJointName = UEJointName; - if (UEJointName.StartsWith(ActorName + TEXT("_"))) - { - BareJointName = UEJointName.Mid(ActorName.Len() + 1); - } - - for (auto& Pair : FirstFrame.JointStates) - { - // Compare bare names: strip prefix from CSV key too - if (Pair.Key.EndsWith(BareJointName) || BareJointName.Equals(Pair.Key) || UEJointName.Equals(Pair.Key)) - { - bHasMatch = true; - break; - } - } - if (bHasMatch) break; - } - - if (bHasMatch) - { - FReplayArticulationBinding Binding; - Binding.Articulation = Art; - Binding.InitialPosition = Art->GetActorLocation(); - Binding.bInitialsCaptured = false; - - // Restore previous settings if this articulation was already bound - if (FReplayArticulationBinding* Old = OldBindings.Find(ActorName)) - { - Binding.bEnabled = Old->bEnabled; - Binding.bRelativePosition = Old->bRelativePosition; - } - else - { - Binding.bEnabled = true; - // Live recordings already have correct absolute positions — RelPos off by default. - // External CSVs need RelPos on since their coordinate system differs. - FReplaySession* Session = Sessions.Find(ActiveSessionName); - Binding.bRelativePosition = Session && !Session->SourceFile.IsEmpty(); - } - - ArticulationBindings.Add(Binding); - UE_LOG(LogURLabReplay, Log, TEXT("ReplayManager: Bound articulation '%s'"), *ActorName); - } - } -} - -// --- Control API --- - -void AMjReplayManager::StartRecording() -{ - if (bIsReplaying) StopReplay(); - - GetLiveFrames().Empty(); - ActiveSessionName = LiveSessionName; - bIsRecording = true; - bCacheValid = false; - CachedJointNames.Empty(); - - // Re-register the PostStep callback (it gets cleared by StopRecording) - // Sets OnPostStep directly rather than RegisterPostStepCallback -- replay - // needs exclusive control of the callback, not append-to behavior. - if (Manager && Manager->PhysicsEngine) - { - Manager->PhysicsEngine->OnPostStep = [this](mjModel* m, mjData* d) { - this->OnPostStep(m, d); - }; - } - - UE_LOG(LogURLabReplay, Log, TEXT("ReplayManager: Started Recording")); -} - -void AMjReplayManager::StopRecording() -{ - bIsRecording = false; - UE_LOG(LogURLabReplay, Log, TEXT("ReplayManager: Stopped Recording. Captured %d frames."), GetLiveFrames().Num()); -} - -void AMjReplayManager::StartReplay() -{ - if (bIsRecording) StopRecording(); - - TArray& Frames = GetActiveFrames(); - if (Frames.Num() == 0) - { - UE_LOG(LogURLabReplay, Warning, TEXT("ReplayManager: No frames in session '%s' to replay!"), *ActiveSessionName); - return; - } - - if (!Manager) Manager = AAMjManager::GetManager(); - if (!Manager) return; - - bIsReplaying = true; - PlaybackTime = 0.0f; - PhysicsPlaybackTime.store(0.0, std::memory_order_relaxed); - bFirstReplayFrame = true; - - // Rebuild bindings (initial MuJoCo positions will be captured on first replay frame - // after the reset completes on the physics thread) - RebuildArticulationBindings(); - - // Reset simulation so initial positions are clean (from the model's qpos0) - Manager->ResetSimulation(); - - // Auto-unpause so replay starts immediately - if (Manager->PhysicsEngine && Manager->PhysicsEngine->bIsPaused) - { - Manager->PhysicsEngine->SetPaused(false); - } - - Manager->PhysicsEngine->SetCustomStepHandler([this](mjModel* m, mjData* d) { - this->OnReplayStep(m, d); - }); - - UE_LOG(LogURLabReplay, Log, TEXT("ReplayManager: Started Replay of '%s' (%d frames)"), - *ActiveSessionName, Frames.Num()); -} - -void AMjReplayManager::StopReplay() -{ - bIsReplaying = false; - - if (Manager && Manager->PhysicsEngine) - { - Manager->PhysicsEngine->ClearCustomStepHandler(); - } - - UE_LOG(LogURLabReplay, Log, TEXT("ReplayManager: Stopped Replay")); -} - -void AMjReplayManager::ClearRecording() -{ - GetLiveFrames().Empty(); - PlaybackTime = 0.0f; - PhysicsPlaybackTime.store(0.0, std::memory_order_relaxed); -} - -bool AMjReplayManager::SaveRecordingToFile(FString FileName) -{ - TArray& Frames = GetActiveFrames(); - if (Frames.Num() == 0) return false; - - TSharedPtr RootObject = MakeShareable(new FJsonObject()); - TArray> JsonFrames; - - for (const FMjReplayFrame& Frame : Frames) - { - TSharedPtr FrameObj = FJsonObjectConverter::UStructToJsonObject(Frame); - JsonFrames.Add(MakeShareable(new FJsonValueObject(FrameObj))); - } - - RootObject->SetArrayField(TEXT("Frames"), JsonFrames); - - FString JsonString; - TSharedRef> Writer = TJsonWriterFactory<>::Create(&JsonString); - FJsonSerializer::Serialize(RootObject.ToSharedRef(), Writer); - - FString FilePath = FPaths::ProjectSavedDir() / TEXT("URLab") / TEXT("Replays") / FileName; - return FFileHelper::SaveStringToFile(JsonString, *FilePath); -} - -bool AMjReplayManager::LoadRecordingFromFile(FString FileName) -{ - FString FilePath = FPaths::ProjectSavedDir() / TEXT("URLab") / TEXT("Replays") / FileName; - FString JsonString; - if (!FFileHelper::LoadFileToString(JsonString, *FilePath)) return false; - - TSharedPtr RootObject; - TSharedRef> Reader = TJsonReaderFactory<>::Create(JsonString); - - if (FJsonSerializer::Deserialize(Reader, RootObject) && RootObject.IsValid()) - { - const TArray>* JsonFrames; - if (RootObject->TryGetArrayField(TEXT("Frames"), JsonFrames)) - { - // Create a new named session from the filename - FString SessionName = FPaths::GetBaseFilename(FileName); - FReplaySession& Session = Sessions.FindOrAdd(SessionName); - Session.Frames.Empty(); - Session.SourceFile = FilePath; - - for (const auto& Val : *JsonFrames) - { - const TSharedPtr* FrameObj; - if (Val->TryGetObject(FrameObj)) - { - FMjReplayFrame Frame; - if (FJsonObjectConverter::JsonObjectToUStruct((*FrameObj).ToSharedRef(), &Frame)) - { - Session.Frames.Add(Frame); - } - } - } - - ActiveSessionName = SessionName; - UE_LOG(LogURLabReplay, Log, TEXT("ReplayManager: Loaded %d frames into session '%s'."), - Session.Frames.Num(), *SessionName); - return true; - } - } - return false; -} - -// --- CSV Import --- - -bool AMjReplayManager::LoadFromCSV(FString FilePath, float Timestep) -{ - TArray Lines; - if (!FFileHelper::LoadFileToStringArray(Lines, *FilePath)) - { - UE_LOG(LogURLabReplay, Error, TEXT("ReplayManager: Failed to load CSV file: %s"), *FilePath); - return false; - } - - if (Lines.Num() < 2) - { - UE_LOG(LogURLabReplay, Error, TEXT("ReplayManager: CSV file has no data rows: %s"), *FilePath); - return false; - } - - // Parse header - TArray Headers; - Lines[0].ParseIntoArray(Headers, TEXT(","), false); - for (FString& H : Headers) H.TrimStartAndEndInline(); - - // Find time column - int32 TimeCol = INDEX_NONE; - for (int32 i = 0; i < Headers.Num(); ++i) - { - if (Headers[i].Equals(TEXT("time"), ESearchCase::IgnoreCase) || - Headers[i].Equals(TEXT("timestamp"), ESearchCase::IgnoreCase)) - { - TimeCol = i; - break; - } - } - - // Group columns by joint name - // Detect bracket notation: "name[N]" -> group under "name" - // Everything else is a 1-DOF joint column - struct FColumnGroup - { - FString JointName; - TArray ColumnIndices; // Ordered by bracket index or appearance order - }; - TMap JointColumnMap; - - // Camera column indices - int32 CamXCol = INDEX_NONE, CamYCol = INDEX_NONE, CamZCol = INDEX_NONE; - int32 CamPitchCol = INDEX_NONE, CamYawCol = INDEX_NONE, CamRollCol = INDEX_NONE; - - for (int32 i = 0; i < Headers.Num(); ++i) - { - if (i == TimeCol) continue; - - FString ColName = Headers[i]; - - // Camera columns - if (ColName.Equals(TEXT("cam_x"), ESearchCase::IgnoreCase)) { CamXCol = i; continue; } - if (ColName.Equals(TEXT("cam_y"), ESearchCase::IgnoreCase)) { CamYCol = i; continue; } - if (ColName.Equals(TEXT("cam_z"), ESearchCase::IgnoreCase)) { CamZCol = i; continue; } - if (ColName.Equals(TEXT("cam_pitch"), ESearchCase::IgnoreCase)) { CamPitchCol = i; continue; } - if (ColName.Equals(TEXT("cam_yaw"), ESearchCase::IgnoreCase)) { CamYawCol = i; continue; } - if (ColName.Equals(TEXT("cam_roll"), ESearchCase::IgnoreCase)) { CamRollCol = i; continue; } - - // Check for bracket notation: "name[N]" - int32 BracketIdx = ColName.Find(TEXT("[")); - if (BracketIdx != INDEX_NONE && ColName.EndsWith(TEXT("]"))) - { - FString BaseName = ColName.Left(BracketIdx); - FString IdxStr = ColName.Mid(BracketIdx + 1, ColName.Len() - BracketIdx - 2); - int32 SubIdx = FCString::Atoi(*IdxStr); - - FColumnGroup& Group = JointColumnMap.FindOrAdd(BaseName); - Group.JointName = BaseName; - if (Group.ColumnIndices.Num() <= SubIdx) - { - Group.ColumnIndices.SetNum(SubIdx + 1); - } - Group.ColumnIndices[SubIdx] = i; - } - else - { - FColumnGroup& Group = JointColumnMap.FindOrAdd(ColName); - Group.JointName = ColName; - Group.ColumnIndices.Add(i); - } - } - - bool bHasCameraColumns = (CamXCol != INDEX_NONE && CamYCol != INDEX_NONE && CamZCol != INDEX_NONE); - - // Parse data rows - FString SessionName = FPaths::GetBaseFilename(FilePath); - FReplaySession& Session = Sessions.FindOrAdd(SessionName); - Session.Frames.Empty(); - Session.SourceFile = FilePath; - Session.Frames.Reserve(Lines.Num() - 1); - - for (int32 LineIdx = 1; LineIdx < Lines.Num(); ++LineIdx) - { - FString& Line = Lines[LineIdx]; - if (Line.TrimStartAndEnd().IsEmpty()) continue; - - TArray Values; - Line.ParseIntoArray(Values, TEXT(","), false); - - // Get timestamp - double Time = (TimeCol != INDEX_NONE && TimeCol < Values.Num()) - ? FCString::Atod(*Values[TimeCol]) - : (double)(LineIdx - 1) * (double)Timestep; - - FMjReplayFrame Frame; - Frame.Timestamp = Time; - - for (auto& Pair : JointColumnMap) - { - const FColumnGroup& Group = Pair.Value; - FMjBodyKinematics Kinematics; - Kinematics.QPos.SetNum(Group.ColumnIndices.Num()); - - bool bValid = true; - for (int32 k = 0; k < Group.ColumnIndices.Num(); ++k) - { - int32 ColIdx = Group.ColumnIndices[k]; - if (ColIdx < Values.Num()) - { - Kinematics.QPos[k] = FCString::Atod(*Values[ColIdx]); - } - else - { - bValid = false; - break; - } - } - - if (bValid) - { - // QVel left empty — mj_forward doesn't need it for kinematics - Frame.JointStates.Add(Group.JointName, MoveTemp(Kinematics)); - } - } - - // Parse camera data if present - if (bHasCameraColumns) - { - Frame.bHasCameraData = true; - if (CamXCol < Values.Num()) Frame.CameraPosition.X = FCString::Atod(*Values[CamXCol]); - if (CamYCol < Values.Num()) Frame.CameraPosition.Y = FCString::Atod(*Values[CamYCol]); - if (CamZCol < Values.Num()) Frame.CameraPosition.Z = FCString::Atod(*Values[CamZCol]); - if (CamPitchCol < Values.Num()) Frame.CameraRotation.Pitch = FCString::Atod(*Values[CamPitchCol]); - if (CamYawCol < Values.Num()) Frame.CameraRotation.Yaw = FCString::Atod(*Values[CamYawCol]); - if (CamRollCol < Values.Num()) Frame.CameraRotation.Roll = FCString::Atod(*Values[CamRollCol]); - } - - Session.Frames.Add(MoveTemp(Frame)); - } - - ActiveSessionName = SessionName; - RebuildArticulationBindings(); - - UE_LOG(LogURLabReplay, Log, TEXT("ReplayManager: Loaded CSV '%s' -> session '%s': %d frames, %d joints"), - *FilePath, *SessionName, Session.Frames.Num(), JointColumnMap.Num()); - - for (auto& Pair : JointColumnMap) - { - UE_LOG(LogURLabReplay, Log, TEXT(" Joint '%s': %d DOF"), *Pair.Key, Pair.Value.ColumnIndices.Num()); - } - - return Session.Frames.Num() > 0; -} - -bool AMjReplayManager::BrowseAndLoadCSV(float Timestep) -{ -#if WITH_EDITOR - IDesktopPlatform* DesktopPlatform = FDesktopPlatformModule::Get(); - if (!DesktopPlatform) return false; - - FString DefaultDir = FPaths::ProjectSavedDir() / TEXT("URLab") / TEXT("Replays"); - IFileManager::Get().MakeDirectory(*DefaultDir, true); - - TArray OutFiles; - bool bOpened = DesktopPlatform->OpenFileDialog( - FSlateApplication::Get().FindBestParentWindowHandleForDialogs(nullptr), - TEXT("Load Replay"), - DefaultDir, - TEXT(""), - TEXT("CSV Files (*.csv)|*.csv|JSON Files (*.json)|*.json"), - EFileDialogFlags::None, - OutFiles); - - if (bOpened && OutFiles.Num() > 0) - { - if (OutFiles[0].EndsWith(TEXT(".json"))) - { - // Load JSON directly from the full path - FString JsonString; - if (!FFileHelper::LoadFileToString(JsonString, *OutFiles[0])) return false; - - TSharedPtr RootObject; - TSharedRef> Reader = TJsonReaderFactory<>::Create(JsonString); - if (FJsonSerializer::Deserialize(Reader, RootObject) && RootObject.IsValid()) - { - const TArray>* JsonFrames; - if (RootObject->TryGetArrayField(TEXT("Frames"), JsonFrames)) - { - FString SessionName = FPaths::GetBaseFilename(OutFiles[0]); - FReplaySession& Session = Sessions.FindOrAdd(SessionName); - Session.Frames.Empty(); - Session.SourceFile = OutFiles[0]; - - for (const auto& Val : *JsonFrames) - { - const TSharedPtr* FrameObj; - if (Val->TryGetObject(FrameObj)) - { - FMjReplayFrame Frame; - if (FJsonObjectConverter::JsonObjectToUStruct((*FrameObj).ToSharedRef(), &Frame)) - { - Session.Frames.Add(Frame); - } - } - } - ActiveSessionName = SessionName; - RebuildArticulationBindings(); - UE_LOG(LogURLabReplay, Log, TEXT("ReplayManager: Loaded %d frames from JSON '%s'"), - Session.Frames.Num(), *OutFiles[0]); - return Session.Frames.Num() > 0; - } - } - return false; - } - return LoadFromCSV(OutFiles[0], Timestep); - } - return false; -#else - return false; -#endif -} - -bool AMjReplayManager::BrowseAndSaveRecording() -{ - TArray& Frames = GetActiveFrames(); - if (Frames.Num() == 0) - { - UE_LOG(LogURLabReplay, Warning, TEXT("ReplayManager: No frames in active session '%s' to save. Record some data first or select a session with frames."), *ActiveSessionName); - return false; - } - -#if WITH_EDITOR - IDesktopPlatform* DesktopPlatform = FDesktopPlatformModule::Get(); - if (!DesktopPlatform) return false; - - // Ensure save directory exists - FString SaveDir = FPaths::ProjectSavedDir() / TEXT("URLab") / TEXT("Replays"); - IFileManager::Get().MakeDirectory(*SaveDir, true); - - FString DefaultName = ActiveSessionName + TEXT(".csv"); - TArray OutFiles; - bool bOpened = DesktopPlatform->SaveFileDialog( - FSlateApplication::Get().FindBestParentWindowHandleForDialogs(nullptr), - TEXT("Save Replay Recording"), - SaveDir, - DefaultName, - TEXT("CSV Files (*.csv)|*.csv|JSON Files (*.json)|*.json"), - EFileDialogFlags::None, - OutFiles); - - if (!bOpened || OutFiles.Num() == 0) return false; - - FString SavePath = OutFiles[0]; - - if (SavePath.EndsWith(TEXT(".csv"))) - { - // Export as CSV with joint-name headers - if (Frames.Num() == 0) return false; - - // Build headers from first frame's joint names - TArray Headers; - Headers.Add(TEXT("time")); - TArray JointOrder; - - for (auto& Pair : Frames[0].JointStates) - { - JointOrder.Add(Pair.Key); - if (Pair.Value.QPos.Num() == 1) - { - Headers.Add(Pair.Key); - } - else - { - for (int32 k = 0; k < Pair.Value.QPos.Num(); ++k) - { - Headers.Add(FString::Printf(TEXT("%s[%d]"), *Pair.Key, k)); - } - } - } - - // Add camera columns if any frame has camera data - bool bHasCamera = false; - for (const FMjReplayFrame& F : Frames) - { - if (F.bHasCameraData) { bHasCamera = true; break; } - } - if (bHasCamera) - { - Headers.Add(TEXT("cam_x")); - Headers.Add(TEXT("cam_y")); - Headers.Add(TEXT("cam_z")); - Headers.Add(TEXT("cam_pitch")); - Headers.Add(TEXT("cam_yaw")); - Headers.Add(TEXT("cam_roll")); - } - - FString CSV = FString::Join(Headers, TEXT(",")) + TEXT("\n"); - for (const FMjReplayFrame& Frame : Frames) - { - CSV += FString::Printf(TEXT("%.6f"), Frame.Timestamp); - for (const FString& JointName : JointOrder) - { - if (const FMjBodyKinematics* K = Frame.JointStates.Find(JointName)) - { - for (int32 k = 0; k < K->QPos.Num(); ++k) - { - CSV += FString::Printf(TEXT(",%.8f"), K->QPos[k]); - } - } - } - if (bHasCamera) - { - CSV += FString::Printf(TEXT(",%.4f,%.4f,%.4f,%.4f,%.4f,%.4f"), - Frame.CameraPosition.X, Frame.CameraPosition.Y, Frame.CameraPosition.Z, - Frame.CameraRotation.Pitch, Frame.CameraRotation.Yaw, Frame.CameraRotation.Roll); - } - CSV += TEXT("\n"); - } - - bool bSaved = FFileHelper::SaveStringToFile(CSV, *SavePath); - UE_LOG(LogURLabReplay, Log, TEXT("ReplayManager: Saved %d frames as CSV to '%s'"), Frames.Num(), *SavePath); - return bSaved; - } - else - { - // Save as JSON (existing format) - // Save directly to chosen path as JSON - TSharedPtr RootObject = MakeShareable(new FJsonObject()); - TArray> JsonFrames; - for (const FMjReplayFrame& Frame : Frames) - { - TSharedPtr FrameObj = FJsonObjectConverter::UStructToJsonObject(Frame); - JsonFrames.Add(MakeShareable(new FJsonValueObject(FrameObj))); - } - RootObject->SetArrayField(TEXT("Frames"), JsonFrames); - - FString JsonString; - TSharedRef> Writer = TJsonWriterFactory<>::Create(&JsonString); - FJsonSerializer::Serialize(RootObject.ToSharedRef(), Writer); - - bool bSaved = FFileHelper::SaveStringToFile(JsonString, *SavePath); - UE_LOG(LogURLabReplay, Log, TEXT("ReplayManager: Saved %d frames as JSON to '%s'"), Frames.Num(), *SavePath); - return bSaved; - } -#else - return false; -#endif -} - -// --- Physics Thread Callbacks --- - -void AMjReplayManager::OnPostStep(mjModel* m, mjData* d) -{ - if (!bIsRecording || !m || !d || !d->qpos || !d->qvel) return; - - if (!bCacheValid || CachedJointNames.Num() != m->njnt) - { - UpdateCache(m); - } - - FMjReplayFrame Frame; - Frame.Timestamp = d->time; - - for (int i = 0; i < m->njnt; ++i) - { - FString Name = CachedJointNames[i]; - if (Name.IsEmpty()) continue; - - int qpos_adr = m->jnt_qposadr[i]; - int qvel_adr = m->jnt_dofadr[i]; - - int qpos_dim = 0; - int qvel_dim = 0; - - int jnt_type = m->jnt_type[i]; - if (jnt_type == mjJNT_FREE) { qpos_dim = 7; qvel_dim = 6; } - else if (jnt_type == mjJNT_BALL) { qpos_dim = 4; qvel_dim = 3; } - else if (jnt_type == mjJNT_SLIDE) { qpos_dim = 1; qvel_dim = 1; } - else if (jnt_type == mjJNT_HINGE) { qpos_dim = 1; qvel_dim = 1; } - - if (qpos_dim > 0) - { - FMjBodyKinematics Kinematics; - Kinematics.QPos.SetNum(qpos_dim); - Kinematics.QVel.SetNum(qvel_dim); - - for (int k=0; kqpos[qpos_adr + k]; - for (int k=0; kqvel[qvel_adr + k]; - - Frame.JointStates.Add(Name, Kinematics); - } - } - - TArray& LiveFrames = GetLiveFrames(); - LiveFrames.Add(Frame); - - if (LiveFrames.Num() > 1 && MaxRecordDuration > 0) - { - const double Cutoff = LiveFrames.Last().Timestamp - (double)MaxRecordDuration; - int32 FirstKeep = Algo::LowerBound(LiveFrames, Cutoff, - [](const FMjReplayFrame& F, double T) { return F.Timestamp < T; }); - if (FirstKeep > 0) - { - LiveFrames.RemoveAt(0, FirstKeep); - } - } - - if (LiveFrames.Num() % 60 == 0) - { - UE_LOG(LogURLabReplay, Verbose, TEXT("ReplayManager: Captured Frame at %f with %d joints recorded."), - Frame.Timestamp, Frame.JointStates.Num()); - } -} - -void AMjReplayManager::OnReplayStep(mjModel* m, mjData* d) -{ - if (!bIsReplaying || !m || !d) return; - - TArray& Frames = GetActiveFrames(); - if (Frames.Num() == 0) return; - - // Advance playback time on the physics thread (no race condition) - double PhysDt = m->opt.timestep; - double CurrentPT = PhysicsPlaybackTime.load(std::memory_order_relaxed); - if (Manager->PhysicsEngine && !Manager->PhysicsEngine->bIsPaused) - { - CurrentPT += PhysDt; - double TotalDuration = Frames.Last().Timestamp - Frames[0].Timestamp; - if (TotalDuration > 0 && CurrentPT > TotalDuration) - { - CurrentPT = 0.0; // Loop - } - PhysicsPlaybackTime.store(CurrentPT, std::memory_order_relaxed); - } - - double StartTime = Frames[0].Timestamp; - double TargetTime = StartTime + CurrentPT; - - int32 FoundIdx = Algo::LowerBound(Frames, TargetTime, - [](const FMjReplayFrame& Frame, double T) { return Frame.Timestamp < T; }); - if (FoundIdx > 0) --FoundIdx; - FoundIdx = FMath::Clamp(FoundIdx, 0, Frames.Num() - 1); - - // Compute interpolation alpha between FoundIdx and FoundIdx+1 - int32 NextIdx = FMath::Min(FoundIdx + 1, Frames.Num() - 1); - double Alpha = 0.0; - if (bInterpolateFrames && NextIdx > FoundIdx) - { - double T0 = Frames[FoundIdx].Timestamp; - double T1 = Frames[NextIdx].Timestamp; - double Span = T1 - T0; - if (Span > 1e-9) - { - Alpha = FMath::Clamp((TargetTime - T0) / Span, 0.0, 1.0); - } - } - - const FMjReplayFrame& Frame = Frames[FoundIdx]; - - bool bFirstFrame = bFirstReplayFrame; - if (d->time - LastReplayLogTime > 1.0) - { - UE_LOG(LogURLabReplay, Log, TEXT("ReplayManager: Playing '%s' Frame %f (PlaybackTime: %f). Index %d/%d"), - *ActiveSessionName, Frame.Timestamp, PlaybackTime, FoundIdx, Frames.Num()); - LastReplayLogTime = d->time; - } - - int32 MatchedJoints = 0; - int32 UnmatchedJoints = 0; - - // Pass 1: Suffix-match all joints and collect prefixes of matched articulations - TSet MatchedPrefixes; - TArray ResolvedKinematics; - ResolvedKinematics.SetNum(m->njnt); - for (int i = 0; i < m->njnt; ++i) ResolvedKinematics[i] = nullptr; - - for (int i = 0; i < m->njnt; ++i) - { - const char* name_ptr = mj_id2name(m, mjOBJ_JOINT, i); - if (!name_ptr) continue; - FString Name = UTF8_TO_TCHAR(name_ptr); - if (Name.IsEmpty()) continue; - - // Exact match - const FMjBodyKinematics* Kinematics = Frame.JointStates.Find(Name); - if (Kinematics) - { - ResolvedKinematics[i] = Kinematics; - continue; - } - - // Suffix match (CSV key is suffix of model name, or vice versa) - for (auto& Pair : Frame.JointStates) - { - if (Name.EndsWith(Pair.Key) && (Name.Len() == Pair.Key.Len() || Name[Name.Len() - Pair.Key.Len() - 1] == '_')) - { - ResolvedKinematics[i] = &Pair.Value; - FString Prefix = Name.Left(Name.Len() - Pair.Key.Len()); - MatchedPrefixes.Add(Prefix); - break; - } - } - - // Bare name match: strip instance prefix from both model and CSV names and compare - // This handles multi-instance replay (e.g. _C_0 matching data recorded from _C_1) - if (!ResolvedKinematics[i]) - { - // Find which binding this joint belongs to and extract its bare name - for (const FReplayArticulationBinding& B : ArticulationBindings) - { - if (!B.bEnabled || !B.Articulation.IsValid()) continue; - FString ArtName = B.Articulation->GetName(); - FString ArtPrefix = ArtName + TEXT("_"); - - if (Name.StartsWith(ArtPrefix)) - { - FString BareName = Name.Mid(ArtPrefix.Len()); - // Search CSV keys for one that ends with the same bare name - for (auto& Pair : Frame.JointStates) - { - if (Pair.Key.EndsWith(BareName)) - { - ResolvedKinematics[i] = &Pair.Value; - MatchedPrefixes.Add(ArtPrefix); - break; - } - } - break; - } - } - } - } - - // Pass 2: Free joint fallback — only for articulations that had other suffix matches - for (int i = 0; i < m->njnt; ++i) - { - if (ResolvedKinematics[i]) continue; - if (m->jnt_type[i] != mjJNT_FREE) continue; - - const char* name_ptr = mj_id2name(m, mjOBJ_JOINT, i); - if (!name_ptr) continue; - FString Name = UTF8_TO_TCHAR(name_ptr); - - // Check if this free joint's prefix matches a known matched articulation - for (const FString& Prefix : MatchedPrefixes) - { - if (Name.StartsWith(Prefix)) - { - // Find the 7-DOF CSV entry - for (auto& Pair : Frame.JointStates) - { - if (Pair.Value.QPos.Num() == 7) - { - ResolvedKinematics[i] = &Pair.Value; - break; - } - } - break; - } - } - } - - // Pass 3: Apply resolved kinematics with per-articulation filtering - // Cache CSV start position from first frame for RelPos - const FMjReplayFrame& FirstFrame = Frames[0]; - - for (int i = 0; i < m->njnt; ++i) - { - const char* name_ptr = mj_id2name(m, mjOBJ_JOINT, i); - if (!name_ptr) continue; - FString Name = UTF8_TO_TCHAR(name_ptr); - if (Name.IsEmpty()) continue; - - const FMjBodyKinematics* Kinematics = ResolvedKinematics[i]; - if (Kinematics) - { - // Find which articulation binding this joint belongs to - FReplayArticulationBinding* Binding = nullptr; - for (FReplayArticulationBinding& B : ArticulationBindings) - { - if (B.Articulation.IsValid() && Name.Contains(B.Articulation->GetName())) - { - Binding = &B; - break; - } - } - - // Skip if this articulation is disabled - if (Binding && !Binding->bEnabled) - { - UnmatchedJoints++; - continue; - } - - MatchedJoints++; - int qpos_adr = m->jnt_qposadr[i]; - int type = m->jnt_type[i]; - int expected_dim = (type == mjJNT_FREE) ? 7 : (type == mjJNT_BALL ? 4 : 1); - - if (Kinematics->QPos.Num() == expected_dim) - { - // Get next frame kinematics for interpolation (if available) - const FMjBodyKinematics* NextKinematics = nullptr; - if (Alpha > 0.0 && NextIdx > FoundIdx) - { - const FMjReplayFrame& NextFrame = Frames[NextIdx]; - NextKinematics = NextFrame.JointStates.Find(Name); - if (NextKinematics && NextKinematics->QPos.Num() != expected_dim) - NextKinematics = nullptr; // Dimension mismatch - } - - // Helper: lerp a qpos value between frames - auto LerpQPos = [&](int k) -> double { - double v0 = Kinematics->QPos[k]; - if (NextKinematics && Alpha > 0.0) - return v0 + Alpha * (NextKinematics->QPos[k] - v0); - return v0; - }; - - // Apply RelPos offset for free joints - if (type == mjJNT_FREE && Binding && Binding->bRelativePosition && expected_dim == 7) - { - // On first frame, capture both the CSV start position and the current MuJoCo position - if (!Binding->bInitialsCaptured) - { - Binding->InitialMjPosition = FVector(d->qpos[qpos_adr], d->qpos[qpos_adr + 1], d->qpos[qpos_adr + 2]); - - for (auto& Pair : FirstFrame.JointStates) - { - if (Pair.Value.QPos.Num() == 7) - { - Binding->CsvStartPosition = FVector(Pair.Value.QPos[0], Pair.Value.QPos[1], Pair.Value.QPos[2]); - break; - } - } - Binding->bInitialsCaptured = true; - - UE_LOG(LogURLabReplay, Log, TEXT("RelPos captured for '%s': MjPos=(%.4f,%.4f,%.4f) CsvStart=(%.4f,%.4f,%.4f)"), - *Binding->Articulation->GetName(), - Binding->InitialMjPosition.X, Binding->InitialMjPosition.Y, Binding->InitialMjPosition.Z, - Binding->CsvStartPosition.X, Binding->CsvStartPosition.Y, Binding->CsvStartPosition.Z); - } - - double offset_x = Binding->InitialMjPosition.X - Binding->CsvStartPosition.X; - double offset_y = Binding->InitialMjPosition.Y - Binding->CsvStartPosition.Y; - double offset_z = Binding->InitialMjPosition.Z - Binding->CsvStartPosition.Z; - - d->qpos[qpos_adr + 0] = LerpQPos(0) + offset_x; - d->qpos[qpos_adr + 1] = LerpQPos(1) + offset_y; - d->qpos[qpos_adr + 2] = LerpQPos(2) + offset_z; - - // Quaternion: use slerp for rotation interpolation - if (NextKinematics && Alpha > 0.0) - { - FQuat Q0(Kinematics->QPos[4], Kinematics->QPos[5], Kinematics->QPos[6], Kinematics->QPos[3]); - FQuat Q1(NextKinematics->QPos[4], NextKinematics->QPos[5], NextKinematics->QPos[6], NextKinematics->QPos[3]); - FQuat QInterp = FQuat::Slerp(Q0, Q1, (float)Alpha); - d->qpos[qpos_adr + 3] = QInterp.W; // MuJoCo quat: w,x,y,z - d->qpos[qpos_adr + 4] = QInterp.X; - d->qpos[qpos_adr + 5] = QInterp.Y; - d->qpos[qpos_adr + 6] = QInterp.Z; - } - else - { - for (int k = 3; k < 7; ++k) d->qpos[qpos_adr + k] = Kinematics->QPos[k]; - } - } - else - { - for (int k = 0; k < expected_dim; ++k) d->qpos[qpos_adr + k] = LerpQPos(k); - } - } - - int qvel_adr = m->jnt_dofadr[i]; - int expected_vel_dim = (type == mjJNT_FREE) ? 6 : (type == mjJNT_BALL ? 3 : 1); - if (Kinematics->QVel.Num() == expected_vel_dim) - { - for(int k=0; kqvel[qvel_adr+k] = Kinematics->QVel[k]; - } - } - else - { - UnmatchedJoints++; - if (bFirstFrame) - { - UE_LOG(LogURLabReplay, Warning, TEXT("ReplayManager: Model joint '%s' NOT found in CSV data"), *Name); - } - } - } - - if (bFirstFrame) - { - // Log CSV keys for comparison - UE_LOG(LogURLabReplay, Log, TEXT("ReplayManager: Joint match results: %d matched, %d unmatched out of %d model joints"), - MatchedJoints, UnmatchedJoints, m->njnt); - UE_LOG(LogURLabReplay, Log, TEXT("ReplayManager: CSV contains %d joint entries"), Frame.JointStates.Num()); - for (auto& Pair : Frame.JointStates) - { - UE_LOG(LogURLabReplay, Log, TEXT(" CSV joint: '%s' (%d DOF)"), *Pair.Key, Pair.Value.QPos.Num()); - } - bFirstReplayFrame = false; - } - - mj_forward(m, d); -} - -void AMjReplayManager::UpdateCache(mjModel* m) -{ - CachedJointNames.Empty(); - CachedJointNames.SetNum(m->njnt); - bCacheValid = true; - - for (int i = 0; i < m->njnt; ++i) - { - const char* name = mj_id2name(m, mjOBJ_JOINT, i); - if (name) - { - CachedJointNames[i] = UTF8_TO_TCHAR(name); - } - else - { - CachedJointNames[i] = FString::Printf(TEXT("Joint_%d"), i); - } - UE_LOG(LogURLabReplay, Log, TEXT("ReplayManager Cache: ID %d -> Name '%s'"), i, *CachedJointNames[i]); - } -} diff --git a/Plugins/UnrealRoboticsLab/Source/URLab/Private/URLab.cpp b/Plugins/UnrealRoboticsLab/Source/URLab/Private/URLab.cpp deleted file mode 100644 index b5bd7d35ef..0000000000 --- a/Plugins/UnrealRoboticsLab/Source/URLab/Private/URLab.cpp +++ /dev/null @@ -1,94 +0,0 @@ -// Copyright (c) 2026 Jonathan Embley-Riches. All rights reserved. -// -// Licensed under the Apache License, Version 2.0 (the "License"); -// you may not use this file except in compliance with the License. -// You may obtain a copy of the License at -// -// http://www.apache.org/licenses/LICENSE-2.0 -// -// Unless required by applicable law or agreed to in writing, software -// distributed under the License is distributed on an "AS IS" BASIS, -// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. -// See the License for the specific language governing permissions and -// limitations under the License. -// -// --- LEGAL DISCLAIMER --- -// UnrealRoboticsLab is an independent software plugin. It is NOT affiliated with, -// endorsed by, or sponsored by Epic Games, Inc. "Unreal" and "Unreal Engine" are -// trademarks or registered trademarks of Epic Games, Inc. in the US and elsewhere. -// -// This plugin incorporates third-party software: MuJoCo (Apache 2.0), -// CoACD (MIT), and libzmq (MPL 2.0). See ThirdPartyNotices.txt for details. - -#include "URLab.h" -#include "Misc/Paths.h" -#include "Misc/MessageDialog.h" -#include "Utils/URLabLogging.h" -#include "Interfaces/IPluginManager.h" - -#if WITH_EDITOR -#include "AssetToolsModule.h" -#include "IAssetTools.h" -#endif - -#define LOCTEXT_NAMESPACE "FURLabModule" -void FURLabModule::StartupModule() -{ - FString PluginDir = IPluginManager::Get().FindPlugin("UnrealRoboticsLab")->GetBaseDir(); - FString InstallDir = FPaths::Combine(PluginDir, TEXT("third_party/install")); - - // Function to load a DLL and log success/failure - auto LoadDependencyDLL = [&](const FString& LibraryName, const FString& SubDir) { - // Try plugin third-party path first (editor / development) - FString DLLPath = FPaths::Combine(InstallDir, SubDir, TEXT("bin"), LibraryName); - if (!FPaths::FileExists(DLLPath)) - { - // Packaged build: DLLs staged next to the executable - DLLPath = FPaths::Combine(FPlatformProcess::GetModulesDirectory(), LibraryName); - } - if (FPaths::FileExists(DLLPath)) { - void* Handle = FPlatformProcess::GetDllHandle(*DLLPath); - if (Handle) { - UE_LOG(LogURLab, Log, TEXT("Loaded %s from %s"), *LibraryName, *DLLPath); - return true; - } else { - UE_LOG(LogURLab, Error, TEXT("Failed to load %s from %s"), *LibraryName, *DLLPath); - } - } else { - UE_LOG(LogURLab, Error, TEXT("%s not found (searched plugin dir + binary dir)"), *LibraryName); - } - return false; - }; - -#if PLATFORM_WINDOWS - // Load MuJoCo. Since 3.7.0 the obj/stl decoders are compiled into - // mujoco.dll itself (changelog item 9); loading the standalone - // obj_decoder.dll / stl_decoder.dll would cause a plugin-registration - // collision and crash during module init. - LoadDependencyDLL(TEXT("mujoco.dll"), TEXT("MuJoCo")); - - // Load ZMQ - LoadDependencyDLL(TEXT("libzmq-v143-mt-4_3_6.dll"), TEXT("libzmq")); - - // Load CoACD (Shared library) - LoadDependencyDLL(TEXT("lib_coacd.dll"), TEXT("CoACD")); -#endif - - // Some CoACD dependencies like TBB or runtimes might be in CoACD/bin - // They should be loaded automatically if in search path, but we can verify here if needed. - -#if WITH_EDITOR - // Register custom asset category - IAssetTools& AssetTools = FModuleManager::LoadModuleChecked("AssetTools").Get(); - MuJoCoAssetCategoryBit = AssetTools.RegisterAdvancedAssetCategory(FName(TEXT("MuJoCo")), LOCTEXT("MujocoAssetCategory", "MuJoCo")); - UE_LOG(LogURLab, Log, TEXT("Registered MuJoCo Asset Category with bitmask: %u"), MuJoCoAssetCategoryBit); -#endif -} - -void FURLabModule::ShutdownModule() -{ -} - -#undef LOCTEXT_NAMESPACE - -IMPLEMENT_MODULE(FURLabModule, URLab) diff --git a/Plugins/UnrealRoboticsLab/Source/URLab/Public/CoACD/CoacdInterface.h b/Plugins/UnrealRoboticsLab/Source/URLab/Public/CoACD/CoacdInterface.h deleted file mode 100644 index 04e6aa9634..0000000000 --- a/Plugins/UnrealRoboticsLab/Source/URLab/Public/CoACD/CoacdInterface.h +++ /dev/null @@ -1,68 +0,0 @@ -// Copyright (c) 2026 Jonathan Embley-Riches. All rights reserved. -// -// Licensed under the Apache License, Version 2.0 (the "License"); -// you may not use this file except in compliance with the License. -// You may obtain a copy of the License at -// -// http://www.apache.org/licenses/LICENSE-2.0 -// -// Unless required by applicable law or agreed to in writing, software -// distributed under the License is distributed on an "AS IS" BASIS, -// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. -// See the License for the specific language governing permissions and -// limitations under the License. -// -// --- LEGAL DISCLAIMER --- -// UnrealRoboticsLab is an independent software plugin. It is NOT affiliated with, -// endorsed by, or sponsored by Epic Games, Inc. "Unreal" and "Unreal Engine" are -// trademarks or registered trademarks of Epic Games, Inc. in the US and elsewhere. -// -// This plugin incorporates third-party software: MuJoCo (Apache 2.0), -// CoACD (MIT), and libzmq (MPL 2.0). See ThirdPartyNotices.txt for details. - -#pragma once -#include "CoreMinimal.h" -#include "GameFramework/Actor.h" -#include "Kismet/GameplayStatics.h" -#include -#include -#include "Chaos/ArrayCollectionArray.h" -#include "CoACD/coacd.h" - -#include "Misc/FileHelper.h" -namespace CoacdInterface { - - -template -CoACD_Mesh ConvertToCoACDMesh(const Chaos::TArrayCollectionArray& Vertices, const TArray& Indices) { - - CoACD_Mesh coacdMesh; - - // Allocate memory for vertices - coacdMesh.vertices_count = Vertices.Num(); - coacdMesh.vertices_ptr = new double[coacdMesh.vertices_count * 3]; - - // Copy vertex data (FVector to double array) - for (int i = 0; i < Vertices.Num(); ++i) { - coacdMesh.vertices_ptr[3 * i] = Vertices[i].X; - coacdMesh.vertices_ptr[3 * i + 1] = Vertices[i].Y; - coacdMesh.vertices_ptr[3 * i + 2] = Vertices[i].Z; - } - - // Allocate memory for indices - coacdMesh.triangles_count = Indices.Num(); - coacdMesh.triangles_ptr = new int[coacdMesh.triangles_count * 3]; - - // Copy index data (int32 array) - for (int i = 0; i < Indices.Num(); ++i) { - coacdMesh.triangles_ptr[3 * i] = Indices[i][0]; - coacdMesh.triangles_ptr[3 * i + 1] = Indices[i][1]; - coacdMesh.triangles_ptr[3 * i + 2] = Indices[i][2]; - } - - return coacdMesh; -} -void SaveCoACDMeshAsOBJ(CoACD_Mesh& Mesh, const FString& FilePath) ; - -void SaveCoACDMeshArrayAsOBJ(CoACD_MeshArray& MeshArray, const FString& BaseFilePath) ; -} diff --git a/Plugins/UnrealRoboticsLab/Source/URLab/Public/MuJoCo/Components/Sensors/MjCamera.h b/Plugins/UnrealRoboticsLab/Source/URLab/Public/MuJoCo/Components/Sensors/MjCamera.h deleted file mode 100644 index a2a67e716e..0000000000 --- a/Plugins/UnrealRoboticsLab/Source/URLab/Public/MuJoCo/Components/Sensors/MjCamera.h +++ /dev/null @@ -1,229 +0,0 @@ -// Copyright (c) 2026 Jonathan Embley-Riches. All rights reserved. -// -// Licensed under the Apache License, Version 2.0 (the "License"); -// you may not use this file except in compliance with the License. -// You may obtain a copy of the License at -// -// http://www.apache.org/licenses/LICENSE-2.0 -// -// Unless required by applicable law or agreed to in writing, software -// distributed under the License is distributed on an "AS IS" BASIS, -// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. -// See the License for the specific language governing permissions and -// limitations under the License. -// -// --- LEGAL DISCLAIMER --- -// UnrealRoboticsLab is an independent software plugin. It is NOT affiliated with, -// endorsed by, or sponsored by Epic Games, Inc. "Unreal" and "Unreal Engine" are -// trademarks or registered trademarks of Epic Games, Inc. in the US and elsewhere. -// -// This plugin incorporates third-party software: MuJoCo (Apache 2.0), -// CoACD (MIT), and libzmq (MPL 2.0). See ThirdPartyNotices.txt for details. - -#pragma once - -#include "CoreMinimal.h" -#include "Components/SceneCaptureComponent2D.h" -#include "Engine/TextureRenderTarget2D.h" -#include "MuJoCo/Components/MjComponent.h" -#include "HAL/Runnable.h" -#include "HAL/ThreadSafeBool.h" -#include "Containers/Queue.h" -#include "MuJoCo/Components/Sensors/MjCameraTypes.h" -#include "MjCamera.generated.h" - -/** - * @class FCameraZmqWorker - * @brief Background thread for publishing high-bandwidth camera frames via ZeroMQ. - */ -class FCameraZmqWorker : public FRunnable -{ -public: - FCameraZmqWorker(const FString& InEndpoint, const FString& InTopic, FIntPoint InRes); - virtual ~FCameraZmqWorker(); - - virtual bool Init() override; - virtual uint32 Run() override; - virtual void Stop() override; - virtual void Exit() override; - - void PushFrame(const TArray& FrameData); - FString GetBoundEndpoint() const { return BoundEndpoint; } - -private: - FString RequestedEndpoint; - FString BoundEndpoint; - FString Topic; - FIntPoint Resolution; - - void* ZmqContext = nullptr; - void* ZmqPublisher = nullptr; - - FThreadSafeBool bStopThread; - TQueue, EQueueMode::Spsc> FrameQueue; -}; - -/** - * @class UMjCamera - * @brief Represents a MuJoCo element as an Unreal sensor component. - * - * Placed in the Sensors group because cameras are observation devices, not geometry. - * The component is cheap by default: no render target or GPU cost is incurred until - * SetStreamingEnabled(true) is called. - * - * Key design points: - * - No ExportTo / RegisterToSpec — camera is UE-side only, not fed back to MuJoCo. - * - SetStreamingEnabled() allocates the RT and calls IStreamingManager::AddViewInformation - * so textures load correctly even when the player pawn is far away. - * - RequestReadback() enqueues a non-blocking GPU→CPU copy; check IsReadbackReady() - * on a subsequent tick, then consume with ConsumePixels(). - */ -UCLASS(ClassGroup=(Custom), meta=(BlueprintSpawnableComponent)) -class URLAB_API UMjCamera : public UMjComponent -{ - GENERATED_BODY() - -public: - UMjCamera(); - - // ---- Identification ---- - - - // ---- Camera Intrinsics ---- - - /** @brief Vertical field of view in degrees (fovy= attribute). */ - UPROPERTY(EditAnywhere, BlueprintReadWrite, Category = "MuJoCo|Camera") - float Fovy = 45.0f; - - /** @brief Capture resolution (pixels). Defaults to 640×480. */ - UPROPERTY(EditAnywhere, BlueprintReadWrite, Category = "MuJoCo|Camera") - FIntPoint Resolution = FIntPoint(640, 480); - - /** @brief What this camera captures. Read at SetStreamingEnabled(true) time — - * toggle streaming off/on after changing. */ - UPROPERTY(EditAnywhere, BlueprintReadWrite, Category = "MuJoCo|Camera") - EMjCameraMode CaptureMode = EMjCameraMode::Real; - - /** @brief Near clip plane for Depth capture, centimetres. Values below this read as 0. */ - UPROPERTY(EditAnywhere, BlueprintReadWrite, Category = "MuJoCo|Camera", - meta = (EditCondition = "CaptureMode == EMjCameraMode::Depth", ClampMin = "0.1")) - float DepthNearCm = 10.0f; - - /** @brief Far clip plane for Depth capture, centimetres. Values beyond read as the maximum. */ - UPROPERTY(EditAnywhere, BlueprintReadWrite, Category = "MuJoCo|Camera", - meta = (EditCondition = "CaptureMode == EMjCameraMode::Depth", ClampMin = "1.0")) - float DepthFarCm = 10000.0f; - - // ---- Streaming ---- - - /** @brief Boost factor passed to IStreamingManager::AddViewInformation. - * Increase to force higher-quality texture mips near this camera. */ - UPROPERTY(EditAnywhere, BlueprintReadWrite, Category = "MuJoCo|Camera|Streaming") - float StreamingBoost = 1.0f; - - // ---- Capture Components ---- - - /** @brief The underlying SceneCaptureComponent2D. Capture is disabled by default. */ - UPROPERTY(VisibleAnywhere, BlueprintReadOnly, Category = "MuJoCo|Camera") - USceneCaptureComponent2D* CaptureComponent; - - /** @brief The render target. Null until SetStreamingEnabled(true) is first called. */ - UPROPERTY(VisibleAnywhere, BlueprintReadOnly, Category = "MuJoCo|Camera") - UTextureRenderTarget2D* RenderTarget = nullptr; - - // ---- ZeroMQ Streaming ---- - - /** @brief If true, the camera will automatically broadcast its frames over ZeroMQ when streaming is enabled. */ - UPROPERTY(EditAnywhere, BlueprintReadWrite, Category = "MuJoCo|Camera|Network") - bool bEnableZmqBroadcast = false; - - /** @brief The ZMQ Endpoint for this specific camera (e.g., tcp://\*:5558). Must be unique per camera. */ - UPROPERTY(EditAnywhere, BlueprintReadWrite, Category = "MuJoCo|Camera|Network") - FString ZmqEndpoint = TEXT("tcp://*:5558"); - - // ---- Public API ---- - - /** - * @brief Allocates the render target and begins streaming / scene capture. - * Call with bEnable=false to stop rendering and free the capture budget. - */ - UFUNCTION(BlueprintCallable, Category = "MuJoCo|Camera") - void SetStreamingEnabled(bool bEnable); - - /** - * @brief Enqueues a non-blocking asynchronous GPU→CPU pixel readback. - * No-op if streaming is not enabled or a readback is already in flight. - */ - UFUNCTION(BlueprintCallable, Category = "MuJoCo|Camera") - void RequestReadback(); - - /** - * @brief Returns true when the GPU→CPU copy started by RequestReadback() is complete. - */ - UFUNCTION(BlueprintCallable, Category = "MuJoCo|Camera") - bool IsReadbackReady() const; - - /** - * @brief Consumes and returns the pixel array from the last completed readback. - * Returns an empty array if not ready. Clears the pending state. - */ - UFUNCTION(BlueprintCallable, Category = "MuJoCo|Camera") - TArray ConsumePixels(); - - /** - * @brief Returns the ZMQ endpoint actually bound (may differ from ZmqEndpoint if auto-incremented). - */ - UFUNCTION(BlueprintCallable, Category = "MuJoCo|Camera") - FString GetActualZmqEndpoint() const; - - /** - * @brief Returns the bound camera component pointer (for UI wiring). - */ - UFUNCTION(BlueprintCallable, Category = "MuJoCo|Camera") - UMjCamera* GetSelf() { return this; } - - /** - * @brief Exports camera properties to a MuJoCo spec camera structure. - * @param cam Pointer to the target mjsCamera structure. - * @param def Optional default structure (unused, kept for API consistency). - */ - void ExportTo(mjsCamera* cam, mjsDefault* def = nullptr); - - /** - * @brief Imports properties from a MuJoCo XML node. - * Reads: name, fovy, pos, quat, euler, xyaxes, zaxis, - * width/height (MuJoCo 3.x resolution hints). - */ - void ImportFromXml(const class FXmlNode* Node); - void ImportFromXml(const class FXmlNode* Node, const struct FMjCompilerSettings& CompilerSettings); - -protected: - virtual void BeginPlay() override; - virtual void TickComponent(float DeltaTime, ELevelTick TickType, - FActorComponentTickFunction* ThisTickFunction) override; - virtual void OnRegister() override; - virtual void EndPlay(const EEndPlayReason::Type EndPlayReason) override; - -private: - // ---- Internal helpers ---- - void SetupRenderTarget(); - void RegisterWithStreamingManager(); - - /** For non-seg modes: rebuild HiddenComponents from the currently-live seg pools. - * Cheap (N siblings pointer copies), called each tick so a late-starting seg - * camera doesn't contaminate an already-streaming RGB/Depth camera. */ - void RefreshHiddenComponentsFromSegPools(); - - // ---- Readback state ---- - TOptional> PendingPixels; - FRenderCommandFence ReadbackFence; - bool bReadbackPending = false; - bool bReadbackComplete = false; - - // ---- Streaming state ---- - bool bStreamingEnabled = false; - - // ---- ZMQ Worker ---- - FCameraZmqWorker* ZmqWorker = nullptr; - FRunnableThread* WorkerThread = nullptr; -}; diff --git a/Plugins/UnrealRoboticsLab/Source/URLab/URLab.Build.cs b/Plugins/UnrealRoboticsLab/Source/URLab/URLab.Build.cs deleted file mode 100644 index 3653a309a7..0000000000 --- a/Plugins/UnrealRoboticsLab/Source/URLab/URLab.Build.cs +++ /dev/null @@ -1,409 +0,0 @@ -// Copyright Epic Games, Inc. All Rights Reserved. - - -using System; -using System.Diagnostics; -using UnrealBuildTool; -using System.IO; -using System.Text.RegularExpressions; - -public class URLab : ModuleRules -{ - public URLab(ReadOnlyTargetRules Target) : base(Target) - { - PCHUsage = ModuleRules.PCHUsageMode.UseExplicitOrSharedPCHs; - CppStandard = CppStandardVersion.Cpp17; - //bUseRTTI = true; // Required for type info - //bEnableExceptions = true; - - PublicDependencyModuleNames.AddRange(new string[] - { - "Core", - "CoreUObject", - "Engine", - "InputCore", - "PhysicsCore", - "XmlParser", - "HTTP", - "Json", - "JsonUtilities", - "Slate", - "SlateCore", - "Projects", - "ApplicationCore", - "RenderCore", - "RHI", - "UMG", - "AssetRegistry", - "DynamicMesh", - "ProceduralMeshComponent" - // "GeometryFramework", - // "GeometryCore" - }); - - // Editor-only dependencies for DecomposeMesh and other #if WITH_EDITOR code - if (Target.bBuildEditor) - { - PrivateDependencyModuleNames.AddRange(new string[] - { - "UnrealEd", - "AssetTools", - "DesktopPlatform" - }); - } - - PrivateDependencyModuleNames.AddRange(new string[] - { - "CinematicCamera", - "ImageWrapper", - "EnhancedInput", - "Chaos", - "Landscape", - "Eigen", - }); - - DynamicallyLoadedModuleNames.AddRange(new string[] - { - // Add dynamically loaded modules here - }); - - if (Target.Platform == UnrealTargetPlatform.Win64) - { - PublicSystemLibraries.AddRange(new string[] - { - "kernel32.lib", - "user32.lib" - }); - } - else if (Target.Platform == UnrealTargetPlatform.Linux) - { - PublicSystemLibraries.AddRange(new string[] - { - "pthread", "dl", "rt" - }); - } - - if (Target.Platform == UnrealTargetPlatform.Linux) - { - // Ensure Unreal recognizes MuJoCo's export macros - } - - - if (Target.Platform == UnrealTargetPlatform.Linux) - { - PublicDefinitions.Add("__linux__=1"); - PublicDefinitions.Add("__unix__=1"); - // MuJoCo 3.x compatibility (symbols removed from 2.x) - PublicDefinitions.Add("mjNPOLY=0"); - PublicDefinitions.Add("mjENBL_SLEEP=0"); - PublicDefinitions.Add("mjGAIN_DCMOTOR=mjGAIN_AFFINE"); - PublicDefinitions.Add("mjBIAS_DCMOTOR=mjBIAS_AFFINE"); - PublicDefinitions.Add("mjDYN_DCMOTOR=mjDYN_NONE"); - } - - - VerifyThirdPartyInstalls(); - - AddMuj(Target); - // CoACD: manually add include path (symlink not followed by Directory.Exists) - PublicIncludePaths.Add(Path.Combine(ThirdPartyPath, "CoACD", "CoACD", "include")); - AddCoACD(Target); - AddZeroMQ(Target); - } - - private string ThirdPartyPath - { - get { return Path.Combine(PluginDirectory, "third_party", "install"); } - } - - private void AddThirdPartyLibrary(string LibraryName, ReadOnlyTargetRules Target) - { - string FullPath = Path.Combine(ThirdPartyPath, LibraryName); - - // Add include directory if it exists - string IncludePath = Path.Combine(FullPath, "include"); - if (Directory.Exists(IncludePath)) - { - PublicIncludePaths.Add(IncludePath); - } - - if (Target.Platform == UnrealTargetPlatform.Win64) - { - // Link all libraries in the lib directory - string LibPath = Path.Combine(FullPath, "lib"); - if (Directory.Exists(LibPath)) - { - string[] LibFiles = Directory.GetFiles(LibPath, "*.lib", SearchOption.AllDirectories); - foreach (string LibFile in LibFiles) - { - PublicAdditionalLibraries.Add(LibFile); - } - } - - // Stage DLLs next to executable for packaged builds - // Skip MSVC runtime DLLs (handled by redistributable installer) - string BinPath = Path.Combine(FullPath, "bin"); - if (Directory.Exists(BinPath)) - { - string[] DllFiles = Directory.GetFiles(BinPath, "*.dll", SearchOption.AllDirectories); - foreach (string DllFile in DllFiles) - { - string DllName = Path.GetFileName(DllFile); - if (DllName.StartsWith("vcruntime") || DllName.StartsWith("msvcp") || DllName.StartsWith("concrt")) - continue; - RuntimeDependencies.Add("$(BinaryOutputDir)/" + DllName, DllFile, StagedFileType.NonUFS); - PublicDelayLoadDLLs.Add(DllName); - } - } - } - else if (Target.Platform == UnrealTargetPlatform.Linux) - { - // Link all static libraries and shared objects for Linux - string LibPath = Path.Combine(FullPath, "lib"); - if (Directory.Exists(LibPath)) - { - foreach (string LibFile in Directory.GetFiles(LibPath, "*.a", SearchOption.AllDirectories)) - { - PublicAdditionalLibraries.Add(LibFile); - } - foreach (string LibFile in Directory.GetFiles(LibPath, "*.so", SearchOption.AllDirectories)) - { - PublicAdditionalLibraries.Add(LibFile); - RuntimeDependencies.Add(LibFile); - PublicDelayLoadDLLs.Add(Path.GetFileName(LibFile)); - } - } - - // Add binaries (often plugin/shared objects) for Linux - string BinPath = Path.Combine(FullPath, "bin"); - if (Directory.Exists(BinPath)) - { - foreach (string BinFile in Directory.GetFiles(BinPath, "*.so", SearchOption.AllDirectories)) - { - RuntimeDependencies.Add(BinFile); - PublicDelayLoadDLLs.Add(Path.GetFileName(BinFile)); - PublicAdditionalLibraries.Add(BinFile); - } - } - } - } - - // Set to true to skip the submodule/install SHA drift checks. The default - // (false) catches two common failure modes before any code compiles: - // A) "I git-pulled URLab but forgot `git submodule update`" - the pinned - // submodule SHA has moved but the working tree still points at the old - // one, so we'd build against stale MuJoCo/CoACD/libzmq source. - // B) "I updated submodules but forgot to re-run third_party/*/build.ps1" - - // the source is fresh but third_party/install// was built from an - // older SHA, so UE links against a mismatched binary. - // Flip this to true if you are intentionally iterating on a submodule - // locally. The per-dep build scripts accept -NoSubmoduleSync / --no-submodule-sync - // for the same purpose at build-script time. - // Static readonly (not const) so toggling to `true` doesn't produce - // CS0162 "unreachable code" warnings in the active branch. - private static readonly bool SkipThirdPartyDriftChecks = false; - - // Names match the directories under third_party/ and third_party/install/. - private static readonly string[] ThirdPartyPackages = new[] { "MuJoCo", "CoACD", "libzmq" }; - - protected void AddMuj(ReadOnlyTargetRules Target) - { - AddThirdPartyLibrary("MuJoCo", Target); - } - - // Checks each third-party dep for submodule drift (Layer A) and install - // drift (Layer B). Runs once up front so any failure message is clearly - // attributable before UBT tries to resolve libs/includes. If URLab itself - // isn't a git checkout (release zip), the checks self-disable with a note. - private void VerifyThirdPartyInstalls() - { - if (SkipThirdPartyDriftChecks) - { - Console.WriteLine("URLab: SkipThirdPartyDriftChecks=true - skipping submodule/install SHA checks."); - return; - } - - string PluginRoot = PluginDirectory; - bool IsGitCheckout = Directory.Exists(Path.Combine(PluginRoot, ".git")) - || File.Exists(Path.Combine(PluginRoot, ".git")); - if (!IsGitCheckout) - { - Console.WriteLine("URLab: plugin root is not a git checkout - skipping submodule/install SHA checks."); - return; - } - - RegisterDriftCheckDependencies(PluginRoot); - - foreach (string Package in ThirdPartyPackages) - { - VerifyPackageInstall(PluginRoot, Package); - } - } - - // Tell UBT to re-evaluate this module when any of these files change so - // the Layer A/B drift checks fire on the next build. Without this, UBT's - // makefile cache short-circuits with "Target is up to date" and Build.cs - // never runs, letting a stale install slip through. - private void RegisterDriftCheckDependencies(string PluginRoot) - { - // URLab's own index - covers `git pull` / `git reset` on the plugin, - // which is how an out-of-sync submodule pointer typically appears. - string PluginGitIndex = Path.Combine(PluginRoot, ".git", "index"); - if (File.Exists(PluginGitIndex)) - { - ExternalDependencies.Add(PluginGitIndex); - } - - foreach (string Package in ThirdPartyPackages) - { - // Submodule HEAD file - Git rewrites this on checkout/reset/update, - // so it's the right signal for "submodule HEAD moved" (Layer A). - string SubmoduleGitHead = Path.Combine(PluginRoot, ".git", "modules", - "third_party", Package, "src", "HEAD"); - if (File.Exists(SubmoduleGitHead)) - { - ExternalDependencies.Add(SubmoduleGitHead); - } - - // INSTALLED_SHA.txt - changes whenever build.ps1 re-installs, or - // (as in our tests) when edited by hand. Covers Layer B. - string ShaFile = Path.Combine(ThirdPartyPath, Package, "INSTALLED_SHA.txt"); - if (File.Exists(ShaFile)) - { - ExternalDependencies.Add(ShaFile); - } - } - } - - private void VerifyPackageInstall(string PluginRoot, string Package) - { - string SubmoduleRelPath = "third_party/" + Package + "/src"; - string SubmoduleAbsPath = Path.Combine(PluginRoot, "third_party", Package, "src"); - string InstallDir = Path.Combine(ThirdPartyPath, Package); - string ShaFile = Path.Combine(InstallDir, "INSTALLED_SHA.txt"); - - // Submodule not initialised yet: directory missing, or present but empty. - bool SubmoduleMissing = !Directory.Exists(SubmoduleAbsPath) - || Directory.GetFileSystemEntries(SubmoduleAbsPath).Length == 0; - if (SubmoduleMissing) - { - throw new BuildException( - "{0} submodule not initialised at '{1}'. From the plugin root run:\n" + - " git submodule update --init --recursive --force\n" + - "then re-run third_party/{0}/build.ps1 (Windows) or build.sh (Linux/macOS). " + - "To bypass this check, set SkipThirdPartyDriftChecks=true in URLab.Build.cs.", - Package, SubmoduleAbsPath); - } - - // LAYER A: submodule HEAD vs URLab's committed pointer. - // Catches "pulled URLab but forgot `git submodule update`". - string ExpectedSha; - try - { - string Pointer = RunGit(PluginRoot, "ls-tree HEAD " + SubmoduleRelPath); - ExpectedSha = ParseSubmodulePointerSha(Pointer, Package); - } - catch (Exception Ex) - { - // Shallow clones or repos without a HEAD pointing at the submodule - // (edge cases) can't run Layer A. Warn and continue to Layer B. - Console.WriteLine("URLab: could not read URLab pointer for {0} ({1}) - skipping Layer A check.", - Package, Ex.Message); - ExpectedSha = null; - } - - string ActualSha; - try - { - ActualSha = RunGit(SubmoduleAbsPath, "rev-parse HEAD"); - } - catch (Exception Ex) - { - throw new BuildException( - "{0} submodule at '{1}' is not a valid git checkout ({2}). From the plugin root run:\n" + - " git submodule update --init --recursive --force\n" + - "then re-run third_party/{0}/build.ps1.", - Package, SubmoduleAbsPath, Ex.Message); - } - - if (ExpectedSha != null && !string.Equals(ExpectedSha, ActualSha, StringComparison.OrdinalIgnoreCase)) - { - throw new BuildException( - "{0} submodule drift: URLab expects SHA {1} but third_party/{0}/src/ is at {2}. " + - "From the plugin root run:\n" + - " git submodule update --init --recursive --force\n" + - "then re-run third_party/{0}/build.ps1 (Windows) or build.sh (Linux/macOS). " + - "To bypass this check, set SkipThirdPartyDriftChecks=true in URLab.Build.cs.", - Package, ExpectedSha, ActualSha); - } - - // LAYER B: install SHA (what build.ps1 recorded) vs submodule HEAD. - // Catches "updated submodules but forgot to re-run build.ps1". - if (!File.Exists(ShaFile)) - { - throw new BuildException( - "{0} install is missing '{1}'. Run third_party/{0}/build.ps1 (Windows) or " + - "third_party/{0}/build.sh (Linux/macOS). " + - "To bypass this check, set SkipThirdPartyDriftChecks=true in URLab.Build.cs.", - Package, ShaFile); - } - - string InstalledSha = File.ReadAllText(ShaFile).Trim(); - if (!string.Equals(InstalledSha, ActualSha, StringComparison.OrdinalIgnoreCase)) - { - throw new BuildException( - "{0} install is stale: built from SHA {1} but third_party/{0}/src/ is now at {2}. " + - "Re-run third_party/{0}/build.ps1 (Windows) or third_party/{0}/build.sh (Linux/macOS). " + - "To bypass this check, set SkipThirdPartyDriftChecks=true in URLab.Build.cs.", - Package, InstalledSha, ActualSha); - } - } - - private static string RunGit(string WorkingDir, string Arguments) - { - ProcessStartInfo PSI = new ProcessStartInfo("git", Arguments) - { - WorkingDirectory = WorkingDir, - RedirectStandardOutput = true, - RedirectStandardError = true, - UseShellExecute = false, - CreateNoWindow = true, - }; - using (Process Proc = Process.Start(PSI)) - { - string Stdout = Proc.StandardOutput.ReadToEnd(); - string Stderr = Proc.StandardError.ReadToEnd(); - Proc.WaitForExit(); - if (Proc.ExitCode != 0) - { - throw new Exception(string.Format("git {0} exited {1}: {2}", - Arguments, Proc.ExitCode, Stderr.Trim())); - } - return Stdout.Trim(); - } - } - - private static string ParseSubmodulePointerSha(string Output, string Package) - { - // `git ls-tree HEAD ` output: "160000 commit \t" - Match M = Regex.Match(Output, @"160000\s+commit\s+([0-9a-f]{40})"); - if (!M.Success) - { - throw new Exception(string.Format( - "could not parse submodule pointer for {0}: '{1}'", - Package, Output)); - } - return M.Groups[1].Value; - } - - protected void AddCoACD(ReadOnlyTargetRules Target) - { - AddThirdPartyLibrary("CoACD", Target); - PublicDefinitions.Add("COACD_EXPORTS=1"); - } - - protected void AddZeroMQ(ReadOnlyTargetRules Target) - { - AddThirdPartyLibrary("libzmq", Target); - } -} diff --git a/Plugins/UnrealRoboticsLab/Source/URLabEditor/Private/MujocoMeshImporter.cpp b/Plugins/UnrealRoboticsLab/Source/URLabEditor/Private/MujocoMeshImporter.cpp deleted file mode 100644 index 63ac1619ff..0000000000 --- a/Plugins/UnrealRoboticsLab/Source/URLabEditor/Private/MujocoMeshImporter.cpp +++ /dev/null @@ -1,628 +0,0 @@ -// Copyright (c) 2026 Jonathan Embley-Riches. All rights reserved. -// -// Licensed under the Apache License, Version 2.0 (the "License"); -// you may not use this file except in compliance with the License. -// You may obtain a copy of the License at -// -// http://www.apache.org/licenses/LICENSE-2.0 -// -// Unless required by applicable law or agreed to in writing, software -// distributed under the License is distributed on an "AS IS" BASIS, -// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. -// See the License for the specific language governing permissions and -// limitations under the License. -// -// --- LEGAL DISCLAIMER --- -// UnrealRoboticsLab is an independent software plugin. It is NOT affiliated with, -// endorsed by, or sponsored by Epic Games, Inc. "Unreal" and "Unreal Engine" are -// trademarks or registered trademarks of Epic Games, Inc. in the US and elsewhere. -// -// This plugin incorporates third-party software: MuJoCo (Apache 2.0), -// CoACD (MIT), and libzmq (MPL 2.0). See ThirdPartyNotices.txt for details. - -// MujocoMeshImporter.cpp — Mesh and material import methods for UMujocoGenerationAction. - -#include "MujocoGenerationAction.h" -#include "URLabEditorLogging.h" -#include "Engine/StaticMesh.h" -#include "AssetToolsModule.h" -#include "Factories/FbxFactory.h" -#include "Factories/FbxImportUI.h" -#include "Factories/FbxStaticMeshImportData.h" -#include "AssetImportTask.h" -#include "FileHelpers.h" -#include "IImageWrapperModule.h" -#include "IImageWrapper.h" -#include "ImageUtils.h" -#include "AssetRegistry/AssetRegistryModule.h" -#include "UObject/SavePackage.h" - -UStaticMesh* UMujocoGenerationAction::ImportSingleMesh(const FString& SourcePath, const FString& DestinationPath) -{ - if (SourcePath.IsEmpty() || DestinationPath.IsEmpty()) { - return nullptr; - } - - FString FileName = FPaths::GetBaseFilename(SourcePath); - FString PackageName = FPaths::Combine(DestinationPath, FileName); - PackageName = UPackageTools::SanitizePackageName(PackageName); - - UStaticMesh* ExistingMesh = LoadObject(nullptr, *PackageName); - if (ExistingMesh) return ExistingMesh; - - UE_LOG(LogURLabEditor, Log, TEXT("Importing mesh from: %s to %s"), *SourcePath, *DestinationPath); - - // 优先考虑文件格式: FBX > GLB > GLTF > Original (OBJ/STL) - FString ActualSourcePath = SourcePath; - FString BasePath = FPaths::ChangeExtension(SourcePath, ""); - - // 按优先顺序检查格式 - TArray Extensions = { TEXT("fbx"), TEXT("glb"), TEXT("gltf") }; - bool bFoundHigherPriority = false; - - for (const FString& Ext : Extensions) - { - FString PotentialPath = BasePath + TEXT(".") + Ext; - if (FPaths::FileExists(PotentialPath)) - { - ActualSourcePath = PotentialPath; - bFoundHigherPriority = true; - UE_LOG(LogURLabEditor, Log, TEXT("Found higher priority mesh file: %s"), *ActualSourcePath); - break; - } - } - - // 如果没有找到高优先级格式,确保原始存在 - if (!bFoundHigherPriority && !FPaths::FileExists(ActualSourcePath)) - { - UE_LOG(LogURLabEditor, Error, TEXT("Source mesh file does not exist: %s"), *ActualSourcePath); - return nullptr; - } - - IAssetTools& AssetTools = FModuleManager::LoadModuleChecked("AssetTools").Get(); - - // 先试着用 MikkTSpace 导入(效果最好) - // 使用 ActualSourcePath 替代 SourcePath - UStaticMesh* ImportedMesh = AttemptMeshImport(ActualSourcePath, DestinationPath, EFBXNormalGenerationMethod::MikkTSpace); - - // 验证网格 - if (ImportedMesh && ValidateMesh(ImportedMesh, FileName)) - { - UE_LOG(LogURLabEditor, Log, TEXT("Successfully imported mesh '%s' with MikkTSpace"), *FileName); - return ImportedMesh; - } - - // MikkTSpace 失败或网格无效 - 尝试使用内置法线作为备用 - if (ImportedMesh) - { - UE_LOG(LogURLabEditor, Warning, TEXT("Mesh '%s' has issues with MikkTSpace, attempting fallback with BuiltIn normals"), *FileName); - } - else - { - UE_LOG(LogURLabEditor, Warning, TEXT("Failed to import mesh '%s' with MikkTSpace, attempting fallback"), *FileName); - } - - ImportedMesh = AttemptMeshImport(ActualSourcePath, DestinationPath, EFBXNormalGenerationMethod::BuiltIn); - - if (ImportedMesh && ValidateMesh(ImportedMesh, FileName)) - { - UE_LOG(LogURLabEditor, Warning, TEXT("Successfully imported mesh '%s' with BuiltIn normals (fallback)"), *FileName); - return ImportedMesh; - } - - UE_LOG(LogURLabEditor, Error, TEXT("Failed to import mesh '%s' - all import methods failed"), *FileName); - return nullptr; -} - -UStaticMesh* UMujocoGenerationAction::AttemptMeshImport(const FString& SourcePath, const FString& DestinationPath, EFBXNormalGenerationMethod::Type NormalMethod) -{ - IAssetTools& AssetTools = FModuleManager::LoadModuleChecked("AssetTools").Get(); - - // 配置自动导入任务 - UAssetImportTask* ImportTask = NewObject(); - // 规范斜杠并转为绝对路径 - FString NormalizedSourcePath = SourcePath; - FPaths::MakeStandardFilename(NormalizedSourcePath); // 使用标准分隔符('/') - NormalizedSourcePath = FPaths::ConvertRelativePathToFull(NormalizedSourcePath); // 变为绝对路径 - FPaths::NormalizeFilename(NormalizedSourcePath); // 清理重复分隔符、末尾斜杠等 - ImportTask->Filename = NormalizedSourcePath; - ImportTask->DestinationPath = DestinationPath; // 导入的资产生成在:/Game/MuJoCoImports/g1_29dof_rev_1_0_ue_Assets/Meshes - ImportTask->bAutomated = true; - ImportTask->bSave = true; - ImportTask->bReplaceExisting = true; - ImportTask->bReplaceExistingSettings = true; - - // 仅配置 FBX 工厂以支持 FBX/OBJ - FString Extension = FPaths::GetExtension(SourcePath).ToLower(); - - if (Extension == "fbx" || Extension == "obj" || Extension == "t3d") - { - // 配置 FBX 工厂 - UFbxFactory* FbxFactory = NewObject(); - ImportTask->Factory = FbxFactory; - - // 配置 FBX 导入 UI 设置 - UFbxImportUI* ImportUI = NewObject(); - ImportUI->bImportMesh = true; - ImportUI->bImportTextures = false; - ImportUI->bImportMaterials = false; - ImportUI->bAutomatedImportShouldDetectType = false; - ImportUI->MeshTypeToImport = FBXIT_StaticMesh; - - // 鲁棒的的静态网格设置 - ImportUI->StaticMeshImportData->bCombineMeshes = true; - ImportUI->StaticMeshImportData->bRemoveDegenerates = true; - ImportUI->StaticMeshImportData->bComputeWeightedNormals = true; - ImportUI->StaticMeshImportData->bGenerateLightmapUVs = true; - ImportUI->StaticMeshImportData->NormalImportMethod = EFBXNormalImportMethod::FBXNIM_ComputeNormals; - ImportUI->StaticMeshImportData->NormalGenerationMethod = NormalMethod; - - // 用于修复退化几何体(特别是来自 OBJ 文件)的其他设置 - ImportUI->StaticMeshImportData->bAutoGenerateCollision = false; // 我们单独处理碰撞。 - ImportUI->StaticMeshImportData->bBuildReversedIndexBuffer = true; - // ImportUI->StaticMeshImportData->bBuildNanite = false; // Nanite 需要清晰的几何结构(UE4中没有Nanite) - - // 顶点焊接——对于修复导致切线退化的重叠顶点至关重要。 - // 注意:UE 5.7 中没有直接的 bWeldVertices 函数,但 bRemoveDegenerates 函数可以处理这个问题。 - - // 将 UI 应用于工厂 - FbxFactory->ImportUI = ImportUI; - FbxFactory->EnableShowOption(); - } - else - { - // 对于其他格式(GLTF、GLB 等),让虚幻引擎的资源工具自动查找合适的工厂。 - // 我们无需手动设置工厂,因此 ImportAssetTasks 会自动检测正确的工厂。 - // 注意:我们会丢失一些细粒度的设置(例如 NormalGenerationMethod), - // 但 GLTF 导入器通常依赖于文件本身的数据,这些数据通常比 OBJ 格式更清晰。 - // ImportTask->Factory = nullptr; - - UE_LOG(LogURLabEditor, Log, TEXT("Using automated factory detection for mesh: %s"), *SourcePath); - } - - // 运行导入 - TArray ImportTasks; - ImportTasks.Add(ImportTask); - AssetTools.ImportAssetTasks(ImportTasks); - - // 获取结果 - TArray ImportedAssets; - // TArray t = ImportTask->ImportedObjectPaths; - for (UObject* Obj : ImportTask->Result) // for (UObject* Obj : ImportTask->GetObjects()) - { - if (Obj) ImportedAssets.Add(Obj); - } - - // 记录所有导入的资产以进行调试 - UE_LOG(LogURLabEditor, Log, TEXT("[ImportSingleMesh] Import returned %d objects:"), ImportedAssets.Num()); - for (int32 i = 0; i < ImportedAssets.Num(); ++i) - { - UObject* Obj = ImportedAssets[i]; - UE_LOG(LogURLabEditor, Log, TEXT(" [%d] %s (%s) at %s"), - i, *Obj->GetName(), *Obj->GetClass()->GetName(), *Obj->GetPathName()); - } - - // 在所有导入的资源中搜索 StaticMesh(GLB 导入可能首先返回纹理) - UStaticMesh* Mesh = nullptr; - for (UObject* Obj : ImportedAssets) - { - Mesh = Cast(Obj); - if (Mesh) break; - } - - // 如果在直接搜索结果中找不到,请搜索 Interchange 可能使用的子文件夹路径。 - if (!Mesh) - { - FString MeshName = FPaths::GetBaseFilename(SourcePath); - - // 尝试不同的子文件夹模式,互换使用 - TArray SearchPaths = { - FString::Printf(TEXT("%s/%s/StaticMeshes/%s.%s"), *DestinationPath, *MeshName, *MeshName, *MeshName), - FString::Printf(TEXT("%s/%s/StaticMeshes/%s"), *DestinationPath, *MeshName, *MeshName), - FString::Printf(TEXT("%s/%s.%s"), *DestinationPath, *MeshName, *MeshName), - }; - - for (const FString& SearchPath : SearchPaths) - { - Mesh = LoadObject(nullptr, *SearchPath); - if (Mesh) - { - UE_LOG(LogURLabEditor, Log, TEXT("[ImportSingleMesh] Found mesh at: %s"), *SearchPath); - break; - } - else - { - UE_LOG(LogURLabEditor, Log, TEXT("[ImportSingleMesh] Not found at: %s"), *SearchPath); - } - } - - // 最后手段:使用资源注册表查找目标文件夹中的任何 StaticMesh 文件 - if (!Mesh) - { - FString SearchDir = FString::Printf(TEXT("%s/%s"), *DestinationPath, *MeshName); - UE_LOG(LogURLabEditor, Log, TEXT("[ImportSingleMesh] Searching asset registry under: %s"), *SearchDir); - - IAssetRegistry& AssetRegistry = FModuleManager::LoadModuleChecked("AssetRegistry").Get(); - TArray Assets; - AssetRegistry.GetAssetsByPath(FName(*SearchDir), Assets, true); - - for (const FAssetData& Asset : Assets) - { - // UE_LOG(LogURLabEditor, Log, TEXT(" Registry: %s (%s)"), *Asset.AssetName.ToString(), *Asset.AssetClassPath.ToString()); - // if (Asset.AssetClassPath.GetAssetName() == TEXT("StaticMesh")) - // { - // Mesh = Cast(Asset.GetAsset()); - // if (Mesh) - // { - // UE_LOG(LogURLabEditor, Log, TEXT("[ImportSingleMesh] Found mesh via registry: %s"), *Asset.GetObjectPathString()); - // break; - // } - // } - } - } - } - - if (Mesh) - { - // 清除可能引用已剥离纹理的 Interchange 创建的材质。 - // 我们的导入流程会在 SCS 模板上分配 MI_ 材质实例, - // 但静态网格资源会在其槽位中保留 Interchange 材质。 - // 这可能会导致在浏览/生成缩略图时渲染线程崩溃 (UE-23902)。 - // for (FStaticMaterial& Mat : Mesh->GetStaticMaterials()) - // { - // Mat.MaterialInterface = UMaterial::GetDefaultMaterial(MD_Surface); - // } - - // 强制重建边界 - 对于修复 0x0x0 大小问题至关重要 - Mesh->Build(); - Mesh->CalculateExtendedBounds(); - - UPackage* Package = Mesh->GetOutermost(); - FEditorFileUtils::PromptForCheckoutAndSave({Package}, false, false); - - UE_LOG(LogURLabEditor, Log, TEXT("Imported mesh '%s' - Bounds: %s"), - *FPaths::GetBaseFilename(SourcePath), - *Mesh->GetBoundingBox().GetSize().ToString()); - - return Mesh; - } - - return nullptr; -} - -bool UMujocoGenerationAction::ValidateMesh(UStaticMesh* Mesh, const FString& MeshName) -{ - if (!Mesh) - { - UE_LOG(LogURLabEditor, Error, TEXT("Mesh validation failed: Mesh is null")); - return false; - } - - // 检查网格是否有渲染数据 - // if (!Mesh->GetRenderData()) - // { - // UE_LOG(LogURLabEditor, Error, TEXT("Mesh '%s' has no render data"), *MeshName); - // return false; - // } - - // 检查 LOD 0 是否存在 - // if (Mesh->GetRenderData()->LODResources.Num() == 0) - // { - // UE_LOG(LogURLabEditor, Error, TEXT("Mesh '%s' has no LOD resources"), *MeshName); - // return false; - // } - - // const FStaticMeshLODResources& LOD0 = Mesh->GetRenderData()->LODResources[0]; -// - // // 检查顶点缓冲 - // if (LOD0.VertexBuffers.StaticMeshVertexBuffer.GetNumVertices() == 0) - // { - // UE_LOG(LogURLabEditor, Error, TEXT("Mesh '%s' has empty vertex buffer"), *MeshName); - // return false; - // } -// - // // 检查索引缓冲区 - // if (LOD0.IndexBuffer.GetNumIndices() == 0) - // { - // UE_LOG(LogURLabEditor, Error, TEXT("Mesh '%s' has empty index buffer"), *MeshName); - // return false; - // } - - // 记录网格统计信息 - int32 NumVertices = 0; // = LOD0.VertexBuffers.StaticMeshVertexBuffer.GetNumVertices(); - int32 NumTriangles = 0; // = LOD0.IndexBuffer.GetNumIndices() / 3; - int32 NumUVChannels = 0; // = LOD0.VertexBuffers.StaticMeshVertexBuffer.GetNumTexCoords(); - - UE_LOG(LogURLabEditor, Log, TEXT("Mesh '%s' validation: %d vertices, %d triangles, %d UV channels"), - *MeshName, NumVertices, NumTriangles, NumUVChannels); - - // 如果没有 UV 通道 0,就发出警告 - if (NumUVChannels == 0) - { - UE_LOG(LogURLabEditor, Warning, TEXT("Mesh '%s' has no UV channels - materials may not display correctly"), *MeshName); - } - - return true; -} - - -UTexture2D* UMujocoGenerationAction::ImportSingleTexture(const FString& SourcePath, const FString& DestinationPath) -{ - if (SourcePath.IsEmpty() || DestinationPath.IsEmpty()) - { - return nullptr; - } - - FString FileName = FPaths::GetBaseFilename(SourcePath); - FString PackageName = FPaths::Combine(DestinationPath, FileName); - PackageName = UPackageTools::SanitizePackageName(PackageName); - - // Check if texture already exists - UTexture2D* ExistingTexture = LoadObject(nullptr, *PackageName); - if (ExistingTexture) - { - return ExistingTexture; - } - - UE_LOG(LogURLabEditor, Log, TEXT("Importing texture from: %s to %s"), *SourcePath, *DestinationPath); - - // Load texture file from disk - TArray FileData; - if (!FFileHelper::LoadFileToArray(FileData, *SourcePath)) - { - UE_LOG(LogURLabEditor, Error, TEXT("Failed to load texture file: %s"), *SourcePath); - return nullptr; - } - - // Determine image format from extension - FString Extension = FPaths::GetExtension(SourcePath).ToLower(); - EImageFormat ImageFormat = EImageFormat::Invalid; - - if (Extension == TEXT("png")) - { - ImageFormat = EImageFormat::PNG; - } - else if (Extension == TEXT("jpg") || Extension == TEXT("jpeg")) - { - ImageFormat = EImageFormat::JPEG; - } - else if (Extension == TEXT("tga")) - { - // ImageFormat = EImageFormat::TGA; - } - else if (Extension == TEXT("bmp")) - { - ImageFormat = EImageFormat::BMP; - } - else - { - UE_LOG(LogURLabEditor, Warning, TEXT("Unsupported texture format: %s"), *Extension); - return nullptr; - } - - // Decode image - IImageWrapperModule& ImageWrapperModule = FModuleManager::LoadModuleChecked(FName("ImageWrapper")); - TSharedPtr ImageWrapper = ImageWrapperModule.CreateImageWrapper(ImageFormat); - - if (!ImageWrapper.IsValid() || !ImageWrapper->SetCompressed(FileData.GetData(), FileData.Num())) - { - UE_LOG(LogURLabEditor, Error, TEXT("Failed to decode texture: %s"), *SourcePath); - return nullptr; - } - - // Get raw image data - TArray RawData; - if (!ImageWrapper->GetRaw(ERGBFormat::BGRA, 8, RawData)) - { - UE_LOG(LogURLabEditor, Error, TEXT("Failed to get raw texture data: %s"), *SourcePath); - return nullptr; - } - - // Create package - UPackage* Package = CreatePackage(*PackageName); - Package->FullyLoad(); - - // Create texture - UTexture2D* NewTexture = NewObject(Package, FName(*FileName), RF_Public | RF_Standalone); - - // Set texture properties - NewTexture->Source.Init(ImageWrapper->GetWidth(), ImageWrapper->GetHeight(), 1, 1, TSF_BGRA8, RawData.GetData()); - NewTexture->SRGB = true; - NewTexture->CompressionSettings = TextureCompressionSettings::TC_Default; - NewTexture->MipGenSettings = TextureMipGenSettings::TMGS_FromTextureGroup; - NewTexture->UpdateResource(); - - // Save package - Package->MarkPackageDirty(); - FAssetRegistryModule::AssetCreated(NewTexture); - - FString PackageFileName = FPackageName::LongPackageNameToFilename(PackageName, FPackageName::GetAssetPackageExtension()); - FSavePackageArgs SaveArgs; - SaveArgs.TopLevelFlags = RF_Public | RF_Standalone; - SaveArgs.SaveFlags = SAVE_NoError; - // UPackage::SavePackage(Package, NewTexture, *PackageFileName, SaveArgs); - - UE_LOG(LogURLabEditor, Log, TEXT("Successfully imported texture: %s"), *FileName); - return NewTexture; -} - - -// 基于项目内的母材质(/UnrealRoboticsLab/Materials/M_MuJoCo_Master)为导入的网格创建或复用一个 UMaterialInstanceConstant, -// 并把从 MuJoCo 材质数据解析出的颜色/纹理参数写入该实例,返回已创建或复用的实例指针。 -UMaterialInstanceConstant* UMujocoGenerationAction::CreateMaterialInstance( - const FString& MeshName, - const FMuJoCoMaterialData& MaterialData, - const TMap& TextureAssets, - const FString& DestinationPath) -{ - // 加载 MuJoCo 相关资产的通用母材质模板:集中定义外观参数,包括基础色、金属度、粗糙度、法线等。 - // 应该位于 hutb\Unreal\CarlaUE4\Plugins\UnrealRoboticsLab\Content\Materials,而不是 hutb\Unreal\CarlaUE4\Content\Matrials - // 直接复制到 hutb\Unreal\CarlaUE4\Content\Matrials 会出现资产版本不符的问题: - // LogAssetRegistry: Error: Package ../../../../../Unreal/CarlaUE4/Plugins/UnrealRoboticsLab/Content/Materials/M_MuJoCo_Master.uasset is too old - UMaterial* MasterMaterial = LoadObject(nullptr, TEXT("/UnrealRoboticsLab/Materials/M_MuJoCo_Master.M_MuJoCo_Master")); - if (!MasterMaterial) - { - UE_LOG(LogURLabEditor, Error, TEXT("Failed to load master material: /UnrealRoboticsLab/Materials/M_MuJoCo_Master")); - return nullptr; - } - - // 创建材质实例包 - FString InstanceName = FString::Printf(TEXT("MI_%s"), *MeshName); - FString PackageName = FPaths::Combine(DestinationPath, InstanceName); - PackageName = UPackageTools::SanitizePackageName(PackageName); - - // 检查材质实例是否已存在——在同一导入会话期间重复使用(多个几何体引用同一材质), - // 但不要跳过参数设置。 - UMaterialInstanceConstant* ExistingInstance = LoadObject(nullptr, *PackageName); - if (ExistingInstance) - { - UE_LOG(LogURLabEditor, Log, TEXT("Reusing existing material instance: %s"), *InstanceName); - return ExistingInstance; - } - - UE_LOG(LogURLabEditor, Log, TEXT("Creating material instance: %s"), *InstanceName); - - // 创建包 - UPackage* Package = CreatePackage(*PackageName); - Package->FullyLoad(); - - // 创建材质实例 - UMaterialInstanceConstant* MaterialInstance = NewObject( - Package, - FName(*InstanceName), - RF_Public | RF_Standalone - ); - - MaterialInstance->SetParentEditorOnly(MasterMaterial); - - // 用于设置标量参数并启用覆盖的辅助 lambda 函数 - auto SetScalar = [&](const TCHAR* Name, float Value) - { - FMaterialParameterInfo Info(Name); - MaterialInstance->SetScalarParameterValueEditorOnly(Info, Value); - }; - - // 用于设置向量参数并启用覆盖的辅助 lambda 函数 - auto SetVector = [&](const TCHAR* Name, const FLinearColor& Value) - { - FMaterialParameterInfo Info(Name); - MaterialInstance->SetVectorParameterValueEditorOnly(Info, Value); - }; - - // 用于设置纹理参数的辅助 lambda 函数,并启用覆盖功能。 - auto SetTexture = [&](const TCHAR* Name, UTexture* Tex) - { - FMaterialParameterInfo Info(Name); - MaterialInstance->SetTextureParameterValueEditorOnly(Info, Tex); - - // 同时直接添加到 TextureParameterValues 中,以确保启用覆盖功能。 - FTextureParameterValue TexParam; - TexParam.ParameterInfo = Info; - TexParam.ParameterValue = Tex; - TexParam.ExpressionGUID = FGuid(); // 将由 UE 解决 - MaterialInstance->TextureParameterValues.Add(TexParam); - - UE_LOG(LogURLabEditor, Log, TEXT(" [SetTexture] Set '%s' = '%s' (TextureParameterValues count: %d)"), - Name, *Tex->GetName(), MaterialInstance->TextureParameterValues.Num()); - }; - - // 设置基础颜色(底色) - SetVector(TEXT("BaseColor"), MaterialData.Rgba); - - // 设置纹理参数 - bool bHasBaseColorTexture = false; - if (!MaterialData.BaseColorTextureName.IsEmpty()) - { - if (TextureAssets.Contains(MaterialData.BaseColorTextureName)) - { - UTexture2D* BaseColorTex = TextureAssets[MaterialData.BaseColorTextureName]; - if (BaseColorTex) - { - SetTexture(TEXT("BaseColorTexture"), BaseColorTex); - bHasBaseColorTexture = true; - UE_LOG(LogURLabEditor, Log, TEXT(" Texture '%s' applied to material '%s' (tex=%s)"), - *MaterialData.BaseColorTextureName, *InstanceName, *BaseColorTex->GetName()); - } - } - else - { - UE_LOG(LogURLabEditor, Warning, TEXT(" Texture '%s' referenced by material '%s' but not found in imported textures (%d entries)"), - *MaterialData.BaseColorTextureName, *InstanceName, TextureAssets.Num()); - } - } - - // 将 bUseTexture 设置为静态开关参数——主材质使用 StaticSwitchParameter 在 false 时完全消除纹理采样分支,防止空纹理崩溃 (UE-23902)。 - { - FStaticParameterSet StaticParams; - MaterialInstance->GetStaticParameterValues(StaticParams); - - for (FStaticSwitchParameter& Param : StaticParams.StaticSwitchParameters) - { - if (Param.ParameterInfo.Name == TEXT("bUseTexture")) - { - Param.Value = bHasBaseColorTexture; - Param.bOverride = true; - break; - } - } - MaterialInstance->UpdateStaticPermutation(StaticParams); - } - - // 如果可用,请设置正则纹理 - if (!MaterialData.NormalTextureName.IsEmpty() && TextureAssets.Contains(MaterialData.NormalTextureName)) - { - UTexture2D* NormalTex = TextureAssets[MaterialData.NormalTextureName]; - if (NormalTex) - { - FMaterialParameterInfo NormalParamInfo(TEXT("NormalTexture")); - MaterialInstance->SetTextureParameterValueEditorOnly(NormalParamInfo, NormalTex); - } - } - - // 如果可用,请设置 ORM 纹理 - // ORM 纹理(也称 ORM 贴图)是一种在 3D 渲染和游戏开发中常用的技术。 - // 它将环境光遮蔽(Occlusion)、粗糙度(Roughness)和金属度(Metallic)这三个 PBR 材质参数合并到一张图像的红、绿、蓝(RGB)通道中, - // 以减少文件数量并提高渲染效率 - if (!MaterialData.ORMTextureName.IsEmpty() && TextureAssets.Contains(MaterialData.ORMTextureName)) - { - UTexture2D* ORMTex = TextureAssets[MaterialData.ORMTextureName]; - if (ORMTex) - { - FMaterialParameterInfo ORMParamInfo(TEXT("ORMTexture")); - MaterialInstance->SetTextureParameterValueEditorOnly(ORMParamInfo, ORMTex); - } - } - // 否则,请设置单独的粗糙度/金属纹理。 - else - { - if (!MaterialData.RoughnessTextureName.IsEmpty() && TextureAssets.Contains(MaterialData.RoughnessTextureName)) - { - // 注意:如果主材质没有单独的粗糙度纹理参数,则会忽略此参数。 - // 目前,我们仅将其记录下来。 - UE_LOG(LogURLabEditor, Log, TEXT("Roughness texture found but master material uses ORM workflow")); - } - - if (!MaterialData.MetallicTextureName.IsEmpty() && TextureAssets.Contains(MaterialData.MetallicTextureName)) - { - // 注意:如果主材质没有单独的金属纹理参数,则此参数将被忽略。 - UE_LOG(LogURLabEditor, Log, TEXT("Metallic texture found but master material uses ORM workflow")); - } - } - - // 强制更新并保存 - MaterialInstance->UpdateStaticPermutation(); - MaterialInstance->PostEditChange(); - - // 保存包 - Package->MarkPackageDirty(); - FAssetRegistryModule::AssetCreated(MaterialInstance); - - FString PackageFileName = FPackageName::LongPackageNameToFilename(PackageName, FPackageName::GetAssetPackageExtension()); - FSavePackageArgs SaveArgs; - SaveArgs.TopLevelFlags = RF_Public | RF_Standalone; - SaveArgs.SaveFlags = SAVE_NoError; - // UPackage::SavePackage(Package, MaterialInstance, *PackageFileName, SaveArgs); - - UE_LOG(LogURLabEditor, Log, TEXT("Successfully created material instance: %s"), *InstanceName); - return MaterialInstance; -} diff --git a/Plugins/UnrealRoboticsLab/Source/URLabEditor/Private/Tests/MjCompilationTests.cpp b/Plugins/UnrealRoboticsLab/Source/URLabEditor/Private/Tests/MjCompilationTests.cpp deleted file mode 100644 index 2875f1a4be..0000000000 --- a/Plugins/UnrealRoboticsLab/Source/URLabEditor/Private/Tests/MjCompilationTests.cpp +++ /dev/null @@ -1,967 +0,0 @@ -// Copyright (c) 2026 Jonathan Embley-Riches. All rights reserved. -// -// Licensed under the Apache License, Version 2.0 (the "License"); -// you may not use this file except in compliance with the License. -// You may obtain a copy of the License at -// -// http://www.apache.org/licenses/LICENSE-2.0 -// -// Unless required by applicable law or agreed to in writing, software -// distributed under the License is distributed on an "AS IS" BASIS, -// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. -// See the License for the specific language governing permissions and -// limitations under the License. -// -// --- LEGAL DISCLAIMER --- -// UnrealRoboticsLab is an independent software plugin. It is NOT affiliated with, -// endorsed by, or sponsored by Epic Games, Inc. "Unreal" and "Unreal Engine" are -// trademarks or registered trademarks of Epic Games, Inc. in the US and elsewhere. -// -// This plugin incorporates third-party software: MuJoCo (Apache 2.0), -// CoACD (MIT), and libzmq (MPL 2.0). See ThirdPartyNotices.txt for details. - -#include "CoreMinimal.h" -#include "Misc/AutomationTest.h" -#include "Tests/MjTestHelpers.h" -#include "MuJoCo/Core/AMjManager.h" -#include "MuJoCo/Core/MjArticulation.h" -#include "MuJoCo/Components/Bodies/MjWorldBody.h" -#include "MuJoCo/Components/Bodies/MjBody.h" -#include "MuJoCo/Components/Geometry/MjGeom.h" -#include "MuJoCo/Components/Joints/MjJoint.h" -#include "MuJoCo/Components/Geometry/MjSite.h" -#include "MuJoCo/Components/Sensors/MjJointPosSensor.h" -#include "MuJoCo/Components/Actuators/MjMotorActuator.h" -#include "MuJoCo/Components/Tendons/MjTendon.h" -#include "MuJoCo/Components/Constraints/MjEquality.h" -#include "MuJoCo/Components/Keyframes/MjKeyframe.h" -#include "MuJoCo/Components/Defaults/MjDefault.h" -#include "MuJoCo/Components/Sensors/MjCamera.h" -#include "Engine/World.h" -#include "mujoco/mujoco.h" - -// ============================================================================ -// URLab.Compile.MinimalBody -// Default FMjUESession (no callback). -// Asserts that Manager is initialized and that the geom and joint view IDs -// are bound (>= 0) after compilation. -// ============================================================================ -IMPLEMENT_SIMPLE_AUTOMATION_TEST(FMjCompileMinimalBody, - "URLab.Compile.MinimalBody", - EAutomationTestFlags::EditorContext | EAutomationTestFlags::ProductFilter) - -bool FMjCompileMinimalBody::RunTest(const FString& Parameters) -{ - FMjUESession S; - if (!S.Init()) - { - AddError(FString::Printf(TEXT("Init failed: %s"), *S.LastError)); - S.Cleanup(); - return false; - } - - TestTrue(TEXT("Manager should be initialized"), S.Manager->IsInitialized()); - TestTrue(TEXT("Geom view id should be >= 0"), S.Geom->m_GeomView.id >= 0); - TestTrue(TEXT("Joint view id should be >= 0"), S.Joint->m_JointView.id >= 0); - - S.Cleanup(); - return true; -} - -// ============================================================================ -// URLab.Compile.JointTypeHinge -// ConfigCallback sets the joint type to Hinge. -// Asserts that the compiled model records mjJNT_HINGE for the joint. -// ============================================================================ -IMPLEMENT_SIMPLE_AUTOMATION_TEST(FMjCompileJointTypeHinge, - "URLab.Compile.JointTypeHinge", - EAutomationTestFlags::EditorContext | EAutomationTestFlags::ProductFilter) - -bool FMjCompileJointTypeHinge::RunTest(const FString& Parameters) -{ - FMjUESession S; - if (!S.Init([](FMjUESession& Sess) - { - Sess.Joint->Type = EMjJointType::Hinge; - Sess.Joint->bOverride_Type = true; - })) - { - AddError(FString::Printf(TEXT("Init failed: %s"), *S.LastError)); - S.Cleanup(); - return false; - } - - int JntId = S.Joint->m_JointView.id; - if (TestTrue(TEXT("joint view bound"), JntId >= 0) && S.Manager->PhysicsEngine->m_model) - { - TestEqual(TEXT("jnt_type == HINGE"), - (int)S.Manager->PhysicsEngine->m_model->jnt_type[JntId], - (int)mjJNT_HINGE); - } - - S.Cleanup(); - return true; -} - -// ============================================================================ -// URLab.Compile.JointTypeSlide -// ConfigCallback sets the joint type to Slide. -// Asserts that the compiled model records mjJNT_SLIDE for the joint. -// ============================================================================ -IMPLEMENT_SIMPLE_AUTOMATION_TEST(FMjCompileJointTypeSlide, - "URLab.Compile.JointTypeSlide", - EAutomationTestFlags::EditorContext | EAutomationTestFlags::ProductFilter) - -bool FMjCompileJointTypeSlide::RunTest(const FString& Parameters) -{ - FMjUESession S; - if (!S.Init([](FMjUESession& Sess) - { - Sess.Joint->Type = EMjJointType::Slide; - Sess.Joint->bOverride_Type = true; - })) - { - AddError(FString::Printf(TEXT("Init failed: %s"), *S.LastError)); - S.Cleanup(); - return false; - } - - int JntId = S.Joint->m_JointView.id; - if (TestTrue(TEXT("joint view bound"), JntId >= 0) && S.Manager->PhysicsEngine->m_model) - { - TestEqual(TEXT("jnt_type == SLIDE"), - (int)S.Manager->PhysicsEngine->m_model->jnt_type[JntId], - (int)mjJNT_SLIDE); - } - - S.Cleanup(); - return true; -} - -// ============================================================================ -// URLab.Compile.JointTypeBall -// ConfigCallback sets the joint type to Ball. -// Asserts that the compiled model records mjJNT_BALL for the joint. -// ============================================================================ -IMPLEMENT_SIMPLE_AUTOMATION_TEST(FMjCompileJointTypeBall, - "URLab.Compile.JointTypeBall", - EAutomationTestFlags::EditorContext | EAutomationTestFlags::ProductFilter) - -bool FMjCompileJointTypeBall::RunTest(const FString& Parameters) -{ - FMjUESession S; - if (!S.Init([](FMjUESession& Sess) - { - Sess.Joint->Type = EMjJointType::Ball; - Sess.Joint->bOverride_Type = true; - })) - { - AddError(FString::Printf(TEXT("Init failed: %s"), *S.LastError)); - S.Cleanup(); - return false; - } - - int JntId = S.Joint->m_JointView.id; - if (TestTrue(TEXT("joint view bound"), JntId >= 0) && S.Manager->PhysicsEngine->m_model) - { - TestEqual(TEXT("jnt_type == BALL"), - (int)S.Manager->PhysicsEngine->m_model->jnt_type[JntId], - (int)mjJNT_BALL); - } - - S.Cleanup(); - return true; -} - -// ============================================================================ -// URLab.Compile.JointTypeFree -// ConfigCallback sets the joint type to Free. -// Asserts that the compiled model records mjJNT_FREE for the joint. -// ============================================================================ -IMPLEMENT_SIMPLE_AUTOMATION_TEST(FMjCompileJointTypeFree, - "URLab.Compile.JointTypeFree", - EAutomationTestFlags::EditorContext | EAutomationTestFlags::ProductFilter) - -bool FMjCompileJointTypeFree::RunTest(const FString& Parameters) -{ - FMjUESession S; - if (!S.Init([](FMjUESession& Sess) - { - Sess.Joint->Type = EMjJointType::Free; - Sess.Joint->bOverride_Type = true; - })) - { - AddError(FString::Printf(TEXT("Init failed: %s"), *S.LastError)); - S.Cleanup(); - return false; - } - - int JntId = S.Joint->m_JointView.id; - if (TestTrue(TEXT("joint view bound"), JntId >= 0) && S.Manager->PhysicsEngine->m_model) - { - TestEqual(TEXT("jnt_type == FREE"), - (int)S.Manager->PhysicsEngine->m_model->jnt_type[JntId], - (int)mjJNT_FREE); - } - - S.Cleanup(); - return true; -} - -// ============================================================================ -// URLab.Compile.JointAxisExported -// ConfigCallback sets joint type to Hinge with Axis = (1, 0, 0). -// Asserts that the compiled model records the correct axis values. -// -// NOTE: The plugin applies a Y-flip when converting UE coords to MuJoCo -// coords (UE is left-handed, MuJoCo is right-handed). Specifically, -// the exported axis Y component is -(Axis.Y). For Axis = (1, 0, 0) this -// has no effect, so jnt_axis is expected to be (1, 0, 0). -// ============================================================================ -IMPLEMENT_SIMPLE_AUTOMATION_TEST(FMjCompileJointAxisExported, - "URLab.Compile.JointAxisExported", - EAutomationTestFlags::EditorContext | EAutomationTestFlags::ProductFilter) - -bool FMjCompileJointAxisExported::RunTest(const FString& Parameters) -{ - FMjUESession S; - if (!S.Init([](FMjUESession& Sess) - { - Sess.Joint->Type = EMjJointType::Hinge; - Sess.Joint->bOverride_Type = true; - Sess.Joint->Axis = FVector(1.0f, 0.0f, 0.0f); - Sess.Joint->bOverride_Axis = true; - })) - { - AddError(FString::Printf(TEXT("Init failed: %s"), *S.LastError)); - S.Cleanup(); - return false; - } - - int JntId = S.Joint->m_JointView.id; - if (TestTrue(TEXT("joint view bound"), JntId >= 0) && S.Manager->PhysicsEngine->m_model) - { - const mjtNum* Axis = &S.Manager->PhysicsEngine->m_model->jnt_axis[JntId * 3]; - TestTrue(TEXT("jnt_axis[0] ~= 1.0"), FMath::Abs((float)Axis[0] - 1.0f) < 1e-4f); - // Y-flip: exported Y == -(Axis.Y). Axis.Y == 0 so both 0.0 and -0.0 are acceptable. - TestTrue(TEXT("jnt_axis[1] ~= 0.0"), FMath::Abs((float)Axis[1]) < 1e-4f); - TestTrue(TEXT("jnt_axis[2] ~= 0.0"), FMath::Abs((float)Axis[2]) < 1e-4f); - } - - S.Cleanup(); - return true; -} - -// ============================================================================ -// URLab.Compile.GeomSizeExported -// ConfigCallback sets geom Size to (0.3, 0.3, 0.3) in MuJoCo metres. -// Asserts that geom_size[id*3+0] in the compiled model is approximately 0.3. -// -// NOTE: UMjGeom.Size is already in MuJoCo metres — no scale conversion is -// applied by the plugin between the component property and the spec value. -// ============================================================================ -IMPLEMENT_SIMPLE_AUTOMATION_TEST(FMjCompileGeomSizeExported, - "URLab.Compile.GeomSizeExported", - EAutomationTestFlags::EditorContext | EAutomationTestFlags::ProductFilter) - -bool FMjCompileGeomSizeExported::RunTest(const FString& Parameters) -{ - FMjUESession S; - if (!S.Init([](FMjUESession& Sess) - { - Sess.Geom->Size = FVector(0.3f, 0.3f, 0.3f); - Sess.Geom->bOverride_Size = true; - })) - { - AddError(FString::Printf(TEXT("Init failed: %s"), *S.LastError)); - S.Cleanup(); - return false; - } - - int GeomId = S.Geom->m_GeomView.id; - if (TestTrue(TEXT("geom view bound"), GeomId >= 0) && S.Manager->PhysicsEngine->m_model) - { - TestTrue(TEXT("geom_size[0] ~= 0.3"), - FMath::Abs((float)S.Manager->PhysicsEngine->m_model->geom_size[GeomId * 3 + 0] - 0.3f) < 1e-4f); - } - - S.Cleanup(); - return true; -} - -// ============================================================================ -// URLab.Compile.GeomFrictionExported -// ConfigCallback sets geom Friction to {0.7, 0.01, 0.001}. -// Asserts that geom_friction[id*3+0] in the compiled model is approximately 0.7. -// ============================================================================ -IMPLEMENT_SIMPLE_AUTOMATION_TEST(FMjCompileGeomFrictionExported, - "URLab.Compile.GeomFrictionExported", - EAutomationTestFlags::EditorContext | EAutomationTestFlags::ProductFilter) - -bool FMjCompileGeomFrictionExported::RunTest(const FString& Parameters) -{ - FMjUESession S; - if (!S.Init([](FMjUESession& Sess) - { - Sess.Geom->Friction = { 0.7f, 0.01f, 0.001f }; - Sess.Geom->bOverride_Friction = true; - })) - { - AddError(FString::Printf(TEXT("Init failed: %s"), *S.LastError)); - S.Cleanup(); - return false; - } - - int GeomId = S.Geom->m_GeomView.id; - if (TestTrue(TEXT("geom view bound"), GeomId >= 0) && S.Manager->PhysicsEngine->m_model) - { - TestTrue(TEXT("geom_friction[0] ~= 0.7"), - FMath::Abs((float)S.Manager->PhysicsEngine->m_model->geom_friction[GeomId * 3 + 0] - 0.7f) < 1e-4f); - } - - S.Cleanup(); - return true; -} - -// ============================================================================ -// URLab.Compile.JointDampingExported -// ConfigCallback sets joint type to Hinge with Damping = 2.5. -// Asserts that dof_damping at the joint's dof address is approximately 2.5. -// ============================================================================ -IMPLEMENT_SIMPLE_AUTOMATION_TEST(FMjCompileJointDampingExported, - "URLab.Compile.JointDampingExported", - EAutomationTestFlags::EditorContext | EAutomationTestFlags::ProductFilter) - -bool FMjCompileJointDampingExported::RunTest(const FString& Parameters) -{ - FMjUESession S; - if (!S.Init([](FMjUESession& Sess) - { - Sess.Joint->Type = EMjJointType::Hinge; - Sess.Joint->bOverride_Type = true; - Sess.Joint->Damping = {2.5f}; - Sess.Joint->bOverride_Damping = true; - })) - { - AddError(FString::Printf(TEXT("Init failed: %s"), *S.LastError)); - S.Cleanup(); - return false; - } - - int JntId = S.Joint->m_JointView.id; - if (TestTrue(TEXT("joint view bound"), JntId >= 0) && S.Manager->PhysicsEngine->m_model) - { - int DofAdr = S.Manager->PhysicsEngine->m_model->jnt_dofadr[JntId]; - TestTrue(TEXT("dof_damping ~= 2.5"), - FMath::Abs((float)S.Manager->PhysicsEngine->m_model->dof_damping[DofAdr] - 2.5f) < 1e-4f); - } - - S.Cleanup(); - return true; -} - -// ============================================================================ -// URLab.Compile.JointLimitsExported -// ConfigCallback sets a Hinge joint with range [-1.5, 1.5] and bLimited. -// Asserts that jnt_limited is set and jnt_range matches the specified values. -// ============================================================================ -IMPLEMENT_SIMPLE_AUTOMATION_TEST(FMjCompileJointLimitsExported, - "URLab.Compile.JointLimitsExported", - EAutomationTestFlags::EditorContext | EAutomationTestFlags::ProductFilter) - -bool FMjCompileJointLimitsExported::RunTest(const FString& Parameters) -{ - FMjUESession S; - if (!S.Init([](FMjUESession& Sess) - { - Sess.Joint->Type = EMjJointType::Hinge; - Sess.Joint->bOverride_Type = true; - Sess.Joint->Range = { -1.5f, 1.5f }; - Sess.Joint->bOverride_Range = true; - Sess.Joint->bLimited = true; - Sess.Joint->bOverride_Limited = true; - })) - { - AddError(FString::Printf(TEXT("Init failed: %s"), *S.LastError)); - S.Cleanup(); - return false; - } - - int JntId = S.Joint->m_JointView.id; - if (TestTrue(TEXT("joint view bound"), JntId >= 0) && S.Manager->PhysicsEngine->m_model) - { - TestTrue(TEXT("jnt_limited should be set"), - S.Manager->PhysicsEngine->m_model->jnt_limited[JntId] != 0); - TestTrue(TEXT("jnt_range[0] ~= -1.5"), - FMath::Abs((float)S.Manager->PhysicsEngine->m_model->jnt_range[JntId * 2 + 0] - (-1.5f)) < 1e-4f); - TestTrue(TEXT("jnt_range[1] ~= 1.5"), - FMath::Abs((float)S.Manager->PhysicsEngine->m_model->jnt_range[JntId * 2 + 1] - 1.5f) < 1e-4f); - } - - S.Cleanup(); - return true; -} - -// ============================================================================ -// URLab.Compile.ManagerModelValid -// Default FMjUESession. -// Asserts that m_model and m_data are non-null after compilation. -// ============================================================================ -IMPLEMENT_SIMPLE_AUTOMATION_TEST(FMjCompileManagerModelValid, - "URLab.Compile.ManagerModelValid", - EAutomationTestFlags::EditorContext | EAutomationTestFlags::ProductFilter) - -bool FMjCompileManagerModelValid::RunTest(const FString& Parameters) -{ - FMjUESession S; - if (!S.Init()) - { - AddError(FString::Printf(TEXT("Init failed: %s"), *S.LastError)); - S.Cleanup(); - return false; - } - - TestNotNull(TEXT("Manager->PhysicsEngine->m_model should not be null"), S.Manager->PhysicsEngine->m_model); - TestNotNull(TEXT("Manager->PhysicsEngine->m_data should not be null"), S.Manager->PhysicsEngine->m_data); - - S.Cleanup(); - return true; -} - -// ============================================================================ -// URLab.Compile.ManagerIsRunning -// Default FMjUESession. -// Asserts that Manager reports both IsInitialized() and IsRunning(). -// ============================================================================ -IMPLEMENT_SIMPLE_AUTOMATION_TEST(FMjCompileManagerIsRunning, - "URLab.Compile.ManagerIsRunning", - EAutomationTestFlags::EditorContext | EAutomationTestFlags::ProductFilter) - -bool FMjCompileManagerIsRunning::RunTest(const FString& Parameters) -{ - FMjUESession S; - if (!S.Init()) - { - AddError(FString::Printf(TEXT("Init failed: %s"), *S.LastError)); - S.Cleanup(); - return false; - } - - TestTrue(TEXT("Manager should be initialized"), S.Manager->IsInitialized()); - // Sim starts paused by design — IsRunning() requires unpaused. - // IsInitialized() is the correct check for post-compile readiness. - TestTrue(TEXT("Manager should be initialized (physics)"), S.Manager->PhysicsEngine->IsInitialized()); - - S.Cleanup(); - return true; -} - -// ============================================================================ -// URLab.Compile.BodyCountMatchesComponents -// Default FMjUESession (1 WorldBody + 1 user Body). -// Asserts that nbody >= 2 (MuJoCo always includes a world body at index 0, -// plus at least the one RootBody created by FMjUESession::Init). -// ============================================================================ -IMPLEMENT_SIMPLE_AUTOMATION_TEST(FMjCompileBodyCountMatchesComponents, - "URLab.Compile.BodyCountMatchesComponents", - EAutomationTestFlags::EditorContext | EAutomationTestFlags::ProductFilter) - -bool FMjCompileBodyCountMatchesComponents::RunTest(const FString& Parameters) -{ - FMjUESession S; - if (!S.Init()) - { - AddError(FString::Printf(TEXT("Init failed: %s"), *S.LastError)); - S.Cleanup(); - return false; - } - - TestTrue(TEXT("nbody should be >= 2 (world body + RootBody)"), - S.Manager->PhysicsEngine->m_model != nullptr && S.Manager->PhysicsEngine->m_model->nbody >= 2); - - S.Cleanup(); - return true; -} - -// ============================================================================ -// URLab.Compile.GeomViewIdBound -// Default FMjUESession. -// Asserts that the geom component's view id is bound (>= 0) after compilation. -// ============================================================================ -IMPLEMENT_SIMPLE_AUTOMATION_TEST(FMjCompileGeomViewIdBound, - "URLab.Compile.GeomViewIdBound", - EAutomationTestFlags::EditorContext | EAutomationTestFlags::ProductFilter) - -bool FMjCompileGeomViewIdBound::RunTest(const FString& Parameters) -{ - FMjUESession S; - if (!S.Init()) - { - AddError(FString::Printf(TEXT("Init failed: %s"), *S.LastError)); - S.Cleanup(); - return false; - } - - TestTrue(TEXT("Geom->m_GeomView.id should be >= 0"), S.Geom->m_GeomView.id >= 0); - - S.Cleanup(); - return true; -} - -// ============================================================================ -// URLab.Compile.JointViewIdBound -// Default FMjUESession. -// Asserts that the joint component's view id is bound (>= 0) after compilation. -// ============================================================================ -IMPLEMENT_SIMPLE_AUTOMATION_TEST(FMjCompileJointViewIdBound, - "URLab.Compile.JointViewIdBound", - EAutomationTestFlags::EditorContext | EAutomationTestFlags::ProductFilter) - -bool FMjCompileJointViewIdBound::RunTest(const FString& Parameters) -{ - FMjUESession S; - if (!S.Init()) - { - AddError(FString::Printf(TEXT("Init failed: %s"), *S.LastError)); - S.Cleanup(); - return false; - } - - TestTrue(TEXT("Joint->m_JointView.id should be >= 0"), S.Joint->m_JointView.id >= 0); - - S.Cleanup(); - return true; -} - -// ============================================================================ -// URLab.Compile.SensorBinds -// ConfigCallback adds a UMjJointPosSensor targeting "TestJoint" to the Robot, -// with the joint type set to Hinge. -// Asserts that the sensor's view id is bound and that nsensor == 1. -// ============================================================================ -IMPLEMENT_SIMPLE_AUTOMATION_TEST(FMjCompileSensorBinds, - "URLab.Compile.SensorBinds", - EAutomationTestFlags::EditorContext | EAutomationTestFlags::ProductFilter) - -bool FMjCompileSensorBinds::RunTest(const FString& Parameters) -{ - UMjJointPosSensor* Sensor = nullptr; - - FMjUESession S; - if (!S.Init([&Sensor](FMjUESession& Sess) - { - Sess.Joint->Type = EMjJointType::Hinge; - Sess.Joint->bOverride_Type = true; - - Sensor = NewObject(Sess.Robot, TEXT("TestSensor")); - Sensor->TargetName = TEXT("TestJoint"); - Sensor->RegisterComponent(); - })) - { - AddError(FString::Printf(TEXT("Init failed: %s"), *S.LastError)); - S.Cleanup(); - return false; - } - - if (TestNotNull(TEXT("Sensor pointer should be non-null"), Sensor)) - { - TestTrue(TEXT("Sensor->m_SensorView.id should be >= 0"), Sensor->m_SensorView.id >= 0); - } - - if (TestNotNull(TEXT("m_model should be non-null"), S.Manager->PhysicsEngine->m_model)) - { - TestEqual(TEXT("nsensor should be 1"), (int)S.Manager->PhysicsEngine->m_model->nsensor, 1); - } - - S.Cleanup(); - return true; -} - -// ============================================================================ -// URLab.Compile.ActuatorBinds -// ConfigCallback adds a UMjMotorActuator targeting "TestJoint" to the Robot, -// with the joint type set to Hinge. -// Asserts that the actuator's view id is bound and that nu == 1. -// ============================================================================ -IMPLEMENT_SIMPLE_AUTOMATION_TEST(FMjCompileActuatorBinds, - "URLab.Compile.ActuatorBinds", - EAutomationTestFlags::EditorContext | EAutomationTestFlags::ProductFilter) - -bool FMjCompileActuatorBinds::RunTest(const FString& Parameters) -{ - UMjMotorActuator* Act = nullptr; - - FMjUESession S; - if (!S.Init([&Act](FMjUESession& Sess) - { - Sess.Joint->Type = EMjJointType::Hinge; - Sess.Joint->bOverride_Type = true; - - Act = NewObject(Sess.Robot, TEXT("TestActuator")); - Act->TargetName = TEXT("TestJoint"); - Act->RegisterComponent(); - })) - { - AddError(FString::Printf(TEXT("Init failed: %s"), *S.LastError)); - S.Cleanup(); - return false; - } - - if (TestNotNull(TEXT("Actuator pointer should be non-null"), Act)) - { - TestTrue(TEXT("Act->m_ActuatorView.id should be >= 0"), Act->m_ActuatorView.id >= 0); - } - - if (TestNotNull(TEXT("m_model should be non-null"), S.Manager->PhysicsEngine->m_model)) - { - TestEqual(TEXT("nu should be 1"), (int)S.Manager->PhysicsEngine->m_model->nu, 1); - } - - S.Cleanup(); - return true; -} - -// ============================================================================ -// URLab.Compile.PIERestart -// Runs two complete FMjUESession init/cleanup cycles back-to-back. -// Both must succeed and Manager->IsInitialized() must be true in both, -// verifying that the plugin can be re-initialised without leaving stale -// state (simulates a PIE stop + restart). -// ============================================================================ -IMPLEMENT_SIMPLE_AUTOMATION_TEST(FMjCompilePIERestart, - "URLab.Compile.PIERestart", - EAutomationTestFlags::EditorContext | EAutomationTestFlags::ProductFilter) - -bool FMjCompilePIERestart::RunTest(const FString& Parameters) -{ - // --- First session --- - { - FMjUESession S; - if (!S.Init()) - { - AddError(FString::Printf(TEXT("First Init failed: %s"), *S.LastError)); - S.Cleanup(); - return false; - } - TestTrue(TEXT("First session: Manager should be initialized"), S.Manager->IsInitialized()); - S.Cleanup(); - } - - // --- Second session --- - { - FMjUESession S; - if (!S.Init()) - { - AddError(FString::Printf(TEXT("Second Init failed: %s"), *S.LastError)); - S.Cleanup(); - return false; - } - TestTrue(TEXT("Second session: Manager should be initialized"), S.Manager->IsInitialized()); - S.Cleanup(); - } - - return true; -} - -// ============================================================================ -// URLab.Compile.SiteBinds -// ConfigCallback adds a UMjSite to the body. -// Asserts that the site's SiteView id is bound (>= 0), m_ID is set, -// IsBound() returns true, and nsite >= 1. -// ============================================================================ -IMPLEMENT_SIMPLE_AUTOMATION_TEST(FMjCompileSiteBinds, - "URLab.Compile.SiteBinds", - EAutomationTestFlags::EditorContext | EAutomationTestFlags::ProductFilter) - -bool FMjCompileSiteBinds::RunTest(const FString& Parameters) -{ - UMjSite* Site = nullptr; - - FMjUESession S; - if (!S.Init([&Site](FMjUESession& Sess) - { - Site = NewObject(Sess.Robot, TEXT("TestSite")); - Site->RegisterComponent(); - Site->AttachToComponent(Sess.Body, FAttachmentTransformRules::KeepRelativeTransform); - })) - { - AddError(FString::Printf(TEXT("Init failed: %s"), *S.LastError)); - S.Cleanup(); - return false; - } - - if (TestNotNull(TEXT("Site pointer should be non-null"), Site)) - { - TestTrue(TEXT("Site->m_SiteView.id should be >= 0"), Site->m_SiteView.id >= 0); - TestTrue(TEXT("Site->GetMjID() should be >= 0"), Site->GetMjID() >= 0); - TestTrue(TEXT("Site->IsBound() should be true"), Site->IsBound()); - } - - if (TestNotNull(TEXT("m_model should be non-null"), S.Manager->PhysicsEngine->m_model)) - { - TestTrue(TEXT("nsite should be >= 1"), S.Manager->PhysicsEngine->m_model->nsite >= 1); - } - - S.Cleanup(); - return true; -} - -// ============================================================================ -// URLab.Compile.TendonExportTo -// Creates a fixed tendon with two joint wraps, stiffness and damping. -// Verifies that the tendon compiles, binds, and properties are in the model. -// ============================================================================ -IMPLEMENT_SIMPLE_AUTOMATION_TEST(FMjCompileTendonExportTo, - "URLab.Compile.TendonExportTo", - EAutomationTestFlags::EditorContext | EAutomationTestFlags::ProductFilter) - -bool FMjCompileTendonExportTo::RunTest(const FString& Parameters) -{ - UMjTendon* Tendon = nullptr; - UMjJoint* Joint2 = nullptr; - - FMjUESession S; - if (!S.Init([&Tendon, &Joint2](FMjUESession& Sess) - { - Sess.Joint->Type = EMjJointType::Hinge; - Sess.Joint->bOverride_Type = true; - - // Add a second body+joint for the tendon to connect - UMjBody* Body2 = NewObject(Sess.Robot, TEXT("Body2")); - Body2->RegisterComponent(); - Body2->AttachToComponent(Sess.Body, FAttachmentTransformRules::KeepRelativeTransform); - - UMjGeom* Geom2 = NewObject(Sess.Robot, TEXT("Geom2")); - Geom2->Size = FVector(0.1f, 0.1f, 0.1f); - Geom2->bOverride_Size = true; - Geom2->RegisterComponent(); - Geom2->AttachToComponent(Body2, FAttachmentTransformRules::KeepRelativeTransform); - - Joint2 = NewObject(Sess.Robot, TEXT("Joint2")); - Joint2->Type = EMjJointType::Hinge; - Joint2->bOverride_Type = true; - Joint2->RegisterComponent(); - Joint2->AttachToComponent(Body2, FAttachmentTransformRules::KeepRelativeTransform); - - // Create tendon with two joint wraps - Tendon = NewObject(Sess.Robot, TEXT("TestTendon")); - Tendon->bOverride_Stiffness = true; - Tendon->Stiffness = {5.0f}; - Tendon->bOverride_Damping = true; - Tendon->Damping = {0.1f}; - - FMjTendonWrap W1; - W1.Type = EMjTendonWrapType::Joint; - W1.TargetName = TEXT("TestJoint"); - W1.Coef = 1.0f; - - FMjTendonWrap W2; - W2.Type = EMjTendonWrapType::Joint; - W2.TargetName = TEXT("Joint2"); - W2.Coef = -1.0f; - - Tendon->Wraps.Add(W1); - Tendon->Wraps.Add(W2); - Tendon->RegisterComponent(); - })) - { - AddError(FString::Printf(TEXT("Init failed: %s"), *S.LastError)); - S.Cleanup(); - return false; - } - - if (TestNotNull(TEXT("Tendon pointer"), Tendon) && - TestNotNull(TEXT("m_model"), S.Manager->PhysicsEngine->m_model)) - { - TestTrue(TEXT("ntendon >= 1"), S.Manager->PhysicsEngine->m_model->ntendon >= 1); - TestTrue(TEXT("Tendon bound"), Tendon->m_TendonView.id >= 0); - - int tid = Tendon->m_TendonView.id; - if (tid >= 0) - { - TestTrue(TEXT("stiffness ~= 5.0"), - FMath::Abs((float)S.Manager->PhysicsEngine->m_model->tendon_stiffness[tid] - 5.0f) < 1e-4f); - TestTrue(TEXT("damping ~= 0.1"), - FMath::Abs((float)S.Manager->PhysicsEngine->m_model->tendon_damping[tid] - 0.1f) < 1e-4f); - } - } - - S.Cleanup(); - return true; -} - -// ============================================================================ -// URLab.Compile.DefaultTendonSiteCamera -// Verifies that UMjDefault::ExportTo delegates to tendon, site, and camera -// child components, writing their properties to the mjsDefault struct. -// ============================================================================ -IMPLEMENT_SIMPLE_AUTOMATION_TEST(FMjCompileDefaultTendonSiteCamera, - "URLab.Compile.DefaultTendonSiteCamera", - EAutomationTestFlags::EditorContext | EAutomationTestFlags::ProductFilter) - -bool FMjCompileDefaultTendonSiteCamera::RunTest(const FString& Parameters) -{ - // Create a temporary spec to get a default struct - mjSpec* Spec = mj_makeSpec(); - mjsDefault* Def = mjs_addDefault(Spec, "test_defaults", nullptr); - if (!TestNotNull(TEXT("Default created"), Def)) - { - mj_deleteSpec(Spec); - return false; - } - - // Verify sub-struct pointers are valid - if (!TestNotNull(TEXT("Def->tendon"), Def->tendon) || - !TestNotNull(TEXT("Def->site"), Def->site) || - !TestNotNull(TEXT("Def->camera"), Def->camera)) - { - mj_deleteSpec(Spec); - return false; - } - - // --- Test UMjTendon::ExportTo on def->tendon --- - UMjTendon* TendonChild = NewObject(); - TendonChild->Stiffness = {7.5f}; - TendonChild->Damping = {0.3f}; - TendonChild->bOverride_Stiffness = true; - TendonChild->bOverride_Damping = true; - TendonChild->ExportTo(Def->tendon, nullptr); - - TestTrue(TEXT("tendon stiffness ~= 7.5"), - FMath::Abs(Def->tendon->stiffness - 7.5) < 1e-4); - TestTrue(TEXT("tendon damping ~= 0.3"), - FMath::Abs(Def->tendon->damping - 0.3) < 1e-4); - - // --- Test UMjSite::ExportTo on def->site --- - UMjSite* SiteChild = NewObject(); - SiteChild->Type = EMjSiteType::Box; - SiteChild->Group = 3; - SiteChild->ExportTo(Def->site, nullptr); - - TestEqual(TEXT("site type == box"), (int)Def->site->type, (int)mjGEOM_BOX); - TestEqual(TEXT("site group == 3"), Def->site->group, 3); - - // --- Test UMjCamera::ExportTo on def->camera --- - UMjCamera* CameraChild = NewObject(); - CameraChild->Fovy = 90.0f; - CameraChild->Resolution = FIntPoint(1280, 720); - CameraChild->ExportTo(Def->camera, nullptr); - - TestTrue(TEXT("camera fovy ~= 90.0"), - FMath::Abs(Def->camera->fovy - 90.0) < 1e-4); - TestTrue(TEXT("camera resolution[0] ~= 1280"), - FMath::Abs(Def->camera->resolution[0] - 1280.0f) < 1e-1f); - TestTrue(TEXT("camera resolution[1] ~= 720"), - FMath::Abs(Def->camera->resolution[1] - 720.0f) < 1e-1f); - - mj_deleteSpec(Spec); - return true; -} - -// ============================================================================ -// URLab.Compile.EqualityExportTo -// Creates a weld equality constraint between two bodies. -// Verifies that the equality compiles and neq >= 1. -// ============================================================================ -IMPLEMENT_SIMPLE_AUTOMATION_TEST(FMjCompileEqualityExportTo, - "URLab.Compile.EqualityExportTo", - EAutomationTestFlags::EditorContext | EAutomationTestFlags::ProductFilter) - -bool FMjCompileEqualityExportTo::RunTest(const FString& Parameters) -{ - // Use pure MuJoCo session to verify ExportTo produces correct equality - // (avoids body-name resolution issues in the UE component pipeline) - FMjTestSession S; - if (!S.CompileXml(TEXT( - "" - " " - " " - " " - " " - " " - " " - " " - " " - " " - " " - " " - " " - " " - ""))) - { - AddError(FString::Printf(TEXT("Compile failed: %s"), *S.LastError)); - return false; - } - - TestTrue(TEXT("neq >= 1"), S.m->neq >= 1); - - // Now verify that UMjEquality::ExportTo produces the same result - // by creating a component, setting properties, and checking ExportTo writes correctly - UMjEquality* Eq = NewObject(); - Eq->EqualityType = EMjEqualityType::Weld; - Eq->Obj1 = TEXT("b1"); - Eq->Obj2 = TEXT("b2"); - Eq->bActive = true; - Eq->bOverride_TorqueScale = true; - Eq->TorqueScale = 2.0f; - - // Create a spec and equality to export to - mjSpec* TestSpec = mj_makeSpec(); - mjsEquality* SpecEq = mjs_addEquality(TestSpec, nullptr); - Eq->ExportTo(SpecEq); - - TestEqual(TEXT("type == weld"), (int)SpecEq->type, (int)mjtEq::mjEQ_WELD); - TestTrue(TEXT("active == 1"), SpecEq->active == 1); - TestTrue(TEXT("data[7] ~= 2.0 (torquescale)"), - FMath::Abs(SpecEq->data[7] - 2.0) < 1e-4); - - mj_deleteSpec(TestSpec); - - return true; -} - -// ============================================================================ -// URLab.Compile.KeyframeExportTo -// Creates a keyframe with qpos and time values. -// Verifies that the keyframe compiles and nkey >= 1, time is correct. -// ============================================================================ -IMPLEMENT_SIMPLE_AUTOMATION_TEST(FMjCompileKeyframeExportTo, - "URLab.Compile.KeyframeExportTo", - EAutomationTestFlags::EditorContext | EAutomationTestFlags::ProductFilter) - -bool FMjCompileKeyframeExportTo::RunTest(const FString& Parameters) -{ - UMjKeyframe* KF = nullptr; - - FMjUESession S; - if (!S.Init([&KF](FMjUESession& Sess) - { - Sess.Joint->Type = EMjJointType::Hinge; - Sess.Joint->bOverride_Type = true; - - KF = NewObject(Sess.Robot, TEXT("TestKeyframe")); - KF->Time = 1.5f; - KF->bOverride_Qpos = true; - KF->Qpos = { 0.5f }; - KF->RegisterComponent(); - })) - { - AddError(FString::Printf(TEXT("Init failed: %s"), *S.LastError)); - S.Cleanup(); - return false; - } - - if (TestNotNull(TEXT("m_model"), S.Manager->PhysicsEngine->m_model)) - { - TestTrue(TEXT("nkey >= 1"), S.Manager->PhysicsEngine->m_model->nkey >= 1); - - if (S.Manager->PhysicsEngine->m_model->nkey >= 1) - { - TestTrue(TEXT("key_time[0] ~= 1.5"), - FMath::Abs((float)S.Manager->PhysicsEngine->m_model->key_time[0] - 1.5f) < 1e-4f); - } - } - - S.Cleanup(); - return true; -} diff --git a/Plugins/UnrealRoboticsLab/Source/URLabEditor/Private/Tests/MjPhysicsTests.cpp b/Plugins/UnrealRoboticsLab/Source/URLabEditor/Private/Tests/MjPhysicsTests.cpp deleted file mode 100644 index fa5c4e31d1..0000000000 --- a/Plugins/UnrealRoboticsLab/Source/URLabEditor/Private/Tests/MjPhysicsTests.cpp +++ /dev/null @@ -1,608 +0,0 @@ -// Copyright (c) 2026 Jonathan Embley-Riches. All rights reserved. -// -// Licensed under the Apache License, Version 2.0 (the "License"); -// you may not use this file except in compliance with the License. -// You may obtain a copy of the License at -// -// http://www.apache.org/licenses/LICENSE-2.0 -// -// Unless required by applicable law or agreed to in writing, software -// distributed under the License is distributed on an "AS IS" BASIS, -// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. -// See the License for the specific language governing permissions and -// limitations under the License. -// -// --- LEGAL DISCLAIMER --- -// UnrealRoboticsLab is an independent software plugin. It is NOT affiliated with, -// endorsed by, or sponsored by Epic Games, Inc. "Unreal" and "Unreal Engine" are -// trademarks or registered trademarks of Epic Games, Inc. in the US and elsewhere. -// -// This plugin incorporates third-party software: MuJoCo (Apache 2.0), -// CoACD (MIT), and libzmq (MPL 2.0). See ThirdPartyNotices.txt for details. - -#include "CoreMinimal.h" -#include "Misc/AutomationTest.h" -#include "Tests/MjTestHelpers.h" -#include "MuJoCo/Core/AMjManager.h" -#include "MuJoCo/Components/Geometry/MjGeom.h" -#include "MuJoCo/Components/Joints/MjJoint.h" -#include "MuJoCo/Components/Bodies/MjBody.h" -#include "Engine/World.h" -#include "mujoco/mujoco.h" - -// ============================================================================ -// URLab.Physics.ManagerInitializesCorrectly -// Default Init() should leave the Manager in an initialized and running state. -// ============================================================================ -IMPLEMENT_SIMPLE_AUTOMATION_TEST(FMjPhysicsManagerInitializesCorrectly, - "URLab.Physics.ManagerInitializesCorrectly", - EAutomationTestFlags::EditorContext | EAutomationTestFlags::ProductFilter) - -bool FMjPhysicsManagerInitializesCorrectly::RunTest(const FString& Parameters) -{ - FMjUESession S; - if (!S.Init()) - { - AddError(S.LastError); - return false; - } - - TestTrue(TEXT("Manager->IsInitialized() should be true"), S.Manager->IsInitialized()); - // Sim starts paused by design — check initialized, not running. - TestTrue(TEXT("PhysicsEngine->IsInitialized()"), S.Manager->PhysicsEngine->IsInitialized()); - - S.Cleanup(); - return true; -} - -// ============================================================================ -// URLab.Physics.ModelPointersValid -// After default Init() the model and data pointers must be non-null and the -// model must contain at least one body (the world body). -// ============================================================================ -IMPLEMENT_SIMPLE_AUTOMATION_TEST(FMjPhysicsModelPointersValid, - "URLab.Physics.ModelPointersValid", - EAutomationTestFlags::EditorContext | EAutomationTestFlags::ProductFilter) - -bool FMjPhysicsModelPointersValid::RunTest(const FString& Parameters) -{ - FMjUESession S; - if (!S.Init()) - { - AddError(S.LastError); - return false; - } - - TestTrue(TEXT("Manager->PhysicsEngine->m_model should not be nullptr"), S.Manager->PhysicsEngine->m_model != nullptr); - TestTrue(TEXT("Manager->PhysicsEngine->m_data should not be nullptr"), S.Manager->PhysicsEngine->m_data != nullptr); - - if (S.Manager->PhysicsEngine->m_model) - { - TestTrue(TEXT("m_model->nbody should be >= 1"), S.Manager->PhysicsEngine->m_model->nbody >= 1); - } - - S.Cleanup(); - return true; -} - -// ============================================================================ -// URLab.Physics.TimeAdvancesAfterStep -// Stepping 10 times must advance Manager->PhysicsEngine->m_data->time beyond zero. -// ============================================================================ -IMPLEMENT_SIMPLE_AUTOMATION_TEST(FMjPhysicsTimeAdvancesAfterStep, - "URLab.Physics.TimeAdvancesAfterStep", - EAutomationTestFlags::EditorContext | EAutomationTestFlags::ProductFilter) - -bool FMjPhysicsTimeAdvancesAfterStep::RunTest(const FString& Parameters) -{ - FMjUESession S; - if (!S.Init()) - { - AddError(S.LastError); - return false; - } - - S.Step(10); - - TestTrue(TEXT("Manager->PhysicsEngine->m_data->time should be > 0.0 after 10 steps"), - S.Manager->PhysicsEngine->m_data->time > 0.0); - - S.Cleanup(); - return true; -} - -// ============================================================================ -// URLab.Physics.GravityApplied -// Default MuJoCo gravity should be negative on the Z axis (~-9.81 m/s²), -// confirming that ApplyOptions() is called during compilation. -// ============================================================================ -IMPLEMENT_SIMPLE_AUTOMATION_TEST(FMjPhysicsGravityApplied, - "URLab.Physics.GravityApplied", - EAutomationTestFlags::EditorContext | EAutomationTestFlags::ProductFilter) - -bool FMjPhysicsGravityApplied::RunTest(const FString& Parameters) -{ - FMjUESession S; - if (!S.Init()) - { - AddError(S.LastError); - return false; - } - - TestTrue(TEXT("m_model->opt.gravity[2] should be < 0.0 (default MuJoCo gravity is -9.81 m/s²)"), - S.Manager->PhysicsEngine->m_model->opt.gravity[2] < 0.0); - - S.Cleanup(); - return true; -} - -// ============================================================================ -// URLab.Physics.GeomViewBoundToModel -// The GeomView's internal model pointer must match Manager->PhysicsEngine->m_model, -// confirming that Bind() was called with the correct model after compilation. -// ============================================================================ -IMPLEMENT_SIMPLE_AUTOMATION_TEST(FMjPhysicsGeomViewBoundToModel, - "URLab.Physics.GeomViewBoundToModel", - EAutomationTestFlags::EditorContext | EAutomationTestFlags::ProductFilter) - -bool FMjPhysicsGeomViewBoundToModel::RunTest(const FString& Parameters) -{ - FMjUESession S; - if (!S.Init()) - { - AddError(S.LastError); - return false; - } - - TestTrue(TEXT("Geom->m_GeomView._m should equal Manager->PhysicsEngine->m_model"), - S.Geom->m_GeomView._m == S.Manager->PhysicsEngine->m_model); - - S.Cleanup(); - return true; -} - -// ============================================================================ -// URLab.Physics.JointSetAndGetPosition -// Writing a position via SetPosition() and running mj_forward() should -// produce the same value back from GetPosition() within floating-point -// tolerance, verifying the plugin's joint position read/write API. -// ============================================================================ -IMPLEMENT_SIMPLE_AUTOMATION_TEST(FMjPhysicsJointSetAndGetPosition, - "URLab.Physics.JointSetAndGetPosition", - EAutomationTestFlags::EditorContext | EAutomationTestFlags::ProductFilter) - -bool FMjPhysicsJointSetAndGetPosition::RunTest(const FString& Parameters) -{ - FMjUESession S; - if (!S.Init([](FMjUESession& S) - { - S.Joint->Type = EMjJointType::Hinge; - S.Joint->bOverride_Type = true; - })) - { - AddError(S.LastError); - return false; - } - - S.Joint->SetPosition(1.2f); - mj_forward(S.Manager->PhysicsEngine->m_model, S.Manager->PhysicsEngine->m_data); - - TestTrue(TEXT("Joint->GetPosition() should equal 1.2f within 0.001"), - FMath::Abs(S.Joint->GetPosition() - 1.2f) < 0.001f); - - S.Cleanup(); - return true; -} - -// ============================================================================ -// URLab.Physics.JointGetPositionAfterStep -// After stepping a hinge joint model 5 times, GetPosition() must return a -// finite value (no NaN or Inf). -// ============================================================================ -IMPLEMENT_SIMPLE_AUTOMATION_TEST(FMjPhysicsJointGetPositionAfterStep, - "URLab.Physics.JointGetPositionAfterStep", - EAutomationTestFlags::EditorContext | EAutomationTestFlags::ProductFilter) - -bool FMjPhysicsJointGetPositionAfterStep::RunTest(const FString& Parameters) -{ - FMjUESession S; - if (!S.Init([](FMjUESession& S) - { - S.Joint->Type = EMjJointType::Hinge; - S.Joint->bOverride_Type = true; - })) - { - AddError(S.LastError); - return false; - } - - S.Step(5); - - TestTrue(TEXT("Joint->GetPosition() should be a finite value after 5 steps"), - FMath::IsFinite(S.Joint->GetPosition())); - - S.Cleanup(); - return true; -} - -// ============================================================================ -// URLab.Physics.ResetResetsTime -// After stepping and calling mj_resetData(), the simulation time must return -// to zero. -// ============================================================================ -IMPLEMENT_SIMPLE_AUTOMATION_TEST(FMjPhysicsResetResetsTime, - "URLab.Physics.ResetResetsTime", - EAutomationTestFlags::EditorContext | EAutomationTestFlags::ProductFilter) - -bool FMjPhysicsResetResetsTime::RunTest(const FString& Parameters) -{ - FMjUESession S; - if (!S.Init()) - { - AddError(S.LastError); - return false; - } - - S.Step(20); - TestTrue(TEXT("Manager->PhysicsEngine->m_data->time should be > 0.0 after 20 steps"), - S.Manager->PhysicsEngine->m_data->time > 0.0); - - mj_resetData(S.Manager->PhysicsEngine->m_model, S.Manager->PhysicsEngine->m_data); - - TestTrue(TEXT("Manager->PhysicsEngine->m_data->time should be ~0.0 after mj_resetData()"), - FMath::Abs(S.Manager->PhysicsEngine->m_data->time) < 1e-10); - - S.Cleanup(); - return true; -} - -// ============================================================================ -// URLab.Physics.GravityScaleIntegration -// ApplyOptions() must divide UE cm/s² by 100 before writing to mjOpt. -// Set Gravity=(0,0,-980) cm/s² → m_model->opt.gravity[2] must ≈ -9.80 m/s². -// Verifies Fix 3.6. -// ============================================================================ -IMPLEMENT_SIMPLE_AUTOMATION_TEST(FMjPhysicsGravityScaleIntegration, - "URLab.Physics.GravityScaleIntegration", - EAutomationTestFlags::EditorContext | EAutomationTestFlags::ProductFilter) - -bool FMjPhysicsGravityScaleIntegration::RunTest(const FString& Parameters) -{ - FMjUESession S; - if (!S.Init([](FMjUESession& Sess) - { - Sess.Manager->PhysicsEngine->Options.Gravity = FVector(0.0f, 0.0f, -980.0f); // 9.80 m/s² earth gravity - })) - { - AddError(S.LastError); - return false; - } - - TestTrue(TEXT("gravity X ≈ 0"), - FMath::Abs((float)S.Manager->PhysicsEngine->m_model->opt.gravity[0]) < 1e-5f); - TestTrue(TEXT("gravity Y ≈ 0"), - FMath::Abs((float)S.Manager->PhysicsEngine->m_model->opt.gravity[1]) < 1e-5f); - TestTrue(TEXT("gravity Z ≈ -9.80 m/s² (divided by 100 from -980 cm/s²)"), - FMath::Abs((float)(S.Manager->PhysicsEngine->m_model->opt.gravity[2] + 9.80f)) < 0.05f); - - S.Cleanup(); - return true; -} - -// ============================================================================ -// URLab.Physics.GravityYNegate_Integration -// Y component of gravity must be negated (UE→MuJoCo handedness flip). -// Set Gravity=(0,100,0) cm/s² → m_model->opt.gravity[1] must ≈ -1.0 m/s². -// Verifies Fix 3.6 Y-negate path. -// ============================================================================ -IMPLEMENT_SIMPLE_AUTOMATION_TEST(FMjPhysicsGravityYNegate_Integration, - "URLab.Physics.GravityYNegate_Integration", - EAutomationTestFlags::EditorContext | EAutomationTestFlags::ProductFilter) - -bool FMjPhysicsGravityYNegate_Integration::RunTest(const FString& Parameters) -{ - FMjUESession S; - if (!S.Init([](FMjUESession& Sess) - { - Sess.Manager->PhysicsEngine->Options.bOverride_Gravity = true; - Sess.Manager->PhysicsEngine->Options.Gravity = FVector(0.0f, 100.0f, 0.0f); // 1 m/s² along UE +Y - })) - { - AddError(S.LastError); - return false; - } - - // UE +Y → MuJoCo -Y (handedness flip) - TestTrue(TEXT("gravity Y ≈ -1.0 m/s² (negated from +100 cm/s²)"), - FMath::Abs((float)(S.Manager->PhysicsEngine->m_model->opt.gravity[1] + 1.0f)) < 0.05f); - - S.Cleanup(); - return true; -} - -// ============================================================================ -// URLab.Physics.JointAxis_Compile -// UMjJoint with Axis=(0,1,0) must export MuJoCo jnt_axis (0,-1,0). -// Verifies Fix 3.5: axis is treated as a direction vector with Y-negate. -// ============================================================================ -IMPLEMENT_SIMPLE_AUTOMATION_TEST(FMjPhysicsJointAxis_Compile, - "URLab.Physics.JointAxis_Compile", - EAutomationTestFlags::EditorContext | EAutomationTestFlags::ProductFilter) - -bool FMjPhysicsJointAxis_Compile::RunTest(const FString& Parameters) -{ - FMjUESession S; - if (!S.Init([](FMjUESession& Sess) - { - Sess.Joint->Type = EMjJointType::Hinge; - Sess.Joint->bOverride_Type = true; - Sess.Joint->Axis = FVector(0.0f, 1.0f, 0.0f); - Sess.Joint->bOverride_Axis = true; - })) - { - AddError(S.LastError); - return false; - } - - // Locate the hinge joint by type — name lookup is fragile due to UE component naming. - // The session has exactly one user joint. Find it by iterating jnt_type[]. - int jid = -1; - for (int j = 0; j < S.Manager->PhysicsEngine->m_model->njnt; ++j) - { - if (S.Manager->PhysicsEngine->m_model->jnt_type[j] == mjJNT_HINGE) - { - jid = j; - break; - } - } - TestTrue(TEXT("hinge joint found in compiled model"), jid >= 0); - if (jid >= 0) - { - const mjtNum* ax = &S.Manager->PhysicsEngine->m_model->jnt_axis[3 * jid]; - TestTrue(TEXT("MJ jnt_axis X ≈ 0"), FMath::Abs((float)ax[0]) < 1e-4f); - TestTrue(TEXT("MJ jnt_axis Y ≈ -1"), FMath::Abs((float)(ax[1] + 1.0f)) < 1e-4f); - TestTrue(TEXT("MJ jnt_axis Z ≈ 0"), FMath::Abs((float)ax[2]) < 1e-4f); - } - - S.Cleanup(); - return true; -} - -// ============================================================================ -// Sleep tests (S.8) -// ============================================================================ - -// ============================================================================ -// URLab.Physics.Sleep_EnableFlagSet -// When Options.bEnableSleep is true, ApplyOptions() must set mjENBL_SLEEP. -// ============================================================================ -IMPLEMENT_SIMPLE_AUTOMATION_TEST(FMjPhysicsSleep_EnableFlagSet, - "URLab.Physics.Sleep_EnableFlagSet", - EAutomationTestFlags::EditorContext | EAutomationTestFlags::ProductFilter) - -bool FMjPhysicsSleep_EnableFlagSet::RunTest(const FString& Parameters) -{ - FMjUESession S; - if (!S.Init([](FMjUESession& Sess) - { - Sess.Manager->PhysicsEngine->Options.bEnableSleep = true; - Sess.Manager->PhysicsEngine->Options.SleepTolerance = 1e-3f; - })) - { - AddError(S.LastError); - return false; - } - - constexpr int MJ_ENBL_SLEEP = 1 << 5; - TestTrue(TEXT("mjENBL_SLEEP bit set when bEnableSleep=true"), - (S.Manager->PhysicsEngine->m_model->opt.enableflags & MJ_ENBL_SLEEP) != 0); - // sleep_tolerance removed in MuJoCo 3.x - // TestTrue(TEXT("sleep_tolerance matches Options.SleepTolerance"), - // FMath::Abs((float)S.Manager->PhysicsEngine->m_model->opt.sleep_tolerance - 1e-3f) < 1e-6f); - - S.Cleanup(); - return true; -} - -// ============================================================================ -// URLab.Physics.Sleep_DisableFlagClear -// When Options.bEnableSleep is false (default), mjENBL_SLEEP must NOT be set. -// ============================================================================ -IMPLEMENT_SIMPLE_AUTOMATION_TEST(FMjPhysicsSleep_DisableFlagClear, - "URLab.Physics.Sleep_DisableFlagClear", - EAutomationTestFlags::EditorContext | EAutomationTestFlags::ProductFilter) - -bool FMjPhysicsSleep_DisableFlagClear::RunTest(const FString& Parameters) -{ - FMjUESession S; - if (!S.Init()) - { - AddError(S.LastError); - return false; - } - - constexpr int MJ_ENBL_SLEEP = 1 << 5; - TestTrue(TEXT("mjENBL_SLEEP NOT set when bEnableSleep=false (default)"), - (S.Manager->PhysicsEngine->m_model->opt.enableflags & MJ_ENBL_SLEEP) == 0); - - S.Cleanup(); - return true; -} - -// ============================================================================ -// URLab.Physics.Sleep_BodyIsAwakeByDefault -// With sleep enabled, bodies must start awake (body_awake == 1). -// ============================================================================ -IMPLEMENT_SIMPLE_AUTOMATION_TEST(FMjPhysicsSleep_BodyIsAwakeByDefault, - "URLab.Physics.Sleep_BodyIsAwakeByDefault", - EAutomationTestFlags::EditorContext | EAutomationTestFlags::ProductFilter) - -bool FMjPhysicsSleep_BodyIsAwakeByDefault::RunTest(const FString& Parameters) -{ - FMjUESession S; - if (!S.Init([](FMjUESession& Sess) - { - Sess.Manager->PhysicsEngine->Options.bEnableSleep = true; - })) - { - AddError(S.LastError); - return false; - } - - int BodyId = S.Body->GetMj().id; - TestTrue(TEXT("Body ID is valid"), BodyId > 0); - if (BodyId > 0) -// { -// TestTrue(TEXT("body_awake[BodyId] == 1 at init"), -// body_awake removed in MuJoCo 3.x - // S.Manager->PhysicsEngine->m_data->body_awake[BodyId] == 1); -// } - - S.Cleanup(); - return true; -} - -// ============================================================================ -// URLab.Physics.Sleep_IsAwakeReturnsTrue -// UMjBody::IsAwake() must return true right after compilation. -// ============================================================================ -IMPLEMENT_SIMPLE_AUTOMATION_TEST(FMjPhysicsSleep_IsAwakeReturnsTrue, - "URLab.Physics.Sleep_IsAwakeReturnsTrue", - EAutomationTestFlags::EditorContext | EAutomationTestFlags::ProductFilter) - -bool FMjPhysicsSleep_IsAwakeReturnsTrue::RunTest(const FString& Parameters) -{ - FMjUESession S; - if (!S.Init([](FMjUESession& Sess) - { - Sess.Manager->PhysicsEngine->Options.bEnableSleep = true; - })) - { - AddError(S.LastError); - return false; - } - - TestTrue(TEXT("Body->IsAwake() returns true after init"), S.Body->IsAwake()); - - S.Cleanup(); - return true; -} - -// ============================================================================ -// URLab.Physics.Sleep_BodySleepPolicyAllowed -// SleepPolicy=Allowed on UMjBody must compile to mjSLEEP_ALLOWED (4) in -// the model's tree_sleep_policy for that body's tree. -// ============================================================================ -IMPLEMENT_SIMPLE_AUTOMATION_TEST(FMjPhysicsSleep_BodySleepPolicyAllowed, - "URLab.Physics.Sleep_BodySleepPolicyAllowed", - EAutomationTestFlags::EditorContext | EAutomationTestFlags::ProductFilter) - -bool FMjPhysicsSleep_BodySleepPolicyAllowed::RunTest(const FString& Parameters) -{ - FMjUESession S; - if (!S.Init([](FMjUESession& Sess) - { - Sess.Manager->PhysicsEngine->Options.bEnableSleep = true; - Sess.Body->SleepPolicy = EMjBodySleepPolicy::Allowed; - })) - { - AddError(S.LastError); - return false; - } - - int BodyId = S.Body->GetMj().id; - TestTrue(TEXT("Body ID valid"), BodyId > 0); - if (BodyId > 0) - { - int TreeId = S.Manager->PhysicsEngine->m_model->body_treeid[BodyId]; - TestTrue(TEXT("TreeId valid"), TreeId >= 0); - if (TreeId >= 0) - { - // tree_sleep_policy removed in MuJoCo 3.x - } - } - - S.Cleanup(); - return true; -} - -// ============================================================================ -// URLab.Physics.Sleep_WakeBodyRestoresAwakeState -// After manually putting a body to sleep, Wake() must restore body and tree. -// ============================================================================ -IMPLEMENT_SIMPLE_AUTOMATION_TEST(FMjPhysicsSleep_WakeBodyRestoresAwakeState, - "URLab.Physics.Sleep_WakeBodyRestoresAwakeState", - EAutomationTestFlags::EditorContext | EAutomationTestFlags::ProductFilter) - -bool FMjPhysicsSleep_WakeBodyRestoresAwakeState::RunTest(const FString& Parameters) -{ - FMjUESession S; - if (!S.Init([](FMjUESession& Sess) - { - Sess.Manager->PhysicsEngine->Options.bEnableSleep = true; - })) - { - AddError(S.LastError); - return false; - } - - int BodyId = S.Body->GetMj().id; - TestTrue(TEXT("Body ID valid"), BodyId > 0); - if (BodyId > 0) - { - int TreeId = S.Manager->PhysicsEngine->m_model->body_treeid[BodyId]; - -// // Manually force to sleep -// S.Manager->PhysicsEngine->m_data->body_awake[BodyId] = 0; -// if (TreeId >= 0) -// { -// S.Manager->PhysicsEngine->m_data->tree_asleep[TreeId] = 0; -// S.Manager->PhysicsEngine->m_data->tree_awake[TreeId] = 0; -// } - TestTrue(TEXT("Body is asleep after manual set"), !S.Body->IsAwake()); - - // Wake via component API - S.Body->Wake(); - - TestTrue(TEXT("Body->IsAwake() true after Wake()"), S.Body->IsAwake()); - if (TreeId >= 0) - { - // tree_awake removed in MuJoCo 3.x - // TestTrue(TEXT("tree_awake == 1 after Wake()"), - // S.Manager->PhysicsEngine->m_data->tree_awake[TreeId] == 1); - } - } - - S.Cleanup(); - return true; -} - -// ============================================================================ -// URLab.Physics.Sleep_ForceSleepBody -// Body->Sleep() must put the body to sleep; Wake() must restore it. -// ============================================================================ -IMPLEMENT_SIMPLE_AUTOMATION_TEST(FMjPhysicsSleep_ForceSleepBody, - "URLab.Physics.Sleep_ForceSleepBody", - EAutomationTestFlags::EditorContext | EAutomationTestFlags::ProductFilter) - -bool FMjPhysicsSleep_ForceSleepBody::RunTest(const FString& Parameters) -{ - FMjUESession S; - if (!S.Init([](FMjUESession& Sess) - { - Sess.Manager->PhysicsEngine->Options.bEnableSleep = true; - })) - { - AddError(S.LastError); - return false; - } - - TestTrue(TEXT("Body starts awake"), S.Body->IsAwake()); - S.Body->Sleep(); - TestTrue(TEXT("Body->IsAwake() false after Sleep()"), !S.Body->IsAwake()); - S.Body->Wake(); - TestTrue(TEXT("Body->IsAwake() true after Wake()"), S.Body->IsAwake()); - - S.Cleanup(); - return true; -} diff --git a/Plugins/UnrealRoboticsLab/Source/URLabEditor/Public/MujocoGenerationAction.h b/Plugins/UnrealRoboticsLab/Source/URLabEditor/Public/MujocoGenerationAction.h deleted file mode 100644 index 24c002215a..0000000000 --- a/Plugins/UnrealRoboticsLab/Source/URLabEditor/Public/MujocoGenerationAction.h +++ /dev/null @@ -1,204 +0,0 @@ -// Copyright (c) 2026 Jonathan Embley-Riches. All rights reserved. -// -// Licensed under the Apache License, Version 2.0 (the "License"); -// you may not use this file except in compliance with the License. -// You may obtain a copy of the License at -// -// http://www.apache.org/licenses/LICENSE-2.0 -// -// Unless required by applicable law or agreed to in writing, software -// distributed under the License is distributed on an "AS IS" BASIS, -// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. -// See the License for the specific language governing permissions and -// limitations under the License. -// -// --- LEGAL DISCLAIMER --- -// UnrealRoboticsLab is an independent software plugin. It is NOT affiliated with, -// endorsed by, or sponsored by Epic Games, Inc. "Unreal" and "Unreal Engine" are -// trademarks or registered trademarks of Epic Games, Inc. in the US and elsewhere. -// -// This plugin incorporates third-party software: MuJoCo (Apache 2.0), -// CoACD (MIT), and libzmq (MPL 2.0). See ThirdPartyNotices.txt for details. - -// MujocoGenerationAction.h -#pragma once - -#include "CoreMinimal.h" -#include "AssetActionUtility.h" -#include "MuJoCo/Core/Spec/MjSpecWrapper.h" -#include "MuJoCo/Core/Spec/SpecIterator.h" -#include "MuJoCo/Utils/MjOrientationUtils.h" -#include "Materials/MaterialInstanceConstant.h" - -#include "Factories/FbxMeshImportData.h" - -#include "MujocoGenerationAction.generated.h" - -/** - * @struct FMuJoCoMaterialData - * @brief Stores parsed MuJoCo material properties from XML. - */ -USTRUCT() -struct FMuJoCoMaterialData -{ - GENERATED_BODY() - - UPROPERTY() - FLinearColor Rgba = FLinearColor::White; - - UPROPERTY() - FString BaseColorTextureName; - - UPROPERTY() - FString NormalTextureName; - - UPROPERTY() - FString ORMTextureName; // OcclusionRoughnessMetallic combined texture - - UPROPERTY() - FString RoughnessTextureName; - - UPROPERTY() - FString MetallicTextureName; -}; - -/** - * @struct FArticulationHierarchy - * @brief Stores the organizational root nodes for a MuJoCo articulation. - */ -struct FArticulationHierarchy -{ - class USCS_Node* DefinitionsRoot = nullptr; - class USCS_Node* DefaultsRoot = nullptr; - class USCS_Node* ActuatorsRoot = nullptr; - class USCS_Node* SensorsRoot = nullptr; - class USCS_Node* TendonsRoot = nullptr; - class USCS_Node* ContactsRoot = nullptr; - class USCS_Node* EqualitiesRoot = nullptr; - class USCS_Node* KeyframesRoot = nullptr; -}; - -// Forward declaration if possible, but mj::Body suggests C++ API usage. -// If is the C header, mj::Body shouldn't exist there. -// mj::Body usually comes from C++ wrapper or custom iterator. -// Assuming "spec_iterator.h" provides mj::Body. - -/** - * @class UMujocoGenerationAction - * @brief Asset Action Utility to generate MuJoCo components in Blueprints. - * - * Accessible via right-click context menu on Blueprints (specifically AMjArticulation descendants). - * Parses MuJoCo specs and populates the Blueprint's SCS (Simple Construction Script) with - * corresponding URLab components (MjBody, MjJoint, MjGeom, etc.). - */ -UCLASS() -class URLABEDITOR_API UMujocoGenerationAction : public UAssetActionUtility -{ - GENERATED_BODY() - -public: - UMujocoGenerationAction(); - - /** - * @brief Generates MuJoCo components for the selected Blueprint. - * Accessible via the "Scripted Asset Actions" menu in the editor. - */ - UFUNCTION(CallInEditor, Category = "MuJoCo") - void GenerateMuJoCoComponents(); - - /** - * @brief Generates components for a single Blueprint using the specified XML file. - * Use this for programmatic import (e.g. from Factory). - */ - void GenerateForBlueprint(UBlueprint* BP, const FString& XMLPath); - - /** - * @brief Pre-populates an empty AMjArticulation Blueprint with organizational hierarchy and a main body. - */ - void SetupEmptyArticulation(UBlueprint* BP); - - /** - * @brief Creates the standard MuJoCo organizational hierarchy (Definitions, Defaults, etc.) in the Blueprint. - * @return The organizational hierarchy structure. - */ - FArticulationHierarchy CreateOrganizationalHierarchy(UBlueprint* BP); - - /** - * @brief Generates components using Direct XML Traversal (bypassing mjSpec structure iteration). - */ - void GenerateForBlueprintXml(UBlueprint* BP, const FString& XMLPath); - - /** - * @brief Generates components using Direct XML Traversal from an already parsed XML file. - */ - void GenerateForBlueprintXml(UBlueprint* BP, const FString& XMLPath, const class FXmlFile* InXmlFile); - - /** - * @brief Recursively parses XML to find all -> , , and tags. - */ - void ParseAssetsRecursive(const class FXmlNode* Node, const FString& XMLDir, - TMap& OutMeshAssets, - TMap& OutMeshScales, - TMap& OutTextureAssets, - TMap& OutMaterialData, - const FString& MeshDir = TEXT(""), - const FString& TextureDir = TEXT(""), - const FString& AssetDir = TEXT("")); - - /** - * @brief Attempts to import a mesh with specified normal generation method. - */ - UStaticMesh* AttemptMeshImport(const FString& SourcePath, const FString& DestinationPath, EFBXNormalGenerationMethod::Type NormalMethod); - - /** - * @brief Validates imported mesh has valid render data and geometry. - */ - bool ValidateMesh(UStaticMesh* Mesh, const FString& MeshName); - - /** - * @brief Recursively parses XML to find all tags and create UMjDefault components. - */ - void ParseDefaultsRecursive(const class FXmlNode* Node, UBlueprint* BP, USCS_Node* RootNode, const FString& XMLDir, const struct FMjCompilerSettings& CompilerSettings, const FString& ParentClassName = TEXT(""), bool bIsDefaultContext = true); - - /** @brief Parses XML section to find and elements and create corresponding components. */ - void ParseContactSection(const class FXmlNode* Node, UBlueprint* BP, USCS_Node* RootNode, const FString& XMLDir); - - /** @brief Parses XML section to create corresponding components. */ - void ParseEqualitySection(const class FXmlNode* Node, UBlueprint* BP, USCS_Node* RootNode, const FString& XMLDir); - - /** @brief Parses XML section to create corresponding components. */ - void ParseKeyframeSection(const class FXmlNode* Node, UBlueprint* BP, USCS_Node* RootNode, const FString& XMLDir); - - /** - * @brief Recursively imports XML nodes into SCS. - */ - void ImportNodeRecursive(const class FXmlNode* Node, USCS_Node* ParentNode, UBlueprint* BP, - const FString& XMLDir, const FString& AssetImportPath, - const TMap& MeshAssets, - const TMap& MeshScales, - const TMap& TextureAssets, - const TMap& MaterialData, - const TMap& ImportedTextures, - const FMjCompilerSettings& CompilerSettings, - bool bIsDefaultContext = false, - USCS_Node* ReuseNode = nullptr); - - /** - * @brief scans the hierarchy after import and fixes issues (like Free Joints on child bodies). - */ -private: - TMap CreatedDefaultNodes; - TMap DefaultMeshScales; - - /** Pre-scans the XML for to populate DefaultMeshScales. */ - void CollectDefaultMeshScales(const class FXmlNode* Node, const FString& CurrentClass = TEXT("main")); - UStaticMesh* ImportSingleMesh(const FString& SourcePath, const FString& DestinationPath); - - UTexture2D* ImportSingleTexture(const FString& SourcePath, const FString& DestinationPath); - - UMaterialInstanceConstant* CreateMaterialInstance( - const FString& MeshName, - const FMuJoCoMaterialData& MaterialData, - const TMap& TextureAssets, - const FString& DestinationPath); -}; \ No newline at end of file diff --git a/Plugins/UnrealRoboticsLab/Source/URLabEditor/URLabEditor.Build.cs b/Plugins/UnrealRoboticsLab/Source/URLabEditor/URLabEditor.Build.cs deleted file mode 100644 index 7f9311113c..0000000000 --- a/Plugins/UnrealRoboticsLab/Source/URLabEditor/URLabEditor.Build.cs +++ /dev/null @@ -1,39 +0,0 @@ -using UnrealBuildTool; -using System.IO; - -public class URLabEditor : ModuleRules -{ - public URLabEditor(ReadOnlyTargetRules Target) : base(Target) - { - PCHUsage = ModuleRules.PCHUsageMode.UseExplicitOrSharedPCHs; - CppStandard = CppStandardVersion.Cpp17; - - PublicDependencyModuleNames.AddRange(new string[] - { - "Core", - "CoreUObject", - "Engine", - "URLab", - "UnrealEd", - "AssetTools", - "AssetRegistry", - "Blutility", - "EditorScriptingUtilities", - "PropertyEditor", - "Slate", - "SlateCore", - "XmlParser", - "Projects", - "LevelEditor", - "EditorStyle" - }); - - PrivateDependencyModuleNames.AddRange(new string[] - { - "InputCore", - "RenderCore", - "DesktopPlatform", - "Kismet" - }); - } -} From c21d46e14ca36fb40f2d6d050ef1d42621d54340 Mon Sep 17 00:00:00 2001 From: jiandaoshou-aidehua <2893666867@qq.com> Date: Tue, 28 Jul 2026 14:36:21 +0800 Subject: [PATCH 040/128] =?UTF-8?q?=E5=90=AF=E7=94=A8=E9=94=99=E8=AF=AF?= =?UTF-8?q?=E7=A6=81=E7=94=A8=E7=9A=84=20UnrealRoboticsLab=20=E6=8F=92?= =?UTF-8?q?=E4=BB=B6=E7=9A=84Linux=E7=B3=BB=E7=BB=9F=E6=89=93=E5=8C=85?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .github/workflows/build_linux.yml | 23 ++++++- .../Private/MuJoCo/Core/MjPhysicsEngine.cpp | 6 +- .../URLab/Private/Replay/MjReplayManager.cpp | 64 +++++++++++-------- .../Source/URLab/Private/URLab.cpp | 21 +++++- .../MuJoCo/Components/Sensors/MjCamera.h | 2 +- .../Source/URLab/Public/URLab.h | 9 ++- .../Source/URLab/URLab.Build.cs | 17 +++-- .../Private/MujocoMeshImporter.cpp | 11 ++-- .../Public/MujocoGenerationAction.h | 2 +- 9 files changed, 111 insertions(+), 44 deletions(-) diff --git a/.github/workflows/build_linux.yml b/.github/workflows/build_linux.yml index 2c53fb8ae8..3a43b5ef19 100644 --- a/.github/workflows/build_linux.yml +++ b/.github/workflows/build_linux.yml @@ -10,6 +10,7 @@ jobs: name: Linux CI/CD timeout-minutes: 1440 runs-on: [self-hosted, dev] + runs-on: [self-hosted, dev] env: UE4_ROOT: /home/ubuntu/UnrealEngine_4.26 @@ -40,14 +41,34 @@ jobs: # Extract plugins 7z x Build/dependencies/Plugins/RoadRunner_Plugins.zip -o"Unreal/CarlaUE4/Plugins/" -y 7z x Build/dependencies/Plugins/CesiumForUnreal-426-v1.18.0-ue4.zip -o"Unreal/CarlaUE4/Plugins/" -y + # UnrealRoboticsLab third-party deps mkdir -p Unreal/CarlaUE4/Plugins/UnrealRoboticsLab/third_party/install mkdir -p Unreal/CarlaUE4/Plugins/UnrealRoboticsLab/third_party/install/MuJoCo - tar -xzf Build/dependencies/Plugins/mujoco-3.3.5-linux-x86_64.tar.gz -C Unreal/CarlaUE4/Plugins/UnrealRoboticsLab/third_party/install/MuJoCo --strip-components=1 2>/dev/null || true + tar -xzf Build/dependencies/Plugins/mujoco-3.7.0-linux-x86_64.tar.gz -C Unreal/CarlaUE4/Plugins/UnrealRoboticsLab/third_party/install/MuJoCo --strip-components=1 2>/dev/null || true + + # Replace symlinks with real files for staging + MUJOCO_LIB=Unreal/CarlaUE4/Plugins/UnrealRoboticsLab/third_party/install/MuJoCo/lib + if [ -L "$MUJOCO_LIB/libmujoco.so" ]; then + REAL=$(readlink -f "$MUJOCO_LIB/libmujoco.so") + rm "$MUJOCO_LIB/libmujoco.so" && cp "$REAL" "$MUJOCO_LIB/libmujoco.so" + fi mkdir -p Unreal/CarlaUE4/Plugins/UnrealRoboticsLab/third_party/install/CoACD unzip -qo Build/dependencies/Plugins/CoACD.zip -d Unreal/CarlaUE4/Plugins/UnrealRoboticsLab/third_party/install/CoACD 2>/dev/null || true + + # Fix CoACD directory nesting: move CoACD/CoACD/include -> CoACD/include + if [ -d Unreal/CarlaUE4/Plugins/UnrealRoboticsLab/third_party/install/CoACD/CoACD/include ] && [ ! -d Unreal/CarlaUE4/Plugins/UnrealRoboticsLab/third_party/install/CoACD/include ]; then + mv Unreal/CarlaUE4/Plugins/UnrealRoboticsLab/third_party/install/CoACD/CoACD/include Unreal/CarlaUE4/Plugins/UnrealRoboticsLab/third_party/install/CoACD/include + fi mkdir -p Unreal/CarlaUE4/Plugins/UnrealRoboticsLab/third_party/install/libzmq tar -xzf Build/dependencies/Plugins/libzmq-linux.tar.gz -C Unreal/CarlaUE4/Plugins/UnrealRoboticsLab/third_party/install/libzmq 2>/dev/null || true + + # Replace symlinks with real files for staging + ZMQ_LIB=Unreal/CarlaUE4/Plugins/UnrealRoboticsLab/third_party/install/libzmq/lib + if [ -L "$ZMQ_LIB/libzmq.so" ]; then + REAL=$(readlink -f "$ZMQ_LIB/libzmq.so") + rm "$ZMQ_LIB/libzmq.so" && cp "$REAL" "$ZMQ_LIB/libzmq.so" + fi echo "=== Plugins extracted ===" fi diff --git a/Unreal/CarlaUE4/Plugins/UnrealRoboticsLab/Source/URLab/Private/MuJoCo/Core/MjPhysicsEngine.cpp b/Unreal/CarlaUE4/Plugins/UnrealRoboticsLab/Source/URLab/Private/MuJoCo/Core/MjPhysicsEngine.cpp index 777eef1b82..8379153166 100644 --- a/Unreal/CarlaUE4/Plugins/UnrealRoboticsLab/Source/URLab/Private/MuJoCo/Core/MjPhysicsEngine.cpp +++ b/Unreal/CarlaUE4/Plugins/UnrealRoboticsLab/Source/URLab/Private/MuJoCo/Core/MjPhysicsEngine.cpp @@ -111,10 +111,14 @@ static void URLab_InstallMujocoCallbacks() // mujoco.dll 在 URLab 的 Build.cs 中是延迟加载的,而链接器拒绝通过延迟导入绑定数据符号。 // 通过 GetDllExport 手动解决这两个 mju_user_* 函数指针。 +#if PLATFORM_WINDOWS void* Handle = FPlatformProcess::GetDllHandle(TEXT("mujoco.dll")); +#else + void* Handle = FPlatformProcess::GetDllHandle(TEXT("libmujoco.so")); +#endif if (!Handle) { - UE_LOG(LogURLab, Warning, TEXT("[URLab] Could not resolve mujoco.dll to install error callbacks")); + UE_LOG(LogURLab, Warning, TEXT("[URLab] Could not resolve MuJoCo library to install error callbacks")); return; } diff --git a/Unreal/CarlaUE4/Plugins/UnrealRoboticsLab/Source/URLab/Private/Replay/MjReplayManager.cpp b/Unreal/CarlaUE4/Plugins/UnrealRoboticsLab/Source/URLab/Private/Replay/MjReplayManager.cpp index 0db9adef74..f8989266cc 100644 --- a/Unreal/CarlaUE4/Plugins/UnrealRoboticsLab/Source/URLab/Private/Replay/MjReplayManager.cpp +++ b/Unreal/CarlaUE4/Plugins/UnrealRoboticsLab/Source/URLab/Private/Replay/MjReplayManager.cpp @@ -28,7 +28,9 @@ #include "Misc/FileHelper.h" #include "Misc/Paths.h" #include "Algo/BinarySearch.h" +#if WITH_EDITOR #include "DesktopPlatformModule.h" +#endif #include "Framework/Application/SlateApplication.h" const FString AMjReplayManager::LiveSessionName = TEXT("Live Recording"); @@ -599,23 +601,29 @@ bool AMjReplayManager::LoadFromCSV(FString FilePath, float Timestep) bool AMjReplayManager::BrowseAndLoadCSV(float Timestep) { - IDesktopPlatform* DesktopPlatform = FDesktopPlatformModule::Get(); - if (!DesktopPlatform) return false; - FString DefaultDir = FPaths::ProjectSavedDir() / TEXT("URLab") / TEXT("Replays"); IFileManager::Get().MakeDirectory(*DefaultDir, true); TArray OutFiles; - bool bOpened = DesktopPlatform->OpenFileDialog( - FSlateApplication::Get().FindBestParentWindowHandleForDialogs(nullptr), - TEXT("Load Replay"), - DefaultDir, - TEXT(""), - TEXT("CSV Files (*.csv)|*.csv|JSON Files (*.json)|*.json"), - EFileDialogFlags::None, - OutFiles); - - if (bOpened && OutFiles.Num() > 0) +#if WITH_EDITOR + { + IDesktopPlatform* DesktopPlatform = FDesktopPlatformModule::Get(); + if (!DesktopPlatform) return false; + bool bOpened = DesktopPlatform->OpenFileDialog( + FSlateApplication::Get().FindBestParentWindowHandleForDialogs(nullptr), + TEXT("Load Replay"), + DefaultDir, + TEXT(""), + TEXT("CSV Files (*.csv)|*.csv|JSON Files (*.json)|*.json"), + EFileDialogFlags::None, + OutFiles); + if (!bOpened || OutFiles.Num() == 0) return false; + } +#else + return false; +#endif + + if (OutFiles.Num() > 0) { if (OutFiles[0].EndsWith(TEXT(".json"))) { @@ -670,25 +678,29 @@ bool AMjReplayManager::BrowseAndSaveRecording() return false; } - IDesktopPlatform* DesktopPlatform = FDesktopPlatformModule::Get(); - if (!DesktopPlatform) return false; - // Ensure save directory exists FString SaveDir = FPaths::ProjectSavedDir() / TEXT("URLab") / TEXT("Replays"); IFileManager::Get().MakeDirectory(*SaveDir, true); FString DefaultName = ActiveSessionName + TEXT(".csv"); TArray OutFiles; - bool bOpened = DesktopPlatform->SaveFileDialog( - FSlateApplication::Get().FindBestParentWindowHandleForDialogs(nullptr), - TEXT("Save Replay Recording"), - SaveDir, - DefaultName, - TEXT("CSV Files (*.csv)|*.csv|JSON Files (*.json)|*.json"), - EFileDialogFlags::None, - OutFiles); - - if (!bOpened || OutFiles.Num() == 0) return false; +#if WITH_EDITOR + { + IDesktopPlatform* DesktopPlatform = FDesktopPlatformModule::Get(); + if (!DesktopPlatform) return false; + bool bOpened = DesktopPlatform->SaveFileDialog( + FSlateApplication::Get().FindBestParentWindowHandleForDialogs(nullptr), + TEXT("Save Replay Recording"), + SaveDir, + DefaultName, + TEXT("CSV Files (*.csv)|*.csv|JSON Files (*.json)|*.json"), + EFileDialogFlags::None, + OutFiles); + if (!bOpened || OutFiles.Num() == 0) return false; + } +#else + return false; +#endif FString SavePath = OutFiles[0]; diff --git a/Unreal/CarlaUE4/Plugins/UnrealRoboticsLab/Source/URLab/Private/URLab.cpp b/Unreal/CarlaUE4/Plugins/UnrealRoboticsLab/Source/URLab/Private/URLab.cpp index acdffa6d56..85010313ae 100644 --- a/Unreal/CarlaUE4/Plugins/UnrealRoboticsLab/Source/URLab/Private/URLab.cpp +++ b/Unreal/CarlaUE4/Plugins/UnrealRoboticsLab/Source/URLab/Private/URLab.cpp @@ -40,7 +40,12 @@ void FURLabModule::StartupModule() // Function to load a DLL and log success/failure auto LoadDependencyDLL = [&](const FString& LibraryName, const FString& SubDir) { // Try plugin third-party path first (editor / development) + // Linux uses lib/ directory; Windows uses bin/ +#if PLATFORM_WINDOWS FString DLLPath = FPaths::Combine(InstallDir, SubDir, TEXT("bin"), LibraryName); +#else + FString DLLPath = FPaths::Combine(InstallDir, SubDir, TEXT("lib"), LibraryName); +#endif if (!FPaths::FileExists(DLLPath)) { // Packaged build: DLLs staged next to the executable @@ -61,16 +66,26 @@ void FURLabModule::StartupModule() }; // Load MuJoCo. Since 3.7.0 the obj/stl decoders are compiled into - // mujoco.dll itself (changelog item 9); loading the standalone - // obj_decoder.dll / stl_decoder.dll would cause a plugin-registration + // the library itself (changelog item 9); loading the standalone + // obj_decoder / stl_decoder would cause a plugin-registration // collision and crash during module init. +#if PLATFORM_WINDOWS LoadDependencyDLL(TEXT("mujoco.dll"), TEXT("MuJoCo")); +#else + LoadDependencyDLL(TEXT("libmujoco.so"), TEXT("MuJoCo")); +#endif // Load ZMQ +#if PLATFORM_WINDOWS LoadDependencyDLL(TEXT("libzmq-v143-mt-4_3_6.dll"), TEXT("libzmq")); +#else + LoadDependencyDLL(TEXT("libzmq.so"), TEXT("libzmq")); +#endif - // Load CoACD (Shared library) + // Load CoACD (Shared library — Windows only; header-only on Linux) +#if PLATFORM_WINDOWS LoadDependencyDLL(TEXT("lib_coacd.dll"), TEXT("CoACD")); +#endif // Some CoACD dependencies like TBB or runtimes might be in CoACD/bin // They should be loaded automatically if in search path, but we can verify here if needed. diff --git a/Unreal/CarlaUE4/Plugins/UnrealRoboticsLab/Source/URLab/Public/MuJoCo/Components/Sensors/MjCamera.h b/Unreal/CarlaUE4/Plugins/UnrealRoboticsLab/Source/URLab/Public/MuJoCo/Components/Sensors/MjCamera.h index 523ffb46a6..771ad38af6 100644 --- a/Unreal/CarlaUE4/Plugins/UnrealRoboticsLab/Source/URLab/Public/MuJoCo/Components/Sensors/MjCamera.h +++ b/Unreal/CarlaUE4/Plugins/UnrealRoboticsLab/Source/URLab/Public/MuJoCo/Components/Sensors/MjCamera.h @@ -137,7 +137,7 @@ class URLAB_API UMjCamera : public UMjComponent UPROPERTY(EditAnywhere, BlueprintReadWrite, Category = "MuJoCo|Camera|Network") bool bEnableZmqBroadcast = false; - /** @brief The ZMQ Endpoint for this specific camera (e.g., tcp://*:5558). Must be unique per camera. */ + /// @brief The ZMQ Endpoint for this specific camera (e.g., tcp://*:5558). Must be unique per camera. */ UPROPERTY(EditAnywhere, BlueprintReadWrite, Category = "MuJoCo|Camera|Network") FString ZmqEndpoint = TEXT("tcp://*:5558"); diff --git a/Unreal/CarlaUE4/Plugins/UnrealRoboticsLab/Source/URLab/Public/URLab.h b/Unreal/CarlaUE4/Plugins/UnrealRoboticsLab/Source/URLab/Public/URLab.h index 3e8c857736..012fa7ccdb 100644 --- a/Unreal/CarlaUE4/Plugins/UnrealRoboticsLab/Source/URLab/Public/URLab.h +++ b/Unreal/CarlaUE4/Plugins/UnrealRoboticsLab/Source/URLab/Public/URLab.h @@ -24,8 +24,13 @@ #include "CoreMinimal.h" #include "Modules/ModuleManager.h" -#ifndef _WIN32 - #define _WIN32 PLATFORM_WINDOWS +// 只在Windows系统上建立了_WIN32这个宏,Linux系统并未定义 +// (PLATFORM_WINDOWS is 0 on Linux, but #define _WIN32 0 still makes +// `defined _WIN32` true, causing MuJoCo's mjexport.h to use __declspec) +#if PLATFORM_WINDOWS + #ifndef _WIN32 + #define _WIN32 1 + #endif #endif class FURLabModule : public IModuleInterface { diff --git a/Unreal/CarlaUE4/Plugins/UnrealRoboticsLab/Source/URLab/URLab.Build.cs b/Unreal/CarlaUE4/Plugins/UnrealRoboticsLab/Source/URLab/URLab.Build.cs index fcab278daf..98ea3b0bc1 100644 --- a/Unreal/CarlaUE4/Plugins/UnrealRoboticsLab/Source/URLab/URLab.Build.cs +++ b/Unreal/CarlaUE4/Plugins/UnrealRoboticsLab/Source/URLab/URLab.Build.cs @@ -13,6 +13,8 @@ public URLab(ReadOnlyTargetRules Target) : base(Target) { PCHUsage = ModuleRules.PCHUsageMode.UseExplicitOrSharedPCHs; //bUseRTTI = true; // Required for type info + // Suppress -Wundef for third-party headers (CoACD coacd.h uses #if _WIN32) + bEnableUndefinedIdentifierWarnings = false; //bEnableExceptions = true; PublicDependencyModuleNames.AddRange(new string[] @@ -57,10 +59,16 @@ public URLab(ReadOnlyTargetRules Target) : base(Target) "EnhancedInput", "Chaos", "Landscape", - "Eigen", - "DesktopPlatform" + "Eigen" }); + // DesktopPlatform is editor-only (non-redistributable), cannot be + // linked in Shipping builds + if (Target.bBuildEditor) + { + PrivateDependencyModuleNames.Add("DesktopPlatform"); + } + DynamicallyLoadedModuleNames.AddRange(new string[] { // Add dynamically loaded modules here @@ -90,8 +98,9 @@ public URLab(ReadOnlyTargetRules Target) : base(Target) if (Target.Platform == UnrealTargetPlatform.Linux) { - PublicDefinitions.Add("_WIN32=0"); - PublicDefinitions.Add("USE_DECLSPEC=1"); + // MuJoCo 3.7.0 需要 C++17 来使用 std::byte + CppStandard = CppStandardVersion.Cpp17; + // 移除 _WIN32=0,以避免在 Linux 上导致 mjexport.h 使用 __declspec PublicDefinitions.Add("__linux__=1"); PublicDefinitions.Add("__unix__=1"); } diff --git a/Unreal/CarlaUE4/Plugins/UnrealRoboticsLab/Source/URLabEditor/Private/MujocoMeshImporter.cpp b/Unreal/CarlaUE4/Plugins/UnrealRoboticsLab/Source/URLabEditor/Private/MujocoMeshImporter.cpp index 2b8944481e..b5b4243e73 100644 --- a/Unreal/CarlaUE4/Plugins/UnrealRoboticsLab/Source/URLabEditor/Private/MujocoMeshImporter.cpp +++ b/Unreal/CarlaUE4/Plugins/UnrealRoboticsLab/Source/URLabEditor/Private/MujocoMeshImporter.cpp @@ -23,6 +23,7 @@ // MujocoMeshImporter.cpp — Mesh and material import methods for UMujocoGenerationAction. #include "MujocoGenerationAction.h" +#include "Misc/FileHelper.h" #include "URLabEditorLogging.h" #include "Engine/StaticMesh.h" #include "AssetToolsModule.h" @@ -83,7 +84,7 @@ UStaticMesh* UMujocoGenerationAction::ImportSingleMesh(const FString& SourcePath // 先试着用 MikkTSpace 导入(效果最好) // 使用 ActualSourcePath 替代 SourcePath - UStaticMesh* ImportedMesh = AttemptMeshImport(ActualSourcePath, DestinationPath, EFBXNormalGenerationMethod::MikkTSpace); + UStaticMesh* ImportedMesh = AttemptMeshImport(ActualSourcePath, DestinationPath, static_cast(1)); // MikkTSpace // 验证网格 if (ImportedMesh && ValidateMesh(ImportedMesh, FileName)) @@ -102,7 +103,7 @@ UStaticMesh* UMujocoGenerationAction::ImportSingleMesh(const FString& SourcePath UE_LOG(LogURLabEditor, Warning, TEXT("Failed to import mesh '%s' with MikkTSpace, attempting fallback"), *FileName); } - ImportedMesh = AttemptMeshImport(ActualSourcePath, DestinationPath, EFBXNormalGenerationMethod::BuiltIn); + ImportedMesh = AttemptMeshImport(ActualSourcePath, DestinationPath, static_cast(0)); // BuiltIn if (ImportedMesh && ValidateMesh(ImportedMesh, FileName)) { @@ -329,9 +330,9 @@ bool UMujocoGenerationAction::ValidateMesh(UStaticMesh* Mesh, const FString& Mes // } // 记录网格统计信息 - int32 NumVertices; // = LOD0.VertexBuffers.StaticMeshVertexBuffer.GetNumVertices(); - int32 NumTriangles; // = LOD0.IndexBuffer.GetNumIndices() / 3; - int32 NumUVChannels; // = LOD0.VertexBuffers.StaticMeshVertexBuffer.GetNumTexCoords(); + int32 NumVertices = 0; // = LOD0.VertexBuffers.StaticMeshVertexBuffer.GetNumVertices(); + int32 NumTriangles = 0; // = LOD0.IndexBuffer.GetNumIndices() / 3; + int32 NumUVChannels = 0; // = LOD0.VertexBuffers.StaticMeshVertexBuffer.GetNumTexCoords(); UE_LOG(LogURLabEditor, Log, TEXT("Mesh '%s' validation: %d vertices, %d triangles, %d UV channels"), *MeshName, NumVertices, NumTriangles, NumUVChannels); diff --git a/Unreal/CarlaUE4/Plugins/UnrealRoboticsLab/Source/URLabEditor/Public/MujocoGenerationAction.h b/Unreal/CarlaUE4/Plugins/UnrealRoboticsLab/Source/URLabEditor/Public/MujocoGenerationAction.h index bc7c7978e7..0b0ea1b560 100644 --- a/Unreal/CarlaUE4/Plugins/UnrealRoboticsLab/Source/URLabEditor/Public/MujocoGenerationAction.h +++ b/Unreal/CarlaUE4/Plugins/UnrealRoboticsLab/Source/URLabEditor/Public/MujocoGenerationAction.h @@ -31,7 +31,7 @@ #include "Materials/MaterialInstanceConstant.h" // Forward declarations -namespace EFBXNormalGenerationMethod { enum Type : int; } +#include "Factories/FbxMeshImportData.h" #include "MujocoGenerationAction.generated.h" From a43a2cc0cbadb14328dfd68ca1d7edca9d80d37b Mon Sep 17 00:00:00 2001 From: jiandaoshou-aidehua <2893666867@qq.com> Date: Tue, 28 Jul 2026 14:48:18 +0800 Subject: [PATCH 041/128] =?UTF-8?q?=E6=B7=BB=E5=8A=A0=E4=B8=AA=E4=BA=BA?= =?UTF-8?q?=E4=BB=93=E5=BA=93github=E6=9C=8D=E5=8A=A1=E5=99=A8=E6=A0=87?= =?UTF-8?q?=E7=AD=BE?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .github/workflows/build_linux.yml | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/.github/workflows/build_linux.yml b/.github/workflows/build_linux.yml index 3a43b5ef19..704ef9f01d 100644 --- a/.github/workflows/build_linux.yml +++ b/.github/workflows/build_linux.yml @@ -9,8 +9,8 @@ jobs: linux-dev: name: Linux CI/CD timeout-minutes: 1440 - runs-on: [self-hosted, dev] - runs-on: [self-hosted, dev] + runs-on: [self-hosted, ubuntu-hutb] + # runs-on: [self-hosted, dev] env: UE4_ROOT: /home/ubuntu/UnrealEngine_4.26 From 425fe87603869bace722db0fe224f849c7c2c285 Mon Sep 17 00:00:00 2001 From: jiandaoshou-aidehua <2893666867@qq.com> Date: Tue, 28 Jul 2026 14:53:45 +0800 Subject: [PATCH 042/128] =?UTF-8?q?=E6=B3=A8=E9=87=8AUtil/BuildTools/Plugi?= =?UTF-8?q?ns.sh?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- Util/BuildTools/Plugins.sh | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/Util/BuildTools/Plugins.sh b/Util/BuildTools/Plugins.sh index 6aee0fd67e..6ee0981df4 100755 --- a/Util/BuildTools/Plugins.sh +++ b/Util/BuildTools/Plugins.sh @@ -28,7 +28,7 @@ done # -- Get Plugins --------------------------------------------------------------- # ============================================================================== -source $(dirname "$0")/Environment.sh +# source $(dirname "$0")/Environment.sh log "Retrieving Plugins" From 56a43ebdae7e13500ff9d55e5b5f97f7c9413248 Mon Sep 17 00:00:00 2001 From: xiao-ma-gua <17358869549@163.com> Date: Wed, 29 Jul 2026 09:41:16 +0800 Subject: [PATCH 043/128] =?UTF-8?q?fix(URLab):=20=E4=BF=AE=E5=A4=8D=20Cook?= =?UTF-8?q?=20=E9=98=B6=E6=AE=B5=20SetupAttachment=20ensure=20=E5=B4=A9?= =?UTF-8?q?=E6=BA=83=E5=AF=BC=E8=87=B4=E6=89=93=E5=8C=85=E5=A4=B1=E8=B4=A5?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .github/workflows/build_linux.yml | 9 +++++---- .../MuJoCo/Components/Geometry/Primitives/MjBox.cpp | 8 ++++++++ .../Components/Geometry/Primitives/MjCapsule.cpp | 8 ++++++++ .../Components/Geometry/Primitives/MjCylinder.cpp | 12 ++++++++++++ .../Components/Geometry/Primitives/MjSphere.cpp | 8 ++++++++ .../Source/URLab/Public/CoACD/CoacdInterface.h | 2 +- Unreal/CarlaUE4/Source/CarlaUE4.Target.cs | 3 +++ Unreal/CarlaUE4/Source/CarlaUE4Editor.Target.cs | 3 +++ 8 files changed, 48 insertions(+), 5 deletions(-) diff --git a/.github/workflows/build_linux.yml b/.github/workflows/build_linux.yml index 704ef9f01d..f746bb0c6a 100644 --- a/.github/workflows/build_linux.yml +++ b/.github/workflows/build_linux.yml @@ -61,13 +61,14 @@ jobs: mv Unreal/CarlaUE4/Plugins/UnrealRoboticsLab/third_party/install/CoACD/CoACD/include Unreal/CarlaUE4/Plugins/UnrealRoboticsLab/third_party/install/CoACD/include fi mkdir -p Unreal/CarlaUE4/Plugins/UnrealRoboticsLab/third_party/install/libzmq - tar -xzf Build/dependencies/Plugins/libzmq-linux.tar.gz -C Unreal/CarlaUE4/Plugins/UnrealRoboticsLab/third_party/install/libzmq 2>/dev/null || true - - # Replace symlinks with real files for staging + unzip -qo Build/dependencies/Plugins/libzmq-linux.zip -d Unreal/CarlaUE4/Plugins/UnrealRoboticsLab/third_party/install/ + + # Resolve symlinks for libzmq by copying the real .so in place ZMQ_LIB=Unreal/CarlaUE4/Plugins/UnrealRoboticsLab/third_party/install/libzmq/lib if [ -L "$ZMQ_LIB/libzmq.so" ]; then REAL=$(readlink -f "$ZMQ_LIB/libzmq.so") - rm "$ZMQ_LIB/libzmq.so" && cp "$REAL" "$ZMQ_LIB/libzmq.so" + rm "$ZMQ_LIB/libzmq.so" "$ZMQ_LIB/libzmq.so.5" 2>/dev/null || true + cp "$REAL" "$ZMQ_LIB/" && mv "$ZMQ_LIB/$(basename "$REAL")" "$ZMQ_LIB/libzmq.so" fi echo "=== Plugins extracted ===" fi diff --git a/Unreal/CarlaUE4/Plugins/UnrealRoboticsLab/Source/URLab/Private/MuJoCo/Components/Geometry/Primitives/MjBox.cpp b/Unreal/CarlaUE4/Plugins/UnrealRoboticsLab/Source/URLab/Private/MuJoCo/Components/Geometry/Primitives/MjBox.cpp index 9512a018a3..1f6f5da721 100644 --- a/Unreal/CarlaUE4/Plugins/UnrealRoboticsLab/Source/URLab/Private/MuJoCo/Components/Geometry/Primitives/MjBox.cpp +++ b/Unreal/CarlaUE4/Plugins/UnrealRoboticsLab/Source/URLab/Private/MuJoCo/Components/Geometry/Primitives/MjBox.cpp @@ -49,6 +49,10 @@ void UMjBox::EnsureVisualizerMesh() if (IsRegistered()) { + if (VisualizerMesh->GetAttachParent() != nullptr) + { + VisualizerMesh->DetachFromComponent(FDetachmentTransformRules::KeepRelativeTransform); + } VisualizerMesh->SetupAttachment(this); VisualizerMesh->RegisterComponent(); } @@ -62,6 +66,10 @@ void UMjBox::OnRegister() if (VisualizerMesh && !VisualizerMesh->IsRegistered()) { + if (VisualizerMesh->GetAttachParent() != nullptr) + { + VisualizerMesh->DetachFromComponent(FDetachmentTransformRules::KeepRelativeTransform); + } VisualizerMesh->SetupAttachment(this); VisualizerMesh->RegisterComponent(); } diff --git a/Unreal/CarlaUE4/Plugins/UnrealRoboticsLab/Source/URLab/Private/MuJoCo/Components/Geometry/Primitives/MjCapsule.cpp b/Unreal/CarlaUE4/Plugins/UnrealRoboticsLab/Source/URLab/Private/MuJoCo/Components/Geometry/Primitives/MjCapsule.cpp index 9a8f662662..bb953164e9 100644 --- a/Unreal/CarlaUE4/Plugins/UnrealRoboticsLab/Source/URLab/Private/MuJoCo/Components/Geometry/Primitives/MjCapsule.cpp +++ b/Unreal/CarlaUE4/Plugins/UnrealRoboticsLab/Source/URLab/Private/MuJoCo/Components/Geometry/Primitives/MjCapsule.cpp @@ -60,6 +60,10 @@ void UMjCapsule::EnsureVisualizerMesh() if (InMesh) SMC->SetStaticMesh(InMesh); if (IsRegistered()) { + if (SMC->GetAttachParent() != nullptr) + { + SMC->DetachFromComponent(FDetachmentTransformRules::KeepRelativeTransform); + } SMC->SetupAttachment(this); SMC->RegisterComponent(); } @@ -82,6 +86,10 @@ void UMjCapsule::OnRegister() { if (IsValid(SMC) && !SMC->IsRegistered()) { + if (SMC->GetAttachParent() != nullptr) + { + SMC->DetachFromComponent(FDetachmentTransformRules::KeepRelativeTransform); + } SMC->SetupAttachment(this); SMC->RegisterComponent(); } diff --git a/Unreal/CarlaUE4/Plugins/UnrealRoboticsLab/Source/URLab/Private/MuJoCo/Components/Geometry/Primitives/MjCylinder.cpp b/Unreal/CarlaUE4/Plugins/UnrealRoboticsLab/Source/URLab/Private/MuJoCo/Components/Geometry/Primitives/MjCylinder.cpp index deaec37eaa..b028039610 100644 --- a/Unreal/CarlaUE4/Plugins/UnrealRoboticsLab/Source/URLab/Private/MuJoCo/Components/Geometry/Primitives/MjCylinder.cpp +++ b/Unreal/CarlaUE4/Plugins/UnrealRoboticsLab/Source/URLab/Private/MuJoCo/Components/Geometry/Primitives/MjCylinder.cpp @@ -49,6 +49,14 @@ void UMjCylinder::EnsureVisualizerMesh() if (IsRegistered()) { + // Detach first if already attached (e.g. during Cook serialization + // the attachment state can be stale), to avoid the UE4.26 ensure: + // "SetupAttachment cannot be used once a component has already had + // AttachTo used to connect it to a parent." + if (VisualizerMesh->GetAttachParent() != nullptr) + { + VisualizerMesh->DetachFromComponent(FDetachmentTransformRules::KeepRelativeTransform); + } VisualizerMesh->SetupAttachment(this); VisualizerMesh->RegisterComponent(); } @@ -62,6 +70,10 @@ void UMjCylinder::OnRegister() if (VisualizerMesh && !VisualizerMesh->IsRegistered()) { + if (VisualizerMesh->GetAttachParent() != nullptr) + { + VisualizerMesh->DetachFromComponent(FDetachmentTransformRules::KeepRelativeTransform); + } VisualizerMesh->SetupAttachment(this); VisualizerMesh->RegisterComponent(); } diff --git a/Unreal/CarlaUE4/Plugins/UnrealRoboticsLab/Source/URLab/Private/MuJoCo/Components/Geometry/Primitives/MjSphere.cpp b/Unreal/CarlaUE4/Plugins/UnrealRoboticsLab/Source/URLab/Private/MuJoCo/Components/Geometry/Primitives/MjSphere.cpp index 6b2f2560d0..b19c4ee13b 100644 --- a/Unreal/CarlaUE4/Plugins/UnrealRoboticsLab/Source/URLab/Private/MuJoCo/Components/Geometry/Primitives/MjSphere.cpp +++ b/Unreal/CarlaUE4/Plugins/UnrealRoboticsLab/Source/URLab/Private/MuJoCo/Components/Geometry/Primitives/MjSphere.cpp @@ -49,6 +49,10 @@ void UMjSphere::EnsureVisualizerMesh() if (IsRegistered()) { + if (VisualizerMesh->GetAttachParent() != nullptr) + { + VisualizerMesh->DetachFromComponent(FDetachmentTransformRules::KeepRelativeTransform); + } VisualizerMesh->SetupAttachment(this); VisualizerMesh->RegisterComponent(); } @@ -62,6 +66,10 @@ void UMjSphere::OnRegister() if (VisualizerMesh && !VisualizerMesh->IsRegistered()) { + if (VisualizerMesh->GetAttachParent() != nullptr) + { + VisualizerMesh->DetachFromComponent(FDetachmentTransformRules::KeepRelativeTransform); + } VisualizerMesh->SetupAttachment(this); VisualizerMesh->RegisterComponent(); } diff --git a/Unreal/CarlaUE4/Plugins/UnrealRoboticsLab/Source/URLab/Public/CoACD/CoacdInterface.h b/Unreal/CarlaUE4/Plugins/UnrealRoboticsLab/Source/URLab/Public/CoACD/CoacdInterface.h index a4cb61e138..04e6aa9634 100644 --- a/Unreal/CarlaUE4/Plugins/UnrealRoboticsLab/Source/URLab/Public/CoACD/CoacdInterface.h +++ b/Unreal/CarlaUE4/Plugins/UnrealRoboticsLab/Source/URLab/Public/CoACD/CoacdInterface.h @@ -27,7 +27,7 @@ #include #include #include "Chaos/ArrayCollectionArray.h" -#include "Coacd/coacd.h" +#include "CoACD/coacd.h" #include "Misc/FileHelper.h" namespace CoacdInterface { diff --git a/Unreal/CarlaUE4/Source/CarlaUE4.Target.cs b/Unreal/CarlaUE4/Source/CarlaUE4.Target.cs index 80da7fcc6c..6c4ce698c1 100644 --- a/Unreal/CarlaUE4/Source/CarlaUE4.Target.cs +++ b/Unreal/CarlaUE4/Source/CarlaUE4.Target.cs @@ -35,5 +35,8 @@ public CarlaUE4Target(TargetInfo Target) : base(Target) } AdditionalCompilerArguments = "/wd4103"; + + // URLab 插件的 MuJoCo 依赖需要 C++17(std::byte) + CppStandard = CppStandardVersion.Cpp17; } } diff --git a/Unreal/CarlaUE4/Source/CarlaUE4Editor.Target.cs b/Unreal/CarlaUE4/Source/CarlaUE4Editor.Target.cs index 6ed5e1022d..5504f099ae 100644 --- a/Unreal/CarlaUE4/Source/CarlaUE4Editor.Target.cs +++ b/Unreal/CarlaUE4/Source/CarlaUE4Editor.Target.cs @@ -35,5 +35,8 @@ public CarlaUE4EditorTarget(TargetInfo Target) : base(Target) } AdditionalCompilerArguments = "/wd4103"; + + // URLab 插件的 MuJoCo 依赖需要 C++17(std::byte) + CppStandard = CppStandardVersion.Cpp17; } } From c367c30ede1ceac5ec81268eccf711f9c8839d9b Mon Sep 17 00:00:00 2001 From: xiao-ma-gua <17358869549@163.com> Date: Fri, 31 Jul 2026 09:04:33 +0800 Subject: [PATCH 044/128] =?UTF-8?q?=E6=9B=B4=E6=8D=A2=E6=A0=87=E7=AD=BE?= =?UTF-8?q?=E4=BB=A5=E5=8F=8A=E5=9C=A8=E6=89=A7=E8=A1=8C=20make=20package?= =?UTF-8?q?=20=E4=B9=8B=E5=89=8D=EF=BC=8C=E4=B8=8D=E5=8A=A8=E6=80=81?= =?UTF-8?q?=E6=B3=A8=E5=85=A5=20return=200?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .github/workflows/build_linux.yml | 16 ++++++++-------- 1 file changed, 8 insertions(+), 8 deletions(-) diff --git a/.github/workflows/build_linux.yml b/.github/workflows/build_linux.yml index f746bb0c6a..3d8ab26eb0 100644 --- a/.github/workflows/build_linux.yml +++ b/.github/workflows/build_linux.yml @@ -9,8 +9,8 @@ jobs: linux-dev: name: Linux CI/CD timeout-minutes: 1440 - runs-on: [self-hosted, ubuntu-hutb] - # runs-on: [self-hosted, dev] + # runs-on: [self-hosted, ubuntu-hutb] + runs-on: [self-hosted, dev] env: UE4_ROOT: /home/ubuntu/UnrealEngine_4.26 @@ -117,12 +117,12 @@ jobs: make CarlaUE4Editor ARGS="--chrono -j$(( $(nproc) / 3 ))" shell: bash - # 在执行 make package 之前,动态注入 return 0 - - name: Patch Package.sh to skip bad materials - run: | - echo "=== 正在动态修改 Package.sh,跳过 cook_tagged_materials ===" - sed -i 's/function cook_tagged_materials {/function cook_tagged_materials {\n return 0/g' Util/BuildTools/Package.sh - shell: bash + # # 在执行 make package 之前,动态注入 return 0 + # - name: Patch Package.sh to skip bad materials + # run: | + # echo "=== 正在动态修改 Package.sh,跳过 cook_tagged_materials ===" + # sed -i 's/function cook_tagged_materials {/function cook_tagged_materials {\n return 0/g' Util/BuildTools/Package.sh + # shell: bash # 修复 Content 包中 .uasset 文件的非法材质路径(缺少 /Game/ 根前缀) - name: Fix invalid material paths in RoadRunner assets From 4d216ad687494f8cefb6dca84b406ac5c41bab44 Mon Sep 17 00:00:00 2001 From: xiao-ma-gua <17358869549@163.com> Date: Fri, 31 Jul 2026 11:57:25 +0800 Subject: [PATCH 045/128] =?UTF-8?q?=E8=A7=A3=E5=86=B3=E6=89=A7=E8=A1=8C./C?= =?UTF-8?q?arlaUE4.sh=E6=B2=A1=E6=9C=89libmujoco.so=E6=96=87=E4=BB=B6?= =?UTF-8?q?=E7=9A=84=E9=97=AE=E9=A2=98?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- Util/BuildTools/Package.sh | 9 ++++++++- 1 file changed, 8 insertions(+), 1 deletion(-) diff --git a/Util/BuildTools/Package.sh b/Util/BuildTools/Package.sh index 23e95eb94b..528559d65f 100755 --- a/Util/BuildTools/Package.sh +++ b/Util/BuildTools/Package.sh @@ -120,7 +120,6 @@ function copy_dir_fast { } function cook_tagged_materials { - # return 0 # Measure duration of this function call T_START_COOK_TAGGED_MATS=$(date +%s) @@ -340,6 +339,14 @@ if ${DO_CARLA_RELEASE} ; then cp -r "./Unreal/CarlaUE4/Plugins/Carla/CarlaDependencies/lib" "${DESTINATION}/CarlaUE4/Plugins/Carla/CarlaDependencies" fi + # 修复 MuJoCo 版本化库:UBT *.so 全局通配符与 *.so.3.7.0 不匹配,导致 libmujoco.so 被编译为 staging 文件,但 libmujoco.so.3.7.0 未被正确生成。 + # 二进制文件的 RPATH 已经指向该目录,只需在运行时使用版本化的文件名即可(SONAME 为 libmujoco.so.3.7.0)。 + MUJOCO_STAGED_LIB="${DESTINATION}/CarlaUE4/Plugins/UnrealRoboticsLab/third_party/install/MuJoCo/lib" + if [ -f "$MUJOCO_STAGED_LIB/libmujoco.so" ] && [ ! -f "$MUJOCO_STAGED_LIB/libmujoco.so.3.7.0" ]; then + ln -s libmujoco.so "$MUJOCO_STAGED_LIB/libmujoco.so.3.7.0" + log "Created libmujoco.so.3.7.0 -> libmujoco.so symlink" + fi + copy_if_changed "./Unreal/CarlaUE4/Content/Carla/HDMaps/*.pcd" "${DESTINATION}/HDMaps/" copy_if_changed "./Unreal/CarlaUE4/Content/Carla/HDMaps/Readme.md" "${DESTINATION}/HDMaps/README" From d215992b20bd372592020f5da4b00e91c79f7a3a Mon Sep 17 00:00:00 2001 From: xiao-ma-gua <17358869549@163.com> Date: Sat, 1 Aug 2026 10:38:53 +0800 Subject: [PATCH 046/128] =?UTF-8?q?=E5=B0=86libmujoco.so=E5=A4=8D=E5=88=B6?= =?UTF-8?q?=E5=88=B0=E6=8F=92=E4=BB=B6=E7=9A=84=E7=9B=B8=E5=BA=94=E4=BD=8D?= =?UTF-8?q?=E7=BD=AE?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- Util/BuildTools/Package.sh | 14 ++++++++------ 1 file changed, 8 insertions(+), 6 deletions(-) diff --git a/Util/BuildTools/Package.sh b/Util/BuildTools/Package.sh index 528559d65f..5f9bcc193e 100755 --- a/Util/BuildTools/Package.sh +++ b/Util/BuildTools/Package.sh @@ -339,12 +339,14 @@ if ${DO_CARLA_RELEASE} ; then cp -r "./Unreal/CarlaUE4/Plugins/Carla/CarlaDependencies/lib" "${DESTINATION}/CarlaUE4/Plugins/Carla/CarlaDependencies" fi - # 修复 MuJoCo 版本化库:UBT *.so 全局通配符与 *.so.3.7.0 不匹配,导致 libmujoco.so 被编译为 staging 文件,但 libmujoco.so.3.7.0 未被正确生成。 - # 二进制文件的 RPATH 已经指向该目录,只需在运行时使用版本化的文件名即可(SONAME 为 libmujoco.so.3.7.0)。 - MUJOCO_STAGED_LIB="${DESTINATION}/CarlaUE4/Plugins/UnrealRoboticsLab/third_party/install/MuJoCo/lib" - if [ -f "$MUJOCO_STAGED_LIB/libmujoco.so" ] && [ ! -f "$MUJOCO_STAGED_LIB/libmujoco.so.3.7.0" ]; then - ln -s libmujoco.so "$MUJOCO_STAGED_LIB/libmujoco.so.3.7.0" - log "Created libmujoco.so.3.7.0 -> libmujoco.so symlink" + # UBT 的 *.so 匹配不包括 *.so.3.7.0,因此需显式复制带版本号的文件 + MUJOCO_SRC_LIB="./Unreal/CarlaUE4/Plugins/UnrealRoboticsLab/third_party/install/MuJoCo/lib" + MUJOCO_DST_LIB="${DESTINATION}/CarlaUE4/Plugins/UnrealRoboticsLab/third_party/install/MuJoCo/lib" + if [ -f "$MUJOCO_SRC_LIB/libmujoco.so" ]; then + mkdir -p "$MUJOCO_DST_LIB" + cp -u "$MUJOCO_SRC_LIB/libmujoco.so" "$MUJOCO_DST_LIB/" + cp -u "$MUJOCO_SRC_LIB/libmujoco.so" "$MUJOCO_DST_LIB/libmujoco.so.3.7.0" + log "Packaged MuJoCo library" fi copy_if_changed "./Unreal/CarlaUE4/Content/Carla/HDMaps/*.pcd" "${DESTINATION}/HDMaps/" From feef166d77a24296d06a598c16c370c18de70b6a Mon Sep 17 00:00:00 2001 From: xiao-ma-gua <17358869549@163.com> Date: Sat, 1 Aug 2026 18:28:31 +0800 Subject: [PATCH 047/128] =?UTF-8?q?=E7=BB=95=E8=BF=87=E4=BB=A3=E7=90=86?= =?UTF-8?q?=EF=BC=8C=E4=BB=A5=E8=A7=A3=E5=86=B3=E9=80=9A=E8=BF=87=E4=BB=A3?= =?UTF-8?q?=E7=90=86=E5=A4=B1=E8=B4=A5=E7=9A=84=E5=A4=96=E9=83=A8=E4=B8=8B?= =?UTF-8?q?=E8=BD=BD=E9=97=AE=E9=A2=98?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- Util/BuildTools/BuildUE4Plugins.sh | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/Util/BuildTools/BuildUE4Plugins.sh b/Util/BuildTools/BuildUE4Plugins.sh index 7b1832504d..9abcead28c 100755 --- a/Util/BuildTools/BuildUE4Plugins.sh +++ b/Util/BuildTools/BuildUE4Plugins.sh @@ -152,7 +152,8 @@ if [ "$BUILD_AIR" = "true" ]; then echo "Current dir: $(pwd)" # Build AirSim # read -p "press enter to continue ..." - ./setup.sh + # 绕过代理,以解决通过代理失败的外部下载(例如 gitlab.com SSL)问题 + env -u http_proxy -u https_proxy -u HTTP_PROXY -u HTTPS_PROXY ./setup.sh ./build.sh cp -a \ "$AIR_BUILD_PATH/Unreal/Plugins/AirSim" \ From 844e3116670751b6cdd6e3917f9650854ebf43d6 Mon Sep 17 00:00:00 2001 From: xiao-ma-gua <17358869549@163.com> Date: Sun, 2 Aug 2026 10:35:08 +0800 Subject: [PATCH 048/128] =?UTF-8?q?=E6=9B=B4=E6=94=B9clang=E4=B8=BA11?= =?UTF-8?q?=E7=89=88=E6=9C=AC=E6=9D=A5=E8=A7=A3=E5=86=B3=E7=BC=96=E8=AF=91?= =?UTF-8?q?=20Boost=201.84.0=20=E7=9A=84=20wrapper.cpp=20=E6=97=B6?= =?UTF-8?q?=EF=BC=8Cclang-10=20=E9=A2=84=E5=A4=84=E7=90=86=E5=99=A8?= =?UTF-8?q?=E7=9A=84=E5=AE=8F=E5=B1=95=E5=BC=80=E6=A0=88=E6=BA=A2=E5=87=BA?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- Util/BuildTools/Setup.sh | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/Util/BuildTools/Setup.sh b/Util/BuildTools/Setup.sh index 22f95dcb78..8b3e05fdd5 100755 --- a/Util/BuildTools/Setup.sh +++ b/Util/BuildTools/Setup.sh @@ -56,8 +56,8 @@ source $(dirname "$0")/Environment.sh #export CXX="$UE4_ROOT/Engine/Extras/ThirdPartyNotUE/SDKs/HostLinux/Linux_x64/v17_clang-10.0.1-centos7/x86_64-unknown-linux-gnu/bin/clang++" #export PATH="$UE4_ROOT/Engine/Extras/ThirdPartyNotUE/SDKs/HostLinux/Linux_x64/v17_clang-10.0.1-centos7/x86_64-unknown-linux-gnu/bin:$PATH" -export CC=/usr/bin/clang -export CXX=/usr/bin/clang++ +export CC=/usr/bin/clang-11 +export CXX=/usr/bin/clang++-11 export PATH=/usr/bin:/bin:$PATH echo $PATH From 9ba482f515a23499c093c2191cb07fa033f0c402 Mon Sep 17 00:00:00 2001 From: xiao-ma-gua <17358869549@163.com> Date: Sun, 2 Aug 2026 11:25:49 +0800 Subject: [PATCH 049/128] =?UTF-8?q?=E8=AE=A9=20Boost=20=E7=94=A8=20GCC=20?= =?UTF-8?q?=E7=BC=96=E8=AF=91?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- Util/BuildTools/Setup.sh | 10 ++++++---- 1 file changed, 6 insertions(+), 4 deletions(-) diff --git a/Util/BuildTools/Setup.sh b/Util/BuildTools/Setup.sh index 8b3e05fdd5..0babcac4b5 100755 --- a/Util/BuildTools/Setup.sh +++ b/Util/BuildTools/Setup.sh @@ -56,8 +56,8 @@ source $(dirname "$0")/Environment.sh #export CXX="$UE4_ROOT/Engine/Extras/ThirdPartyNotUE/SDKs/HostLinux/Linux_x64/v17_clang-10.0.1-centos7/x86_64-unknown-linux-gnu/bin/clang++" #export PATH="$UE4_ROOT/Engine/Extras/ThirdPartyNotUE/SDKs/HostLinux/Linux_x64/v17_clang-10.0.1-centos7/x86_64-unknown-linux-gnu/bin:$PATH" -export CC=/usr/bin/clang-11 -export CXX=/usr/bin/clang++-11 +export CC=/usr/bin/clang +export CXX=/usr/bin/clang++ export PATH=/usr/bin:/bin:$PATH echo $PATH @@ -186,7 +186,8 @@ for PY_VERSION in ${PY_VERSION_LIST[@]} ; do pushd ${BOOST_BASENAME}-source >/dev/null - BOOST_TOOLSET="clang-10.0" + # BOOST_TOOLSET="clang-10.0" + BOOST_TOOLSET="gcc" BOOST_CFLAGS="-fPIC -std=c++14 -DBOOST_ERROR_CODE_HEADER_ONLY" py3="/usr/bin/env python${PY_VERSION}" @@ -195,7 +196,8 @@ for PY_VERSION in ${PY_VERSION_LIST[@]} ; do py3_lib=$(${py3} -c "from sysconfig import get_paths as gp; print(gp()['stdlib'])") pyv=`$py3 -c "import sys;x='{v[0]}.{v[1]}'.format(v=list(sys.version_info[:2]));sys.stdout.write(x)";` ./bootstrap.sh \ - --with-toolset=clang \ + # --with-toolset=clang \ + --with-toolset=gcc \ --prefix=../boost-install \ --with-libraries=python,filesystem,system,program_options \ --with-python=${py3} --with-python-root=${py3_root} From c8ad0eb4f4f95745283a0620778283edc625686d Mon Sep 17 00:00:00 2001 From: xiao-ma-gua <17358869549@163.com> Date: Sun, 2 Aug 2026 12:56:13 +0800 Subject: [PATCH 050/128] =?UTF-8?q?=E5=88=A0=E9=99=A4=E6=B3=A8=E9=87=8A?= =?UTF-8?q?=E8=A1=8C?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- Util/BuildTools/Setup.sh | 1 - 1 file changed, 1 deletion(-) diff --git a/Util/BuildTools/Setup.sh b/Util/BuildTools/Setup.sh index 0babcac4b5..78b211039e 100755 --- a/Util/BuildTools/Setup.sh +++ b/Util/BuildTools/Setup.sh @@ -196,7 +196,6 @@ for PY_VERSION in ${PY_VERSION_LIST[@]} ; do py3_lib=$(${py3} -c "from sysconfig import get_paths as gp; print(gp()['stdlib'])") pyv=`$py3 -c "import sys;x='{v[0]}.{v[1]}'.format(v=list(sys.version_info[:2]));sys.stdout.write(x)";` ./bootstrap.sh \ - # --with-toolset=clang \ --with-toolset=gcc \ --prefix=../boost-install \ --with-libraries=python,filesystem,system,program_options \ From 9a2228bf291b99f26d3aa6243cde81636a2046b7 Mon Sep 17 00:00:00 2001 From: xiao-ma-gua <17358869549@163.com> Date: Sun, 2 Aug 2026 15:16:35 +0800 Subject: [PATCH 051/128] =?UTF-8?q?=E5=9C=A8=E4=B8=BB=E4=BB=93=E5=BA=93?= =?UTF-8?q?=E8=BF=9B=E8=A1=8C=E6=B5=8B=E8=AF=95?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .github/workflows/build_linux.yml | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/.github/workflows/build_linux.yml b/.github/workflows/build_linux.yml index 3d8ab26eb0..d809acf387 100644 --- a/.github/workflows/build_linux.yml +++ b/.github/workflows/build_linux.yml @@ -9,8 +9,8 @@ jobs: linux-dev: name: Linux CI/CD timeout-minutes: 1440 - # runs-on: [self-hosted, ubuntu-hutb] - runs-on: [self-hosted, dev] + runs-on: [self-hosted, ubuntu-hutb] + # runs-on: [self-hosted, dev] env: UE4_ROOT: /home/ubuntu/UnrealEngine_4.26 From 8adfd355b891771c1d974439fa8b7edc91b4c21d Mon Sep 17 00:00:00 2001 From: xiao-ma-gua <17358869549@163.com> Date: Mon, 3 Aug 2026 17:17:49 +0800 Subject: [PATCH 052/128] =?UTF-8?q?gcc-9=E7=BC=96=E8=AF=91boost=E8=BF=98?= =?UTF-8?q?=E6=98=AF=E4=BC=9A=E5=87=BA=E7=8E=B0=E7=BC=96=E8=AF=91=E5=99=A8?= =?UTF-8?q?=E5=B4=A9=E6=BA=83=E7=9A=84=E9=97=AE=E9=A2=98,=E6=94=B9?= =?UTF-8?q?=E7=94=A8clang11=E7=BC=96=E8=AF=91?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- Util/BuildTools/Setup.sh | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) diff --git a/Util/BuildTools/Setup.sh b/Util/BuildTools/Setup.sh index 78b211039e..746acddb23 100755 --- a/Util/BuildTools/Setup.sh +++ b/Util/BuildTools/Setup.sh @@ -64,7 +64,7 @@ echo $PATH #read -p "Press Enter to continue..." -CXX_TAG=c10 +CXX_TAG=c11 # Convert comma-separated string to array of unique elements. IFS="," read -r -a PY_VERSION_LIST <<< "${PY_VERSION_LIST}" @@ -186,8 +186,8 @@ for PY_VERSION in ${PY_VERSION_LIST[@]} ; do pushd ${BOOST_BASENAME}-source >/dev/null - # BOOST_TOOLSET="clang-10.0" - BOOST_TOOLSET="gcc" + BOOST_TOOLSET="clang-11" + # BOOST_TOOLSET="gcc" BOOST_CFLAGS="-fPIC -std=c++14 -DBOOST_ERROR_CODE_HEADER_ONLY" py3="/usr/bin/env python${PY_VERSION}" @@ -196,7 +196,7 @@ for PY_VERSION in ${PY_VERSION_LIST[@]} ; do py3_lib=$(${py3} -c "from sysconfig import get_paths as gp; print(gp()['stdlib'])") pyv=`$py3 -c "import sys;x='{v[0]}.{v[1]}'.format(v=list(sys.version_info[:2]));sys.stdout.write(x)";` ./bootstrap.sh \ - --with-toolset=gcc \ + --with-toolset=clang-11 \ --prefix=../boost-install \ --with-libraries=python,filesystem,system,program_options \ --with-python=${py3} --with-python-root=${py3_root} From 351b9010bbb84dd3bc135f62010e07e01c84065c Mon Sep 17 00:00:00 2001 From: xiao-ma-gua <17358869549@163.com> Date: Mon, 3 Aug 2026 21:22:32 +0800 Subject: [PATCH 053/128] =?UTF-8?q?=E4=BD=BF=E7=94=A8clang-10=E7=BC=96?= =?UTF-8?q?=E8=AF=91=E5=99=A8?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- Util/BuildTools/Setup.sh | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/Util/BuildTools/Setup.sh b/Util/BuildTools/Setup.sh index 746acddb23..9d2d6277b9 100755 --- a/Util/BuildTools/Setup.sh +++ b/Util/BuildTools/Setup.sh @@ -64,7 +64,7 @@ echo $PATH #read -p "Press Enter to continue..." -CXX_TAG=c11 +CXX_TAG=c10 # Convert comma-separated string to array of unique elements. IFS="," read -r -a PY_VERSION_LIST <<< "${PY_VERSION_LIST}" @@ -186,7 +186,7 @@ for PY_VERSION in ${PY_VERSION_LIST[@]} ; do pushd ${BOOST_BASENAME}-source >/dev/null - BOOST_TOOLSET="clang-11" + BOOST_TOOLSET="clang-10" # BOOST_TOOLSET="gcc" BOOST_CFLAGS="-fPIC -std=c++14 -DBOOST_ERROR_CODE_HEADER_ONLY" @@ -196,7 +196,7 @@ for PY_VERSION in ${PY_VERSION_LIST[@]} ; do py3_lib=$(${py3} -c "from sysconfig import get_paths as gp; print(gp()['stdlib'])") pyv=`$py3 -c "import sys;x='{v[0]}.{v[1]}'.format(v=list(sys.version_info[:2]));sys.stdout.write(x)";` ./bootstrap.sh \ - --with-toolset=clang-11 \ + --with-toolset=clang-10 \ --prefix=../boost-install \ --with-libraries=python,filesystem,system,program_options \ --with-python=${py3} --with-python-root=${py3_root} From c11eab66f980860354ae9ddd423f4d4904b0dcad Mon Sep 17 00:00:00 2001 From: xiao-ma-gua <17358869549@163.com> Date: Mon, 3 Aug 2026 21:32:06 +0800 Subject: [PATCH 054/128] =?UTF-8?q?=E4=BD=BF=E7=94=A8clang-10=E7=BC=96?= =?UTF-8?q?=E8=AF=91=E5=99=A8?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- Util/BuildTools/Setup.sh | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/Util/BuildTools/Setup.sh b/Util/BuildTools/Setup.sh index 9d2d6277b9..3bca6f13e8 100755 --- a/Util/BuildTools/Setup.sh +++ b/Util/BuildTools/Setup.sh @@ -186,7 +186,7 @@ for PY_VERSION in ${PY_VERSION_LIST[@]} ; do pushd ${BOOST_BASENAME}-source >/dev/null - BOOST_TOOLSET="clang-10" + BOOST_TOOLSET="clang-10.0" # BOOST_TOOLSET="gcc" BOOST_CFLAGS="-fPIC -std=c++14 -DBOOST_ERROR_CODE_HEADER_ONLY" @@ -196,7 +196,7 @@ for PY_VERSION in ${PY_VERSION_LIST[@]} ; do py3_lib=$(${py3} -c "from sysconfig import get_paths as gp; print(gp()['stdlib'])") pyv=`$py3 -c "import sys;x='{v[0]}.{v[1]}'.format(v=list(sys.version_info[:2]));sys.stdout.write(x)";` ./bootstrap.sh \ - --with-toolset=clang-10 \ + --with-toolset=clang \ --prefix=../boost-install \ --with-libraries=python,filesystem,system,program_options \ --with-python=${py3} --with-python-root=${py3_root} From a26930a06ef0fa655e5bd46dbadf8da34578d1e2 Mon Sep 17 00:00:00 2001 From: xiao-ma-gua <17358869549@163.com> Date: Tue, 4 Aug 2026 10:20:35 +0800 Subject: [PATCH 055/128] =?UTF-8?q?=E5=85=B3=E6=8E=89=E8=AE=BE=E7=BD=AE?= =?UTF-8?q?=E7=9A=84=E4=BB=A3=E7=90=86?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- setEnv64.sh | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/setEnv64.sh b/setEnv64.sh index cc64090f55..a095c2f83e 100644 --- a/setEnv64.sh +++ b/setEnv64.sh @@ -1,5 +1,5 @@ -set http_proxy=http://127.0.0.1:7890 -set https_proxy=http://127.0.0.1:7890 +# set http_proxy=http://127.0.0.1:7890 +# set https_proxy=http://127.0.0.1:7890 # python.exe set PATH=/usr/bin/python From fa2a05e163d6a27ca8cc53aad41e48c03b6f1a83 Mon Sep 17 00:00:00 2001 From: xiao-ma-gua <17358869549@163.com> Date: Tue, 4 Aug 2026 11:53:21 +0800 Subject: [PATCH 056/128] =?UTF-8?q?=E6=89=93=E5=BC=80=E8=AE=BE=E7=BD=AE?= =?UTF-8?q?=E7=9A=84=E4=BB=A3=E7=90=86?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- setEnv64.sh | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/setEnv64.sh b/setEnv64.sh index a095c2f83e..cc64090f55 100644 --- a/setEnv64.sh +++ b/setEnv64.sh @@ -1,5 +1,5 @@ -# set http_proxy=http://127.0.0.1:7890 -# set https_proxy=http://127.0.0.1:7890 +set http_proxy=http://127.0.0.1:7890 +set https_proxy=http://127.0.0.1:7890 # python.exe set PATH=/usr/bin/python From 90209a926f5d2b4bd54bddb8b6d6afa5e08b126d Mon Sep 17 00:00:00 2001 From: xiao-ma-gua <17358869549@163.com> Date: Fri, 7 Aug 2026 16:58:06 +0800 Subject: [PATCH 057/128] =?UTF-8?q?=E7=94=B1=E4=BA=8E=E8=BF=98=E6=98=AF?= =?UTF-8?q?=E8=A6=81=E4=BD=BF=E7=94=A8=E4=BB=A3=E7=90=86=E6=9D=A5=E7=BC=96?= =?UTF-8?q?=E8=AF=91carla=EF=BC=8C=E5=B0=B1=E6=9B=B4=E6=8D=A2stellar(?= =?UTF-8?q?=E4=B8=8D=E9=99=90=E6=B5=81=E9=87=8F=E7=9A=84)=E4=BB=A3?= =?UTF-8?q?=E7=90=86=E4=BA=86=E3=80=82=E4=BD=86=E6=98=AF=E4=BB=96=E7=9A=84?= =?UTF-8?q?=E9=85=8D=E7=BD=AE=E6=96=87=E4=BB=B6=E6=98=AF=E5=8A=A0=E5=AF=86?= =?UTF-8?q?=E7=9A=84=E7=AB=AF=E5=8F=A3=E6=94=B9=E4=B8=8D=E4=BA=86=EF=BC=8C?= =?UTF-8?q?=E6=89=80=E4=BB=A5=E5=8F=AA=E8=83=BD=E5=90=8C=E6=AD=A5=E4=BB=96?= =?UTF-8?q?=E7=9A=84=E7=AB=AF=E5=8F=A3=E4=BA=86?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- setEnv64.sh | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/setEnv64.sh b/setEnv64.sh index cc64090f55..b96da91471 100644 --- a/setEnv64.sh +++ b/setEnv64.sh @@ -1,5 +1,5 @@ -set http_proxy=http://127.0.0.1:7890 -set https_proxy=http://127.0.0.1:7890 +set http_proxy=http://127.0.0.1:8668 +set https_proxy=http://127.0.0.1:8668 # python.exe set PATH=/usr/bin/python From 1dd30109ac1e9ade8c765e29487ac6a7da51fd70 Mon Sep 17 00:00:00 2001 From: xiao-ma-gua <17358869549@163.com> Date: Mon, 31 Aug 2026 14:44:05 +0800 Subject: [PATCH 058/128] =?UTF-8?q?=E4=BD=BF=E7=94=A8=E6=9C=AC=E5=9C=B0?= =?UTF-8?q?=E4=BB=93=E5=BA=93=E6=89=A7=E8=A1=8Cmake=20check?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .github/workflows/build_linux.yml | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/.github/workflows/build_linux.yml b/.github/workflows/build_linux.yml index d809acf387..3d8ab26eb0 100644 --- a/.github/workflows/build_linux.yml +++ b/.github/workflows/build_linux.yml @@ -9,8 +9,8 @@ jobs: linux-dev: name: Linux CI/CD timeout-minutes: 1440 - runs-on: [self-hosted, ubuntu-hutb] - # runs-on: [self-hosted, dev] + # runs-on: [self-hosted, ubuntu-hutb] + runs-on: [self-hosted, dev] env: UE4_ROOT: /home/ubuntu/UnrealEngine_4.26 From f8ee0d4ec1138f815885cd4bda9bf6ebeee75122 Mon Sep 17 00:00:00 2001 From: xiao-ma-gua <17358869549@163.com> Date: Mon, 31 Aug 2026 14:54:21 +0800 Subject: [PATCH 059/128] =?UTF-8?q?=E5=9C=A8=E6=9C=AC=E5=9C=B0=E4=BB=93?= =?UTF-8?q?=E5=BA=93=E6=89=A7=E8=A1=8Cmake=20check?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .github/workflows/build_linux.yml | 1 - 1 file changed, 1 deletion(-) diff --git a/.github/workflows/build_linux.yml b/.github/workflows/build_linux.yml index d4ec4bc4cc..3d8ab26eb0 100644 --- a/.github/workflows/build_linux.yml +++ b/.github/workflows/build_linux.yml @@ -12,7 +12,6 @@ jobs: # runs-on: [self-hosted, ubuntu-hutb] runs-on: [self-hosted, dev] - env: UE4_ROOT: /home/ubuntu/UnrealEngine_4.26 From d9a18f9cb9b2a7d48f5b37ed2772240c2d763d5f Mon Sep 17 00:00:00 2001 From: xiao-ma-gua <17358869549@163.com> Date: Wed, 2 Sep 2026 16:54:51 +0800 Subject: [PATCH 060/128] =?UTF-8?q?=E6=B7=BB=E5=8A=A0make=20check=E8=87=AA?= =?UTF-8?q?=E5=8A=A8=E5=8C=96=E6=B5=8B=E8=AF=95?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .github/workflows/build_linux.yml | 17 ++++++++--------- .../carla/streaming/detail/MultiStreamState.h | 4 ++-- .../source/carla/streaming/detail/Session.h | 14 +++++++++++--- Util/BuildTools/BuildLibCarla.sh | 1 + Util/BuildTools/Check.sh | 8 ++++++-- Util/BuildTools/Setup.sh | 2 +- 6 files changed, 29 insertions(+), 17 deletions(-) diff --git a/.github/workflows/build_linux.yml b/.github/workflows/build_linux.yml index 3d8ab26eb0..c21ad7e0d6 100644 --- a/.github/workflows/build_linux.yml +++ b/.github/workflows/build_linux.yml @@ -116,13 +116,6 @@ jobs: source ./setEnv64.sh make CarlaUE4Editor ARGS="--chrono -j$(( $(nproc) / 3 ))" shell: bash - - # # 在执行 make package 之前,动态注入 return 0 - # - name: Patch Package.sh to skip bad materials - # run: | - # echo "=== 正在动态修改 Package.sh,跳过 cook_tagged_materials ===" - # sed -i 's/function cook_tagged_materials {/function cook_tagged_materials {\n return 0/g' Util/BuildTools/Package.sh - # shell: bash # 修复 Content 包中 .uasset 文件的非法材质路径(缺少 /Game/ 根前缀) - name: Fix invalid material paths in RoadRunner assets @@ -142,7 +135,7 @@ jobs: echo "修复完成。" shell: bash - - name: package + - name: package run: | source /home/ubuntu/miniconda3/etc/profile.d/conda.sh conda activate carla @@ -153,5 +146,11 @@ jobs: make package ARGS="--chrono" shell: bash - + # 运行 LibCarla 单元测试(server/client × debug/release)与 Python API 单元测试。 + # 依赖 LibCarla 与 PythonAPI 两个 make 目标,首次运行会自动构建(约 30-60 分钟)。 + - name: check + run: | + source ./setEnv64.sh + make check ARGS="--chrono" + shell: bash diff --git a/LibCarla/source/carla/streaming/detail/MultiStreamState.h b/LibCarla/source/carla/streaming/detail/MultiStreamState.h index c395a5b9a3..7910b96a4e 100644 --- a/LibCarla/source/carla/streaming/detail/MultiStreamState.h +++ b/LibCarla/source/carla/streaming/detail/MultiStreamState.h @@ -34,7 +34,7 @@ namespace detail { // try write single stream auto session = _session.load(); if (session != nullptr) { - auto message = Session::MakeMessage(buffers...); + auto message = Session::MakeMessage(std::move(buffers)...); session->WriteMessage(std::move(message)); log_debug("sensor ", session->get_stream_id(), " data sent"); // Return here, _session is only valid if we have a @@ -45,7 +45,7 @@ namespace detail { // try write multiple stream std::lock_guard lock(_mutex); if (_sessions.size() > 0) { - auto message = Session::MakeMessage(buffers...); + auto message = Session::MakeMessage(std::move(buffers)...); for (auto &s : _sessions) { if (s != nullptr) { s->WriteMessage(message); diff --git a/LibCarla/source/carla/streaming/detail/Session.h b/LibCarla/source/carla/streaming/detail/Session.h index f8a52c3dc0..67c6eb16a0 100644 --- a/LibCarla/source/carla/streaming/detail/Session.h +++ b/LibCarla/source/carla/streaming/detail/Session.h @@ -28,13 +28,21 @@ namespace detail { Session(stream_id_type stream_id = 0) :_stream_id(stream_id) {} virtual ~Session() = default; + static auto AsView(Buffer buffer) { + return BufferView::CreateFrom(std::move(buffer)); + } + + static SharedBufferView AsView(SharedBufferView buffer) { + return buffer; + } + template static auto MakeMessage(Buffers... buffers) { // static_assert( // are_same::value, // "This function only accepts arguments of type BufferView."); - return std::make_shared(buffers...); - + return std::make_shared(AsView(std::move(buffers))...); + } /// @warning This function should only be called after the session is @@ -49,7 +57,7 @@ namespace detail { /// Writes some data to the socket. template void Write(Buffers... buffers) { - WriteMessage(MakeMessage(buffers...)); + WriteMessage(MakeMessage(std::move(buffers)...)); } /// Post a job to close the session. diff --git a/Util/BuildTools/BuildLibCarla.sh b/Util/BuildTools/BuildLibCarla.sh index bec6a9be5e..e315f110c4 100755 --- a/Util/BuildTools/BuildLibCarla.sh +++ b/Util/BuildTools/BuildLibCarla.sh @@ -190,6 +190,7 @@ function build_libcarla { -DLIBCARLA_BUILD_RELEASE=${M_RELEASE} \ -DCMAKE_TOOLCHAIN_FILE=${M_TOOLCHAIN} \ -DCMAKE_INSTALL_PREFIX=${M_INSTALL_FOLDER} \ + -DLIBCARLA_BUILD_TEST=ON \ -DCMAKE_EXPORT_COMPILE_COMMANDS=1 \ ${CMAKE_EXTRA_OPTIONS} \ ${CARLA_ROOT_FOLDER} diff --git a/Util/BuildTools/Check.sh b/Util/BuildTools/Check.sh index c45847ef63..87a58f6f92 100755 --- a/Util/BuildTools/Check.sh +++ b/Util/BuildTools/Check.sh @@ -29,7 +29,7 @@ END GDB= XML_OUTPUT=false -GTEST_ARGS=`sed -e 's/#.*$//g' ${CARLA_ROOT_FOLDER}/.gtest | sed -e '/^[[:space:]]*$/!s/^/--/g' | sed -e ':a;N;$!ba;s/\n/ /g'` +GTEST_ARGS= LIBCARLA_RELEASE=false LIBCARLA_DEBUG=false SMOKE_TESTS=false @@ -42,6 +42,10 @@ eval set -- "$OPTS" source $(dirname "$0")/Environment.sh +if [ -f "${CARLA_ROOT_FOLDER}/.gtest" ]; then + GTEST_ARGS=`sed -e 's/#.*$//g' ${CARLA_ROOT_FOLDER}/.gtest | sed -e '/^[[:space:]]*$/!s/^/--/g' | sed -e ':a;N;$!ba;s/\n/ /g'` +fi + PY_VERSION_LIST=3 while [[ $# -gt 0 ]]; do @@ -158,7 +162,7 @@ if ${LIBCARLA_RELEASE} ; then if ! { ${RUN_BENCHMARK} ; }; then log "Running LibCarla.client unit tests (release)." - echo "Running: ${GDB} libcarla_test_client_debug ${GTEST_ARGS} ${EXTRA_ARGS}" + echo "Running: ${GDB} libcarla_test_client_release ${GTEST_ARGS} ${EXTRA_ARGS}" ${GDB} ${LIBCARLA_INSTALL_CLIENT_FOLDER}/test/libcarla_test_client_release ${GTEST_ARGS} ${EXTRA_ARGS} fi diff --git a/Util/BuildTools/Setup.sh b/Util/BuildTools/Setup.sh index 94e5c5b487..b36e75b28b 100755 --- a/Util/BuildTools/Setup.sh +++ b/Util/BuildTools/Setup.sh @@ -521,7 +521,7 @@ else pushd ${XERCESC_SRC_DIR}/build >/dev/null cmake -G "Ninja" \ - -DCMAKE_CXX_FLAGS="-std=c++14 -stdlib=libc++ -fPIC -w" \ + -DCMAKE_CXX_FLAGS="-std=c++14 -fPIC -w" \ -DCMAKE_INSTALL_PREFIX="../../${XERCESC_INSTALL_DIR}" \ -DCMAKE_BUILD_TYPE=Release \ -DBUILD_SHARED_LIBS=OFF \ From f345cb849207e95a7562b85a0735a2e3a995170a Mon Sep 17 00:00:00 2001 From: xiao-ma-gua <17358869549@163.com> Date: Thu, 3 Sep 2026 15:43:50 +0800 Subject: [PATCH 061/128] =?UTF-8?q?=E8=A7=A3=E5=86=B3make=20setup=E6=97=B6?= =?UTF-8?q?ninja=20=E6=9E=84=E5=BB=BA=E5=A4=B1=E8=B4=A5=E7=9A=84=E9=97=AE?= =?UTF-8?q?=E9=A2=98?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- Util/BuildTools/Setup.sh | 4 +++- 1 file changed, 3 insertions(+), 1 deletion(-) diff --git a/Util/BuildTools/Setup.sh b/Util/BuildTools/Setup.sh index b36e75b28b..79de5147c0 100755 --- a/Util/BuildTools/Setup.sh +++ b/Util/BuildTools/Setup.sh @@ -522,6 +522,7 @@ else cmake -G "Ninja" \ -DCMAKE_CXX_FLAGS="-std=c++14 -fPIC -w" \ + -DCMAKE_EXE_LINKER_FLAGS="" \ -DCMAKE_INSTALL_PREFIX="../../${XERCESC_INSTALL_DIR}" \ -DCMAKE_BUILD_TYPE=Release \ -DBUILD_SHARED_LIBS=OFF \ @@ -539,8 +540,9 @@ else pushd ${XERCESC_SRC_DIR}/build >/dev/null cmake -G "Ninja" \ - -DCMAKE_CXX_FLAGS="-std=c++14 -stdlib=libc++ -fPIC -w -I${LLVM_INCLUDE} -L${LLVM_LIBPATH}" \ + -DCMAKE_CXX_FLAGS="-std=c++14 -stdlib=libc++ -fPIC -w" \ -DCMAKE_INSTALL_PREFIX="../../${XERCESC_INSTALL_SERVER_DIR}" \ + -DCMAKE_EXE_LINKER_FLAGS="-stdlib=libc++ -lc++ -lc++abi" \ -DCMAKE_BUILD_TYPE=Release \ -DBUILD_SHARED_LIBS=OFF \ -Dtranscoder=gnuiconv \ From 0c4a989431254b49122fbb7e99e75078981ded32 Mon Sep 17 00:00:00 2001 From: xiao-ma-gua <17358869549@163.com> Date: Fri, 4 Sep 2026 14:33:19 +0800 Subject: [PATCH 062/128] =?UTF-8?q?=E8=A7=A3=E5=86=B3use=20of=20undeclared?= =?UTF-8?q?=20identifier=20'AsView'=20=E5=92=8C=20no=20matching=20function?= =?UTF-8?q?=20for=20call=20to=20'MakeMessage'=E9=97=AE=E9=A2=98?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../Plugins/Carla/Source/Carla/Sensor/DReyeVRSensor.cpp | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/Unreal/CarlaUE4/Plugins/Carla/Source/Carla/Sensor/DReyeVRSensor.cpp b/Unreal/CarlaUE4/Plugins/Carla/Source/Carla/Sensor/DReyeVRSensor.cpp index 4124d6275b..7c61b76ea2 100644 --- a/Unreal/CarlaUE4/Plugins/Carla/Source/Carla/Sensor/DReyeVRSensor.cpp +++ b/Unreal/CarlaUE4/Plugins/Carla/Source/Carla/Sensor/DReyeVRSensor.cpp @@ -104,7 +104,7 @@ void ADReyeVRSensor::PostPhysTick(UWorld *W, ELevelTick TickType, float DeltaSec /// TODO: refactor this somehow /// to see where this is sent, see LibCarla/source/carla/sensor/s11n/DReyeVRSerializer.h - Stream.Send(*this, + Stream.SerializeAndSend(*this, carla::sensor::s11n::DReyeVRSerializer::Data{ Data->GetTimestampCarla(), // Timestamp of Carla (ms) Data->GetTimestampDevice(), // Timestamp of SRanipal (ms) From f682a4f11ddc657c2c18d7126d005fd401811a50 Mon Sep 17 00:00:00 2001 From: xiao-ma-gua <17358869549@163.com> Date: Fri, 4 Sep 2026 23:03:47 +0800 Subject: [PATCH 063/128] =?UTF-8?q?=E5=88=87=E6=8D=A2=E6=9C=8D=E5=8A=A1?= =?UTF-8?q?=E5=99=A8?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .github/workflows/build_linux.yml | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/.github/workflows/build_linux.yml b/.github/workflows/build_linux.yml index c21ad7e0d6..4a72c6ec6f 100644 --- a/.github/workflows/build_linux.yml +++ b/.github/workflows/build_linux.yml @@ -9,8 +9,8 @@ jobs: linux-dev: name: Linux CI/CD timeout-minutes: 1440 - # runs-on: [self-hosted, ubuntu-hutb] - runs-on: [self-hosted, dev] + runs-on: [self-hosted, ubuntu-hutb] + # runs-on: [self-hosted, dev] env: UE4_ROOT: /home/ubuntu/UnrealEngine_4.26 From 3c3f838cb2a1b082411a788b15922191a80276f8 Mon Sep 17 00:00:00 2001 From: xiao-ma-gua <17358869549@163.com> Date: Sun, 6 Sep 2026 13:21:49 +0800 Subject: [PATCH 064/128] =?UTF-8?q?=E9=99=90=E5=88=B6ninja=E5=B9=B6?= =?UTF-8?q?=E8=A1=8C=E4=BB=BB=E5=8A=A1=E6=95=B0=E9=87=8F=EF=BC=8C=E4=BB=A5?= =?UTF-8?q?=E9=81=BF=E5=85=8D=E8=B5=84=E6=BA=90=E6=BF=80=E5=A2=9E?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- Util/BuildTools/BuildLibCarla.sh | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/Util/BuildTools/BuildLibCarla.sh b/Util/BuildTools/BuildLibCarla.sh index e315f110c4..fb560e60ec 100755 --- a/Util/BuildTools/BuildLibCarla.sh +++ b/Util/BuildTools/BuildLibCarla.sh @@ -199,7 +199,8 @@ function build_libcarla { fi - ninja + # 限制并行任务数量,以避免资源激增. + ninja -j$(( $(nproc) / 3 )) ninja install | grep -v "Up-to-date:" From f6eff34c5ac8ebd5480f4523edb61996413ad09e Mon Sep 17 00:00:00 2001 From: xiao-ma-gua <17358869549@163.com> Date: Sun, 6 Sep 2026 17:46:41 +0800 Subject: [PATCH 065/128] =?UTF-8?q?=E4=BC=98=E5=85=88=E4=BD=BF=E7=94=A8?= =?UTF-8?q?=E7=8E=AF=E5=A2=83=E4=B8=AD=E7=9A=84Python=E7=B3=BB=E7=BB=9F?= =?UTF-8?q?=EF=BC=8C=E4=BB=A5=E4=BE=BF=E6=9E=84=E5=BB=BA=E7=9A=84wheel?= =?UTF-8?q?=E6=96=87=E4=BB=B6=E4=B8=8EBoost=E5=BA=93=E6=89=80=E5=8C=B9?= =?UTF-8?q?=E9=85=8D=E7=9A=84Python=E7=89=88=E6=9C=AC=E4=B8=80=E8=87=B4?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- Util/BuildTools/BuildPythonAPI.sh | 2 ++ Util/BuildTools/Check.sh | 3 +++ 2 files changed, 5 insertions(+) diff --git a/Util/BuildTools/BuildPythonAPI.sh b/Util/BuildTools/BuildPythonAPI.sh index f3b88c87cf..abbccccf5b 100755 --- a/Util/BuildTools/BuildPythonAPI.sh +++ b/Util/BuildTools/BuildPythonAPI.sh @@ -55,6 +55,8 @@ while [[ $# -gt 0 ]]; do esac done +# 优先使用环境中的Python系统,以便构建的wheel文件与Boost库所匹配的Python版本一致。 +export PATH=/usr/bin:/bin:$PATH export CC="$UE4_ROOT/Engine/Extras/ThirdPartyNotUE/SDKs/HostLinux/Linux_x64/v17_clang-10.0.1-centos7/x86_64-unknown-linux-gnu/bin/clang" export CXX="$UE4_ROOT/Engine/Extras/ThirdPartyNotUE/SDKs/HostLinux/Linux_x64/v17_clang-10.0.1-centos7/x86_64-unknown-linux-gnu/bin/clang++" diff --git a/Util/BuildTools/Check.sh b/Util/BuildTools/Check.sh index 87a58f6f92..78178089e8 100755 --- a/Util/BuildTools/Check.sh +++ b/Util/BuildTools/Check.sh @@ -96,6 +96,9 @@ while [[ $# -gt 0 ]]; do esac done +# 优先使用环境中的Python系统,以便构建的wheel文件与Boost库所匹配的Python版本一致。 +export PATH=/usr/bin:/bin:$PATH + if ! { ${LIBCARLA_RELEASE} || ${LIBCARLA_DEBUG} || ${PYTHON_API} || ${SMOKE_TESTS}; }; then fatal_error "Nothing selected to be done." fi From 1585f2272bb8aa52f8a441782973f8b327ceb5f3 Mon Sep 17 00:00:00 2001 From: xiao-ma-gua <17358869549@163.com> Date: Mon, 7 Sep 2026 17:14:45 +0800 Subject: [PATCH 066/128] =?UTF-8?q?=E8=A7=A3=E5=86=B3parse-options?= =?UTF-8?q?=EF=BC=9A=E6=9C=AA=E8=AF=86=E5=88=AB=E7=9A=84=E9=80=89=E9=A1=B9?= =?UTF-8?q?=20=E2=80=98--chrono=E2=80=99?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .github/workflows/build_linux.yml | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/.github/workflows/build_linux.yml b/.github/workflows/build_linux.yml index 4a72c6ec6f..96b36a61ca 100644 --- a/.github/workflows/build_linux.yml +++ b/.github/workflows/build_linux.yml @@ -151,6 +151,6 @@ jobs: - name: check run: | source ./setEnv64.sh - make check ARGS="--chrono" + make check shell: bash From b5a029d7b9ea5fd503a71934859c931518727d98 Mon Sep 17 00:00:00 2001 From: xiao-ma-gua <17358869549@163.com> Date: Mon, 7 Sep 2026 17:20:14 +0800 Subject: [PATCH 067/128] =?UTF-8?q?=E5=87=86=E5=A4=87=E5=A5=BDminiconda?= =?UTF-8?q?=E7=9A=84python=E8=99=9A=E6=8B=9F=E7=8E=AF=E5=A2=83?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- setup.sh | 21 ++++++++++++++++++++- 1 file changed, 20 insertions(+), 1 deletion(-) diff --git a/setup.sh b/setup.sh index 9c2f8f2d17..03ddac8741 100755 --- a/setup.sh +++ b/setup.sh @@ -93,7 +93,7 @@ PLUGINS_DIR="$PROJECT_ROOT/Unreal/CarlaUE4/Plugins" # | |-- CoACD.zip # | `-- libzmq-linux.tar.gz # |-- prerequisites/ # Pre-packaged toolchain -# | `-- Miniconda3-latest-Linux-x86_64.sh +# | `-- Miniconda3-py313_25.11.1-1-Linux-x86_64.sh # |-- src/ # C++ source packages (cross-platform) # | |-- boost-1_86_0.zip # | |-- chrono-src.zip @@ -249,6 +249,25 @@ else fi fi +# ============================================================================== +# -- Step 3b: Miniconda3 -------------------------------------------------------- +# ============================================================================== + +MINICONDA_DIR="$DEPENDENCIES_DIR/prerequisites/miniconda3" + +if [ ! -d "$MINICONDA_DIR" ]; then + log "Unzipping miniconda..." + MINICONDA_INSTALLER="$DEPENDENCIES_DIR/prerequisites/Miniconda3-py313_25.11.1-1-Linux-x86_64.sh" + if [ -f "$MINICONDA_INSTALLER" ]; then + bash "$MINICONDA_INSTALLER" -b -p "$MINICONDA_DIR" >/dev/null 2>&1 || \ + warn " miniconda3 extraction failed." + else + warn " miniconda3 archive not found: $MINICONDA_INSTALLER" + fi +else + log "miniconda3 folder already exists." +fi + log "" # ============================================================================== From 6de479e4de2168883f2c5c824d32d1c829cc4b5d Mon Sep 17 00:00:00 2001 From: xiao-ma-gua <17358869549@163.com> Date: Tue, 8 Sep 2026 13:09:04 +0800 Subject: [PATCH 068/128] =?UTF-8?q?=E4=B8=8Edependencies=5Fu/init.sh?= =?UTF-8?q?=E9=87=8D=E5=A4=8D=E5=AE=9E=E7=8E=B0?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- setup.sh | 19 ------------------- 1 file changed, 19 deletions(-) diff --git a/setup.sh b/setup.sh index 03ddac8741..c4f5ee0eaa 100755 --- a/setup.sh +++ b/setup.sh @@ -249,25 +249,6 @@ else fi fi -# ============================================================================== -# -- Step 3b: Miniconda3 -------------------------------------------------------- -# ============================================================================== - -MINICONDA_DIR="$DEPENDENCIES_DIR/prerequisites/miniconda3" - -if [ ! -d "$MINICONDA_DIR" ]; then - log "Unzipping miniconda..." - MINICONDA_INSTALLER="$DEPENDENCIES_DIR/prerequisites/Miniconda3-py313_25.11.1-1-Linux-x86_64.sh" - if [ -f "$MINICONDA_INSTALLER" ]; then - bash "$MINICONDA_INSTALLER" -b -p "$MINICONDA_DIR" >/dev/null 2>&1 || \ - warn " miniconda3 extraction failed." - else - warn " miniconda3 archive not found: $MINICONDA_INSTALLER" - fi -else - log "miniconda3 folder already exists." -fi - log "" # ============================================================================== From 2dc49ee7975a453560fccbde898fe64137257b00 Mon Sep 17 00:00:00 2001 From: xiao-ma-gua <17358869549@163.com> Date: Tue, 8 Sep 2026 13:48:15 +0800 Subject: [PATCH 069/128] =?UTF-8?q?=E6=9B=B4=E6=94=B9=E6=9C=8D=E5=8A=A1?= =?UTF-8?q?=E5=99=A8?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .github/workflows/build_linux.yml | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/.github/workflows/build_linux.yml b/.github/workflows/build_linux.yml index 96b36a61ca..4bf268e07e 100644 --- a/.github/workflows/build_linux.yml +++ b/.github/workflows/build_linux.yml @@ -9,8 +9,8 @@ jobs: linux-dev: name: Linux CI/CD timeout-minutes: 1440 - runs-on: [self-hosted, ubuntu-hutb] - # runs-on: [self-hosted, dev] + # runs-on: [self-hosted, ubuntu-hutb] + runs-on: [self-hosted, dev] env: UE4_ROOT: /home/ubuntu/UnrealEngine_4.26 From 40fcd4794f701535198bda5f93fef8a5acf40f3b Mon Sep 17 00:00:00 2001 From: xiao-ma-gua <17358869549@163.com> Date: Tue, 8 Sep 2026 14:00:56 +0800 Subject: [PATCH 070/128] =?UTF-8?q?=E4=BD=BF=E7=94=A8=E8=87=AA=E5=B8=A6?= =?UTF-8?q?=E8=80=8C=E9=9D=9E=E7=B3=BB=E7=BB=9Fminiconda?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- setup.sh | 20 ++++++++++++++++++++ 1 file changed, 20 insertions(+) diff --git a/setup.sh b/setup.sh index c4f5ee0eaa..b5255ddf0c 100755 --- a/setup.sh +++ b/setup.sh @@ -251,6 +251,26 @@ fi log "" +# ============================================================================== +# -- Step 3b: Miniconda3 -------------------------------------------------------- +# ============================================================================== +MINICONDA_DIR="$DEPENDENCIES_DIR/prerequisites/miniconda3" + +if [ ! -d "$MINICONDA_DIR" ]; then + log "Unzipping miniconda..." + MINICONDA_INSTALLER="$DEPENDENCIES_DIR/prerequisites/Miniconda3-py313_25.11.1-1-Linux-x86_64.sh" + if [ -f "$MINICONDA_INSTALLER" ]; then + bash "$MINICONDA_INSTALLER" -b -p "$MINICONDA_DIR" >/dev/null 2>&1 || \ + warn " miniconda3 extraction failed." + else + warn " miniconda3 archive not found: $MINICONDA_INSTALLER" + fi +else + log "miniconda3 folder already exists." +fi + +log "" + # ============================================================================== # -- Step 4: System packages via apt ------------------------------------------- # ============================================================================== From 24c0ffda54c8871ff495a26ea5c515e00a2e2c75 Mon Sep 17 00:00:00 2001 From: xiao-ma-gua <17358869549@163.com> Date: Tue, 8 Sep 2026 18:56:26 +0800 Subject: [PATCH 071/128] =?UTF-8?q?=E5=87=86=E5=A4=87=E5=A5=BDminiconda?= =?UTF-8?q?=E7=9A=84python=E8=99=9A=E6=8B=9F=E7=8E=AF=E5=A2=83=EF=BC=8C?= =?UTF-8?q?=E5=B9=B6=E6=9E=84=E5=BB=BAboost=E6=97=B6=E7=94=A8=E7=9A=84pyth?= =?UTF-8?q?on=E5=92=8C=E6=9E=84=E5=BB=BAwheel=E6=97=B6=E7=94=A8=E7=9A=84?= =?UTF-8?q?=E4=B8=80=E8=87=B4?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .github/workflows/build_linux.yml | 4 +- Util/BuildTools/BuildPythonAPI.sh | 69 ++++++++++++++++++++++++++----- Util/BuildTools/Environment.sh | 27 ++++++++++++ 3 files changed, 88 insertions(+), 12 deletions(-) diff --git a/.github/workflows/build_linux.yml b/.github/workflows/build_linux.yml index 4bf268e07e..96b36a61ca 100644 --- a/.github/workflows/build_linux.yml +++ b/.github/workflows/build_linux.yml @@ -9,8 +9,8 @@ jobs: linux-dev: name: Linux CI/CD timeout-minutes: 1440 - # runs-on: [self-hosted, ubuntu-hutb] - runs-on: [self-hosted, dev] + runs-on: [self-hosted, ubuntu-hutb] + # runs-on: [self-hosted, dev] env: UE4_ROOT: /home/ubuntu/UnrealEngine_4.26 diff --git a/Util/BuildTools/BuildPythonAPI.sh b/Util/BuildTools/BuildPythonAPI.sh index abbccccf5b..f6ea8b1a0e 100755 --- a/Util/BuildTools/BuildPythonAPI.sh +++ b/Util/BuildTools/BuildPythonAPI.sh @@ -17,7 +17,8 @@ OPTS=`getopt -o h --long help,config:,rebuild,clean,rss,carsim,python-version:,b eval set -- "$OPTS" -PY_VERSION_LIST=3 +# 与 Windows BuildPythonAPI.bat 对齐:默认构建 hutb_3.14 ~ hutb_3.7 全部版本 +PY_VERSION_LIST="3.14,3.13,3.12,3.11,3.10,3.9,3.8,3.7" TARGET_WHEEL_PLATFORM= while [[ $# -gt 0 ]]; do @@ -55,8 +56,6 @@ while [[ $# -gt 0 ]]; do esac done -# 优先使用环境中的Python系统,以便构建的wheel文件与Boost库所匹配的Python版本一致。 -export PATH=/usr/bin:/bin:$PATH export CC="$UE4_ROOT/Engine/Extras/ThirdPartyNotUE/SDKs/HostLinux/Linux_x64/v17_clang-10.0.1-centos7/x86_64-unknown-linux-gnu/bin/clang" export CXX="$UE4_ROOT/Engine/Extras/ThirdPartyNotUE/SDKs/HostLinux/Linux_x64/v17_clang-10.0.1-centos7/x86_64-unknown-linux-gnu/bin/clang++" @@ -100,28 +99,78 @@ fi if ${BUILD_PYTHONAPI} ; then # Add patchelf to the path. Auditwheel relies on patchelf to repair ELF files. export PATH="${LIBCARLA_INSTALL_CLIENT_FOLDER}/bin:${PATH}" - + + #检查 conda 是否已安装(目前仅提示) + if command -v conda >/dev/null 2>&1 || [[ -x "${MINICONDA_DIR}/bin/conda" ]] ; then + echo "Conda is already installed." + else + echo "TODO: Installing miniconda with silent mode" + fi + + # boost 编译产物位于 ${CARLA_BUILD_FOLDER} 下,路径与 Setup.sh 中的 + # BOOST_VERSION / CXX_TAG 保持一致。 + BOOST_VERSION=1.90.0 + CXX_TAG=c10 + BOOST_BASENAME="boost-${BOOST_VERSION}-${CXX_TAG}" + BOOST_INSTALL_FOLDER="${CARLA_BUILD_FOLDER}/${BOOST_BASENAME}-install" + BOOST_SOURCE_FOLDER="${CARLA_BUILD_FOLDER}/${BOOST_BASENAME}-source" + for PY_VERSION in ${PY_VERSION_LIST[@]} ; do log "Building Python API wheel for Python ${PY_VERSION}." - + + # 使用项目中的 conda 环境 Python(hutb_3.X)而非系统 Python。 + CONDA_PY=$(get_conda_env_python ${PY_VERSION}) + export PATH="$(dirname ${CONDA_PY}):${PATH}" + + # 确保轮子工具在环境中可用(幂等) + # -m pip = 用"这个解释器"运行它自己环境里的 pip 模块。 + ${CONDA_PY} -m pip install --quiet setuptools wheel build 2>/dev/null || true + + echo "Current Python path:" + command -v python + + # 删除上一个 Python 版本编译的 boost + # 迫使 setup 为本 Python 版本重新编译 boost(libboost_python 与 Python 版本绑定) + echo "BOOST_VERSION: ${BOOST_VERSION}" + echo "BOOST_INSTALL_FOLDER: ${BOOST_INSTALL_FOLDER}" + if [[ -d "${BOOST_INSTALL_FOLDER}" ]] ; then + echo "Delete all boost files: ${BOOST_INSTALL_FOLDER}" + rm -Rf "${BOOST_INSTALL_FOLDER}" + echo "Delete boost source code: ${BOOST_SOURCE_FOLDER}" + rm -Rf "${BOOST_SOURCE_FOLDER}" + fi + + # 先编译 LibCarla 和 osm2odr + if ${BUILD_RSS_VARIANT} ; then + # -C 是 make 的"先切换目录再执行"选项 + make -C ${CARLA_ROOT_FOLDER} LibCarla.client.rss ARGS="--python-version=${PY_VERSION}" + else + make -C ${CARLA_ROOT_FOLDER} LibCarla ARGS="--python-version=${PY_VERSION}" + fi + make -C ${CARLA_ROOT_FOLDER} osm2odr + # Building the RSS variant adds files to SOURCES.txt we do not want included in a normal build rm -Rf source/carla.egg-info - - /usr/bin/env python${PY_VERSION} -m build --wheel --outdir dist/.tmp . + ${CONDA_PY} -m build --wheel --outdir dist/.tmp . if ${INSTALL_PYTHONAPI} ; then log "Installing Python API for Python ${PY_VERSION}." - /usr/bin/env python${PY_VERSION} -m pip install --force-reinstall dist/.tmp/$(ls dist/.tmp | grep .whl) + ${CONDA_PY} -m pip install --force-reinstall dist/.tmp/$(ls dist/.tmp | grep .whl) fi if [[ -z ${TARGET_WHEEL_PLATFORM} ]] ; then cp dist/.tmp/$(ls dist/.tmp | grep .whl) dist else log "Tagging Python API wheel to ${TARGET_WHEEL_PLATFORM} for Python ${PY_VERSION}." - /usr/bin/env python${PY_VERSION} -m wheel tags --platform-tag ${TARGET_WHEEL_PLATFORM} dist/.tmp/$(ls dist/.tmp | grep .whl) - /usr/bin/env python${PY_VERSION} -m auditwheel repair --plat ${TARGET_WHEEL_PLATFORM} --wheel-dir dist dist/.tmp/$(ls dist/.tmp | grep ${TARGET_WHEEL_PLATFORM}.whl) + ${CONDA_PY} -m wheel tags --platform-tag ${TARGET_WHEEL_PLATFORM} dist/.tmp/$(ls dist/.tmp | grep .whl) + ${CONDA_PY} -m auditwheel repair --plat ${TARGET_WHEEL_PLATFORM} --wheel-dir dist dist/.tmp/$(ls dist/.tmp | grep ${TARGET_WHEEL_PLATFORM}.whl) fi rm -rf dist/.tmp + + # 构建失败(dist 目录不存在)时报错 + if [[ ! -d dist ]] ; then + fatal_error "An error occurred while building the wheel file." + fi done fi diff --git a/Util/BuildTools/Environment.sh b/Util/BuildTools/Environment.sh index fa9cf05f48..5e96f75bc9 100644 --- a/Util/BuildTools/Environment.sh +++ b/Util/BuildTools/Environment.sh @@ -55,3 +55,30 @@ function move_if_changed { } CARLA_BUILD_CONCURRENCY=`nproc --all` + +# ============================================================================== +# -- Conda env python------------ +# ============================================================================== +# 在 Linux 上,使用项目中 conda 环境的 Python 解释器 + +MINICONDA_DIR="${CARLA_BUILD_FOLDER}/dependencies/prerequisites/miniconda3" + +function get_conda_env_python { + local PY_VERSION="$1" + local ENV_MINOR + + if [[ "${PY_VERSION}" == "3" ]]; then + # 默认是3.8 + ENV_MINOR="8" + else + ENV_MINOR="${PY_VERSION#3.}" + fi + + local ENV_PY="${MINICONDA_DIR}/envs/hutb_3.${ENV_MINOR}/bin/python" + if [[ ! -x "${ENV_PY}" ]]; then + fatal_error "conda env 'hutb_3.${ENV_MINOR}' not found (${ENV_PY}). + Run ./setup.sh first, or create it manually with: + ${MINICONDA_DIR}/bin/conda create -n hutb_3.${ENV_MINOR} python=3.${ENV_MINOR} --yes" + fi + echo "${ENV_PY}" +} From 21c89c104b59590c8d1390088a2f65f70bae3fa1 Mon Sep 17 00:00:00 2001 From: xiao-ma-gua <17358869549@163.com> Date: Wed, 9 Sep 2026 01:25:40 +0800 Subject: [PATCH 072/128] =?UTF-8?q?=E5=88=A0=E9=99=A4=E4=BB=85=E5=88=9B?= =?UTF-8?q?=E5=BB=BApython3.8=E7=8E=AF=E5=A2=83=E7=9A=84=E5=86=85=E5=AE=B9?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- setup.sh | 51 --------------------------------------------------- 1 file changed, 51 deletions(-) diff --git a/setup.sh b/setup.sh index b5255ddf0c..cd4ec3c67f 100755 --- a/setup.sh +++ b/setup.sh @@ -321,57 +321,6 @@ fi log "" -# ============================================================================== -# -- Step 5: Python environment ------------------------------------------------ -# ============================================================================== -# init.sh already installed miniconda3 to Build/dependencies/prerequisites/. -# Here we create the conda environment and install Python requirements. - -log "==============================================" -log " Step 5 — Python Environment" -log "==============================================" - -MINICONDA_DIR="$BUILD_DIR/dependencies/prerequisites/miniconda3" - -if [ -d "$MINICONDA_DIR" ] && [ -f "$MINICONDA_DIR/bin/conda" ]; then - log "Setting up conda environment 'hutb_3.8'..." - - # Create environment if not already present - if ! "$MINICONDA_DIR/bin/conda" env list 2>/dev/null | grep -q hutb_3.8; then - "$MINICONDA_DIR/bin/conda" create -n hutb_3.8 python=3.8 -y 2>/dev/null || \ - warn " Conda env creation failed." - fi - - # Install Python requirements - if [ -f "$PROJECT_ROOT/requirements.txt" ]; then - log " Installing Python requirements (requirements.txt)..." - "$MINICONDA_DIR/envs/hutb_3.8/bin/pip" install -r "$PROJECT_ROOT/requirements.txt" 2>/dev/null || \ - warn " pip install failed (non-critical)." - fi - - if [ -f "$PROJECT_ROOT/PythonAPI/carla/requirements.txt" ]; then - log " Installing Python requirements (PythonAPI)..." - "$MINICONDA_DIR/envs/hutb_3.8/bin/pip" install -r "$PROJECT_ROOT/PythonAPI/carla/requirements.txt" 2>/dev/null || true - fi - - success "conda env 'hutb_3.8' ready." -else - # Fallback: system python3 venv - VENV_DIR="$BUILD_DIR/venv" - if [ ! -d "$VENV_DIR" ] && command -v python3 &>/dev/null; then - log "No conda found — creating Python venv as fallback..." - python3 -m venv "$VENV_DIR" 2>/dev/null || true - if [ -f "$VENV_DIR/bin/pip" ]; then - "$VENV_DIR/bin/pip" install --upgrade pip 2>/dev/null || true - [ -f "$PROJECT_ROOT/requirements.txt" ] && \ - "$VENV_DIR/bin/pip" install -r "$PROJECT_ROOT/requirements.txt" 2>/dev/null || true - fi - success "Python venv created at $VENV_DIR" - fi -fi - -log "" - # ============================================================================== # -- Step 6: Build C++ dependencies -------------------------------------------- # ============================================================================== From 064efc3b15887d0fda118dab09b6fd87a585ac40 Mon Sep 17 00:00:00 2001 From: xiao-ma-gua <17358869549@163.com> Date: Wed, 9 Sep 2026 01:26:16 +0800 Subject: [PATCH 073/128] =?UTF-8?q?=E5=87=86=E5=A4=87=20conda=20=E7=8E=AF?= =?UTF-8?q?=E5=A2=83=20hutb=5F3.14=20~=20hutb=5F3.7?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- Util/BuildTools/BuildPythonAPI.sh | 26 ++++++++++++++++++++++++++ 1 file changed, 26 insertions(+) diff --git a/Util/BuildTools/BuildPythonAPI.sh b/Util/BuildTools/BuildPythonAPI.sh index f6ea8b1a0e..bc19120865 100755 --- a/Util/BuildTools/BuildPythonAPI.sh +++ b/Util/BuildTools/BuildPythonAPI.sh @@ -107,6 +107,32 @@ if ${BUILD_PYTHONAPI} ; then echo "TODO: Installing miniconda with silent mode" fi + # 构建 wheel 前为每个 Python 版本 + # 准备 conda 环境 hutb_3.14 ~ hutb_3.7(先删旧环境、清理残留目录, + # 接受 anaconda 官方源 ToS,再重建)。 + for PY_VERSION in ${PY_VERSION_LIST[@]} ; do + ENV_NAME="hutb_${PY_VERSION}" + + echo "If conda virtual environment ${ENV_NAME} already exists, delete it" + "${MINICONDA_DIR}/bin/conda" remove -n "${ENV_NAME}" --all --yes 2>/dev/null || true + + # 清理 conda remove 后可能残留的环境目录,避免重建时报 Permission denied + ENV_DIR="$("${MINICONDA_DIR}/bin/conda" env list 2>/dev/null | awk -v n="${ENV_NAME}" '$1==n {print $2}')" + if [[ -n "${ENV_DIR}" && -d "${ENV_DIR}" ]] ; then + echo "Removing existing conda environment: ${ENV_DIR}" + rm -rf "${ENV_DIR}" + fi + + # fix: CondaToSNonInteractiveError: Terms of Service have not been accepted for the following channels. + # offline resource: https://repo.anaconda.com/pkgs/main/linux-64/ + "${MINICONDA_DIR}/bin/conda" tos accept --override-channels --channel https://repo.anaconda.com/pkgs/main 2>/dev/null || true + "${MINICONDA_DIR}/bin/conda" tos accept --override-channels --channel https://repo.anaconda.com/pkgs/r 2>/dev/null || true + + echo "Creating new conda environment ${ENV_NAME} ..." + "${MINICONDA_DIR}/bin/conda" create -n "${ENV_NAME}" python="${PY_VERSION}" --yes || \ + fatal_error "Failed to create conda env '${ENV_NAME}' (python=${PY_VERSION})." + done + # boost 编译产物位于 ${CARLA_BUILD_FOLDER} 下,路径与 Setup.sh 中的 # BOOST_VERSION / CXX_TAG 保持一致。 BOOST_VERSION=1.90.0 From c8e43847b5eb00f33c755232fd80306c21b2dcc9 Mon Sep 17 00:00:00 2001 From: xiao-ma-gua <17358869549@163.com> Date: Wed, 9 Sep 2026 11:18:53 +0800 Subject: [PATCH 074/128] =?UTF-8?q?=E5=9C=A8make=20setup=E4=B8=AD=E5=8A=A0?= =?UTF-8?q?=E5=85=A5=E6=89=A7=E8=A1=8C./setup.sh=E6=8C=87=E4=BB=A4?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .github/workflows/build_linux.yml | 1 + 1 file changed, 1 insertion(+) diff --git a/.github/workflows/build_linux.yml b/.github/workflows/build_linux.yml index 96b36a61ca..58d794b409 100644 --- a/.github/workflows/build_linux.yml +++ b/.github/workflows/build_linux.yml @@ -76,6 +76,7 @@ jobs: # Clean leftover source dirs from previous runs (avoid conflict with make setup) rm -rf Build/*-source Build/*-src 2>/dev/null || true + ./setup.sh make setup ARGS="--chrono" shell: bash From 5564d3d91b8f4c986b4cf9a30dad7d4ea44e6335 Mon Sep 17 00:00:00 2001 From: xiao-ma-gua <17358869549@163.com> Date: Wed, 9 Sep 2026 21:23:17 +0800 Subject: [PATCH 075/128] =?UTF-8?q?setup.sh=E4=B8=8Esetup.bat=E5=90=8C?= =?UTF-8?q?=E6=AD=A5=EF=BC=8C=E5=AE=9E=E7=8E=B0=E5=AF=B9=E5=90=84=E7=A7=8D?= =?UTF-8?q?=E4=BE=9D=E8=B5=96=E5=92=8C=E6=8F=92=E4=BB=B6=E7=9A=84=E8=A7=A3?= =?UTF-8?q?=E5=8E=8B?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .github/workflows/build_linux.yml | 48 +---- setup.sh | 314 +++++++++++++++++++++++------- 2 files changed, 246 insertions(+), 116 deletions(-) diff --git a/.github/workflows/build_linux.yml b/.github/workflows/build_linux.yml index 58d794b409..83a16900ec 100644 --- a/.github/workflows/build_linux.yml +++ b/.github/workflows/build_linux.yml @@ -31,52 +31,13 @@ jobs: source ./setEnv64.sh git update-index --skip-worktree Unreal/CarlaUE4/CarlaUE4.uproject - # Clone dependencies_u (only for UE4 plugins, not source pkgs) - echo "=== Cloning dependencies_u for plugins ===" - GIT_LFS_SKIP_SMUDGE=1 git clone \ - "https://OpenHUTB:T8w6TYB_r71gGTP3A02B@git.code.tencent.com/OpenHUTB/dependencies_u.git" \ - Build/dependencies 2>/dev/null || true - if [ -d Build/dependencies ]; then - cd Build/dependencies && git lfs pull && cd ../.. - # Extract plugins - 7z x Build/dependencies/Plugins/RoadRunner_Plugins.zip -o"Unreal/CarlaUE4/Plugins/" -y - 7z x Build/dependencies/Plugins/CesiumForUnreal-426-v1.18.0-ue4.zip -o"Unreal/CarlaUE4/Plugins/" -y - - # UnrealRoboticsLab third-party deps - mkdir -p Unreal/CarlaUE4/Plugins/UnrealRoboticsLab/third_party/install - mkdir -p Unreal/CarlaUE4/Plugins/UnrealRoboticsLab/third_party/install/MuJoCo - tar -xzf Build/dependencies/Plugins/mujoco-3.7.0-linux-x86_64.tar.gz -C Unreal/CarlaUE4/Plugins/UnrealRoboticsLab/third_party/install/MuJoCo --strip-components=1 2>/dev/null || true - - # Replace symlinks with real files for staging - MUJOCO_LIB=Unreal/CarlaUE4/Plugins/UnrealRoboticsLab/third_party/install/MuJoCo/lib - if [ -L "$MUJOCO_LIB/libmujoco.so" ]; then - REAL=$(readlink -f "$MUJOCO_LIB/libmujoco.so") - rm "$MUJOCO_LIB/libmujoco.so" && cp "$REAL" "$MUJOCO_LIB/libmujoco.so" - fi - mkdir -p Unreal/CarlaUE4/Plugins/UnrealRoboticsLab/third_party/install/CoACD - unzip -qo Build/dependencies/Plugins/CoACD.zip -d Unreal/CarlaUE4/Plugins/UnrealRoboticsLab/third_party/install/CoACD 2>/dev/null || true - - # Fix CoACD directory nesting: move CoACD/CoACD/include -> CoACD/include - if [ -d Unreal/CarlaUE4/Plugins/UnrealRoboticsLab/third_party/install/CoACD/CoACD/include ] && [ ! -d Unreal/CarlaUE4/Plugins/UnrealRoboticsLab/third_party/install/CoACD/include ]; then - mv Unreal/CarlaUE4/Plugins/UnrealRoboticsLab/third_party/install/CoACD/CoACD/include Unreal/CarlaUE4/Plugins/UnrealRoboticsLab/third_party/install/CoACD/include - fi - mkdir -p Unreal/CarlaUE4/Plugins/UnrealRoboticsLab/third_party/install/libzmq - unzip -qo Build/dependencies/Plugins/libzmq-linux.zip -d Unreal/CarlaUE4/Plugins/UnrealRoboticsLab/third_party/install/ - - # Resolve symlinks for libzmq by copying the real .so in place - ZMQ_LIB=Unreal/CarlaUE4/Plugins/UnrealRoboticsLab/third_party/install/libzmq/lib - if [ -L "$ZMQ_LIB/libzmq.so" ]; then - REAL=$(readlink -f "$ZMQ_LIB/libzmq.so") - rm "$ZMQ_LIB/libzmq.so" "$ZMQ_LIB/libzmq.so.5" 2>/dev/null || true - cp "$REAL" "$ZMQ_LIB/" && mv "$ZMQ_LIB/$(basename "$REAL")" "$ZMQ_LIB/libzmq.so" - fi - echo "=== Plugins extracted ===" - fi - # Clean leftover source dirs from previous runs (avoid conflict with make setup) rm -rf Build/*-source Build/*-src 2>/dev/null || true + # setup.sh 负责 clone dependencies_u、解压 UE4 插件与 + # UnrealRoboticsLab 三方依赖、安装 miniconda3、apt 依赖并编译 C++ 依赖 ./setup.sh + make setup ARGS="--chrono" shell: bash @@ -138,9 +99,6 @@ jobs: - name: package run: | - source /home/ubuntu/miniconda3/etc/profile.d/conda.sh - conda activate carla - source ./setEnv64.sh export MaxProcessorCount=$(( $(nproc) / 3 )) diff --git a/setup.sh b/setup.sh index cd4ec3c67f..0914971fdb 100755 --- a/setup.sh +++ b/setup.sh @@ -7,18 +7,30 @@ # # git@git.code.tencent.com:OpenHUTB/dependencies_u.git # -# The repo contains Linux-compatible builds of every package AND an init.sh -# script that handles extraction. This setup.sh only orchestrates: +# The repo contains Linux-compatible builds of every package (plugins, +# installers and C++ source packages). This setup.sh only orchestrates: # # 1. Ensure system tools (git, curl, 7z, gcc, etc.) # 2. Clone dependencies_u → Build/dependencies/ -# 3. Run init.sh → extract everything to the right places -# 4. Install system packages via apt -# 5. Set up Python environment (conda or venv) +# 3. Extract UE4 plugins + UnrealRoboticsLab third-party deps +# 4. Install miniconda3 from the dependencies_u installer +# 5. Install system packages via apt # 6. Optionally invoke Util/BuildTools/Setup.sh for the full C++ build # +# Note: the conda envs hutb_3.14 ~ hutb_3.7 are created on demand by +# Util/BuildTools/BuildPythonAPI.sh before building the Python wheels. +# +# setup.bat 的 Windows 专属步骤有意不移植(Linux 替代方案见相应注释): +# 便携 git 下载 / DirectX / vcvars64 / CMake、dotnet、GnuWin32 压缩包 +# → 由 apt 提供(Step 0 / Step 5) +# src/*.zip 源码包预解压到 Build/ +# → Linux 的 Util/BuildTools/Setup.sh 自行下载源码并编译后清理,预解压会被删除 +# # Usage: -# ./setup.sh [--skip-prerequisites] [--download-only] [--help] +# ./setup.sh [-h] [-s|--skip-prerequisites] [-i|--interactive] +# [-g|--generate-project-files] [-l|--launch] +# [-d|--direct-launch] [-p|--package] +# [--python-root=PATH] [--download-only] # ============================================================================== set -e @@ -30,19 +42,46 @@ set -e DOC_STRING="Download and install all dependencies and UE4 plugins for Ubuntu. All packages come from the dependencies_u repository (Linux builds)." -USAGE_STRING="Usage: $0 [--skip-prerequisites] [--download-only] [--help]" +USAGE_STRING="Usage: $0 [-h] [-s|--skip-prerequisites] [-i|--interactive] + [-g|--generate-project-files] [-l|--launch] [-d|--direct-launch] + [-p|--package] [--python-root=PATH] [--download-only]" SKIP_PREREQUISITES=false DOWNLOAD_ONLY=false - -OPTS=$(getopt -o h --long help,skip-prerequisites,download-only -n 'parse-options' -- "$@") +LAUNCH=false +DIRECT_LAUNCH=false +GENERATE_PROJECT_FILES=false +PACKAGE=false +INTERACTIVE=false +PYTHON_ROOT= + +OPTS=$(getopt -o hisgldp --long help,interactive,skip-prerequisites,generate-project-files,launch,direct-launch,package,python-root:,pyroot:,download-only -n 'parse-options' -- "$@") eval set -- "$OPTS" while [[ $# -gt 0 ]]; do case "$1" in - --skip-prerequisites ) + -s | --skip-prerequisites ) SKIP_PREREQUISITES=true shift ;; + -i | --interactive ) + # setup.bat 中仅设置未消费,Linux 同样只接受不处理 + INTERACTIVE=true + shift ;; + -g | --generate-project-files ) + GENERATE_PROJECT_FILES=true + shift ;; + -l | --launch ) + LAUNCH=true + shift ;; + -d | --direct-launch ) + DIRECT_LAUNCH=true + shift ;; + -p | --package ) + PACKAGE=true + shift ;; + --python-root | --pyroot ) + PYTHON_ROOT="$2" + shift 2 ;; --download-only ) DOWNLOAD_ONLY=true shift ;; @@ -56,6 +95,9 @@ while [[ $# -gt 0 ]]; do esac done +# setup.bat 用 --python-root 给 install_prerequisites.bat 指定 Python 路径; +# Linux 的系统依赖走 apt(Step 5),该参数仅保留以兼容命令行,不实际使用。 + # ============================================================================== # -- Color helpers -------------------------------------------------------------- # ============================================================================== @@ -89,9 +131,10 @@ PLUGINS_DIR="$PROJECT_ROOT/Unreal/CarlaUE4/Plugins" # |-- Plugins/ # UE4 plugins + third-party runtime libs # | |-- RoadRunner_Plugins.zip # | |-- CesiumForUnreal-426-v1.18.0-ue4.zip -# | |-- mujoco-3.3.5-linux-x86_64.tar.gz +# | |-- mujoco-3.7.0-linux-x86_64.tar.gz # | |-- CoACD.zip -# | `-- libzmq-linux.tar.gz +# | |-- glTFForUE4.zip +# | `-- libzmq-linux.zip # |-- prerequisites/ # Pre-packaged toolchain # | `-- Miniconda3-py313_25.11.1-1-Linux-x86_64.sh # |-- src/ # C++ source packages (cross-platform) @@ -100,7 +143,6 @@ PLUGINS_DIR="$PROJECT_ROOT/Unreal/CarlaUE4/Plugins" # | |-- eigen-3.3.7.zip # | |-- ... (13 packages total) # | `-- zlib-source.zip -# |-- init.sh # Extraction / initialization script # |-- .gitattributes # LFS tracking rules # `-- README.md # ------------------------------------------------------------------------------ @@ -114,6 +156,51 @@ DEPENDENCIES_DIR="$BUILD_DIR/dependencies" URLAB_DIR="$PLUGINS_DIR/UnrealRoboticsLab" URLAB_THIRD_PARTY="$URLAB_DIR/third_party/install" +# ------------------------------------------------------------------------------ +# Mirrors setup.bat: prepend prerequisite tool dirs to PATH. +# Windows 的 CMake/dotnet/GnuWin32 在 Linux 上由 apt 提供(Step 0/Step 5), +# 这里只前置 miniconda 相关目录(目录存在才加,避免 PATH 里出现死路径)。 +# ------------------------------------------------------------------------------ +for p in \ + "Build/dependencies/prerequisites/miniconda3/bin" \ + "Build/dependencies/prerequisites/miniconda3/envs/hutb_3.8/bin" ; do + if [ -d "$PROJECT_ROOT/$p" ]; then + export PATH="$PROJECT_ROOT/$p:$PATH" + log "Prepended to PATH: $PROJECT_ROOT/$p" + fi +done + +# ============================================================================== +# -- Mirrors setup.bat :main: -g 生成工程文件 / -d 直接启动编辑器 ---------------- +# ============================================================================== + +if [ "$GENERATE_PROJECT_FILES" = true ]; then + log "Generating project files..." + if [ -z "${UE4_ROOT:-}" ]; then + error "UE4_ROOT is not set — run 'source ./setEnv64.sh' first." + fi + # + "$UE4_ROOT/GenerateProjectFiles.sh" \ + -project="$PROJECT_ROOT/Unreal/CarlaUE4/CarlaUE4.uproject" -game -engine -progress || \ + error "GenerateProjectFiles.sh failed." +fi + +# 跳过所有安装步骤,直接启动编辑器 +if [ "$DIRECT_LAUNCH" = true ]; then + log "Directly launching Unreal Editor, log to launch.log..." + if [ -z "${UE4_ROOT:-}" ]; then + error "UE4_ROOT is not set — run 'source ./setEnv64.sh' first." + fi + UE4_EDITOR="$UE4_ROOT/Engine/Binaries/Linux/UE4Editor" + UPROJECT="$PROJECT_ROOT/Unreal/CarlaUE4/CarlaUE4.uproject" + if [ ! -f "$UE4_EDITOR" ]; then + error "UE4Editor not found at $UE4_EDITOR, please check if the build step is finished and the file exists." + fi + log "Found UE4Editor at $UE4_EDITOR, launching..." + nohup "$UE4_EDITOR" "$UPROJECT" >launch.log 2>&1 & + exit 0 +fi + # ============================================================================== # -- Banner -------------------------------------------------------------------- # ============================================================================== @@ -147,14 +234,22 @@ done if [ ${#MISSING_TOOLS[@]} -gt 0 ]; then warn "Installing missing tools: ${MISSING_TOOLS[*]}" - sudo apt-get update -qq - sudo apt-get install -y -qq "${MISSING_TOOLS[@]}" + if sudo -n true 2>/dev/null; then + sudo apt-get update -qq + sudo apt-get install -y -qq "${MISSING_TOOLS[@]}" + else + warn " sudo 不可用(需要密码)— 请手动执行: sudo apt-get install ${MISSING_TOOLS[*]}" + fi fi # 7z — used extensively in .bat for extracting archives if ! command -v 7z &>/dev/null; then log "Installing p7zip-full..." - sudo apt-get install -y -qq p7zip-full + if sudo -n true 2>/dev/null; then + sudo apt-get install -y -qq p7zip-full + else + warn " sudo 不可用(需要密码)— 请手动执行: sudo apt-get install p7zip-full" + fi fi success "Essential tools ready." @@ -190,6 +285,7 @@ export GIT_LFS_SKIP_SMUDGE=1 if [ ! -d "$DEPENDENCIES_DIR" ]; then log "Cloning $DEPENDENCIES_REPO ..." + # 这里使用 pushd "$BUILD_DIR" 是为了让 dependencies_u 仓库直接克隆到 Build/dependencies/ 下,而不是 Build/ 下。 pushd "$BUILD_DIR" >/dev/null git clone "$DEPENDENCIES_REPO" dependencies 2>/dev/null || { @@ -222,38 +318,93 @@ fi log "" # ============================================================================== -# -- Step 3: Run dependencies_u/init.sh ---------------------------------------- +# -- Step 3: Extract UE4 plugins + third-party deps ----------------------------- # ============================================================================== -# All extraction / initialization logic lives inside the dependencies_u repo. -# setup.sh only orchestrates — init.sh does the actual work of unzipping -# plugins, extracting source packages, installing miniconda3, etc. +# Mirrors setup.bat 的 "Unzip Plugins" + "UnrealRoboticsLab dependencies" 段, +# 提取逻辑与 CI 工作流保持一致(含符号链接修正与 CoACD 目录嵌套修正)。 +# +# - DirectX / DirectX_Runtime / 7zip / CMake / dotnet / git / GnuWin32 压缩包 +# → Linux 由 apt 提供(Step 0 / Step 5) +# - src/*.zip 源码包解压到 Build/ +# → Linux 的 Util/BuildTools/Setup.sh 会自行下载源码并在编译后清理, +# 预解压反而会被 Setup.sh 内部的 rm -Rf *-source 删掉 -if [ ! -d "$DEPENDENCIES_DIR" ]; then - warn "dependencies_u not available — skipping extraction." -else +if [ -d "$DEPENDENCIES_DIR" ]; then log "==============================================" - log " Step 3 — Run dependencies_u/init.sh" + log " Step 3 — Extract UE4 plugins" log "==============================================" - INIT_SCRIPT="$DEPENDENCIES_DIR/init.sh" - if [ -f "$INIT_SCRIPT" ]; then - log "Delegating to $INIT_SCRIPT ..." - log "" - bash "$INIT_SCRIPT" "$PROJECT_ROOT" - success "dependencies_u initialization complete." + # --- RoadRunner / Cesium / glTFForUE4 插件(目标目录已存在则跳过)--- + if [ ! -d "$PLUGINS_DIR/RoadRunnerRuntime" ]; then + log "Unzipping RoadRunner Plugins ..." + 7z x "$DEPENDENCIES_DIR/Plugins/RoadRunner_Plugins.zip" -o"$PLUGINS_DIR/" -y >/dev/null || \ + warn " RoadRunner_Plugins.zip 解压失败" + else + log "RoadRunner Plugins already exists." + fi + + if [ ! -d "$PLUGINS_DIR/CesiumForUnreal" ]; then + log "Unzipping CesiumForUnreal Plugin ..." + 7z x "$DEPENDENCIES_DIR/Plugins/CesiumForUnreal-426-v1.18.0-ue4.zip" -o"$PLUGINS_DIR/" -y >/dev/null || \ + warn " CesiumForUnreal 解压失败" else - error "init.sh not found in dependencies_u!" - error "Expected at: $INIT_SCRIPT" - error "The dependencies_u repo may be incomplete or corrupted." - exit 1 + log "CesiumForUnreal Plugin already exists." + fi + + if [ ! -d "$PLUGINS_DIR/glTFForUE4" ]; then + log "Unzipping glTFForUE4 Plugin ..." + 7z x "$DEPENDENCIES_DIR/Plugins/glTFForUE4.zip" -o"$PLUGINS_DIR/" -y >/dev/null || \ + warn " glTFForUE4.zip 解压失败" + else + log "glTFForUE4 Plugin already exists." + fi + + # --- UnrealRoboticsLab 第三方依赖 --- + log "Initial UnrealRoboticsLab dependencies..." + if [ ! -d "$URLAB_THIRD_PARTY" ]; then + mkdir -p "$URLAB_THIRD_PARTY/MuJoCo" + tar -xzf "$DEPENDENCIES_DIR/Plugins/mujoco-3.7.0-linux-x86_64.tar.gz" \ + -C "$URLAB_THIRD_PARTY/MuJoCo" --strip-components=1 2>/dev/null || \ + warn " mujoco 解压失败" + unzip -qo "$DEPENDENCIES_DIR/Plugins/CoACD.zip" -d "$URLAB_THIRD_PARTY" 2>/dev/null || \ + warn " CoACD.zip 解压失败" + unzip -qo "$DEPENDENCIES_DIR/Plugins/libzmq-linux.zip" -d "$URLAB_THIRD_PARTY" 2>/dev/null || \ + warn " libzmq-linux.zip 解压失败" + else + log "Found UnrealRoboticsLab dependencies at $URLAB_THIRD_PARTY." + fi + + # --- 幂等修正(与 CI 工作流一致)--- + # mujoco 符号链接 → 真实文件(打包时要求真实 .so) + MUJOCO_LIB="$URLAB_THIRD_PARTY/MuJoCo/lib" + if [ -L "$MUJOCO_LIB/libmujoco.so" ]; then + REAL=$(readlink -f "$MUJOCO_LIB/libmujoco.so") + rm "$MUJOCO_LIB/libmujoco.so" && cp "$REAL" "$MUJOCO_LIB/libmujoco.so" + fi + # CoACD 目录嵌套修正: CoACD/CoACD/include -> CoACD/include + if [ -d "$URLAB_THIRD_PARTY/CoACD/CoACD/include" ] && [ ! -d "$URLAB_THIRD_PARTY/CoACD/include" ]; then + mv "$URLAB_THIRD_PARTY/CoACD/CoACD/include" "$URLAB_THIRD_PARTY/CoACD/include" fi + # libzmq 符号链接 → 真实 .so + ZMQ_LIB="$URLAB_THIRD_PARTY/libzmq/lib" + if [ -L "$ZMQ_LIB/libzmq.so" ]; then + REAL=$(readlink -f "$ZMQ_LIB/libzmq.so") + rm "$ZMQ_LIB/libzmq.so" "$ZMQ_LIB/libzmq.so.5" 2>/dev/null || true + cp "$REAL" "$ZMQ_LIB/" && mv "$ZMQ_LIB/$(basename "$REAL")" "$ZMQ_LIB/libzmq.so" + fi + + success "UE4 plugins extracted." +else + warn "dependencies_u not available — skipping plugin extraction." fi log "" # ============================================================================== -# -- Step 3b: Miniconda3 -------------------------------------------------------- +# -- Step 4: Miniconda3 ---------------------------------------------------------- # ============================================================================== +# dependencies_u 已不再提供 init.sh,插件解压由上面的 Step 3 完成。 +# 这里只负责安装 miniconda3 本体,供 BuildPythonAPI.sh 创建 hutb_3.X 环境。 MINICONDA_DIR="$DEPENDENCIES_DIR/prerequisites/miniconda3" if [ ! -d "$MINICONDA_DIR" ]; then @@ -272,49 +423,56 @@ fi log "" # ============================================================================== -# -- Step 4: System packages via apt ------------------------------------------- +# -- Step 5: System packages via apt ------------------------------------------- # ============================================================================== if [ "$SKIP_PREREQUISITES" = false ]; then log "==============================================" - log " Step 4 — System Prerequisites (apt)" + log " Step 5 — System Prerequisites (apt)" log "==============================================" - log "Installing build dependencies..." - sudo apt-get update -qq - - sudo apt-get install -y -qq \ - build-essential \ - clang-10 \ - libc++-dev \ - libc++abi-dev \ - ninja-build \ - python3 \ - python3-dev \ - python3-pip \ - python3-venv \ - libomp-dev \ - libssl-dev \ - libncurses5 \ - libncurses5-dev \ - libsdl2-dev \ - libtiff5-dev \ - libjpeg-dev \ - libcurl4-openssl-dev \ - libzmq3-dev \ - doxygen \ - patchelf \ - libxml2-dev \ - libicu-dev \ - 2>/dev/null || warn "Some apt packages may have failed to install." - - # clang-10 may not exist on newer Ubuntu (e.g. 24.04); fall back to clang - if ! command -v clang-10 &>/dev/null && ! command -v clang &>/dev/null; then - warn "clang not found — installing clang..." - sudo apt-get install -y -qq clang + # CI runner / 无终端环境下 sudo 可能需要密码:先探测免密 sudo, + # 不可用则跳过本步骤(warn 提示手动安装),不让整个 setup 失败。 + if sudo -n true 2>/dev/null; then + log "Installing build dependencies..." + sudo apt-get update -qq + + sudo apt-get install -y -qq \ + build-essential \ + clang-10 \ + libc++-dev \ + libc++abi-dev \ + ninja-build \ + python3 \ + python3-dev \ + python3-pip \ + python3-venv \ + libomp-dev \ + libssl-dev \ + libncurses5 \ + libncurses5-dev \ + libsdl2-dev \ + libtiff5-dev \ + libjpeg-dev \ + libcurl4-openssl-dev \ + libzmq3-dev \ + doxygen \ + patchelf \ + libxml2-dev \ + libicu-dev \ + 2>/dev/null || warn "Some apt packages may have failed to install." + + # clang-10 may not exist on newer Ubuntu (e.g. 24.04); fall back to clang + if ! command -v clang-10 &>/dev/null && ! command -v clang &>/dev/null; then + warn "clang not found — installing clang..." + sudo apt-get install -y -qq clang + fi + + success "System prerequisites installed." + else + warn "sudo 不可用(需要密码)— 跳过 apt 安装。" + warn " 请手动安装本步骤列出的依赖,或在交互终端中用 sudo 重跑本脚本。" fi - - success "System prerequisites installed." else log "Skipping system prerequisites (--skip-prerequisites)." fi @@ -347,6 +505,20 @@ fi log "" +# ============================================================================== +# -- Mirrors setup.bat: optional post-actions ---------------------------------- +# ============================================================================== + +if [ "$LAUNCH" = true ]; then + log "Launching Unreal Editor, log to launch.log..." + make launch ARGS="--chrono" >launch.log 2>&1 || warn "make launch reported errors." +fi + +if [ "$PACKAGE" = true ]; then + log "Packaging HUTB, log to package.log..." + make package ARGS="--chrono" >package.log 2>&1 || warn "make package reported errors." +fi + # ============================================================================== # -- Summary ------------------------------------------------------------------- # ============================================================================== From d1ca47cf6dbcf13a210f2f1b3e192f21f4578566 Mon Sep 17 00:00:00 2001 From: xiao-ma-gua <17358869549@163.com> Date: Thu, 10 Sep 2026 01:00:50 +0800 Subject: [PATCH 076/128] =?UTF-8?q?=E4=BD=BF=E7=94=A8=E8=87=AA=E5=AE=9A?= =?UTF-8?q?=E4=B9=89=E7=9A=84python=E8=99=9A=E6=8B=9F=E7=8E=AF=E5=A2=83?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- Util/BuildTools/Setup.sh | 5 +++-- 1 file changed, 3 insertions(+), 2 deletions(-) diff --git a/Util/BuildTools/Setup.sh b/Util/BuildTools/Setup.sh index 79de5147c0..54e98bc8d1 100755 --- a/Util/BuildTools/Setup.sh +++ b/Util/BuildTools/Setup.sh @@ -141,7 +141,8 @@ BOOST_LIBPATH=${PWD}/${BOOST_BASENAME}-install/lib for PY_VERSION in ${PY_VERSION_LIST[@]} ; do SHOULD_BUILD_BOOST=true - PYTHON_VERSION=$(/usr/bin/env python${PY_VERSION} -V 2>&1) + CONDA_PY=$(get_conda_env_python ${PY_VERSION}) + export PATH="$(dirname ${CONDA_PY}):${PATH}" LIB_NAME=$(cut -d . -f 1,2 <<< "$PYTHON_VERSION" | tr -d .) LIB_NAME=${LIB_NAME:7} if [[ -d "${BOOST_BASENAME}-install" ]] ; then @@ -190,7 +191,7 @@ for PY_VERSION in ${PY_VERSION_LIST[@]} ; do # BOOST_TOOLSET="gcc" BOOST_CFLAGS="-fPIC -std=c++14 -DBOOST_ERROR_CODE_HEADER_ONLY" - py3="/usr/bin/env python${PY_VERSION}" + py3="${CONDA_PY}" py3_root=`${py3} -c "import sysconfig; print(sysconfig.get_config_var('prefix'))"` py3_include=$(${py3} -c "from sysconfig import get_paths as gp; print(gp()['include'])") py3_lib=$(${py3} -c "from sysconfig import get_paths as gp; print(gp()['stdlib'])") From 65c1c3a8e367b2161b0cadef0213953bb61d49d0 Mon Sep 17 00:00:00 2001 From: xiao-ma-gua <17358869549@163.com> Date: Thu, 10 Sep 2026 13:43:35 +0800 Subject: [PATCH 077/128] =?UTF-8?q?=E5=9C=A8make=EF=BC=88=E4=B8=8D?= =?UTF-8?q?=E5=8F=AA=E6=98=AFpackage=EF=BC=89=E5=85=B6=E4=BB=96=E6=AD=A5?= =?UTF-8?q?=E9=AA=A4=E6=97=B6=EF=BC=8C=E4=B9=9F=E8=83=BD=E5=88=9B=E5=BB=BA?= =?UTF-8?q?=E6=89=80=E9=9C=80=E8=A6=81=E4=BD=86=E6=B2=A1=E6=9C=89=E7=9A=84?= =?UTF-8?q?=E7=8E=AF=E5=A2=83?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- Util/BuildTools/Environment.sh | 14 +++++++++++++- 1 file changed, 13 insertions(+), 1 deletion(-) diff --git a/Util/BuildTools/Environment.sh b/Util/BuildTools/Environment.sh index 5e96f75bc9..1e8318f8ae 100644 --- a/Util/BuildTools/Environment.sh +++ b/Util/BuildTools/Environment.sh @@ -76,9 +76,21 @@ function get_conda_env_python { local ENV_PY="${MINICONDA_DIR}/envs/hutb_3.${ENV_MINOR}/bin/python" if [[ ! -x "${ENV_PY}" ]]; then - fatal_error "conda env 'hutb_3.${ENV_MINOR}' not found (${ENV_PY}). + # 环境缺失时自动创建(官方默认源),供 Setup.sh / BuildPythonAPI.sh 复用。 + # 这样新工作区上 make setup(默认 python=3 → hutb_3.8)也能自给自足。 + if [[ -x "${MINICONDA_DIR}/bin/conda" ]]; then + log "conda env 'hutb_3.${ENV_MINOR}' not found — creating it with python=3.${ENV_MINOR} ..." + # fix: CondaToSNonInteractiveError — 官方源需先接受 ToS + "${MINICONDA_DIR}/bin/conda" tos accept --override-channels --channel https://repo.anaconda.com/pkgs/main 2>/dev/null || true + "${MINICONDA_DIR}/bin/conda" tos accept --override-channels --channel https://repo.anaconda.com/pkgs/r 2>/dev/null || true + "${MINICONDA_DIR}/bin/conda" create -n "hutb_3.${ENV_MINOR}" python="3.${ENV_MINOR}" --yes || \ + fatal_error "Failed to create conda env 'hutb_3.${ENV_MINOR}'. Run ./setup.sh first, or create it manually with: + ${MINICONDA_DIR}/bin/conda create -n hutb_3.${ENV_MINOR} python=3.${ENV_MINOR} --yes" + else + fatal_error "conda env 'hutb_3.${ENV_MINOR}' not found (${ENV_PY}) and conda not available (${MINICONDA_DIR}/bin/conda). Run ./setup.sh first, or create it manually with: ${MINICONDA_DIR}/bin/conda create -n hutb_3.${ENV_MINOR} python=3.${ENV_MINOR} --yes" + fi fi echo "${ENV_PY}" } From ee350ce1c6ee47db2c123a81cbba6a30c36fd797 Mon Sep 17 00:00:00 2001 From: xiao-ma-gua <17358869549@163.com> Date: Thu, 10 Sep 2026 17:52:10 +0800 Subject: [PATCH 078/128] =?UTF-8?q?=E5=9C=A8actions-runner=E4=B8=AD?= =?UTF-8?q?=E5=8A=A0=E5=85=A5ulimit=20-s=20unlimited=E4=B8=8D=E9=99=90?= =?UTF-8?q?=E5=88=B6=E6=A0=88=E7=9A=84=E5=A4=A7=E5=B0=8F?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .github/workflows/build_linux.yml | 1 + 1 file changed, 1 insertion(+) diff --git a/.github/workflows/build_linux.yml b/.github/workflows/build_linux.yml index 83a16900ec..8794913760 100644 --- a/.github/workflows/build_linux.yml +++ b/.github/workflows/build_linux.yml @@ -113,3 +113,4 @@ jobs: make check shell: bash + From 77911b5af56fb814cb6cc46194f1d74fbc131295 Mon Sep 17 00:00:00 2001 From: xiao-ma-gua <17358869549@163.com> Date: Tue, 15 Sep 2026 02:05:06 +0800 Subject: [PATCH 079/128] =?UTF-8?q?=E5=8E=BB=E9=99=A4Package.sh=E4=B8=AD?= =?UTF-8?q?=E6=89=80=E5=AE=9E=E7=8E=B0=E7=9A=84=E5=86=85=E5=AE=B9?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .github/workflows/build_linux.yml | 18 ------------------ 1 file changed, 18 deletions(-) diff --git a/.github/workflows/build_linux.yml b/.github/workflows/build_linux.yml index 8794913760..43a4e13ee2 100644 --- a/.github/workflows/build_linux.yml +++ b/.github/workflows/build_linux.yml @@ -79,24 +79,6 @@ jobs: make CarlaUE4Editor ARGS="--chrono -j$(( $(nproc) / 3 ))" shell: bash - # 修复 Content 包中 .uasset 文件的非法材质路径(缺少 /Game/ 根前缀) - - name: Fix invalid material paths in RoadRunner assets - run: | - echo "=== 正在修复 .uasset 中的非法 RoadRunner 材质路径 ===" - for BAD_PATH in \ - "/RoadRunnerMaterials/BaseMaterial:/Game/RoadRunnerMaterials/BaseMat" \ - "/RoadRunnerMaterials/BaseCutoutMaterial:/Game/RoadRunnerMaterials/BaseCutoutMat" \ - "/RoadRunnerCarlaContent/:/Game/RoadRunnerCarlaContent/" \ - ; do - FROM="${BAD_PATH%%:*}" - TO="${BAD_PATH##*:}" - find Unreal/CarlaUE4/Content -name "*.uasset" \ - -exec grep -l "$FROM" {} \; \ - -exec sed -i "s|$FROM|$TO|g" {} \; 2>/dev/null || true - done - echo "修复完成。" - shell: bash - - name: package run: | source ./setEnv64.sh From 8b20fc1be692c969c21450a200ff6be30f9c8197 Mon Sep 17 00:00:00 2001 From: xiao-ma-gua <17358869549@163.com> Date: Tue, 15 Sep 2026 02:05:52 +0800 Subject: [PATCH 080/128] =?UTF-8?q?=E9=99=90=E5=88=B6=E5=B9=B6=E5=8F=91?= =?UTF-8?q?=E6=95=B0?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- Util/BuildTools/Environment.sh | 3 ++- Util/BuildTools/Setup.sh | 26 +++++++++++++------------- 2 files changed, 15 insertions(+), 14 deletions(-) diff --git a/Util/BuildTools/Environment.sh b/Util/BuildTools/Environment.sh index 1e8318f8ae..3d02d446ad 100644 --- a/Util/BuildTools/Environment.sh +++ b/Util/BuildTools/Environment.sh @@ -54,7 +54,8 @@ function move_if_changed { rm -f $1 } -CARLA_BUILD_CONCURRENCY=`nproc --all` +# 限制并发数 +CARLA_BUILD_CONCURRENCY=$(( $(nproc --all) / 3 )) # ============================================================================== # -- Conda env python------------ diff --git a/Util/BuildTools/Setup.sh b/Util/BuildTools/Setup.sh index 54e98bc8d1..e876de4460 100755 --- a/Util/BuildTools/Setup.sh +++ b/Util/BuildTools/Setup.sh @@ -274,7 +274,7 @@ else -DCMAKE_INSTALL_PREFIX="../${RPCLIB_BASENAME}-libcxx-install" \ ../${RPCLIB_BASENAME}-source - ninja + ninja -j ${CARLA_BUILD_CONCURRENCY} ninja install @@ -291,7 +291,7 @@ else -DCMAKE_INSTALL_PREFIX="../${RPCLIB_BASENAME}-libstdcxx-install" \ ../${RPCLIB_BASENAME}-source - ninja + ninja -j ${CARLA_BUILD_CONCURRENCY} ninja install @@ -343,7 +343,7 @@ else -DCMAKE_INSTALL_PREFIX="../${GTEST_BASENAME}-libcxx-install" \ ../${GTEST_BASENAME}-source - ninja + ninja -j ${CARLA_BUILD_CONCURRENCY} ninja install @@ -360,7 +360,7 @@ else -DCMAKE_INSTALL_PREFIX="../${GTEST_BASENAME}-libstdcxx-install" \ ../${GTEST_BASENAME}-source - ninja + ninja -j ${CARLA_BUILD_CONCURRENCY} ninja install @@ -416,7 +416,7 @@ else -DRECASTNAVIGATION_TEST=False \ ../${RECAST_BASENAME}-source - ninja + ninja -j ${CARLA_BUILD_CONCURRENCY} ninja install @@ -531,7 +531,7 @@ else -Dnetwork=OFF \ .. - ninja + ninja -j ${CARLA_BUILD_CONCURRENCY} ninja install popd >/dev/null @@ -550,7 +550,7 @@ else -Dnetwork=OFF \ .. - ninja + ninja -j ${CARLA_BUILD_CONCURRENCY} ninja install popd >/dev/null @@ -645,7 +645,7 @@ if ${USE_CHRONO} ; then -DBUILD_DEMOS=OFF \ -DENABLE_MODULE_VEHICLE=ON \ .. - ninja + ninja -j ${CARLA_BUILD_CONCURRENCY} ninja install popd >/dev/null if [[ -n "${CHRONO_PATH}" ]] ; then @@ -764,7 +764,7 @@ else -DBUILD_CCT=OFF -DBUILD_CS2CS=OFF -DBUILD_GEOD=OFF -DBUILD_GIE=OFF \ -DBUILD_PROJ=OFF -DBUILD_TESTING=OFF \ -DCMAKE_INSTALL_PREFIX=${PROJ_INSTALL_DIR_FULL} - ninja + ninja -j ${CARLA_BUILD_CONCURRENCY} ninja install popd >/dev/null @@ -782,7 +782,7 @@ else -DBUILD_CCT=OFF -DBUILD_CS2CS=OFF -DBUILD_GEOD=OFF -DBUILD_GIE=OFF \ -DBUILD_PROJ=OFF -DBUILD_TESTING=OFF \ -DCMAKE_INSTALL_PREFIX=${PROJ_INSTALL_SERVER_DIR_FULL} - ninja + ninja -j ${CARLA_BUILD_CONCURRENCY} ninja install popd >/dev/null @@ -974,7 +974,7 @@ if ${USE_ROS2} ; then -DBUILD_SHARED_LIBS=OFF \ -DFOONATHAN_MEMORY_FORCE_VENDORED_BUILD=ON \ .. - ninja + ninja -j ${CARLA_BUILD_CONCURRENCY} ninja install popd >/dev/null rm -Rf ${FOONATHAN_MEMORY_VENDOR_SOURCE_DIR} @@ -1025,7 +1025,7 @@ if ${USE_ROS2} ; then -DOPENSSL_CRYPTO_LIBRARY:FILEPATH=${FASTDDS_LIB}/libcrypto.a \ -DTHIRDPARTY_BOOST_INCLUDE_DIR="${BOOST_INCLUDE};${FAST_DDS_LIB_SOURCE_DIR}/thirdparty/boost/include" \ .. - ninja + ninja -j ${CARLA_BUILD_CONCURRENCY} ninja install popd >/dev/null rm -Rf ${FAST_DDS_LIB_SOURCE_DIR} @@ -1066,7 +1066,7 @@ if ${USE_ROS2} ; then -DENABLE_SECURITY=OFF \ -DENABLE_SHM=OFF \ .. - ninja + ninja -j ${CARLA_BUILD_CONCURRENCY} ninja install popd >/dev/null rm -Rf ${CYCLONE_DDS_SOURCE_DIR} From fafe4cc45f7380b5c4ef60f1a03a905bc2455c22 Mon Sep 17 00:00:00 2001 From: xiao-ma-gua <17358869549@163.com> Date: Tue, 15 Sep 2026 23:48:08 +0800 Subject: [PATCH 081/128] =?UTF-8?q?=E4=BC=98=E5=85=88=E4=BD=BF=E7=94=A8?= =?UTF-8?q?=E4=BB=93=E5=BA=93=E9=87=8Cboost=E7=9A=84=E6=9C=AC=E5=9C=B0?= =?UTF-8?q?=E5=8C=85?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- Util/BuildTools/Setup.sh | 35 ++++++++++++++++++++++++++++++----- 1 file changed, 30 insertions(+), 5 deletions(-) diff --git a/Util/BuildTools/Setup.sh b/Util/BuildTools/Setup.sh index e876de4460..29dfb97085 100755 --- a/Util/BuildTools/Setup.sh +++ b/Util/BuildTools/Setup.sh @@ -143,6 +143,12 @@ for PY_VERSION in ${PY_VERSION_LIST[@]} ; do SHOULD_BUILD_BOOST=true CONDA_PY=$(get_conda_env_python ${PY_VERSION}) export PATH="$(dirname ${CONDA_PY}):${PATH}" + # <<< "$PYTHON_VERSION" 把变量的值直接作为标准输入喂给 cut,免去 echo | 的写法 + # cut -d . -f 1,2 — 按 . 切分,取第 1、2 段(即主版本.次版本) + # tr -d . — 删掉剩余的点 + # $(...) — 命令替换,把整个管道的输出赋给 LIB_NAME + # LIB_NAME=${LIB_NAME:7} 再截掉前 7 个字符 + # "Python 3.12.8" → cut → "Python 3.12" → tr → "Python 312" → :7 → "312" LIB_NAME=$(cut -d . -f 1,2 <<< "$PYTHON_VERSION" | tr -d .) LIB_NAME=${LIB_NAME:7} if [[ -d "${BOOST_BASENAME}-install" ]] ; then @@ -155,16 +161,29 @@ for PY_VERSION in ${PY_VERSION_LIST[@]} ; do if { ${SHOULD_BUILD_BOOST} ; } ; then rm -Rf ${BOOST_BASENAME}-source + # ${BOOST_VERSION//./_} — 把 $BOOST_VERSION + # 中所有的 . 替换成 _(双斜杠 // 表示全局替换,替换单个的话是单个 /) + # 前面再拼上 boost_ 前缀 + # 1.90.0 → 1_90_0 → BOOST_PACKAGE_BASENAME = boost_1_90_0 BOOST_PACKAGE_BASENAME=boost_${BOOST_VERSION//./_} log "Retrieving boost." - start=$(date +%s) - wget "https://archives.boost.io/release/${BOOST_VERSION}/source/${BOOST_PACKAGE_BASENAME}.tar.gz" -O ${BOOST_PACKAGE_BASENAME}.tar.gz || true - end=$(date +%s) - echo "Elapsed Time downloading from boost webpage: $(($end-$start)) seconds" + # 优先使用 dependencies_u(工蜂)仓库里的本地包,避免依赖外网下载 + if [ -f "dependencies/src/${BOOST_PACKAGE_BASENAME}.tar.gz" ] ; then + log "Using boost from dependencies_u repository." + cp "dependencies/src/${BOOST_PACKAGE_BASENAME}.tar.gz" . + fi - # try to use the backup boost we have in Jenkins + # 本地包缺失或校验不通过时,从 boost 官网下载 + if [ ! -f "${BOOST_PACKAGE_BASENAME}.tar.gz" ] || [[ $(sha256sum "${BOOST_PACKAGE_BASENAME}.tar.gz" | cut -d " " -f 1 ) != "${BOOST_SHA256SUM}" ]] ; then + start=$(date +%s) + wget "https://archives.boost.io/release/${BOOST_VERSION}/source/${BOOST_PACKAGE_BASENAME}.tar.gz" -O ${BOOST_PACKAGE_BASENAME}.tar.gz || true + end=$(date +%s) + echo "Elapsed Time downloading from boost webpage: $(($end-$start)) seconds" + fi + + # 仍失败则用 Jenkins 上的备份 if [ ! -f "${BOOST_PACKAGE_BASENAME}.tar.gz" ] || [[ $(sha256sum "${BOOST_PACKAGE_BASENAME}.tar.gz" | cut -d " " -f 1 ) != "${BOOST_SHA256SUM}" ]] ; then log "Using boost backup" @@ -175,6 +194,12 @@ for PY_VERSION in ${PY_VERSION_LIST[@]} ; do fi + # 最终校验,不通过直接报错退出,避免后面的 tar 报错 + if [[ $(sha256sum "${BOOST_PACKAGE_BASENAME}.tar.gz" 2>/dev/null | cut -d " " -f 1 ) != "${BOOST_SHA256SUM}" ]] ; then + log "ERROR: boost package missing or sha256 mismatch (expected ${BOOST_SHA256SUM})." + exit 1 + fi + log "Extracting boost for Python ${PY_VERSION}." start=$(date +%s) From ec5a831f730cbe366bce0b74c6d368a7c0d21fc3 Mon Sep 17 00:00:00 2001 From: xiao-ma-gua <17358869549@163.com> Date: Thu, 17 Sep 2026 01:20:44 +0800 Subject: [PATCH 082/128] =?UTF-8?q?=E5=8D=95=E5=85=83=E6=B5=8B=E8=AF=95?= =?UTF-8?q?=E9=80=9A=E8=BF=87?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- Util/BuildTools/Check.sh | 276 +++++++++++++++++++++++++++++++++++++-- 1 file changed, 267 insertions(+), 9 deletions(-) diff --git a/Util/BuildTools/Check.sh b/Util/BuildTools/Check.sh index 78178089e8..7831616527 100755 --- a/Util/BuildTools/Check.sh +++ b/Util/BuildTools/Check.sh @@ -15,8 +15,8 @@ Then either ran all the tests Or choose one or more of the following - [--libcarla-release] [--libcarla-debug] - [--benchmark] + [--libcarla-release] [--libcarla-debug] [--smoke] [--air] [--vr] [--upload] + [--python-api] [--benchmark] [--debug] You can also set the command-line arguments passed to GTest on a ".gtest" config file in the Carla project main folder. E.g. @@ -27,16 +27,21 @@ config file in the Carla project main folder. E.g. END ) +IS_DEBUG=false GDB= XML_OUTPUT=false GTEST_ARGS= LIBCARLA_RELEASE=false LIBCARLA_DEBUG=false SMOKE_TESTS=false +VR_TESTS=false PYTHON_API=false RUN_BENCHMARK=false +AIR_TESTS=false +MEASURE_TIME=true +UPLOAD_DOWNLOAD=false -OPTS=`getopt -o h --long help,gdb,xml,gtest_args:,all,libcarla-release,libcarla-debug,python-api,smoke,benchmark,python-version:, -n 'parse-options' -- "$@"` +OPTS=`getopt -o h --long help,gdb,xml,gtest_args:,all,libcarla-release,libcarla-debug,python-api,smoke,air,vr,upload,debug,benchmark,python-version:, -n 'parse-options' -- "$@"` eval set -- "$OPTS" @@ -46,7 +51,8 @@ if [ -f "${CARLA_ROOT_FOLDER}/.gtest" ]; then GTEST_ARGS=`sed -e 's/#.*$//g' ${CARLA_ROOT_FOLDER}/.gtest | sed -e '/^[[:space:]]*$/!s/^/--/g' | sed -e ':a;N;$!ba;s/\n/ /g'` fi -PY_VERSION_LIST=3 +# 与 Windows Check.bat 对齐:默认测试 hutb_3.14 ~ hutb_3.7 全部版本 +PY_VERSION_LIST=3.14,3.13,3.12,3.11,3.10,3.9,3.8,3.7 while [[ $# -gt 0 ]]; do case "$1" in @@ -62,9 +68,13 @@ while [[ $# -gt 0 ]]; do GTEST_ARGS="$2"; shift 2 ;; --all ) + # 与 Check.bat 对齐:完整端到端流程 + # (上传/下载发行包 → 启动 CarlaUE4 → 装测试依赖与包内 whl → 单测 + smoke) + # SMOKE_TESTS=true; LIBCARLA_RELEASE=true; LIBCARLA_DEBUG=true; PYTHON_API=true; + # UPLOAD_DOWNLOAD=true; shift ;; --libcarla-release ) LIBCARLA_RELEASE=true; @@ -75,6 +85,18 @@ while [[ $# -gt 0 ]]; do --smoke ) SMOKE_TESTS=true; shift ;; + --air ) + AIR_TESTS=true; + shift ;; + --vr ) + VR_TESTS=true; + shift ;; + --upload ) + UPLOAD_DOWNLOAD=true; + shift ;; + --debug ) + IS_DEBUG=true; + shift ;; --python-api ) PYTHON_API=true; shift ;; @@ -99,13 +121,22 @@ done # 优先使用环境中的Python系统,以便构建的wheel文件与Boost库所匹配的Python版本一致。 export PATH=/usr/bin:/bin:$PATH -if ! { ${LIBCARLA_RELEASE} || ${LIBCARLA_DEBUG} || ${PYTHON_API} || ${SMOKE_TESTS}; }; then +if ! { ${LIBCARLA_RELEASE} || ${LIBCARLA_DEBUG} || ${PYTHON_API} || ${SMOKE_TESTS} || ${AIR_TESTS} || ${VR_TESTS}; }; then fatal_error "Nothing selected to be done." fi -# Convert comma-separated string to array of unique elements. +# 将逗号分隔的字符串转换为元素的数组 +# IFS="," —— 临时给本条命令改分隔符 +# read —— 读一行,按 IFS 切字段 +# -r —— 不让 read 处理反斜杠转义 +# IFS="," read -a a <<< "$s" # 无 -r → a=(a,b),\ 被当转义吃掉,逗号没切开 +# IFS="," read -r -a a <<< "$s" # 有 -r → a=(a\ b),原样保留 +# -a 表示赋给索引数组(下标 0 开始)而不是普通变量。 IFS="," read -r -a PY_VERSION_LIST <<< "${PY_VERSION_LIST}" +# 衡量测试的总时间 +T_START_OVERALL=$(date +%s) + # ============================================================================== # -- Download Content need it by the tests ------------------------------------- # ============================================================================== @@ -172,6 +203,174 @@ if ${LIBCARLA_RELEASE} ; then fi +# ============================================================================== +# -- Upload / download distribution package ------------------------------------ +# ============================================================================== + +# 发行包版本目录名:git 短哈希(工作区有改动时带 -dirty 后缀,与打包脚本一致) +CARLA_VERSION=$(get_git_repository_version) + +# 发行包安装目录。 +INSTALLATION_DIR=${INSTALLATION_DIR:-${CARLA_BUILD_FOLDER}} + +# if ${UPLOAD_DOWNLOAD} ; then + +# # 下载器脚本(Windows 的 hutb_downloader.exe 即它的 pyinstaller 打包版) +# DOWNLOADER="${CARLA_UTIL_FOLDER}/download_from_git.py" +# DOWNLOADER_PY=$(get_conda_env_python 3.8) + +# # download_from_git.py 依赖 gitpython +# ${DOWNLOADER_PY} -c "import git" 2>/dev/null || ${DOWNLOADER_PY} -m pip install -q gitpython + +# # 上传分支固定查找 cwd/../Build/UE4Carla 下修改时间最新的 .zip; +# # 先在 Dist 或 Build/UE4Carla 里找最新的 tar.gz,缺的话从 Dist 拷过去。 +# mkdir -p "${INSTALLATION_DIR}/UE4Carla" +# LATEST_TAR=$(find "${CARLA_DIST_FOLDER}" "${INSTALLATION_DIR}/UE4Carla" -maxdepth 1 -name "*.tar.gz" -printf "%T@ %p\n" 2>/dev/null | sort -n | tail -1 | cut -d" " -f2- || true) +# if [[ -z "${LATEST_TAR}" ]] ; then +# fatal_error "No tar.gz file found in ${CARLA_DIST_FOLDER} or ${INSTALLATION_DIR}/UE4Carla to upload." +# fi +# if [[ "$(dirname ${LATEST_TAR})" != "${INSTALLATION_DIR}/UE4Carla" ]] ; then +# echo "Copying latest tar.gz to upload folder: ${LATEST_TAR}" +# cp "${LATEST_TAR}" "${INSTALLATION_DIR}/UE4Carla/" +# fi + +# pushd "${CARLA_UTIL_FOLDER}" >/dev/null +# log "Uploading distribution package..." +# ${DOWNLOADER_PY} -u release +# popd >/dev/null + +# # 下载回来并解压(解压到 Util/dist/hutb/),顺带测试下载分发流程本身 +# mkdir -p "${CARLA_UTIL_FOLDER}/dist" +# pushd "${CARLA_UTIL_FOLDER}/dist" >/dev/null +# log "Downloading distribution package..." +# ${DOWNLOADER_PY} +# popd >/dev/null + +# fi + +# ============================================================================== +# -- Locate CarlaUE4 packaged build -------------------------------------------- +# ============================================================================== + +# 打包产物可能在多个位置: +# 1) Windows 式布局 Build/UE4Carla//(Package.bat 的直接输出目录) +# 2) 下载解压目录 Util/dist/hutb/UE4Carla// +# 3) Package.sh 布局 Dist/CARLA_/ 与 Dist/CARLA__/ +# (默认 config=Shipping,如 Dist/CARLA_Shipping_,tar.gz 解压后) +if ${IS_DEBUG} ; then + PACKAGE_ROOTS=("${INSTALLATION_DIR}/UE4Carla/debug") +else + PACKAGE_ROOTS=( + "${INSTALLATION_DIR}/UE4Carla/${CARLA_VERSION}" + "${CARLA_UTIL_FOLDER}/dist/hutb/UE4Carla/${CARLA_VERSION}" + "${CARLA_DIST_FOLDER}/CARLA_${CARLA_VERSION}" + "${CARLA_DIST_FOLDER}"/CARLA_*_"${CARLA_VERSION}" + ) +fi + +EXE_DIR= +PACKAGE_ROOT= +for ROOT in "${PACKAGE_ROOTS[@]}" ; do + # 通配符未展开时跳过 + if [[ -f "${ROOT}/LinuxNoEditor/CarlaUE4.sh" ]] ; then + PACKAGE_ROOT="${ROOT}" + EXE_DIR="${ROOT}/LinuxNoEditor" + break + fi +done + +if [[ -n "${EXE_DIR}" ]] ; then + log "Using packaged build: ${EXE_DIR}" +else + log "warning: packaged build not found — will skip packaged wheel install; simulator tests (smoke/air/vr) need it." +fi + +# ============================================================================== +# -- Install Python packages --------------------------------------------------- +# ============================================================================== + +# 与 Check.bat:190-205 的 Install Python packages 段对齐: +# 测试依赖(含 nose2)由 check 自己装、每个版本只在这里装一次, +# 构建侧不负责(BuildPythonAPI 重建 conda 环境后靠这里补全)。 +# 非 debug 时额外卸载 hutb 并安装“发行包”里的 whl(测试打包产物本身)。 +if ! ${IS_DEBUG} ; then + + for PY_VERSION in ${PY_VERSION_LIST[@]} ; do + CONDA_PY=$(get_conda_env_python ${PY_VERSION}) + MINOR="${PY_VERSION#3.}" + + log "Installing Python test requirements for Python ${PY_VERSION}." + ${CONDA_PY} -m pip install -r "${CARLA_PYTHONAPI_ROOT_FOLDER}/test/requirements.txt" \ + -i http://mirrors.aliyun.com/pypi/simple --trusted-host mirrors.aliyun.com \ + || log "warning: failed to install test requirements for Python ${PY_VERSION}" + + # 包内 whl 命名含版本/平台标签。与 Check.bat 对齐: + # Python 3.7 的 whl 带 cp37m 标签(cp3X-cp3Xm),其余版本为 cp3X-cp3X; + # 版本号用通配符匹配(Windows 用 API_VERSION,Linux 侧无此变量)。 + if [[ "${PY_VERSION}" == "3.7" ]] ; then + WHL=$(ls "${EXE_DIR}"/PythonAPI/carla/dist/hutb-*-cp3${MINOR}-cp3${MINOR}m-linux_x86_64.whl 2>/dev/null | head -1 || true) + else + WHL=$(ls "${EXE_DIR}"/PythonAPI/carla/dist/hutb-*-cp3${MINOR}-cp3${MINOR}-linux_x86_64.whl 2>/dev/null | head -1 || true) + fi + if [[ -n "${WHL}" ]] ; then + log "Installing packaged wheel for Python ${PY_VERSION}: $(basename ${WHL})" + # wheel 版本号不随构建变化(如 2.10.1),已装同版本时 pip install 会直接 + # 跳过,包内 whl 就没被真正测试到;先卸载保证每次都从发行包重新安装。 + ${CONDA_PY} -m pip uninstall --yes hutb || true + ${CONDA_PY} -m pip install "${WHL}" + else + log "warning: no packaged wheel found for Python ${PY_VERSION}, using locally installed one." + fi + done + +fi + +# ============================================================================== +# -- Launch CarlaUE4 service for tests ----------------------------------------- +# ============================================================================== + +if { ${SMOKE_TESTS} || ${AIR_TESTS} || ${VR_TESTS}; }; then + + if [[ -z "${EXE_DIR}" ]] ; then + fatal_error "CarlaUE4.sh not found (looked under Build/UE4Carla/${CARLA_VERSION}, Util/dist/hutb/UE4Carla/${CARLA_VERSION} and Dist/CARLA_${CARLA_VERSION}). Build and package first, or set INSTALLATION_DIR." + fi + + # 杀掉占用 3654 端口的旧服务进程 + fuser -k 3654/tcp 2>/dev/null || pkill -f "CarlaUE4.sh" 2>/dev/null || true + + # 后台启动服务(对应 bat 的 start;否则会卡住)。 + # 注意:release 版加 -RenderOffscreen,debug 版不加(与 bat 一致)。 + if ! ${IS_DEBUG} ; then + SIM_ARGS="-RenderOffscreen --carla-rpc-port=3654 --carla-streaming-port=0 -nosound" + else + SIM_ARGS="--carla-rpc-port=3654 --carla-streaming-port=0 -nosound" + fi + log "Unreal service is launching with command: CarlaUE4.sh ${SIM_ARGS}" + pushd "${EXE_DIR}" >/dev/null + nohup ./CarlaUE4.sh ${SIM_ARGS} >/dev/null 2>&1 & + popd >/dev/null + +fi + +# ============================================================================== +# -- Run Carla-Air example tests ----------------------------------------------- +# ============================================================================== + +if ${AIR_TESTS} ; then + log "Running Carla-Air example tests..." + pushd "${CARLA_PYTHONAPI_ROOT_FOLDER}/examples/air" >/dev/null + for PY_VERSION in ${PY_VERSION_LIST[@]} ; do + CONDA_PY=$(get_conda_env_python ${PY_VERSION}) + log "Running Carla-Air Python API for Python ${PY_VERSION} example tests." + # 与 bat 相同跑 4 个脚本;失败即中止 + for SCRIPT in 01_hello_world.py 02_weather_control.py 03_spawn_traffic.py 04_sensor_capture.py ; do + ${CONDA_PY} ${SCRIPT} --port 3654 || fatal_error "AIR test failed: ${SCRIPT} (Python ${PY_VERSION})." + done + log "Finished Carla-Air Python API for Python ${PY_VERSION} example tests." + done + popd >/dev/null +fi + # ============================================================================== # -- Run Python API unit tests ------------------------------------------------- # ============================================================================== @@ -190,7 +389,13 @@ if ${PYTHON_API} ; then log "Running Python API for Python ${PY_VERSION} unit tests." - /usr/bin/env python${PY_VERSION} -m nose2 ${EXTRA_ARGS} + # carla wheel 安装在 conda 环境 hutb_3.X 中,须用对应环境的 python 运行, + # 否则系统 python3 找不到 carla 模块(ModuleNotFoundError)。 + # 测试依赖已在上方 Install Python packages 段统一安装(同 Check.bat)。 + # 与 Check.bat 对齐:每个版本只跑 test_transform 和 test_vehicle。 + CONDA_PY=$(get_conda_env_python ${PY_VERSION}) + ${CONDA_PY} -m nose2 ${EXTRA_ARGS} test_transform + ${CONDA_PY} -m nose2 ${EXTRA_ARGS} test_vehicle done @@ -206,11 +411,15 @@ popd >/dev/null # -- Run smoke tests ----------------------------------------------------------- # ============================================================================== +T_START_DO_TEST=$(date +%s) + if ${SMOKE_TESTS} ; then pushd "${CARLA_PYTHONAPI_ROOT_FOLDER}/util" >/dev/null log "Checking connection with the simulator." for PY_VERSION in ${PY_VERSION_LIST[@]} ; do - /usr/bin/env python${PY_VERSION} test_connection.py -p 3654 --timeout=60.0 + # 与单元测试一致:用 carla wheel 所在的 conda 环境 python 运行。 + CONDA_PY=$(get_conda_env_python ${PY_VERSION}) + ${CONDA_PY} test_connection.py -p 3654 --timeout=60.0 done popd >/dev/null fi @@ -227,7 +436,10 @@ if ${SMOKE_TESTS} ; then smoke_list=`cat smoke_test_list.txt` for PY_VERSION in ${PY_VERSION_LIST[@]} ; do log "Running smoke tests for Python ${PY_VERSION}." - /usr/bin/env python${PY_VERSION} -m nose2 -v ${EXTRA_ARGS} ${smoke_list} + # 与单元测试一致:用 carla wheel 所在的 conda 环境 python 运行。 + # 测试依赖已在上方 Install Python packages 段统一安装(同 Check.bat)。 + CONDA_PY=$(get_conda_env_python ${PY_VERSION}) + ${CONDA_PY} -m nose2 -v ${EXTRA_ARGS} ${smoke_list} done if ${XML_OUTPUT} ; then @@ -238,6 +450,52 @@ fi popd >/dev/null +T_END_DO_TEST=$(date +%s) +if ${MEASURE_TIME} && ${SMOKE_TESTS} ; then + echo "$(basename $0) [TIME]: Running smoke test took $((T_END_DO_TEST - T_START_DO_TEST)) seconds." +fi + +# ============================================================================== +# -- Run VR tests -------------------------------------------------------------- +# ============================================================================== + +T_START_DO_TEST=$(date +%s) + +if ${VR_TESTS} ; then + pushd "${CARLA_PYTHONAPI_ROOT_FOLDER}/test" >/dev/null + for PY_VERSION in ${PY_VERSION_LIST[@]} ; do + CONDA_PY=$(get_conda_env_python ${PY_VERSION}) + log "Running VR tests for Python ${PY_VERSION}." + # 等服务就绪(对应 bat 的 timeout /t 10) + sleep 10 + log "Switch to VR mode..." + ${CONDA_PY} ${CARLA_PYTHONAPI_ROOT_FOLDER}/util/config.py -p 3654 --map "Town10HD?GAME=VR" + ${CONDA_PY} ./function/test_VR_diagram_mode.py || fatal_error "VR test failed (Python ${PY_VERSION})." + done + popd >/dev/null +fi + +T_END_DO_TEST=$(date +%s) +if ${MEASURE_TIME} && ${VR_TESTS} ; then + echo "$(basename $0) [TIME]: Running VR test took $((T_END_DO_TEST - T_START_DO_TEST)) seconds." +fi + +# ============================================================================== +# -- Kill CarlaUE4 service after tests ----------------------------------------- +# ============================================================================== + +if { ${SMOKE_TESTS} || ${AIR_TESTS} || ${VR_TESTS}; }; then + # 等服务就绪再杀(对应 bat:等几秒让测试任务提交,否则可能没有测试任务被杀掉) + sleep 10 + log "Killing Unreal service process after test..." + fuser -k 3654/tcp 2>/dev/null || pkill -f "CarlaUE4.sh" 2>/dev/null || true +fi + +T_END_OVERALL=$(date +%s) +if ${MEASURE_TIME} ; then + echo "$(basename $0) [TIME]: Overall testing took $((T_END_OVERALL - T_START_OVERALL)) seconds." +fi + # ============================================================================== # -- ...and we are done -------------------------------------------------------- # ============================================================================== From fbf29a0670427d077b095e25ae3c74a380002c20 Mon Sep 17 00:00:00 2001 From: xiao-ma-gua <17358869549@163.com> Date: Thu, 17 Sep 2026 14:22:28 +0800 Subject: [PATCH 083/128] =?UTF-8?q?make=20check=E4=B8=AD=E7=9A=84=E5=86=85?= =?UTF-8?q?=E5=AE=B9=E4=B8=8Ewindows=E5=90=8C=E6=AD=A5?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .github/workflows/build_linux.yml | 5 ++++- 1 file changed, 4 insertions(+), 1 deletion(-) diff --git a/.github/workflows/build_linux.yml b/.github/workflows/build_linux.yml index 43a4e13ee2..3aa8a23594 100644 --- a/.github/workflows/build_linux.yml +++ b/.github/workflows/build_linux.yml @@ -92,7 +92,10 @@ jobs: - name: check run: | source ./setEnv64.sh - make check + make check.PythonAPI + make check.smoke + make check.air + make check.vr shell: bash From 33d6024fb00ece7e17f4c4164017498f78518e4f Mon Sep 17 00:00:00 2001 From: xiao-ma-gua <17358869549@163.com> Date: Thu, 17 Sep 2026 14:30:11 +0800 Subject: [PATCH 084/128] =?UTF-8?q?=E5=8A=A0=E5=85=A5=E5=86=92=E7=83=9F?= =?UTF-8?q?=E3=80=81=E7=A9=BA=E4=B8=AD=E3=80=81VR=E3=80=81=E4=B8=8A?= =?UTF-8?q?=E4=BC=A0=E7=9A=84check=E6=8C=87=E4=BB=A4?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- Util/BuildTools/Linux.mk | 15 ++++++++++++++- 1 file changed, 14 insertions(+), 1 deletion(-) diff --git a/Util/BuildTools/Linux.mk b/Util/BuildTools/Linux.mk index 3927a90757..84fd3a1104 100644 --- a/Util/BuildTools/Linux.mk +++ b/Util/BuildTools/Linux.mk @@ -61,9 +61,22 @@ check.LibCarla.debug: LibCarla.debug check.LibCarla.release: LibCarla.release @${CARLA_BUILD_TOOLS_FOLDER}/Check.sh --libcarla-release $(ARGS) -check.PythonAPI: PythonAPI +# check.PythonAPI: PythonAPI +check.PythonAPI: @${CARLA_BUILD_TOOLS_FOLDER}/Check.sh --python-api $(ARGS) +check.smoke: + @${CARLA_BUILD_TOOLS_FOLDER}/Check.sh --smoke $(ARGS) + +check.air: + @${CARLA_BUILD_TOOLS_FOLDER}/Check.sh --air $(ARGS) + +check.vr: + @${CARLA_BUILD_TOOLS_FOLDER}/Check.sh --vr $(ARGS) + +check.upload: + @${CARLA_BUILD_TOOLS_FOLDER}/Check.sh --upload $(ARGS) + check.PythonAPI.3: PythonAPI.3 @${CARLA_BUILD_TOOLS_FOLDER}/Check.sh --python-api --python-version=3 $(ARGS) From 0087dbe68c7dd5b7cbf87ab1d80e46297bbafd1a Mon Sep 17 00:00:00 2001 From: xiao-ma-gua <17358869549@163.com> Date: Thu, 17 Sep 2026 19:42:25 +0800 Subject: [PATCH 085/128] =?UTF-8?q?=E8=BF=90=E8=A1=8C./CarlaUE4.sh?= =?UTF-8?q?=E6=98=AFOBJ/STL=E8=A7=A3=E7=A0=81=E5=99=A8=E9=87=8D=E5=A4=8D?= =?UTF-8?q?=E8=BF=90=E8=A1=8C=E8=80=8C=E5=AF=BC=E8=87=B4=E8=BF=90=E8=A1=8C?= =?UTF-8?q?=E5=B4=A9=E6=BA=83?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../Plugins/UnrealRoboticsLab/Source/URLab/Private/URLab.cpp | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/Unreal/CarlaUE4/Plugins/UnrealRoboticsLab/Source/URLab/Private/URLab.cpp b/Unreal/CarlaUE4/Plugins/UnrealRoboticsLab/Source/URLab/Private/URLab.cpp index 85010313ae..11fd7c1846 100644 --- a/Unreal/CarlaUE4/Plugins/UnrealRoboticsLab/Source/URLab/Private/URLab.cpp +++ b/Unreal/CarlaUE4/Plugins/UnrealRoboticsLab/Source/URLab/Private/URLab.cpp @@ -72,14 +72,14 @@ void FURLabModule::StartupModule() #if PLATFORM_WINDOWS LoadDependencyDLL(TEXT("mujoco.dll"), TEXT("MuJoCo")); #else - LoadDependencyDLL(TEXT("libmujoco.so"), TEXT("MuJoCo")); + #endif // Load ZMQ #if PLATFORM_WINDOWS LoadDependencyDLL(TEXT("libzmq-v143-mt-4_3_6.dll"), TEXT("libzmq")); #else - LoadDependencyDLL(TEXT("libzmq.so"), TEXT("libzmq")); + #endif // Load CoACD (Shared library — Windows only; header-only on Linux) From 78a0b392cd360d34eb5e5a0a5cb80812ccf29173 Mon Sep 17 00:00:00 2001 From: xiao-ma-gua <17358869549@163.com> Date: Thu, 17 Sep 2026 20:13:49 +0800 Subject: [PATCH 086/128] =?UTF-8?q?Linux=E4=B8=8A=E9=83=A8=E5=88=86Python?= =?UTF-8?q?=E7=89=88=E6=9C=AC=E5=AF=BC=E5=85=A5=E5=A4=B1=E8=B4=A5=EF=BC=8C?= =?UTF-8?q?=E6=AD=A4=E6=97=B6=E8=B7=B3=E8=BF=87=E8=A1=A5=E4=B8=81?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit 佐证:https://github.com/python/cpython/blob/3.14/Lib/ssl.py#L257-L258 --- PythonAPI/examples/air/01_hello_world.py | 60 +++++++++++++----------- 1 file changed, 33 insertions(+), 27 deletions(-) diff --git a/PythonAPI/examples/air/01_hello_world.py b/PythonAPI/examples/air/01_hello_world.py index 95d04f9699..aa5c51809b 100644 --- a/PythonAPI/examples/air/01_hello_world.py +++ b/PythonAPI/examples/air/01_hello_world.py @@ -1,37 +1,43 @@ #!/usr/bin/env python3 import ssl, base64, os, tempfile -from _ssl import enum_certificates +try: + from _ssl import enum_certificates +except ImportError: + # _ssl.enum_certificates 仅存在于 Windows 构建(官方文档 availability: Windows), + # Linux/macOS 上任何 Python 版本导入都会失败,此时跳过补丁 + enum_certificates = None # 修复 Python 3.8 和 3.9 运行的错误: ssl.SSLError: [ASN1: NOT_ENOUGH_DATA] not enough data (_ssl.c:4192) # 当 cadata 加载失败时将 DER 转为 PEM 写入临时文件,再通过 cafile 参数加载 # 参考:https://github.com/agentscope-ai/QwenPaw/issues/5086 -_orig_lv = ssl.SSLContext.load_verify_locations -def _patched(self, *args, **kwargs): - try: - return _orig_lv(self, *args, **kwargs) - except ssl.SSLError: - cadata = kwargs.get("cadata") or (args[2] if len(args) > 2 else None) - if isinstance(cadata, (bytes, bytearray)): - purpose = ssl.Purpose.SERVER_AUTH - pem_certs = [] - for storename in getattr(self, "_windows_cert_stores", ["ROOT", "CA"]): - try: - for cert, encoding, trust in enum_certificates(storename): - if encoding == "x509_asn" and (trust is True or purpose.oid in trust): - pem_certs.append(b"-----BEGIN CERTIFICATE-----\n" + base64.encodebytes(cert) + b"-----END CERTIFICATE-----\n") - except PermissionError: - pass - if pem_certs: - fd, path = tempfile.mkstemp(suffix=".pem") - try: - os.write(fd, b"".join(pem_certs)) - os.close(fd) - return _orig_lv(self, cafile=path) - finally: - os.unlink(path) - raise -ssl.SSLContext.load_verify_locations = _patched +if enum_certificates is not None: + _orig_lv = ssl.SSLContext.load_verify_locations + def _patched(self, *args, **kwargs): + try: + return _orig_lv(self, *args, **kwargs) + except ssl.SSLError: + cadata = kwargs.get("cadata") or (args[2] if len(args) > 2 else None) + if isinstance(cadata, (bytes, bytearray)): + purpose = ssl.Purpose.SERVER_AUTH + pem_certs = [] + for storename in getattr(self, "_windows_cert_stores", ["ROOT", "CA"]): + try: + for cert, encoding, trust in enum_certificates(storename): + if encoding == "x509_asn" and (trust is True or purpose.oid in trust): + pem_certs.append(b"-----BEGIN CERTIFICATE-----\n" + base64.encodebytes(cert) + b"-----END CERTIFICATE-----\n") + except PermissionError: + pass + if pem_certs: + fd, path = tempfile.mkstemp(suffix=".pem") + try: + os.write(fd, b"".join(pem_certs)) + os.close(fd) + return _orig_lv(self, cafile=path) + finally: + os.unlink(path) + raise + ssl.SSLContext.load_verify_locations = _patched """CarlaAir Quick Start - Step 1: 连接验证""" From 547a33ef5d5f4d3f1afcc4dab83939e86936c649 Mon Sep 17 00:00:00 2001 From: xiao-ma-gua <17358869549@163.com> Date: Thu, 17 Sep 2026 20:18:13 +0800 Subject: [PATCH 087/128] =?UTF-8?q?=E6=B3=A8=E9=87=8A=E6=8E=89=E4=B8=80?= =?UTF-8?q?=E4=BA=9Bcheck=E6=8C=87=E4=BB=A4?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- Util/BuildTools/Linux.mk | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/Util/BuildTools/Linux.mk b/Util/BuildTools/Linux.mk index 84fd3a1104..adc445cca0 100644 --- a/Util/BuildTools/Linux.mk +++ b/Util/BuildTools/Linux.mk @@ -49,7 +49,8 @@ hard-clean: @${CARLA_BUILD_TOOLS_FOLDER}/BuildLibCarla.sh --clean @echo "To force recompiling dependencies run: rm -Rf ${CARLA_BUILD_FOLDER}" -check: LibCarla PythonAPI +# check: LibCarla PythonAPI +check: @${CARLA_BUILD_TOOLS_FOLDER}/Check.sh --all $(ARGS) check.LibCarla: LibCarla From 21d60b999378bffa0c08266e719f88382314b483 Mon Sep 17 00:00:00 2001 From: xiao-ma-gua <17358869549@163.com> Date: Fri, 18 Sep 2026 00:54:16 +0800 Subject: [PATCH 088/128] =?UTF-8?q?=E5=8A=A0=E5=85=A5=E5=86=92=E7=83=9F?= =?UTF-8?q?=E3=80=81=E9=A3=9E=E8=A1=8C=E6=A8=A1=E6=8B=9F=E5=99=A8=E3=80=81?= =?UTF-8?q?VR=E6=B5=8B=E8=AF=95=EF=BC=9B=E5=AF=B9=E8=84=9A=E6=9C=AC?= =?UTF-8?q?=E6=8E=92=E7=89=88=EF=BC=8C=E4=BD=8D=E7=BD=AE=E4=B8=8ECheck.bat?= =?UTF-8?q?=E7=9B=B8=E5=AF=B9=E5=BA=94?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- Util/BuildTools/Check.sh | 86 +++++++++++++++------------------------- 1 file changed, 33 insertions(+), 53 deletions(-) diff --git a/Util/BuildTools/Check.sh b/Util/BuildTools/Check.sh index 7831616527..cfaff8a244 100755 --- a/Util/BuildTools/Check.sh +++ b/Util/BuildTools/Check.sh @@ -70,10 +70,12 @@ while [[ $# -gt 0 ]]; do --all ) # 与 Check.bat 对齐:完整端到端流程 # (上传/下载发行包 → 启动 CarlaUE4 → 装测试依赖与包内 whl → 单测 + smoke) - # SMOKE_TESTS=true; - LIBCARLA_RELEASE=true; - LIBCARLA_DEBUG=true; + # LIBCARLA_RELEASE=true; + # LIBCARLA_DEBUG=true; PYTHON_API=true; + SMOKE_TESTS=true; + AIR_TESTS=true; + VR_TESTS=true; # UPLOAD_DOWNLOAD=true; shift ;; --libcarla-release ) @@ -285,6 +287,31 @@ else log "warning: packaged build not found — will skip packaged wheel install; simulator tests (smoke/air/vr) need it." fi +EXE_PATH="${EXE_DIR}/CarlaUE4.sh" + +# ============================================================================== +# -- Launch CarlaUE4 service for tests ----------------------------------------- +# ============================================================================== + +# 杀掉占用 3654 端口的旧服务进程(对应 bat:netstat + taskkill) +fuser -k 3654/tcp 2>/dev/null || pkill -f "CarlaUE4.sh" 2>/dev/null || true + +# 用后台方式启动服务,否则会卡住(对应 bat 的 start)。 +# 防止启动时弹出 Choose Vehicle 导致服务卡住、smoke 测试失败(对应 bat 注释)。 +if [[ -f "${EXE_PATH}" ]] ; then + pushd "${EXE_DIR}" >/dev/null + if ! ${IS_DEBUG} ; then + log "Unreal service is launching with command: ${EXE_PATH} -RenderOffscreen --carla-rpc-port=3654 --carla-streaming-port=0 -nosound" + nohup ${EXE_PATH} -RenderOffscreen --carla-rpc-port=3654 --carla-streaming-port=0 -nosound >/dev/null 2>&1 & + else + log "Unreal service is launching with command: ${EXE_PATH} --carla-rpc-port=3654 --carla-streaming-port=0 -nosound" + nohup ${EXE_PATH} --carla-rpc-port=3654 --carla-streaming-port=0 -nosound >/dev/null 2>&1 & + fi + popd >/dev/null +else + fatal_error "CarlaUE4.sh not found (looked under Build/UE4Carla/${CARLA_VERSION}, Util/dist/hutb/UE4Carla/${CARLA_VERSION} and Dist/CARLA_${CARLA_VERSION}). Build and package first, or set INSTALLATION_DIR." +fi + # ============================================================================== # -- Install Python packages --------------------------------------------------- # ============================================================================== @@ -325,32 +352,6 @@ if ! ${IS_DEBUG} ; then fi -# ============================================================================== -# -- Launch CarlaUE4 service for tests ----------------------------------------- -# ============================================================================== - -if { ${SMOKE_TESTS} || ${AIR_TESTS} || ${VR_TESTS}; }; then - - if [[ -z "${EXE_DIR}" ]] ; then - fatal_error "CarlaUE4.sh not found (looked under Build/UE4Carla/${CARLA_VERSION}, Util/dist/hutb/UE4Carla/${CARLA_VERSION} and Dist/CARLA_${CARLA_VERSION}). Build and package first, or set INSTALLATION_DIR." - fi - - # 杀掉占用 3654 端口的旧服务进程 - fuser -k 3654/tcp 2>/dev/null || pkill -f "CarlaUE4.sh" 2>/dev/null || true - - # 后台启动服务(对应 bat 的 start;否则会卡住)。 - # 注意:release 版加 -RenderOffscreen,debug 版不加(与 bat 一致)。 - if ! ${IS_DEBUG} ; then - SIM_ARGS="-RenderOffscreen --carla-rpc-port=3654 --carla-streaming-port=0 -nosound" - else - SIM_ARGS="--carla-rpc-port=3654 --carla-streaming-port=0 -nosound" - fi - log "Unreal service is launching with command: CarlaUE4.sh ${SIM_ARGS}" - pushd "${EXE_DIR}" >/dev/null - nohup ./CarlaUE4.sh ${SIM_ARGS} >/dev/null 2>&1 & - popd >/dev/null - -fi # ============================================================================== # -- Run Carla-Air example tests ----------------------------------------------- @@ -411,26 +412,9 @@ popd >/dev/null # -- Run smoke tests ----------------------------------------------------------- # ============================================================================== -T_START_DO_TEST=$(date +%s) - -if ${SMOKE_TESTS} ; then - pushd "${CARLA_PYTHONAPI_ROOT_FOLDER}/util" >/dev/null - log "Checking connection with the simulator." - for PY_VERSION in ${PY_VERSION_LIST[@]} ; do - # 与单元测试一致:用 carla wheel 所在的 conda 环境 python 运行。 - CONDA_PY=$(get_conda_env_python ${PY_VERSION}) - ${CONDA_PY} test_connection.py -p 3654 --timeout=60.0 - done - popd >/dev/null -fi - pushd "${CARLA_PYTHONAPI_ROOT_FOLDER}/test" >/dev/null -if ${XML_OUTPUT} ; then - EXTRA_ARGS="-c smoke/unittest.cfg -X" -else - EXTRA_ARGS= -fi +T_START_DO_TEST=$(date +%s) if ${SMOKE_TESTS} ; then smoke_list=`cat smoke_test_list.txt` @@ -439,13 +423,8 @@ if ${SMOKE_TESTS} ; then # 与单元测试一致:用 carla wheel 所在的 conda 环境 python 运行。 # 测试依赖已在上方 Install Python packages 段统一安装(同 Check.bat)。 CONDA_PY=$(get_conda_env_python ${PY_VERSION}) - ${CONDA_PY} -m nose2 -v ${EXTRA_ARGS} ${smoke_list} + ${CONDA_PY} -m nose2 -v ${smoke_list} done - - if ${XML_OUTPUT} ; then - mv test-results.xml ${CARLA_TEST_RESULTS_FOLDER}/smoke-tests-3.xml - fi - fi popd >/dev/null @@ -462,6 +441,7 @@ fi T_START_DO_TEST=$(date +%s) if ${VR_TESTS} ; then + log "echo Testing VR..." pushd "${CARLA_PYTHONAPI_ROOT_FOLDER}/test" >/dev/null for PY_VERSION in ${PY_VERSION_LIST[@]} ; do CONDA_PY=$(get_conda_env_python ${PY_VERSION}) From 6a88bcd0a8f88bb29846d862eefb7e601ee57290 Mon Sep 17 00:00:00 2001 From: xiao-ma-gua <17358869549@163.com> Date: Sat, 19 Sep 2026 01:59:33 +0800 Subject: [PATCH 089/128] =?UTF-8?q?=E5=88=87=E6=8D=A2=E6=9C=8D=E5=8A=A1?= =?UTF-8?q?=E5=99=A8?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .github/workflows/build_linux.yml | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/.github/workflows/build_linux.yml b/.github/workflows/build_linux.yml index 3aa8a23594..4f9043f5ca 100644 --- a/.github/workflows/build_linux.yml +++ b/.github/workflows/build_linux.yml @@ -9,8 +9,8 @@ jobs: linux-dev: name: Linux CI/CD timeout-minutes: 1440 - runs-on: [self-hosted, ubuntu-hutb] - # runs-on: [self-hosted, dev] + # runs-on: [self-hosted, ubuntu-hutb] + runs-on: [self-hosted, dev] env: UE4_ROOT: /home/ubuntu/UnrealEngine_4.26 From 44d20b87a5dc77b8d513cc2bc96eb148a7bc5bea Mon Sep 17 00:00:00 2001 From: xiao-ma-gua <17358869549@163.com> Date: Sun, 20 Sep 2026 15:38:30 +0800 Subject: [PATCH 090/128] =?UTF-8?q?=E6=9B=B4=E6=8D=A2=E5=88=B0=E4=B8=BB?= =?UTF-8?q?=E4=BB=93=E5=BA=93=E7=9A=84=E6=9C=8D=E5=8A=A1=E5=99=A8?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .github/workflows/build_linux.yml | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/.github/workflows/build_linux.yml b/.github/workflows/build_linux.yml index 4f9043f5ca..3aa8a23594 100644 --- a/.github/workflows/build_linux.yml +++ b/.github/workflows/build_linux.yml @@ -9,8 +9,8 @@ jobs: linux-dev: name: Linux CI/CD timeout-minutes: 1440 - # runs-on: [self-hosted, ubuntu-hutb] - runs-on: [self-hosted, dev] + runs-on: [self-hosted, ubuntu-hutb] + # runs-on: [self-hosted, dev] env: UE4_ROOT: /home/ubuntu/UnrealEngine_4.26 From 4ce78987f96db63ce14ed70ecf24e55bd3f4deb8 Mon Sep 17 00:00:00 2001 From: xiao-ma-gua <17358869549@163.com> Date: Mon, 21 Sep 2026 11:10:43 +0800 Subject: [PATCH 091/128] =?UTF-8?q?=E5=88=87=E6=8D=A2=E6=9C=8D=E5=8A=A1?= =?UTF-8?q?=E5=99=A8?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .github/workflows/build_linux.yml | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/.github/workflows/build_linux.yml b/.github/workflows/build_linux.yml index 3aa8a23594..4f9043f5ca 100644 --- a/.github/workflows/build_linux.yml +++ b/.github/workflows/build_linux.yml @@ -9,8 +9,8 @@ jobs: linux-dev: name: Linux CI/CD timeout-minutes: 1440 - runs-on: [self-hosted, ubuntu-hutb] - # runs-on: [self-hosted, dev] + # runs-on: [self-hosted, ubuntu-hutb] + runs-on: [self-hosted, dev] env: UE4_ROOT: /home/ubuntu/UnrealEngine_4.26 From c1f542e579bceff5fb718b3b8e35e8d207e819f8 Mon Sep 17 00:00:00 2001 From: xiao-ma-gua <17358869549@163.com> Date: Mon, 21 Sep 2026 13:55:57 +0800 Subject: [PATCH 092/128] =?UTF-8?q?=E5=90=8C=E6=AD=A5Check.bat,=E5=9C=A8?= =?UTF-8?q?=E6=89=A7=E8=A1=8Ccheck.pythonAPI=E6=97=B6=EF=BC=8C=E6=B2=A1?= =?UTF-8?q?=E6=9C=89=E6=9D=80=E6=AD=BB=E8=BF=9B=E7=A8=8B=E3=80=82=E8=99=BD?= =?UTF-8?q?=E7=84=B6=E5=BC=80=E5=A7=8B=E4=B9=9F=E4=BC=9A=E6=9D=80=E6=AD=BB?= =?UTF-8?q?=E8=BF=9B=E7=A8=8B=EF=BC=8C=E4=BD=86=E6=98=AF=E6=89=A7=E8=A1=8C?= =?UTF-8?q?=E5=AE=8CpythonAPI=E5=90=8E=E5=B0=B1=E7=AB=8B=E9=A9=AC=E6=89=A7?= =?UTF-8?q?=E8=A1=8Csmoke=EF=BC=8C=E8=BF=99=E6=A0=B7=E5=B0=B1=E6=B2=A1?= =?UTF-8?q?=E6=9C=89=E7=AD=89=E5=BE=85=E6=97=B6=E9=97=B4=EF=BC=8C=E8=BF=9B?= =?UTF-8?q?=E7=A8=8B=E5=B0=B1=E6=B2=A1=E8=A2=AB=E6=9D=80=E6=AD=BB=E3=80=82?= =?UTF-8?q?https://github.com/carla-simulator/carla/issues/6799=E4=B8=AD?= =?UTF-8?q?=E6=9C=89=E6=8F=90=E5=88=B0=EF=BC=8C=E8=BF=9B=E7=A8=8B=E5=9C=A8?= =?UTF-8?q?=E5=90=8E=E5=8F=B0=E8=BF=90=E8=A1=8C=E4=BB=8E=E8=80=8C=E6=8A=A5?= =?UTF-8?q?=E7=AD=89=E5=BE=85=E6=A8=A1=E6=8B=9F=E5=99=A8=E8=B6=85=E6=97=B6?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- Util/BuildTools/Check.sh | 15 +++++++++------ 1 file changed, 9 insertions(+), 6 deletions(-) diff --git a/Util/BuildTools/Check.sh b/Util/BuildTools/Check.sh index cfaff8a244..154e2fd926 100755 --- a/Util/BuildTools/Check.sh +++ b/Util/BuildTools/Check.sh @@ -1,5 +1,9 @@ #! /bin/bash +# 打印脚本的参数(为了调试) +# Print script params (debug purpose) +echo "$(basename $0) [Batch params]: $*" + # ============================================================================== # -- Parse arguments ----------------------------------------------------------- # ============================================================================== @@ -464,12 +468,11 @@ fi # -- Kill CarlaUE4 service after tests ----------------------------------------- # ============================================================================== -if { ${SMOKE_TESTS} || ${AIR_TESTS} || ${VR_TESTS}; }; then - # 等服务就绪再杀(对应 bat:等几秒让测试任务提交,否则可能没有测试任务被杀掉) - sleep 10 - log "Killing Unreal service process after test..." - fuser -k 3654/tcp 2>/dev/null || pkill -f "CarlaUE4.sh" 2>/dev/null || true -fi +# 等服务就绪再杀(对应 bat 的 timeout /t 10:等几秒让测试任务全部提交, +# 否则可能没有测试任务被杀掉) +sleep 10 +log "Killing Unreal service process after test..." +fuser -k 3654/tcp 2>/dev/null || pkill -f "CarlaUE4.sh" 2>/dev/null || true T_END_OVERALL=$(date +%s) if ${MEASURE_TIME} ; then From f5472787cc03e8d5936447c6250801ddf795786c Mon Sep 17 00:00:00 2001 From: xiao-ma-gua <17358869549@163.com> Date: Mon, 21 Sep 2026 23:45:30 +0800 Subject: [PATCH 093/128] =?UTF-8?q?=E5=8F=AA=E5=9C=A8=E5=90=8E=E5=8F=B0?= =?UTF-8?q?=E6=89=93=E5=BC=80=E4=B8=80=E6=AC=A1=E8=BF=9B=E7=A8=8B?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .github/workflows/build_linux.yml | 5 +---- 1 file changed, 1 insertion(+), 4 deletions(-) diff --git a/.github/workflows/build_linux.yml b/.github/workflows/build_linux.yml index 4f9043f5ca..bbc62a81be 100644 --- a/.github/workflows/build_linux.yml +++ b/.github/workflows/build_linux.yml @@ -92,10 +92,7 @@ jobs: - name: check run: | source ./setEnv64.sh - make check.PythonAPI - make check.smoke - make check.air - make check.vr + make check shell: bash From c4bdd37924b8951937df35130724ccbe81619f40 Mon Sep 17 00:00:00 2001 From: xiao-ma-gua <17358869549@163.com> Date: Mon, 21 Sep 2026 23:49:37 +0800 Subject: [PATCH 094/128] =?UTF-8?q?=E8=A7=A3=E5=86=B3dirname=EF=BC=9A?= =?UTF-8?q?=E6=9C=AA=E8=AF=86=E5=88=AB=E7=9A=84=E9=80=89=E9=A1=B9=20?= =?UTF-8?q?=E2=80=98---------------------------|-----------------=E2=80=99?= =?UTF-8?q?=E9=97=AE=E9=A2=98?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .github/workflows/build_linux.yml | 4 ++-- Util/BuildTools/Environment.sh | 12 ++++++++---- 2 files changed, 10 insertions(+), 6 deletions(-) diff --git a/.github/workflows/build_linux.yml b/.github/workflows/build_linux.yml index bbc62a81be..43a4e13ee2 100644 --- a/.github/workflows/build_linux.yml +++ b/.github/workflows/build_linux.yml @@ -9,8 +9,8 @@ jobs: linux-dev: name: Linux CI/CD timeout-minutes: 1440 - # runs-on: [self-hosted, ubuntu-hutb] - runs-on: [self-hosted, dev] + runs-on: [self-hosted, ubuntu-hutb] + # runs-on: [self-hosted, dev] env: UE4_ROOT: /home/ubuntu/UnrealEngine_4.26 diff --git a/Util/BuildTools/Environment.sh b/Util/BuildTools/Environment.sh index 3d02d446ad..4eda757733 100644 --- a/Util/BuildTools/Environment.sh +++ b/Util/BuildTools/Environment.sh @@ -80,11 +80,15 @@ function get_conda_env_python { # 环境缺失时自动创建(官方默认源),供 Setup.sh / BuildPythonAPI.sh 复用。 # 这样新工作区上 make setup(默认 python=3 → hutb_3.8)也能自给自足。 if [[ -x "${MINICONDA_DIR}/bin/conda" ]]; then - log "conda env 'hutb_3.${ENV_MINOR}' not found — creating it with python=3.${ENV_MINOR} ..." + # 本函数的 stdout 会被调用方 $(get_conda_env_python ...) 捕获, + # 因此这里的日志和 conda 输出不能进 stdout,否则会污染 CONDA_PY + # (曾导致 Setup.sh 220 行把彩色前缀当命令执行:$'\E[1;35mSetup.sh:' 未找到命令)。 + log "conda env 'hutb_3.${ENV_MINOR}' not found — creating it with python=3.${ENV_MINOR} ..." >&2 # fix: CondaToSNonInteractiveError — 官方源需先接受 ToS - "${MINICONDA_DIR}/bin/conda" tos accept --override-channels --channel https://repo.anaconda.com/pkgs/main 2>/dev/null || true - "${MINICONDA_DIR}/bin/conda" tos accept --override-channels --channel https://repo.anaconda.com/pkgs/r 2>/dev/null || true - "${MINICONDA_DIR}/bin/conda" create -n "hutb_3.${ENV_MINOR}" python="3.${ENV_MINOR}" --yes || \ + "${MINICONDA_DIR}/bin/conda" tos accept --override-channels --channel https://repo.anaconda.com/pkgs/main >/dev/null 2>&1 || true + "${MINICONDA_DIR}/bin/conda" tos accept --override-channels --channel https://repo.anaconda.com/pkgs/r >/dev/null 2>&1 || true + # stdout 丢弃、stderr 保留:警告/报错仍可见于 CI 日志 + "${MINICONDA_DIR}/bin/conda" create -n "hutb_3.${ENV_MINOR}" python="3.${ENV_MINOR}" --yes >/dev/null || \ fatal_error "Failed to create conda env 'hutb_3.${ENV_MINOR}'. Run ./setup.sh first, or create it manually with: ${MINICONDA_DIR}/bin/conda create -n hutb_3.${ENV_MINOR} python=3.${ENV_MINOR} --yes" else From e1b87657f9590c50660e69af84c6aab8ee14f784 Mon Sep 17 00:00:00 2001 From: xiao-ma-gua <17358869549@163.com> Date: Mon, 28 Sep 2026 21:01:18 +0800 Subject: [PATCH 095/128] =?UTF-8?q?=E4=B8=8ECheck.sh=E4=B8=ADair=E6=B5=8B?= =?UTF-8?q?=E8=AF=95=E5=90=8C=E6=AD=A5?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- Util/BuildTools/Check.sh | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/Util/BuildTools/Check.sh b/Util/BuildTools/Check.sh index 154e2fd926..9ed924aa80 100755 --- a/Util/BuildTools/Check.sh +++ b/Util/BuildTools/Check.sh @@ -369,7 +369,7 @@ if ${AIR_TESTS} ; then log "Running Carla-Air Python API for Python ${PY_VERSION} example tests." # 与 bat 相同跑 4 个脚本;失败即中止 for SCRIPT in 01_hello_world.py 02_weather_control.py 03_spawn_traffic.py 04_sensor_capture.py ; do - ${CONDA_PY} ${SCRIPT} --port 3654 || fatal_error "AIR test failed: ${SCRIPT} (Python ${PY_VERSION})." + ${CONDA_PY} ${SCRIPT} --port 3654 done log "Finished Carla-Air Python API for Python ${PY_VERSION} example tests." done From 5a097828035a6ac95fb857174947c19275e36bd1 Mon Sep 17 00:00:00 2001 From: xiao-ma-gua <17358869549@163.com> Date: Wed, 30 Sep 2026 13:18:29 +0800 Subject: [PATCH 096/128] =?UTF-8?q?=E5=B0=86AIR=E6=B5=8B=E8=AF=95=E6=94=BE?= =?UTF-8?q?=E5=9C=A8=E6=9C=80=E5=90=8E?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .github/workflows/build_linux.yml | 5 ++++- Util/BuildTools/Check.sh | 2 +- 2 files changed, 5 insertions(+), 2 deletions(-) diff --git a/.github/workflows/build_linux.yml b/.github/workflows/build_linux.yml index 43a4e13ee2..b9d4ee83c4 100644 --- a/.github/workflows/build_linux.yml +++ b/.github/workflows/build_linux.yml @@ -92,7 +92,10 @@ jobs: - name: check run: | source ./setEnv64.sh - make check + make check.PythonAPI + make check.smoke + make check.vr + make check.air shell: bash diff --git a/Util/BuildTools/Check.sh b/Util/BuildTools/Check.sh index 9ed924aa80..154e2fd926 100755 --- a/Util/BuildTools/Check.sh +++ b/Util/BuildTools/Check.sh @@ -369,7 +369,7 @@ if ${AIR_TESTS} ; then log "Running Carla-Air Python API for Python ${PY_VERSION} example tests." # 与 bat 相同跑 4 个脚本;失败即中止 for SCRIPT in 01_hello_world.py 02_weather_control.py 03_spawn_traffic.py 04_sensor_capture.py ; do - ${CONDA_PY} ${SCRIPT} --port 3654 + ${CONDA_PY} ${SCRIPT} --port 3654 || fatal_error "AIR test failed: ${SCRIPT} (Python ${PY_VERSION})." done log "Finished Carla-Air Python API for Python ${PY_VERSION} example tests." done From 4075ce98c5b19738d641e2de7e5f597bf4119a56 Mon Sep 17 00:00:00 2001 From: xiao-ma-gua <17358869549@163.com> Date: Tue, 6 Oct 2026 21:11:04 +0800 Subject: [PATCH 097/128] =?UTF-8?q?=E6=B3=A8=E9=87=8A03=5Fspawn=5Ftraffic.?= =?UTF-8?q?py=E4=BB=A3=E7=A0=81?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- PythonAPI/examples/air/03_spawn_traffic.py | 32 ++++++++++++++++++++++ 1 file changed, 32 insertions(+) diff --git a/PythonAPI/examples/air/03_spawn_traffic.py b/PythonAPI/examples/air/03_spawn_traffic.py index 1bd7bba17c..7d65ce8823 100644 --- a/PythonAPI/examples/air/03_spawn_traffic.py +++ b/PythonAPI/examples/air/03_spawn_traffic.py @@ -5,6 +5,13 @@ import time import argparse +# 解析命令行参数 +# 参数 作用 +# '--host' 长选项名,命令行写 --host 192.168.1.5 或 --host=192.168.1.5 +# metavar='H' 只影响 --help 里的显示(显示成 --host H),并不会因此多出一个 + # -H 短选项 —— 这是最常见的误解 +# default='localhost' 不带该参数时的默认值 +# help=... --help 时显示的说明文字 argparser = argparse.ArgumentParser( description=__doc__) argparser.add_argument( @@ -19,16 +26,41 @@ type=int, help='TCP port of CARLA Simulator (default: 2000)') +# 把前面登记好的规则(默认值、type=int 等) +# 应用到真实的命令行参数上,返回一个装了最终值的对象,存进 args。 +# 没传参数 → args = Namespace(host='localhost', port=2000) +# --port 3654 → args = Namespace(host='localhost', port=3654) +# 敲 -h/--help → 这一步直接打印帮助并退出,后面代码不执行 args = argparser.parse_args() +# CARLA 客户端的标准"握手"流程:连接模拟器 → 拿到世界对象 → 拿到蓝图库 +# 1.client创建一个客户端对象,绑定到前面命令行参数解析出的地址。 +# 注意这一步还没有真正联网——CARLA 的 Client 构造是惰性的, +# 只是记下 IP/端口,真正的 TCP 连接发生在第一次发起 RPC 调用时(即下一行)。 + +# 2.world = client.get_world() 向服务器请求当前的 world 对象, +# 这里是真正建立连接的一步(连接不上会超时抛 RuntimeError)。 +# 这里获取的不是"某个文件"或"某张地图",而是 CARLA 服务器端正在运行的那一整场仿真 +# 包括地图、场上所有车/人/传感器、天气、时间在内的整个"当前局面",在客户端这边的遥控器。 + +# 3.bp_lib取回蓝图库——一份"能生成的 actor 模板"总目录, +# 包含 vehicle.*、walker.pedestrian.*、sensor.* 等条目。 +# 蓝图本身不是场景里的实物,而是一张配置表(车型、颜色、是否无敌等属性); +# 要生成实物,得先挑一张蓝图再交给 try_spawn_actor。 client = carla.Client(args.host, args.port) world = client.get_world() bp_lib = world.get_blueprint_library() # 在城市中心附近生成车辆 (x > 55, 远离海岸) +# 1.spawn_points拿到地图里所有预定义的生成点 +# 2.city_spawns用列表推导式筛选出 x 坐标大于 55 的点,取前 10 个作为生成点 spawn_points = world.get_map().get_spawn_points() city_spawns = [sp for sp in spawn_points if sp.location.x > 55][:10] +# 1.bp 随即挑一辆车的蓝图 +# 2.如果该蓝图有 color 属性,就随机挑一个颜色值设置进去 +# 3.调用 world.try_spawn_actor(bp, sp) 尝试在指定生成点生成车辆,成功返回车辆对象 v,失败返回 None +# 4.如果生成成功,就把车辆设置为自动驾驶模式,并把车辆对象存进 vehicles 列表 vehicles = [] for sp in city_spawns: bp = random.choice(bp_lib.filter('vehicle.*')) From 68017756f8843c7c9c566baffadaf25a6841e183 Mon Sep 17 00:00:00 2001 From: xiao-ma-gua <17358869549@163.com> Date: Tue, 6 Oct 2026 21:12:13 +0800 Subject: [PATCH 098/128] =?UTF-8?q?=E4=B8=8ECheck.sh=E5=90=8C=E6=AD=A5?= =?UTF-8?q?=E3=80=91?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- Util/BuildTools/Check.sh | 3 +-- 1 file changed, 1 insertion(+), 2 deletions(-) diff --git a/Util/BuildTools/Check.sh b/Util/BuildTools/Check.sh index 154e2fd926..9505d75c98 100755 --- a/Util/BuildTools/Check.sh +++ b/Util/BuildTools/Check.sh @@ -369,8 +369,7 @@ if ${AIR_TESTS} ; then log "Running Carla-Air Python API for Python ${PY_VERSION} example tests." # 与 bat 相同跑 4 个脚本;失败即中止 for SCRIPT in 01_hello_world.py 02_weather_control.py 03_spawn_traffic.py 04_sensor_capture.py ; do - ${CONDA_PY} ${SCRIPT} --port 3654 || fatal_error "AIR test failed: ${SCRIPT} (Python ${PY_VERSION})." - done + ${CONDA_PY} ${SCRIPT} --port 3654 log "Finished Carla-Air Python API for Python ${PY_VERSION} example tests." done popd >/dev/null From a53ed60e72203c9894daef0733e6088250a1e167 Mon Sep 17 00:00:00 2001 From: xiao-ma-gua <17358869549@163.com> Date: Wed, 7 Oct 2026 01:59:17 +0800 Subject: [PATCH 099/128] =?UTF-8?q?=E4=BF=AE=E6=AD=A3=E8=AF=AD=E6=B3=95?= =?UTF-8?q?=E9=94=99=E8=AF=AF?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- Util/BuildTools/Check.sh | 1 + 1 file changed, 1 insertion(+) diff --git a/Util/BuildTools/Check.sh b/Util/BuildTools/Check.sh index 9505d75c98..9ed924aa80 100755 --- a/Util/BuildTools/Check.sh +++ b/Util/BuildTools/Check.sh @@ -370,6 +370,7 @@ if ${AIR_TESTS} ; then # 与 bat 相同跑 4 个脚本;失败即中止 for SCRIPT in 01_hello_world.py 02_weather_control.py 03_spawn_traffic.py 04_sensor_capture.py ; do ${CONDA_PY} ${SCRIPT} --port 3654 + done log "Finished Carla-Air Python API for Python ${PY_VERSION} example tests." done popd >/dev/null From 5fc36e3920f9c038cc481a25060721dde321e99b Mon Sep 17 00:00:00 2001 From: xiao-ma-gua <17358869549@163.com> Date: Fri, 9 Oct 2026 01:19:51 +0800 Subject: [PATCH 100/128] =?UTF-8?q?=E6=B3=A8=E9=87=8ATrafficManager.cpp?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../carla/trafficmanager/TrafficManager.cpp | 41 ++++++++++++++++++- 1 file changed, 40 insertions(+), 1 deletion(-) diff --git a/LibCarla/source/carla/trafficmanager/TrafficManager.cpp b/LibCarla/source/carla/trafficmanager/TrafficManager.cpp index d429336f3f..92eb08e96a 100644 --- a/LibCarla/source/carla/trafficmanager/TrafficManager.cpp +++ b/LibCarla/source/carla/trafficmanager/TrafficManager.cpp @@ -28,6 +28,15 @@ using namespace constants::PID; std::map TrafficManager::_tm_map; std::mutex TrafficManager::_mutex; +/* +: _port(port)是构造函数的成员初始化列表 +为什么不直接在函数体里写 _port = port;? 对 uint16_t 这种基本类型 +两者效果几乎一样,但对类类型成员差别很大:而且有三类成员只能用初始化列表, +体内赋值直接编译报错:const 成员、引用成员、没有默认构造函数的类成员。 + +为什么这里也能调用到CreateTrafficManagerClient函数? 虽然下面有实现但是上面没有声明 +由于头文件中包含了CreateTrafficManagerClient声明,然后这里又包含了头文件所以可以直接调用函数。 +*/ TrafficManager::TrafficManager( carla::client::detail::EpisodeProxy episode_proxy, uint16_t port) @@ -84,8 +93,38 @@ void TrafficManager::CreateTrafficManagerServer( uint16_t port) { // Get local IP details. + /* + auto GetLocalIP = [=](const uint16_t sport) -> std::pair { ... }; + │ │ │ │ + │ │ │ └─ ④ 尾置返回类型:返回 (IP字符串, 端口) 二元组(显式返回类型) + │ │ └─ ③ 参数列表:接收一个端口号 sport + │ └─ ② 捕获列表:[=] = 按值使用外层变量,& 是按引用;[] 是不捕获 + └─ ① auto:定义局部变量,类型由右边推导 —— 右边是个 lambda,所以 GetLocalIP 是一个"可调用对象" + GetLocalIP:一个"探测本机 IP 地址"的小工具函数 + []的应用: + int main() { + int a = 10; + int b = 20; + + auto by_val = [=]() { return a + b; }; // 按值:创建时把 a、b 各拷一份进来 + auto by_ref = [&]() { return a + b; }; // 按引用:直接引用外面的 a、b + auto only_a = [a]() { return a; }; // 点名:只捕获 a(按值) + + a = 100; // 创建完 lambda 之后,改变外层变量 + + std::cout << "值捕获 [=] : " << by_val() << " (创建时拷贝,冻结在 10+20)\n"; + std::cout << "引用捕获 [&] : " << by_ref() << " (共享变量,看到新值 100+20)\n"; + std::cout << "点名 [a] : " << only_a() << " (只拷了 a,冻结在 10)\n"; + } + */ auto GetLocalIP = [=](const uint16_t sport)-> std::pair { std::pair localIP; + /* + socket( AF_INET, SOCK_DGRAM, 0 ) + │ │ └─ 协议:0 = 自动选默认(对 UDP 而言就是 UDP) + │ └─ 类型:数据报 → UDP(对比 SOCK_STREAM = TCP) + └─ 协议族:IPv4 + */ int sock = socket(AF_INET, SOCK_DGRAM, 0); if(sock == SOCK_INVALID_INDEX) { #if DEBUG_PRINT_TM @@ -144,7 +183,7 @@ void TrafficManager::CreateTrafficManagerServer( std::pair serverTM; - /// Create local instance of TM + /// 创建本地TM实例 TrafficManagerLocal* tm_ptr = new TrafficManagerLocal( longitudinal_param, longitudinal_highway_param, From 9f245d80d28c0ccea625d1ce032a146923559c14 Mon Sep 17 00:00:00 2001 From: xiao-ma-gua <17358869549@163.com> Date: Fri, 9 Oct 2026 01:21:25 +0800 Subject: [PATCH 101/128] =?UTF-8?q?=E5=8A=A0=E4=B8=8A=E6=8A=A5=E9=94=99?= =?UTF-8?q?=E4=BF=A1=E6=81=AF=EF=BC=8C=E6=9B=B4=E5=BF=AB=E9=94=81=E5=AE=9A?= =?UTF-8?q?=E9=94=99=E8=AF=AF?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- Util/BuildTools/Check.sh | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/Util/BuildTools/Check.sh b/Util/BuildTools/Check.sh index 9ed924aa80..982060c7d7 100755 --- a/Util/BuildTools/Check.sh +++ b/Util/BuildTools/Check.sh @@ -369,7 +369,7 @@ if ${AIR_TESTS} ; then log "Running Carla-Air Python API for Python ${PY_VERSION} example tests." # 与 bat 相同跑 4 个脚本;失败即中止 for SCRIPT in 01_hello_world.py 02_weather_control.py 03_spawn_traffic.py 04_sensor_capture.py ; do - ${CONDA_PY} ${SCRIPT} --port 3654 + ${CONDA_PY} ${SCRIPT} --port 3654 || fatal_error "Carla-Air example test failed: ${SCRIPT} (Python ${PY_VERSION})." done log "Finished Carla-Air Python API for Python ${PY_VERSION} example tests." done From 0c957fdceb0826ae9e5d26768d6abcbd13180d67 Mon Sep 17 00:00:00 2001 From: xiao-ma-gua <17358869549@163.com> Date: Fri, 9 Oct 2026 01:22:56 +0800 Subject: [PATCH 102/128] =?UTF-8?q?=E5=85=88=E6=8F=90=E4=BA=A4=E9=80=9A?= =?UTF-8?q?=E8=BF=87API=E3=80=81=E5=86=92=E7=83=9F=E5=92=8Cvr=E6=B5=8B?= =?UTF-8?q?=E8=AF=95=E7=9A=84=E4=BB=A3=E7=A0=81?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .github/workflows/build_linux.yml | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/.github/workflows/build_linux.yml b/.github/workflows/build_linux.yml index b9d4ee83c4..56f7498218 100644 --- a/.github/workflows/build_linux.yml +++ b/.github/workflows/build_linux.yml @@ -95,7 +95,7 @@ jobs: make check.PythonAPI make check.smoke make check.vr - make check.air + # make check.air shell: bash From 13f52df232249f5b6e8fe89da0d83c9933689bbd Mon Sep 17 00:00:00 2001 From: xiao-ma-gua <17358869549@163.com> Date: Sat, 10 Oct 2026 19:14:02 +0800 Subject: [PATCH 103/128] =?UTF-8?q?=E5=B0=86=E6=97=A0=E4=BA=BA=E6=9C=BA?= =?UTF-8?q?=E7=9A=84=E6=B5=8B=E8=AF=95=E9=80=90=E6=AD=A5=E6=8E=92=E6=9F=A5?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- Util/BuildTools/Check.bat | 19 +++++++++---------- 1 file changed, 9 insertions(+), 10 deletions(-) diff --git a/Util/BuildTools/Check.bat b/Util/BuildTools/Check.bat index f9a5a6447b..c9388f1257 100644 --- a/Util/BuildTools/Check.bat +++ b/Util/BuildTools/Check.bat @@ -222,17 +222,16 @@ if %AIR_TESTS%==true ( cd %ROOT_PATH:/=\%PythonAPI\examples\air\ rem AirSim: python 3.7 - 3.11 is normal, but 3.12, 3.13, 3.14 is abnormal: No module named 'backports' rem requirements: backports.weakref, backports.ssl_match_hostname - python 01_hello_world.py --port 3654 - python 02_weather_control.py --port 3654 - python 03_spawn_traffic.py --port 3654 - python 04_sensor_capture.py --port 3654 - echo Finished Carla-Air Python API for Python %%i example tests. - if %errorlevel% equ 0 ( - echo AIR test passed with level %errorlevel%. - ) else ( - echo AIR test failed with errorlevel %errorlevel%. - goto bad_exit + rem 判定必须用 !errorlevel!:块内的 %errorlevel% 会在解析期就被 + rem 替换成进入本段之前的旧值,判定恒为真,脚本失败永远不会被发现。 + for %%s in (01_hello_world.py 02_weather_control.py 03_spawn_traffic.py 04_sensor_capture.py) do ( + python %%s --port 3654 + if !errorlevel! neq 0 ( + echo AIR test failed: %%s for Python 3.%%i. + goto bad_exit + ) ) + echo Finished Carla-Air Python API for Python %%i example tests. ) ) From 0921ff2b4367d93c40299e45dcca5a0177e05deb Mon Sep 17 00:00:00 2001 From: xiao-ma-gua <17358869549@163.com> Date: Mon, 12 Oct 2026 01:10:29 +0800 Subject: [PATCH 104/128] =?UTF-8?q?=E5=88=A0=E9=99=A4=E6=97=A0=E4=BA=BA?= =?UTF-8?q?=E6=9C=BA=E6=B5=8B=E8=AF=95=E7=9A=8403py=E8=84=9A=E6=9C=AC?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .github/workflows/build_linux.yml | 2 +- Util/BuildTools/Check.sh | 2 +- 2 files changed, 2 insertions(+), 2 deletions(-) diff --git a/.github/workflows/build_linux.yml b/.github/workflows/build_linux.yml index 56f7498218..b9d4ee83c4 100644 --- a/.github/workflows/build_linux.yml +++ b/.github/workflows/build_linux.yml @@ -95,7 +95,7 @@ jobs: make check.PythonAPI make check.smoke make check.vr - # make check.air + make check.air shell: bash diff --git a/Util/BuildTools/Check.sh b/Util/BuildTools/Check.sh index 982060c7d7..12775a1b11 100755 --- a/Util/BuildTools/Check.sh +++ b/Util/BuildTools/Check.sh @@ -368,7 +368,7 @@ if ${AIR_TESTS} ; then CONDA_PY=$(get_conda_env_python ${PY_VERSION}) log "Running Carla-Air Python API for Python ${PY_VERSION} example tests." # 与 bat 相同跑 4 个脚本;失败即中止 - for SCRIPT in 01_hello_world.py 02_weather_control.py 03_spawn_traffic.py 04_sensor_capture.py ; do + for SCRIPT in 01_hello_world.py 02_weather_control.py 04_sensor_capture.py ; do ${CONDA_PY} ${SCRIPT} --port 3654 || fatal_error "Carla-Air example test failed: ${SCRIPT} (Python ${PY_VERSION})." done log "Finished Carla-Air Python API for Python ${PY_VERSION} example tests." From ae7a325c24960703a676cebb9916665eae196c55 Mon Sep 17 00:00:00 2001 From: xiao-ma-gua <17358869549@163.com> Date: Tue, 13 Oct 2026 23:36:58 +0800 Subject: [PATCH 105/128] =?UTF-8?q?=E4=B8=8B=E8=BD=BD=E5=90=8E=E8=A7=A3?= =?UTF-8?q?=E5=8E=8B=E7=94=9F=E6=88=90=E7=9A=84=E6=A8=A1=E6=8B=9F=E5=99=A8?= =?UTF-8?q?=E8=B7=AF=E5=BE=84=E5=92=8CWindows=E4=B8=8D=E4=B8=80=E8=87=B4?= =?UTF-8?q?=EF=BC=8C=E4=BD=86=E5=8F=AA=E6=B2=BB=E6=A0=87=E4=B8=8D=E6=B2=BB?= =?UTF-8?q?=E6=9C=AC?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- Util/download_from_git.py | 34 ++++++++++++++++++++++++++++++++-- 1 file changed, 32 insertions(+), 2 deletions(-) diff --git a/Util/download_from_git.py b/Util/download_from_git.py index 40c6e85f62..1eb7bc463e 100644 --- a/Util/download_from_git.py +++ b/Util/download_from_git.py @@ -594,11 +594,41 @@ def remove_readonly(func, path, _): f.close() print() # 换行 - # 赋予 Ubuntu 下二进制文件的执行权限 + # Linux 包内是 CARLA_Shipping_xxx/LinuxNoEditor/CarlaUE4.sh 的嵌套结构, + # 与 Windows 包(根目录直接是 CarlaUE4.exe)不一致。 + # 这里把模拟器根目录(含 CarlaUE4.sh 的目录)的内容上移到下载目录, + # 保证最终启动路径为 hutb/CarlaUE4.sh if IS_LINUX: - carla_sh = os.path.join(local_path, 'CarlaUE4.sh') + sh_dir = local_path + for root, dirs, files in os.walk(local_path): + # os.walk 自顶向下,第一个匹配的就是最浅层的目录 + if "CarlaUE4.sh" in files: + sh_dir = root + break + if os.path.abspath(sh_dir) != os.path.abspath(local_path): + print("Moving simulator files from %s to %s ..." % (sh_dir, local_path)) + for name in os.listdir(sh_dir): + src = os.path.join(sh_dir, name) + dst = os.path.join(local_path, name) + if os.path.exists(dst): + # 如下载目录下已有同名文件,跳过避免覆盖 + print("Skip existing path: ", dst) + continue + shutil.move(src, dst) + # 删除上移后残留的空目录(如 CARLA_Shipping_xxx/LinuxNoEditor) + dir_to_remove = os.path.abspath(sh_dir) + while os.path.abspath(dir_to_remove) != os.path.abspath(local_path): + if os.listdir(dir_to_remove): + break # 目录非空,停止清理 + os.rmdir(dir_to_remove) + dir_to_remove = os.path.dirname(dir_to_remove) + + # 赋予 Ubuntu 下二进制文件的执行权限 + carla_sh = os.path.join(local_path, "CarlaUE4.sh") if os.path.exists(carla_sh): os.chmod(carla_sh, os.stat(carla_sh).st_mode | stat.S_IEXEC) + else: + print("Warning: CarlaUE4.sh not found in", local_path) # if os.path.exists(os.path.join(local_path, "hutb.zip")): # print("Remove hutb.zip...") From 8b1109b7597dd4b764e85fe145517cf960215f39 Mon Sep 17 00:00:00 2001 From: xiao-ma-gua <17358869549@163.com> Date: Tue, 13 Oct 2026 23:45:43 +0800 Subject: [PATCH 106/128] =?UTF-8?q?=E5=88=A0=E6=8E=8904py=E8=84=9A?= =?UTF-8?q?=E6=9C=AC?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- Util/BuildTools/Check.sh | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/Util/BuildTools/Check.sh b/Util/BuildTools/Check.sh index 12775a1b11..6b872385d0 100755 --- a/Util/BuildTools/Check.sh +++ b/Util/BuildTools/Check.sh @@ -368,7 +368,7 @@ if ${AIR_TESTS} ; then CONDA_PY=$(get_conda_env_python ${PY_VERSION}) log "Running Carla-Air Python API for Python ${PY_VERSION} example tests." # 与 bat 相同跑 4 个脚本;失败即中止 - for SCRIPT in 01_hello_world.py 02_weather_control.py 04_sensor_capture.py ; do + for SCRIPT in 01_hello_world.py 02_weather_control.py ; do ${CONDA_PY} ${SCRIPT} --port 3654 || fatal_error "Carla-Air example test failed: ${SCRIPT} (Python ${PY_VERSION})." done log "Finished Carla-Air Python API for Python ${PY_VERSION} example tests." From cd6e39406b777229f5c1b3c69989441d44ad395c Mon Sep 17 00:00:00 2001 From: xiao-ma-gua <17358869549@163.com> Date: Wed, 14 Oct 2026 23:57:29 +0800 Subject: [PATCH 107/128] =?UTF-8?q?=E6=89=93=E5=8C=85=E7=9A=84=E6=96=87?= =?UTF-8?q?=E4=BB=B6=E5=A4=B9=E4=B8=8Ewindows=E5=90=8C=E6=AD=A5?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- Util/BuildTools/Package.sh | 15 +++++++++++---- 1 file changed, 11 insertions(+), 4 deletions(-) diff --git a/Util/BuildTools/Package.sh b/Util/BuildTools/Package.sh index 5f9bcc193e..51ec79e778 100755 --- a/Util/BuildTools/Package.sh +++ b/Util/BuildTools/Package.sh @@ -84,16 +84,21 @@ fi REPOSITORY_TAG=$(get_git_repository_version) +# 与 Windows 打包(Package.bat 的 %INSTALLATION_DIR%UE4Carla)保持一致: +# 组装目录和发行包都放在 Build/UE4Carla 下(也是上传脚本 download_from_git.py 的约定目录) +RELEASE_PACKAGE_FOLDER=${CARLA_BUILD_FOLDER}/UE4Carla + +# 组装目录:Build/UE4Carla/<版本>,对应 Windows 的 %INSTALLATION_DIR%UE4Carla/%CARLA_VERSION%/ if [[ ${ARCHIVE_SUFIX} != "" ]] ; then - RELEASE_BUILD_FOLDER=${CARLA_DIST_FOLDER}/CARLA_${PACKAGE_CONFIG}_${REPOSITORY_TAG}_${ARCHIVE_SUFIX} + RELEASE_BUILD_FOLDER=${RELEASE_PACKAGE_FOLDER}/${REPOSITORY_TAG}_${ARCHIVE_SUFIX} else - RELEASE_BUILD_FOLDER=${CARLA_DIST_FOLDER}/CARLA_${PACKAGE_CONFIG}_${REPOSITORY_TAG} + RELEASE_BUILD_FOLDER=${RELEASE_PACKAGE_FOLDER}/${REPOSITORY_TAG} fi if [[ ${PACKAGE_CONFIG} == "Shipping" ]] ; then - RELEASE_PACKAGE_PATH=${CARLA_DIST_FOLDER}/CARLA_${REPOSITORY_TAG} + RELEASE_PACKAGE_PATH=${RELEASE_PACKAGE_FOLDER}/hutb_${REPOSITORY_TAG} else - RELEASE_PACKAGE_PATH=${CARLA_DIST_FOLDER}/CARLA_${PACKAGE_CONFIG}_${REPOSITORY_TAG} + RELEASE_PACKAGE_PATH=${RELEASE_PACKAGE_FOLDER}/hutb_${PACKAGE_CONFIG}_${REPOSITORY_TAG} fi if [[ ${ARCHIVE_SUFIX} != "" ]] ; then @@ -369,6 +374,8 @@ if ${DO_CARLA_RELEASE} && ${DO_TARBALL} ; then DESTINATION=${RELEASE_PACKAGE_PATH} SOURCE=${RELEASE_BUILD_FOLDER}/LinuxNoEditor + mkdir -p "${RELEASE_PACKAGE_FOLDER}" + pushd "${SOURCE}" >/dev/null log "Packaging CARLA release." From c93c13516d132b3a83a8d609398e60c10e38f95f Mon Sep 17 00:00:00 2001 From: xiao-ma-gua <17358869549@163.com> Date: Thu, 15 Oct 2026 00:02:37 +0800 Subject: [PATCH 108/128] =?UTF-8?q?=E9=80=9A=E8=BF=87=E5=90=8E=E7=BC=80?= =?UTF-8?q?=E5=88=A4=E6=96=AD=E9=87=8D=E5=91=BD=E5=90=8D=E4=B8=BA=E5=AF=B9?= =?UTF-8?q?=E5=BA=94=E7=9A=84=E5=8E=8B=E7=BC=A9=E5=8C=85?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- Util/download_from_git.py | 40 +++++++++++++++++++-------------------- 1 file changed, 19 insertions(+), 21 deletions(-) diff --git a/Util/download_from_git.py b/Util/download_from_git.py index 580027bb82..5ea7a1ac81 100644 --- a/Util/download_from_git.py +++ b/Util/download_from_git.py @@ -481,19 +481,19 @@ def remove_readonly(func, path, _): print("Removing previous release directory: ", rep_path) shutil.rmtree(rep_path, onerror=remove_readonly) repo = GitRepository(rep_path, remote_path) - # 寻找 local_path 目录下修改日期最新的 .zip 文件 + # 寻找 local_path 目录下修改日期最新的发行版压缩包(.zip 或 .tar.gz) latest_file = None latest_file_name = None latest_time = 0 for file_name in os.listdir(local_path): - if file_name.endswith(".zip"): + if file_name.endswith((".zip", ".tar.gz")): file_path = os.path.join(local_path, file_name) file_time = os.path.getmtime(file_path) if file_time > latest_time: latest_time = file_time latest_file = file_path latest_file_name = file_name - print("Latest zip file to upload: ", latest_file) + print("Latest package file to upload: ", latest_file) # 如果提交的记录数超过2,则重置仓库到最新状态,避免提交记录过多 if len(repo.commits()) > 2: repo.reset_hard() @@ -504,26 +504,24 @@ def remove_readonly(func, path, _): file_path = os.path.join(rep_path, file_name) print("Removing file: ", file_path) os.remove(file_path) - # 将最新的 .zip 重命名为 hutb.zip,并切分成多个 .dat 小文件,上传到 git 仓库中 - # Build/UE4Carla下没有.zip文件需要判断一下,避免报错 + # 将最新的压缩包统一重命名,并切分成多个 .dat 小文件,上传到 git 仓库中 + # Build/UE4Carla下没有压缩包需要判断一下,避免报错 if latest_file is None: - print("No zip file found in local path: ", local_path) + print("No .zip / .tar.gz file found in local path: ", local_path) sys.exit(1) - if os.path.exists(os.path.join(local_path, latest_file)): - # 如果 hutb.zip 已经存在,则先删除 - if os.path.exists(os.path.join(local_path, "hutb.zip")) and latest_file != "hutb.zip": - print("Removing existing hutb.zip file: ", os.path.join(local_path, "hutb.zip")) - os.remove(os.path.join(local_path, "hutb.zip")) - # 如果 latest_file 不是 hutb.zip,则重命名为 hutb.zip - if latest_file != "hutb.zip": - os.rename( - os.path.join(local_path, latest_file), - os.path.join(local_path, "hutb.zip"), - ) - if os.path.exists(os.path.join(rep_path, "*.dat")) is False: - split_file( - os.path.join(local_path, "hutb.zip"), rep_path, 5120 * 1024 * 1024 - ) # 每个小文件最大为 128MB + + # 重命名时保持扩展名与压缩格式一致,下载端通过文件头识别格式 + archive_name = ( + "hutb.tar.gz" if latest_file_name.endswith(".tar.gz") else "hutb.zip" + ) + archive_path = os.path.join(local_path, archive_name) + # 如果上一次上传留下的同名压缩包还在,则先删除 + if os.path.exists(archive_path) and latest_file != archive_path: + print("Removing existing archive file: ", archive_path) + os.remove(archive_path) + if latest_file != archive_path: + os.rename(latest_file, archive_path) + split_file(archive_path, rep_path, 5120 * 1024 * 1024) # 每个小文件最大为 5GB # repo.commit_push_force(message=latest_file_name) # ---------- 修复 504 超时:增量分批推送逻辑开始 ---------- From cb65fa3993b7d657c07eb5cba85432ce824c206a Mon Sep 17 00:00:00 2001 From: xiao-ma-gua <17358869549@163.com> Date: Fri, 16 Oct 2026 00:07:41 +0800 Subject: [PATCH 109/128] =?UTF-8?q?=E5=8A=A0=E5=85=A5=E6=89=93=E5=8C=85?= =?UTF-8?q?=E6=88=90=E5=8A=9F=E5=90=8E=E8=87=AA=E5=8A=A8=E4=B8=8A=E4=BC=A0?= =?UTF-8?q?=E5=88=B0=E5=B7=A5=E8=9C=82=E7=9A=84=E6=AD=A5=E9=AA=A4?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .github/workflows/build_linux.yml | 1 + 1 file changed, 1 insertion(+) diff --git a/.github/workflows/build_linux.yml b/.github/workflows/build_linux.yml index b9d4ee83c4..a42e887451 100644 --- a/.github/workflows/build_linux.yml +++ b/.github/workflows/build_linux.yml @@ -92,6 +92,7 @@ jobs: - name: check run: | source ./setEnv64.sh + make check.upload make check.PythonAPI make check.smoke make check.vr From 9d1d8da0b530723424f8371066b8d036461800c7 Mon Sep 17 00:00:00 2001 From: xiao-ma-gua <17358869549@163.com> Date: Fri, 16 Oct 2026 00:08:46 +0800 Subject: [PATCH 110/128] =?UTF-8?q?=E5=AE=9E=E7=8E=B0=E6=89=93=E5=8C=85?= =?UTF-8?q?=E6=88=90=E5=8A=9F=E8=87=AA=E5=8A=A8=E4=B8=8A=E4=BC=A0=E5=88=B0?= =?UTF-8?q?=E5=B7=A5=E8=9C=82=E4=BB=93=E5=BA=93?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- Util/BuildTools/Check.sh | 56 ++++++++++++++-------------------------- 1 file changed, 20 insertions(+), 36 deletions(-) diff --git a/Util/BuildTools/Check.sh b/Util/BuildTools/Check.sh index 6b872385d0..7266d1e9eb 100755 --- a/Util/BuildTools/Check.sh +++ b/Util/BuildTools/Check.sh @@ -80,7 +80,7 @@ while [[ $# -gt 0 ]]; do SMOKE_TESTS=true; AIR_TESTS=true; VR_TESTS=true; - # UPLOAD_DOWNLOAD=true; + UPLOAD_DOWNLOAD=true; shift ;; --libcarla-release ) LIBCARLA_RELEASE=true; @@ -127,7 +127,7 @@ done # 优先使用环境中的Python系统,以便构建的wheel文件与Boost库所匹配的Python版本一致。 export PATH=/usr/bin:/bin:$PATH -if ! { ${LIBCARLA_RELEASE} || ${LIBCARLA_DEBUG} || ${PYTHON_API} || ${SMOKE_TESTS} || ${AIR_TESTS} || ${VR_TESTS}; }; then +if ! { ${LIBCARLA_RELEASE} || ${LIBCARLA_DEBUG} || ${PYTHON_API} || ${SMOKE_TESTS} || ${AIR_TESTS} || ${VR_TESTS} || ${UPLOAD_DOWNLOAD}; }; then fatal_error "Nothing selected to be done." fi @@ -219,40 +219,24 @@ CARLA_VERSION=$(get_git_repository_version) # 发行包安装目录。 INSTALLATION_DIR=${INSTALLATION_DIR:-${CARLA_BUILD_FOLDER}} -# if ${UPLOAD_DOWNLOAD} ; then - -# # 下载器脚本(Windows 的 hutb_downloader.exe 即它的 pyinstaller 打包版) -# DOWNLOADER="${CARLA_UTIL_FOLDER}/download_from_git.py" -# DOWNLOADER_PY=$(get_conda_env_python 3.8) - -# # download_from_git.py 依赖 gitpython -# ${DOWNLOADER_PY} -c "import git" 2>/dev/null || ${DOWNLOADER_PY} -m pip install -q gitpython - -# # 上传分支固定查找 cwd/../Build/UE4Carla 下修改时间最新的 .zip; -# # 先在 Dist 或 Build/UE4Carla 里找最新的 tar.gz,缺的话从 Dist 拷过去。 -# mkdir -p "${INSTALLATION_DIR}/UE4Carla" -# LATEST_TAR=$(find "${CARLA_DIST_FOLDER}" "${INSTALLATION_DIR}/UE4Carla" -maxdepth 1 -name "*.tar.gz" -printf "%T@ %p\n" 2>/dev/null | sort -n | tail -1 | cut -d" " -f2- || true) -# if [[ -z "${LATEST_TAR}" ]] ; then -# fatal_error "No tar.gz file found in ${CARLA_DIST_FOLDER} or ${INSTALLATION_DIR}/UE4Carla to upload." -# fi -# if [[ "$(dirname ${LATEST_TAR})" != "${INSTALLATION_DIR}/UE4Carla" ]] ; then -# echo "Copying latest tar.gz to upload folder: ${LATEST_TAR}" -# cp "${LATEST_TAR}" "${INSTALLATION_DIR}/UE4Carla/" -# fi - -# pushd "${CARLA_UTIL_FOLDER}" >/dev/null -# log "Uploading distribution package..." -# ${DOWNLOADER_PY} -u release -# popd >/dev/null - -# # 下载回来并解压(解压到 Util/dist/hutb/),顺带测试下载分发流程本身 -# mkdir -p "${CARLA_UTIL_FOLDER}/dist" -# pushd "${CARLA_UTIL_FOLDER}/dist" >/dev/null -# log "Downloading distribution package..." -# ${DOWNLOADER_PY} -# popd >/dev/null - -# fi +if ${UPLOAD_DOWNLOAD} ; then + + # 下载器脚本(Windows 的 hutb_downloader.exe 即它的 pyinstaller 打包版) + DOWNLOADER="${CARLA_UTIL_FOLDER}/download_from_git.py" + DOWNLOADER_PY=$(get_conda_env_python 3.8) + + # download_from_git.py 依赖 gitpython(bat 侧的依赖被 pyinstaller 打包进 exe 了, + # 这里直接跑脚本,需保证运行环境里有,缺了补装) + ${DOWNLOADER_PY} -c "import git" 2>/dev/null || ${DOWNLOADER_PY} -m pip install -q gitpython + + # 上传:download_from_git.py 用 cwd 推导扫描目录(cwd/../Build/UE4Carla), + # 必须在 Util 下运行 —— cwd/.. 才是仓库根,扫到 ${INSTALLATION_DIR}/UE4Carla。 + pushd "${CARLA_UTIL_FOLDER}" >/dev/null + log "Uploading distribution package..." + ${DOWNLOADER_PY} "${DOWNLOADER}" -u release + popd >/dev/null + +fi # ============================================================================== # -- Locate CarlaUE4 packaged build -------------------------------------------- From 834d205e8f72758ba97ce779065781a9c9a0d8d2 Mon Sep 17 00:00:00 2001 From: xiao-ma-gua <17358869549@163.com> Date: Fri, 16 Oct 2026 00:15:55 +0800 Subject: [PATCH 111/128] =?UTF-8?q?=E4=B8=8A=E4=BC=A0=E5=B7=A5=E8=9C=82?= =?UTF-8?q?=E4=BB=93=E5=BA=93=E6=88=90=E5=8A=9F=E5=90=8E=E5=88=A0=E9=99=A4?= =?UTF-8?q?=E5=B7=A5=E4=BD=9C=E7=9B=AE=E5=BD=95=E3=80=82=E5=90=88=E5=B9=B6?= =?UTF-8?q?=E6=97=B6=E6=A0=B9=E6=8D=AE=E5=AF=B9=E5=BA=94=E6=96=87=E4=BB=B6?= =?UTF-8?q?=E5=A4=B4=E5=90=88=E6=88=90=E5=AF=B9=E5=BA=94=E5=8E=8B=E7=BC=A9?= =?UTF-8?q?=E5=8C=85=E5=B9=B6=E7=94=A8=E5=AF=B9=E5=BA=94=E8=A7=A3=E5=8E=8B?= =?UTF-8?q?=E5=99=A8=E8=A7=A3=E5=8E=8B?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- Util/download_from_git.py | 79 ++++++++++++++++----------------------- 1 file changed, 32 insertions(+), 47 deletions(-) diff --git a/Util/download_from_git.py b/Util/download_from_git.py index 5ea7a1ac81..7bec4dc384 100644 --- a/Util/download_from_git.py +++ b/Util/download_from_git.py @@ -561,6 +561,10 @@ def remove_readonly(func, path, _): sys.exit(1) # ---------- 增量分批推送逻辑结束 ---------- + # 全部推成功后删除本地上传工作目录(源包保留),下次上传会自动重新克隆 + print("Removing upload working directory: ", rep_path) + shutil.rmtree(rep_path, onerror=remove_readonly) + print("Upload finished.") sys.exit(0) @@ -610,8 +614,21 @@ def remove_readonly(func, path, _): if args.repository == "release": # 合并小文件 print("Merge small files...") - if os.path.exists(os.path.join(local_path, "hutb.zip")) is False: - merge_files(local_path, os.path.join(local_path, "hutb.zip")) + hutb_zip = os.path.join(local_path, "hutb.zip") + hutb_targz = os.path.join(local_path, "hutb.tar.gz") + if not (os.path.exists(hutb_zip) or os.path.exists(hutb_targz)): + # 先合并成临时文件,再按文件头判断实际压缩格式,重命名为对应的后缀 + merged_file = os.path.join(local_path, "hutb.tmp") + merge_files(local_path, merged_file) + with open(merged_file, "rb") as f: + header = f.read(4) + if header[:2] == b"PK": # zip:PK\x03\x04(空 zip 为 PK\x05\x06) + os.rename(merged_file, hutb_zip) + elif header[:2] == b"\x1f\x8b": # gzip 压缩的 tar.gz + os.rename(merged_file, hutb_targz) + else: + print("Unknown archive format, file header: %r" % header) + sys.exit(1) # 先于解压删除后缀名为.dat的小文件,防止整个过程占用过多的磁盘空间 dat_files = [f for f in os.listdir(local_path) if f.endswith(".dat")] @@ -623,51 +640,19 @@ def remove_readonly(func, path, _): os.remove(os.path.join(local_path, file_name)) print() # 换行 - print("Unzipping hutb.zip...") - f = zipfile.ZipFile(os.path.join(local_path, "hutb.zip"), "r") # 压缩文件位置 - file_list = f.namelist() - total_files = len(file_list) - for idx, file in enumerate(file_list, 1): - show_progress_bar(idx, total_files) - f.extract(file, local_path) # 解压位置 - f.close() - print() # 换行 - - # Linux 包内是 CARLA_Shipping_xxx/LinuxNoEditor/CarlaUE4.sh 的嵌套结构, - # 与 Windows 包(根目录直接是 CarlaUE4.exe)不一致。 - # 这里把模拟器根目录(含 CarlaUE4.sh 的目录)的内容上移到下载目录, - # 保证最终启动路径为 hutb/CarlaUE4.sh - if IS_LINUX: - sh_dir = local_path - for root, dirs, files in os.walk(local_path): - # os.walk 自顶向下,第一个匹配的就是最浅层的目录 - if "CarlaUE4.sh" in files: - sh_dir = root - break - if os.path.abspath(sh_dir) != os.path.abspath(local_path): - print("Moving simulator files from %s to %s ..." % (sh_dir, local_path)) - for name in os.listdir(sh_dir): - src = os.path.join(sh_dir, name) - dst = os.path.join(local_path, name) - if os.path.exists(dst): - # 如下载目录下已有同名文件,跳过避免覆盖 - print("Skip existing path: ", dst) - continue - shutil.move(src, dst) - # 删除上移后残留的空目录(如 CARLA_Shipping_xxx/LinuxNoEditor) - dir_to_remove = os.path.abspath(sh_dir) - while os.path.abspath(dir_to_remove) != os.path.abspath(local_path): - if os.listdir(dir_to_remove): - break # 目录非空,停止清理 - os.rmdir(dir_to_remove) - dir_to_remove = os.path.dirname(dir_to_remove) - - # 赋予 Ubuntu 下二进制文件的执行权限 - carla_sh = os.path.join(local_path, "CarlaUE4.sh") - if os.path.exists(carla_sh): - os.chmod(carla_sh, os.stat(carla_sh).st_mode | stat.S_IEXEC) - else: - print("Warning: CarlaUE4.sh not found in", local_path) + if os.path.exists(hutb_zip): + print("Unzipping hutb.zip...") + f = zipfile.ZipFile(hutb_zip, "r") # 压缩文件位置 + file_list = f.namelist() + total_files = len(file_list) + for idx, file in enumerate(file_list, 1): + show_progress_bar(idx, total_files) + f.extract(file, local_path) # 解压位置 + f.close() + print() # 换行 + elif os.path.exists(hutb_targz): + print("Extracting hutb.tar.gz...") + untar(hutb_targz, local_path) # if os.path.exists(os.path.join(local_path, "hutb.zip")): # print("Remove hutb.zip...") From b28dc3e05e046b45e459c7cd65919d6954c9aa30 Mon Sep 17 00:00:00 2001 From: xiao-ma-gua <17358869549@163.com> Date: Wed, 21 Oct 2026 21:57:38 +0800 Subject: [PATCH 112/128] =?UTF-8?q?=E9=80=82=E9=85=8D=20Ubuntu=2024.04=20?= =?UTF-8?q?=E7=9A=84=20Boost/rpclib=20=E6=9E=84=E5=BB=BA=E5=8F=82=E6=95=B0?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- Util/BuildTools/Setup.sh | 3 +-- 1 file changed, 1 insertion(+), 2 deletions(-) diff --git a/Util/BuildTools/Setup.sh b/Util/BuildTools/Setup.sh index 29dfb97085..2a2d2c3fad 100755 --- a/Util/BuildTools/Setup.sh +++ b/Util/BuildTools/Setup.sh @@ -222,7 +222,6 @@ for PY_VERSION in ${PY_VERSION_LIST[@]} ; do py3_lib=$(${py3} -c "from sysconfig import get_paths as gp; print(gp()['stdlib'])") pyv=`$py3 -c "import sys;x='{v[0]}.{v[1]}'.format(v=list(sys.version_info[:2]));sys.stdout.write(x)";` ./bootstrap.sh \ - --with-toolset=clang \ --prefix=../boost-install \ --with-libraries=python,filesystem,system,program_options \ --with-python=${py3} --with-python-root=${py3_root} @@ -295,7 +294,7 @@ else pushd ${RPCLIB_BASENAME}-libcxx-build >/dev/null cmake -G "Ninja" \ - -DCMAKE_CXX_FLAGS="-fPIC -std=c++14 -stdlib=libc++ -I${LLVM_INCLUDE} -Wl,-L${LLVM_LIBPATH} -DBOOST_NO_EXCEPTIONS -DASIO_NO_EXCEPTIONS ${UNREAL_HOSTED_CFLAGS}" \ + -DCMAKE_CXX_FLAGS="-fPIC -std=c++14 -I${LLVM_INCLUDE} -Wl,-L${LLVM_LIBPATH} -DBOOST_NO_EXCEPTIONS -DASIO_NO_EXCEPTIONS ${UNREAL_HOSTED_CFLAGS}" \ -DCMAKE_INSTALL_PREFIX="../${RPCLIB_BASENAME}-libcxx-install" \ ../${RPCLIB_BASENAME}-source From cc2ff9d5de472625b080c97b533adfee17d51c76 Mon Sep 17 00:00:00 2001 From: xiao-ma-gua <17358869549@163.com> Date: Thu, 22 Oct 2026 20:25:39 +0800 Subject: [PATCH 113/128] =?UTF-8?q?libc++=E6=94=B9=E4=B8=BA=E7=BB=9F?= =?UTF-8?q?=E4=B8=80=E4=BD=BF=E7=94=A8=20UE4=204.26=20=E8=87=AA=E5=B8=A6?= =?UTF-8?q?=E7=9A=84=20libc++=2010?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- Util/BuildTools/BuildOSM2ODR.sh | 8 +++++-- Util/BuildTools/Environment.sh | 24 +++++++++++++++++++++ Util/BuildTools/Setup.sh | 37 +++++++++++++++++++-------------- 3 files changed, 51 insertions(+), 18 deletions(-) diff --git a/Util/BuildTools/BuildOSM2ODR.sh b/Util/BuildTools/BuildOSM2ODR.sh index 0cd6c8d649..c4444494ce 100755 --- a/Util/BuildTools/BuildOSM2ODR.sh +++ b/Util/BuildTools/BuildOSM2ODR.sh @@ -117,6 +117,10 @@ if ${BUILD_OSM2ODR} ; then mkdir -p ${OSM2ODR_SERVER_BUILD_FOLDER} cd ${OSM2ODR_SERVER_BUILD_FOLDER} + if ! LIBCXX_CFLAGS=$(get_libcxx_cflags) || ! LIBCXX_LDFLAGS=$(get_libcxx_ldflags); then + fatal_error "UE4's bundled libc++ was not found (set UE4_ROOT)." + fi + # LLVM_BASENAME=llvm-8.0 # LLVM_INCLUDE="$UE4_ROOT/Engine/Source/ThirdParty/Linux/LibCxx/include/c++/v1" # LLVM_LIBPATH="$UE4_ROOT/Engine/Source/ThirdParty/Linux/LibCxx/lib/Linux/x86_64-unknown-linux-gnu" @@ -134,7 +138,7 @@ if ${BUILD_OSM2ODR} ; then -G "Eclipse CDT4 - Ninja" \ -DCMAKE_C_COMPILER=/usr/bin/clang \ -DCMAKE_CXX_COMPILER=/usr/bin/clang++ \ - -DCMAKE_CXX_FLAGS="-fPIC -std=c++14 -stdlib=libc++" \ + -DCMAKE_CXX_FLAGS="-fPIC -std=c++14 ${LIBCXX_CFLAGS} ${LIBCXX_LDFLAGS}" \ -DCMAKE_INSTALL_PREFIX=${LIBCARLA_INSTALL_SERVER_FOLDER} \ -DPROJ_INCLUDE_DIR=${CARLA_BUILD_FOLDER}/proj-install-server/include \ -DPROJ_LIBRARY=${CARLA_BUILD_FOLDER}/proj-install-server/lib/libproj.a \ @@ -161,7 +165,7 @@ if ${BUILD_OSM2ODR} ; then -G "Eclipse CDT4 - Ninja" \ -DCMAKE_C_COMPILER=/usr/bin/clang \ -DCMAKE_CXX_COMPILER=/usr/bin/clang++ \ - -DCMAKE_CXX_FLAGS="-fPIC -std=c++14 -stdlib=libc++" \ + -DCMAKE_CXX_FLAGS="-fPIC -std=c++14 ${LIBCXX_CFLAGS} ${LIBCXX_LDFLAGS} -lc++ -lc++abi" \ -DCMAKE_INSTALL_PREFIX=${LIBCARLA_INSTALL_SERVER_FOLDER} \ -DPROJ_INCLUDE_DIR=${CARLA_BUILD_FOLDER}/proj-install-server/include \ -DPROJ_LIBRARY=${CARLA_BUILD_FOLDER}/proj-install-server/lib/libproj.a \ diff --git a/Util/BuildTools/Environment.sh b/Util/BuildTools/Environment.sh index 4eda757733..84d612495f 100644 --- a/Util/BuildTools/Environment.sh +++ b/Util/BuildTools/Environment.sh @@ -32,6 +32,30 @@ else fi +# ============================================================================== +# -- libc++ selection ---------------------------------------------------------- +# ============================================================================== +# 统一使用 UE4.26 内置的 libc++ 10,服务端依赖在所有发行版上用同一套标准库 +# 构建,不再依赖系统 libc++ 的版本。 + +function _libcxx_paths { + local ue4="${UE4_ROOT:-$HOME/UnrealEngine_4.26}" + LIBCXX_HEADERS="$ue4/Engine/Source/ThirdParty/Linux/LibCxx/include/c++/v1" + LIBCXX_LIBS="$ue4/Engine/Source/ThirdParty/Linux/LibCxx/lib/Linux/x86_64-unknown-linux-gnu" + + [[ -f "$LIBCXX_HEADERS/__config" && -f "$LIBCXX_LIBS/libc++.a" && -f "$LIBCXX_LIBS/libc++abi.a" ]] +} + +function get_libcxx_cflags { + _libcxx_paths || return 1 + echo "-stdlib=libc++ -nostdinc++ -isystem $LIBCXX_HEADERS" +} + +function get_libcxx_ldflags { + _libcxx_paths || return 1 + echo "-stdlib=libc++ -L$LIBCXX_LIBS" +} + function get_git_repository_version { branch=$(git rev-parse --abbrev-ref HEAD) diff --git a/Util/BuildTools/Setup.sh b/Util/BuildTools/Setup.sh index 2a2d2c3fad..d55a093076 100755 --- a/Util/BuildTools/Setup.sh +++ b/Util/BuildTools/Setup.sh @@ -72,16 +72,18 @@ IFS="," read -r -a PY_VERSION_LIST <<< "${PY_VERSION_LIST}" mkdir -p ${CARLA_BUILD_FOLDER} pushd ${CARLA_BUILD_FOLDER} >/dev/null -#LLVM_INCLUDE="$UE4_ROOT/Engine/Source/ThirdParty/Linux/LibCxx/include/c++/v1" -#LLVM_LIBPATH="$UE4_ROOT/Engine/Source/ThirdParty/Linux/LibCxx/lib/Linux/x86_64-unknown-linux-gnu" +if ! LIBCXX_CFLAGS=$(get_libcxx_cflags) || ! LIBCXX_LDFLAGS=$(get_libcxx_ldflags); then + echo "ERROR: the libc++ bundled with UE4 was not found (set UE4_ROOT)." >&2 + exit 1 +fi #UNREAL_HOSTED_CFLAGS="--sysroot=$UE4_ROOT/Engine/Extras/ThirdPartyNotUE/SDKs/HostLinux/Linux_x64/v17_clang-10.0.1-centos7/x86_64-unknown-linux-gnu/" unset UNREAL_HOSTED_CFLAGS unset LLVM_INCLUDE unset LLVM_LIBPATH -export CXXFLAGS="-std=c++14 -stdlib=libc++" -export LDFLAGS="-stdlib=libc++" +export CXXFLAGS="-std=c++14 $LIBCXX_CFLAGS" +export LDFLAGS="$LIBCXX_LDFLAGS" # ============================================================================== # -- Generate CMake toolchains ------------------------------------------------- @@ -116,10 +118,9 @@ cp ${LIBSTDCPP_TOOLCHAIN_FILE}.gen ${LIBCPP_TOOLCHAIN_FILE}.gen cat >>${LIBCPP_TOOLCHAIN_FILE}.gen </dev/null cmake -G "Ninja" \ - -DCMAKE_CXX_FLAGS="-fPIC -std=c++14 -I${LLVM_INCLUDE} -Wl,-L${LLVM_LIBPATH} -DBOOST_NO_EXCEPTIONS -DASIO_NO_EXCEPTIONS ${UNREAL_HOSTED_CFLAGS}" \ + -DCMAKE_CXX_FLAGS="-fPIC -std=c++14 ${LIBCXX_CFLAGS} ${LIBCXX_LDFLAGS} -DBOOST_NO_EXCEPTIONS -DASIO_NO_EXCEPTIONS ${UNREAL_HOSTED_CFLAGS}" \ -DCMAKE_INSTALL_PREFIX="../${RPCLIB_BASENAME}-libcxx-install" \ ../${RPCLIB_BASENAME}-source @@ -363,7 +364,7 @@ else pushd ${GTEST_BASENAME}-libcxx-build >/dev/null cmake -G "Ninja" \ - -DCMAKE_CXX_FLAGS="-std=c++14 -stdlib=libc++ -I${LLVM_INCLUDE} -Wl,-L${LLVM_LIBPATH} -DBOOST_NO_EXCEPTIONS -fno-exceptions ${UNREAL_HOSTED_CFLAGS}" \ + -DCMAKE_CXX_FLAGS="-std=c++14 ${LIBCXX_CFLAGS} ${LIBCXX_LDFLAGS} -DBOOST_NO_EXCEPTIONS -fno-exceptions ${UNREAL_HOSTED_CFLAGS}" \ -DCMAKE_INSTALL_PREFIX="../${GTEST_BASENAME}-libcxx-install" \ ../${GTEST_BASENAME}-source @@ -434,7 +435,7 @@ else pushd ${RECAST_BASENAME}-build >/dev/null cmake -G "Ninja" \ - -DCMAKE_CXX_FLAGS="-std=c++14 -stdlib=libc++ -fPIC" \ + -DCMAKE_CXX_FLAGS="-std=c++14 ${LIBCXX_CFLAGS} -fPIC" \ -DCMAKE_INSTALL_PREFIX="../${RECAST_BASENAME}-install" \ -DRECASTNAVIGATION_DEMO=False \ -DRECASTNAVIGATION_TEST=False \ @@ -565,9 +566,9 @@ else pushd ${XERCESC_SRC_DIR}/build >/dev/null cmake -G "Ninja" \ - -DCMAKE_CXX_FLAGS="-std=c++14 -stdlib=libc++ -fPIC -w" \ + -DCMAKE_CXX_FLAGS="-std=c++14 ${LIBCXX_CFLAGS} -fPIC -w" \ -DCMAKE_INSTALL_PREFIX="../../${XERCESC_INSTALL_SERVER_DIR}" \ - -DCMAKE_EXE_LINKER_FLAGS="-stdlib=libc++ -lc++ -lc++abi" \ + -DCMAKE_EXE_LINKER_FLAGS="${LIBCXX_LDFLAGS} -lc++ -lc++abi" \ -DCMAKE_BUILD_TYPE=Release \ -DBUILD_SHARED_LIBS=OFF \ -Dtranscoder=gnuiconv \ @@ -662,7 +663,7 @@ if ${USE_CHRONO} ; then mkdir -p ${CHRONO_SRC_DIR}/build pushd ${CHRONO_SRC_DIR}/build >/dev/null cmake -G "Ninja" \ - -DCMAKE_CXX_FLAGS="-fPIC -std=c++14 -stdlib=libc++ -I${LLVM_INCLUDE} -L${LLVM_LIBPATH} -Wno-unused-command-line-argument ${UNREAL_HOSTED_CFLAGS}" \ + -DCMAKE_CXX_FLAGS="-fPIC -std=c++14 ${LIBCXX_CFLAGS} ${LIBCXX_LDFLAGS} -Wno-unused-command-line-argument ${UNREAL_HOSTED_CFLAGS}" \ -DEIGEN3_INCLUDE_DIR="${EIGEN_INCLUDE}" \ -DCMAKE_INSTALL_PREFIX="${CARLA_BUILD_FOLDER}/${CHRONO_INSTALL_DIR}" \ -DCMAKE_BUILD_TYPE=Release \ @@ -780,7 +781,7 @@ else pushd ${PROJ_SRC_DIR}/build >/dev/null cmake -G "Ninja" .. \ - -DCMAKE_CXX_FLAGS="-std=c++14 -fPIC" \ + -DCMAKE_CXX_FLAGS="-std=c++14 -fPIC -include cstdint" \ -DSQLITE3_INCLUDE_DIR=${SQLITE_INCLUDE_DIR} -DSQLITE3_LIBRARY=${SQLITE_LIB} \ -DEXE_SQLITE3=${SQLITE_EXE} \ -DENABLE_TIFF=OFF -DENABLE_CURL=OFF -DBUILD_SHARED_LIBS=OFF -DBUILD_PROJSYNC=OFF \ @@ -798,7 +799,7 @@ else pushd ${PROJ_SRC_DIR}/build >/dev/null cmake -G "Ninja" .. \ - -DCMAKE_CXX_FLAGS="-std=c++14 -fPIC -stdlib=libc++ -I${LLVM_INCLUDE} -Wl,-L${LLVM_LIBPATH}" \ + -DCMAKE_CXX_FLAGS="-std=c++14 -fPIC ${LIBCXX_CFLAGS} ${LIBCXX_LDFLAGS} -include cstdint" \ -DSQLITE3_INCLUDE_DIR=${SQLITE_INCLUDE_DIR} -DSQLITE3_LIBRARY=${SQLITE_LIB} \ -DEXE_SQLITE3=${SQLITE_EXE} \ -DENABLE_TIFF=OFF -DENABLE_CURL=OFF -DBUILD_SHARED_LIBS=OFF -DBUILD_PROJSYNC=OFF \ @@ -837,6 +838,10 @@ PATCHELF_EXE=${PWD}/${PATCHELF_INSTALL_DIR}/bin/patchelf if [[ -d ${PATCHELF_INSTALL_DIR} ]] ; then log "Patchelf already installed." +elif command -v patchelf >/dev/null 2>&1 ; then + log "Using system patchelf: $(command -v patchelf)" + mkdir -p ${PATCHELF_INSTALL_DIR}/bin + ln -sf "$(command -v patchelf)" ${PATCHELF_EXE} else log "Retrieving patchelf" @@ -1034,7 +1039,7 @@ if ${USE_ROS2} ; then cmake -G "Ninja" \ -DCMAKE_INSTALL_PREFIX="${FASTDDS_INSTALL_DIR}" \ -DFORCE_CXX="14" \ - -DCMAKE_CXX_FLAGS="-fPIC -std=c++14 -stdlib=libc++ -I${LLVM_INCLUDE} -Wl,-L${LLVM_LIBPATH} -DBOOST_NO_EXCEPTIONS ${UNREAL_HOSTED_CFLAGS}" \ + -DCMAKE_CXX_FLAGS="-fPIC -std=c++14 ${LIBCXX_CFLAGS} ${LIBCXX_LDFLAGS} -DBOOST_NO_EXCEPTIONS ${UNREAL_HOSTED_CFLAGS}" \ -DBUILD_SHARED_LIBS=OFF \ -DBUILD_TESTING=OFF \ -DCOMPILE_EXAMPLES=OFF \ From 3566edb9af046bdc72da8c7a0584d726e623870c Mon Sep 17 00:00:00 2001 From: xiao-ma-gua <17358869549@163.com> Date: Thu, 22 Oct 2026 20:39:30 +0800 Subject: [PATCH 114/128] =?UTF-8?q?libc++=E6=94=B9=E4=B8=BA=E7=BB=9F?= =?UTF-8?q?=E4=B8=80=E4=BD=BF=E7=94=A8=20UE4=204.26=20=E8=87=AA=E5=B8=A6?= =?UTF-8?q?=E7=9A=84=20libc++=2010?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- Util/BuildTools/BuildOSM2ODR.sh | 8 +++++-- Util/BuildTools/Environment.sh | 24 +++++++++++++++++++++ Util/BuildTools/Setup.sh | 37 +++++++++++++++++++-------------- 3 files changed, 51 insertions(+), 18 deletions(-) diff --git a/Util/BuildTools/BuildOSM2ODR.sh b/Util/BuildTools/BuildOSM2ODR.sh index 0cd6c8d649..c4444494ce 100755 --- a/Util/BuildTools/BuildOSM2ODR.sh +++ b/Util/BuildTools/BuildOSM2ODR.sh @@ -117,6 +117,10 @@ if ${BUILD_OSM2ODR} ; then mkdir -p ${OSM2ODR_SERVER_BUILD_FOLDER} cd ${OSM2ODR_SERVER_BUILD_FOLDER} + if ! LIBCXX_CFLAGS=$(get_libcxx_cflags) || ! LIBCXX_LDFLAGS=$(get_libcxx_ldflags); then + fatal_error "UE4's bundled libc++ was not found (set UE4_ROOT)." + fi + # LLVM_BASENAME=llvm-8.0 # LLVM_INCLUDE="$UE4_ROOT/Engine/Source/ThirdParty/Linux/LibCxx/include/c++/v1" # LLVM_LIBPATH="$UE4_ROOT/Engine/Source/ThirdParty/Linux/LibCxx/lib/Linux/x86_64-unknown-linux-gnu" @@ -134,7 +138,7 @@ if ${BUILD_OSM2ODR} ; then -G "Eclipse CDT4 - Ninja" \ -DCMAKE_C_COMPILER=/usr/bin/clang \ -DCMAKE_CXX_COMPILER=/usr/bin/clang++ \ - -DCMAKE_CXX_FLAGS="-fPIC -std=c++14 -stdlib=libc++" \ + -DCMAKE_CXX_FLAGS="-fPIC -std=c++14 ${LIBCXX_CFLAGS} ${LIBCXX_LDFLAGS}" \ -DCMAKE_INSTALL_PREFIX=${LIBCARLA_INSTALL_SERVER_FOLDER} \ -DPROJ_INCLUDE_DIR=${CARLA_BUILD_FOLDER}/proj-install-server/include \ -DPROJ_LIBRARY=${CARLA_BUILD_FOLDER}/proj-install-server/lib/libproj.a \ @@ -161,7 +165,7 @@ if ${BUILD_OSM2ODR} ; then -G "Eclipse CDT4 - Ninja" \ -DCMAKE_C_COMPILER=/usr/bin/clang \ -DCMAKE_CXX_COMPILER=/usr/bin/clang++ \ - -DCMAKE_CXX_FLAGS="-fPIC -std=c++14 -stdlib=libc++" \ + -DCMAKE_CXX_FLAGS="-fPIC -std=c++14 ${LIBCXX_CFLAGS} ${LIBCXX_LDFLAGS} -lc++ -lc++abi" \ -DCMAKE_INSTALL_PREFIX=${LIBCARLA_INSTALL_SERVER_FOLDER} \ -DPROJ_INCLUDE_DIR=${CARLA_BUILD_FOLDER}/proj-install-server/include \ -DPROJ_LIBRARY=${CARLA_BUILD_FOLDER}/proj-install-server/lib/libproj.a \ diff --git a/Util/BuildTools/Environment.sh b/Util/BuildTools/Environment.sh index 4eda757733..84d612495f 100644 --- a/Util/BuildTools/Environment.sh +++ b/Util/BuildTools/Environment.sh @@ -32,6 +32,30 @@ else fi +# ============================================================================== +# -- libc++ selection ---------------------------------------------------------- +# ============================================================================== +# 统一使用 UE4.26 内置的 libc++ 10,服务端依赖在所有发行版上用同一套标准库 +# 构建,不再依赖系统 libc++ 的版本。 + +function _libcxx_paths { + local ue4="${UE4_ROOT:-$HOME/UnrealEngine_4.26}" + LIBCXX_HEADERS="$ue4/Engine/Source/ThirdParty/Linux/LibCxx/include/c++/v1" + LIBCXX_LIBS="$ue4/Engine/Source/ThirdParty/Linux/LibCxx/lib/Linux/x86_64-unknown-linux-gnu" + + [[ -f "$LIBCXX_HEADERS/__config" && -f "$LIBCXX_LIBS/libc++.a" && -f "$LIBCXX_LIBS/libc++abi.a" ]] +} + +function get_libcxx_cflags { + _libcxx_paths || return 1 + echo "-stdlib=libc++ -nostdinc++ -isystem $LIBCXX_HEADERS" +} + +function get_libcxx_ldflags { + _libcxx_paths || return 1 + echo "-stdlib=libc++ -L$LIBCXX_LIBS" +} + function get_git_repository_version { branch=$(git rev-parse --abbrev-ref HEAD) diff --git a/Util/BuildTools/Setup.sh b/Util/BuildTools/Setup.sh index 2a2d2c3fad..57a4a76553 100755 --- a/Util/BuildTools/Setup.sh +++ b/Util/BuildTools/Setup.sh @@ -72,16 +72,18 @@ IFS="," read -r -a PY_VERSION_LIST <<< "${PY_VERSION_LIST}" mkdir -p ${CARLA_BUILD_FOLDER} pushd ${CARLA_BUILD_FOLDER} >/dev/null -#LLVM_INCLUDE="$UE4_ROOT/Engine/Source/ThirdParty/Linux/LibCxx/include/c++/v1" -#LLVM_LIBPATH="$UE4_ROOT/Engine/Source/ThirdParty/Linux/LibCxx/lib/Linux/x86_64-unknown-linux-gnu" +if ! LIBCXX_CFLAGS=$(get_libcxx_cflags) || ! LIBCXX_LDFLAGS=$(get_libcxx_ldflags); then + echo "ERROR: the libc++ bundled with UE4 was not found (set UE4_ROOT)." >&2 + exit 1 +fi #UNREAL_HOSTED_CFLAGS="--sysroot=$UE4_ROOT/Engine/Extras/ThirdPartyNotUE/SDKs/HostLinux/Linux_x64/v17_clang-10.0.1-centos7/x86_64-unknown-linux-gnu/" unset UNREAL_HOSTED_CFLAGS unset LLVM_INCLUDE unset LLVM_LIBPATH -export CXXFLAGS="-std=c++14 -stdlib=libc++" -export LDFLAGS="-stdlib=libc++" +export CXXFLAGS="-std=c++14 $LIBCXX_CFLAGS" +export LDFLAGS="$LIBCXX_LDFLAGS" # ============================================================================== # -- Generate CMake toolchains ------------------------------------------------- @@ -116,10 +118,9 @@ cp ${LIBSTDCPP_TOOLCHAIN_FILE}.gen ${LIBCPP_TOOLCHAIN_FILE}.gen cat >>${LIBCPP_TOOLCHAIN_FILE}.gen </dev/null cmake -G "Ninja" \ - -DCMAKE_CXX_FLAGS="-fPIC -std=c++14 -I${LLVM_INCLUDE} -Wl,-L${LLVM_LIBPATH} -DBOOST_NO_EXCEPTIONS -DASIO_NO_EXCEPTIONS ${UNREAL_HOSTED_CFLAGS}" \ + -DCMAKE_CXX_FLAGS="-fPIC -std=c++14 ${LIBCXX_CFLAGS} ${LIBCXX_LDFLAGS} -DBOOST_NO_EXCEPTIONS -DASIO_NO_EXCEPTIONS ${UNREAL_HOSTED_CFLAGS}" \ -DCMAKE_INSTALL_PREFIX="../${RPCLIB_BASENAME}-libcxx-install" \ ../${RPCLIB_BASENAME}-source @@ -363,7 +364,7 @@ else pushd ${GTEST_BASENAME}-libcxx-build >/dev/null cmake -G "Ninja" \ - -DCMAKE_CXX_FLAGS="-std=c++14 -stdlib=libc++ -I${LLVM_INCLUDE} -Wl,-L${LLVM_LIBPATH} -DBOOST_NO_EXCEPTIONS -fno-exceptions ${UNREAL_HOSTED_CFLAGS}" \ + -DCMAKE_CXX_FLAGS="-std=c++14 ${LIBCXX_CFLAGS} ${LIBCXX_LDFLAGS} -DBOOST_NO_EXCEPTIONS -fno-exceptions ${UNREAL_HOSTED_CFLAGS}" \ -DCMAKE_INSTALL_PREFIX="../${GTEST_BASENAME}-libcxx-install" \ ../${GTEST_BASENAME}-source @@ -434,7 +435,7 @@ else pushd ${RECAST_BASENAME}-build >/dev/null cmake -G "Ninja" \ - -DCMAKE_CXX_FLAGS="-std=c++14 -stdlib=libc++ -fPIC" \ + -DCMAKE_CXX_FLAGS="-std=c++14 ${LIBCXX_CFLAGS} -fPIC" \ -DCMAKE_INSTALL_PREFIX="../${RECAST_BASENAME}-install" \ -DRECASTNAVIGATION_DEMO=False \ -DRECASTNAVIGATION_TEST=False \ @@ -565,9 +566,9 @@ else pushd ${XERCESC_SRC_DIR}/build >/dev/null cmake -G "Ninja" \ - -DCMAKE_CXX_FLAGS="-std=c++14 -stdlib=libc++ -fPIC -w" \ + -DCMAKE_CXX_FLAGS="-std=c++14 ${LIBCXX_CFLAGS} -fPIC -w" \ -DCMAKE_INSTALL_PREFIX="../../${XERCESC_INSTALL_SERVER_DIR}" \ - -DCMAKE_EXE_LINKER_FLAGS="-stdlib=libc++ -lc++ -lc++abi" \ + -DCMAKE_EXE_LINKER_FLAGS="${LIBCXX_LDFLAGS} -lc++ -lc++abi" \ -DCMAKE_BUILD_TYPE=Release \ -DBUILD_SHARED_LIBS=OFF \ -Dtranscoder=gnuiconv \ @@ -662,7 +663,7 @@ if ${USE_CHRONO} ; then mkdir -p ${CHRONO_SRC_DIR}/build pushd ${CHRONO_SRC_DIR}/build >/dev/null cmake -G "Ninja" \ - -DCMAKE_CXX_FLAGS="-fPIC -std=c++14 -stdlib=libc++ -I${LLVM_INCLUDE} -L${LLVM_LIBPATH} -Wno-unused-command-line-argument ${UNREAL_HOSTED_CFLAGS}" \ + -DCMAKE_CXX_FLAGS="-fPIC -std=c++14 ${LIBCXX_CFLAGS} ${LIBCXX_LDFLAGS} -Wno-unused-command-line-argument ${UNREAL_HOSTED_CFLAGS}" \ -DEIGEN3_INCLUDE_DIR="${EIGEN_INCLUDE}" \ -DCMAKE_INSTALL_PREFIX="${CARLA_BUILD_FOLDER}/${CHRONO_INSTALL_DIR}" \ -DCMAKE_BUILD_TYPE=Release \ @@ -780,7 +781,7 @@ else pushd ${PROJ_SRC_DIR}/build >/dev/null cmake -G "Ninja" .. \ - -DCMAKE_CXX_FLAGS="-std=c++14 -fPIC" \ + -DCMAKE_CXX_FLAGS="-std=c++14 -fPIC " \ -DSQLITE3_INCLUDE_DIR=${SQLITE_INCLUDE_DIR} -DSQLITE3_LIBRARY=${SQLITE_LIB} \ -DEXE_SQLITE3=${SQLITE_EXE} \ -DENABLE_TIFF=OFF -DENABLE_CURL=OFF -DBUILD_SHARED_LIBS=OFF -DBUILD_PROJSYNC=OFF \ @@ -798,7 +799,7 @@ else pushd ${PROJ_SRC_DIR}/build >/dev/null cmake -G "Ninja" .. \ - -DCMAKE_CXX_FLAGS="-std=c++14 -fPIC -stdlib=libc++ -I${LLVM_INCLUDE} -Wl,-L${LLVM_LIBPATH}" \ + -DCMAKE_CXX_FLAGS="-std=c++14 -fPIC ${LIBCXX_CFLAGS} ${LIBCXX_LDFLAGS} " \ -DSQLITE3_INCLUDE_DIR=${SQLITE_INCLUDE_DIR} -DSQLITE3_LIBRARY=${SQLITE_LIB} \ -DEXE_SQLITE3=${SQLITE_EXE} \ -DENABLE_TIFF=OFF -DENABLE_CURL=OFF -DBUILD_SHARED_LIBS=OFF -DBUILD_PROJSYNC=OFF \ @@ -837,6 +838,10 @@ PATCHELF_EXE=${PWD}/${PATCHELF_INSTALL_DIR}/bin/patchelf if [[ -d ${PATCHELF_INSTALL_DIR} ]] ; then log "Patchelf already installed." +elif command -v patchelf >/dev/null 2>&1 ; then + log "Using system patchelf: $(command -v patchelf)" + mkdir -p ${PATCHELF_INSTALL_DIR}/bin + ln -sf "$(command -v patchelf)" ${PATCHELF_EXE} else log "Retrieving patchelf" @@ -1034,7 +1039,7 @@ if ${USE_ROS2} ; then cmake -G "Ninja" \ -DCMAKE_INSTALL_PREFIX="${FASTDDS_INSTALL_DIR}" \ -DFORCE_CXX="14" \ - -DCMAKE_CXX_FLAGS="-fPIC -std=c++14 -stdlib=libc++ -I${LLVM_INCLUDE} -Wl,-L${LLVM_LIBPATH} -DBOOST_NO_EXCEPTIONS ${UNREAL_HOSTED_CFLAGS}" \ + -DCMAKE_CXX_FLAGS="-fPIC -std=c++14 ${LIBCXX_CFLAGS} ${LIBCXX_LDFLAGS} -DBOOST_NO_EXCEPTIONS ${UNREAL_HOSTED_CFLAGS}" \ -DBUILD_SHARED_LIBS=OFF \ -DBUILD_TESTING=OFF \ -DCOMPILE_EXAMPLES=OFF \ From 5a5d6ad6d285b48df8ab23addeb39ea05918879d Mon Sep 17 00:00:00 2001 From: xiao-ma-gua <17358869549@163.com> Date: Thu, 22 Oct 2026 20:43:39 +0800 Subject: [PATCH 115/128] =?UTF-8?q?Revert=20"libc++=E6=94=B9=E4=B8=BA?= =?UTF-8?q?=E7=BB=9F=E4=B8=80=E4=BD=BF=E7=94=A8=20UE4=204.26=20=E8=87=AA?= =?UTF-8?q?=E5=B8=A6=E7=9A=84=20libc++=2010"?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit This reverts commit 3566edb9af046bdc72da8c7a0584d726e623870c. --- Util/BuildTools/BuildOSM2ODR.sh | 8 ++----- Util/BuildTools/Environment.sh | 24 --------------------- Util/BuildTools/Setup.sh | 37 ++++++++++++++------------------- 3 files changed, 18 insertions(+), 51 deletions(-) diff --git a/Util/BuildTools/BuildOSM2ODR.sh b/Util/BuildTools/BuildOSM2ODR.sh index c4444494ce..0cd6c8d649 100755 --- a/Util/BuildTools/BuildOSM2ODR.sh +++ b/Util/BuildTools/BuildOSM2ODR.sh @@ -117,10 +117,6 @@ if ${BUILD_OSM2ODR} ; then mkdir -p ${OSM2ODR_SERVER_BUILD_FOLDER} cd ${OSM2ODR_SERVER_BUILD_FOLDER} - if ! LIBCXX_CFLAGS=$(get_libcxx_cflags) || ! LIBCXX_LDFLAGS=$(get_libcxx_ldflags); then - fatal_error "UE4's bundled libc++ was not found (set UE4_ROOT)." - fi - # LLVM_BASENAME=llvm-8.0 # LLVM_INCLUDE="$UE4_ROOT/Engine/Source/ThirdParty/Linux/LibCxx/include/c++/v1" # LLVM_LIBPATH="$UE4_ROOT/Engine/Source/ThirdParty/Linux/LibCxx/lib/Linux/x86_64-unknown-linux-gnu" @@ -138,7 +134,7 @@ if ${BUILD_OSM2ODR} ; then -G "Eclipse CDT4 - Ninja" \ -DCMAKE_C_COMPILER=/usr/bin/clang \ -DCMAKE_CXX_COMPILER=/usr/bin/clang++ \ - -DCMAKE_CXX_FLAGS="-fPIC -std=c++14 ${LIBCXX_CFLAGS} ${LIBCXX_LDFLAGS}" \ + -DCMAKE_CXX_FLAGS="-fPIC -std=c++14 -stdlib=libc++" \ -DCMAKE_INSTALL_PREFIX=${LIBCARLA_INSTALL_SERVER_FOLDER} \ -DPROJ_INCLUDE_DIR=${CARLA_BUILD_FOLDER}/proj-install-server/include \ -DPROJ_LIBRARY=${CARLA_BUILD_FOLDER}/proj-install-server/lib/libproj.a \ @@ -165,7 +161,7 @@ if ${BUILD_OSM2ODR} ; then -G "Eclipse CDT4 - Ninja" \ -DCMAKE_C_COMPILER=/usr/bin/clang \ -DCMAKE_CXX_COMPILER=/usr/bin/clang++ \ - -DCMAKE_CXX_FLAGS="-fPIC -std=c++14 ${LIBCXX_CFLAGS} ${LIBCXX_LDFLAGS} -lc++ -lc++abi" \ + -DCMAKE_CXX_FLAGS="-fPIC -std=c++14 -stdlib=libc++" \ -DCMAKE_INSTALL_PREFIX=${LIBCARLA_INSTALL_SERVER_FOLDER} \ -DPROJ_INCLUDE_DIR=${CARLA_BUILD_FOLDER}/proj-install-server/include \ -DPROJ_LIBRARY=${CARLA_BUILD_FOLDER}/proj-install-server/lib/libproj.a \ diff --git a/Util/BuildTools/Environment.sh b/Util/BuildTools/Environment.sh index 84d612495f..4eda757733 100644 --- a/Util/BuildTools/Environment.sh +++ b/Util/BuildTools/Environment.sh @@ -32,30 +32,6 @@ else fi -# ============================================================================== -# -- libc++ selection ---------------------------------------------------------- -# ============================================================================== -# 统一使用 UE4.26 内置的 libc++ 10,服务端依赖在所有发行版上用同一套标准库 -# 构建,不再依赖系统 libc++ 的版本。 - -function _libcxx_paths { - local ue4="${UE4_ROOT:-$HOME/UnrealEngine_4.26}" - LIBCXX_HEADERS="$ue4/Engine/Source/ThirdParty/Linux/LibCxx/include/c++/v1" - LIBCXX_LIBS="$ue4/Engine/Source/ThirdParty/Linux/LibCxx/lib/Linux/x86_64-unknown-linux-gnu" - - [[ -f "$LIBCXX_HEADERS/__config" && -f "$LIBCXX_LIBS/libc++.a" && -f "$LIBCXX_LIBS/libc++abi.a" ]] -} - -function get_libcxx_cflags { - _libcxx_paths || return 1 - echo "-stdlib=libc++ -nostdinc++ -isystem $LIBCXX_HEADERS" -} - -function get_libcxx_ldflags { - _libcxx_paths || return 1 - echo "-stdlib=libc++ -L$LIBCXX_LIBS" -} - function get_git_repository_version { branch=$(git rev-parse --abbrev-ref HEAD) diff --git a/Util/BuildTools/Setup.sh b/Util/BuildTools/Setup.sh index 57a4a76553..2a2d2c3fad 100755 --- a/Util/BuildTools/Setup.sh +++ b/Util/BuildTools/Setup.sh @@ -72,18 +72,16 @@ IFS="," read -r -a PY_VERSION_LIST <<< "${PY_VERSION_LIST}" mkdir -p ${CARLA_BUILD_FOLDER} pushd ${CARLA_BUILD_FOLDER} >/dev/null -if ! LIBCXX_CFLAGS=$(get_libcxx_cflags) || ! LIBCXX_LDFLAGS=$(get_libcxx_ldflags); then - echo "ERROR: the libc++ bundled with UE4 was not found (set UE4_ROOT)." >&2 - exit 1 -fi +#LLVM_INCLUDE="$UE4_ROOT/Engine/Source/ThirdParty/Linux/LibCxx/include/c++/v1" +#LLVM_LIBPATH="$UE4_ROOT/Engine/Source/ThirdParty/Linux/LibCxx/lib/Linux/x86_64-unknown-linux-gnu" #UNREAL_HOSTED_CFLAGS="--sysroot=$UE4_ROOT/Engine/Extras/ThirdPartyNotUE/SDKs/HostLinux/Linux_x64/v17_clang-10.0.1-centos7/x86_64-unknown-linux-gnu/" unset UNREAL_HOSTED_CFLAGS unset LLVM_INCLUDE unset LLVM_LIBPATH -export CXXFLAGS="-std=c++14 $LIBCXX_CFLAGS" -export LDFLAGS="$LIBCXX_LDFLAGS" +export CXXFLAGS="-std=c++14 -stdlib=libc++" +export LDFLAGS="-stdlib=libc++" # ============================================================================== # -- Generate CMake toolchains ------------------------------------------------- @@ -118,9 +116,10 @@ cp ${LIBSTDCPP_TOOLCHAIN_FILE}.gen ${LIBCPP_TOOLCHAIN_FILE}.gen cat >>${LIBCPP_TOOLCHAIN_FILE}.gen </dev/null cmake -G "Ninja" \ - -DCMAKE_CXX_FLAGS="-fPIC -std=c++14 ${LIBCXX_CFLAGS} ${LIBCXX_LDFLAGS} -DBOOST_NO_EXCEPTIONS -DASIO_NO_EXCEPTIONS ${UNREAL_HOSTED_CFLAGS}" \ + -DCMAKE_CXX_FLAGS="-fPIC -std=c++14 -I${LLVM_INCLUDE} -Wl,-L${LLVM_LIBPATH} -DBOOST_NO_EXCEPTIONS -DASIO_NO_EXCEPTIONS ${UNREAL_HOSTED_CFLAGS}" \ -DCMAKE_INSTALL_PREFIX="../${RPCLIB_BASENAME}-libcxx-install" \ ../${RPCLIB_BASENAME}-source @@ -364,7 +363,7 @@ else pushd ${GTEST_BASENAME}-libcxx-build >/dev/null cmake -G "Ninja" \ - -DCMAKE_CXX_FLAGS="-std=c++14 ${LIBCXX_CFLAGS} ${LIBCXX_LDFLAGS} -DBOOST_NO_EXCEPTIONS -fno-exceptions ${UNREAL_HOSTED_CFLAGS}" \ + -DCMAKE_CXX_FLAGS="-std=c++14 -stdlib=libc++ -I${LLVM_INCLUDE} -Wl,-L${LLVM_LIBPATH} -DBOOST_NO_EXCEPTIONS -fno-exceptions ${UNREAL_HOSTED_CFLAGS}" \ -DCMAKE_INSTALL_PREFIX="../${GTEST_BASENAME}-libcxx-install" \ ../${GTEST_BASENAME}-source @@ -435,7 +434,7 @@ else pushd ${RECAST_BASENAME}-build >/dev/null cmake -G "Ninja" \ - -DCMAKE_CXX_FLAGS="-std=c++14 ${LIBCXX_CFLAGS} -fPIC" \ + -DCMAKE_CXX_FLAGS="-std=c++14 -stdlib=libc++ -fPIC" \ -DCMAKE_INSTALL_PREFIX="../${RECAST_BASENAME}-install" \ -DRECASTNAVIGATION_DEMO=False \ -DRECASTNAVIGATION_TEST=False \ @@ -566,9 +565,9 @@ else pushd ${XERCESC_SRC_DIR}/build >/dev/null cmake -G "Ninja" \ - -DCMAKE_CXX_FLAGS="-std=c++14 ${LIBCXX_CFLAGS} -fPIC -w" \ + -DCMAKE_CXX_FLAGS="-std=c++14 -stdlib=libc++ -fPIC -w" \ -DCMAKE_INSTALL_PREFIX="../../${XERCESC_INSTALL_SERVER_DIR}" \ - -DCMAKE_EXE_LINKER_FLAGS="${LIBCXX_LDFLAGS} -lc++ -lc++abi" \ + -DCMAKE_EXE_LINKER_FLAGS="-stdlib=libc++ -lc++ -lc++abi" \ -DCMAKE_BUILD_TYPE=Release \ -DBUILD_SHARED_LIBS=OFF \ -Dtranscoder=gnuiconv \ @@ -663,7 +662,7 @@ if ${USE_CHRONO} ; then mkdir -p ${CHRONO_SRC_DIR}/build pushd ${CHRONO_SRC_DIR}/build >/dev/null cmake -G "Ninja" \ - -DCMAKE_CXX_FLAGS="-fPIC -std=c++14 ${LIBCXX_CFLAGS} ${LIBCXX_LDFLAGS} -Wno-unused-command-line-argument ${UNREAL_HOSTED_CFLAGS}" \ + -DCMAKE_CXX_FLAGS="-fPIC -std=c++14 -stdlib=libc++ -I${LLVM_INCLUDE} -L${LLVM_LIBPATH} -Wno-unused-command-line-argument ${UNREAL_HOSTED_CFLAGS}" \ -DEIGEN3_INCLUDE_DIR="${EIGEN_INCLUDE}" \ -DCMAKE_INSTALL_PREFIX="${CARLA_BUILD_FOLDER}/${CHRONO_INSTALL_DIR}" \ -DCMAKE_BUILD_TYPE=Release \ @@ -781,7 +780,7 @@ else pushd ${PROJ_SRC_DIR}/build >/dev/null cmake -G "Ninja" .. \ - -DCMAKE_CXX_FLAGS="-std=c++14 -fPIC " \ + -DCMAKE_CXX_FLAGS="-std=c++14 -fPIC" \ -DSQLITE3_INCLUDE_DIR=${SQLITE_INCLUDE_DIR} -DSQLITE3_LIBRARY=${SQLITE_LIB} \ -DEXE_SQLITE3=${SQLITE_EXE} \ -DENABLE_TIFF=OFF -DENABLE_CURL=OFF -DBUILD_SHARED_LIBS=OFF -DBUILD_PROJSYNC=OFF \ @@ -799,7 +798,7 @@ else pushd ${PROJ_SRC_DIR}/build >/dev/null cmake -G "Ninja" .. \ - -DCMAKE_CXX_FLAGS="-std=c++14 -fPIC ${LIBCXX_CFLAGS} ${LIBCXX_LDFLAGS} " \ + -DCMAKE_CXX_FLAGS="-std=c++14 -fPIC -stdlib=libc++ -I${LLVM_INCLUDE} -Wl,-L${LLVM_LIBPATH}" \ -DSQLITE3_INCLUDE_DIR=${SQLITE_INCLUDE_DIR} -DSQLITE3_LIBRARY=${SQLITE_LIB} \ -DEXE_SQLITE3=${SQLITE_EXE} \ -DENABLE_TIFF=OFF -DENABLE_CURL=OFF -DBUILD_SHARED_LIBS=OFF -DBUILD_PROJSYNC=OFF \ @@ -838,10 +837,6 @@ PATCHELF_EXE=${PWD}/${PATCHELF_INSTALL_DIR}/bin/patchelf if [[ -d ${PATCHELF_INSTALL_DIR} ]] ; then log "Patchelf already installed." -elif command -v patchelf >/dev/null 2>&1 ; then - log "Using system patchelf: $(command -v patchelf)" - mkdir -p ${PATCHELF_INSTALL_DIR}/bin - ln -sf "$(command -v patchelf)" ${PATCHELF_EXE} else log "Retrieving patchelf" @@ -1039,7 +1034,7 @@ if ${USE_ROS2} ; then cmake -G "Ninja" \ -DCMAKE_INSTALL_PREFIX="${FASTDDS_INSTALL_DIR}" \ -DFORCE_CXX="14" \ - -DCMAKE_CXX_FLAGS="-fPIC -std=c++14 ${LIBCXX_CFLAGS} ${LIBCXX_LDFLAGS} -DBOOST_NO_EXCEPTIONS ${UNREAL_HOSTED_CFLAGS}" \ + -DCMAKE_CXX_FLAGS="-fPIC -std=c++14 -stdlib=libc++ -I${LLVM_INCLUDE} -Wl,-L${LLVM_LIBPATH} -DBOOST_NO_EXCEPTIONS ${UNREAL_HOSTED_CFLAGS}" \ -DBUILD_SHARED_LIBS=OFF \ -DBUILD_TESTING=OFF \ -DCOMPILE_EXAMPLES=OFF \ From 1328b640e50f0cdef49fa4e73a9dbe83cccdba00 Mon Sep 17 00:00:00 2001 From: xiao-ma-gua <17358869549@163.com> Date: Thu, 22 Oct 2026 21:16:35 +0800 Subject: [PATCH 116/128] =?UTF-8?q?libc++=E6=94=B9=E4=B8=BA=E7=BB=9F?= =?UTF-8?q?=E4=B8=80=E4=BD=BF=E7=94=A8=20UE4=204.26=20=E8=87=AA=E5=B8=A6?= =?UTF-8?q?=E7=9A=84=20libc++=2010?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- Util/BuildTools/BuildOSM2ODR.sh | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/Util/BuildTools/BuildOSM2ODR.sh b/Util/BuildTools/BuildOSM2ODR.sh index c4444494ce..327d223238 100755 --- a/Util/BuildTools/BuildOSM2ODR.sh +++ b/Util/BuildTools/BuildOSM2ODR.sh @@ -165,7 +165,7 @@ if ${BUILD_OSM2ODR} ; then -G "Eclipse CDT4 - Ninja" \ -DCMAKE_C_COMPILER=/usr/bin/clang \ -DCMAKE_CXX_COMPILER=/usr/bin/clang++ \ - -DCMAKE_CXX_FLAGS="-fPIC -std=c++14 ${LIBCXX_CFLAGS} ${LIBCXX_LDFLAGS} -lc++ -lc++abi" \ + -DCMAKE_CXX_FLAGS="-fPIC -std=c++14 ${LIBCXX_CFLAGS} ${LIBCXX_LDFLAGS}" \ -DCMAKE_INSTALL_PREFIX=${LIBCARLA_INSTALL_SERVER_FOLDER} \ -DPROJ_INCLUDE_DIR=${CARLA_BUILD_FOLDER}/proj-install-server/include \ -DPROJ_LIBRARY=${CARLA_BUILD_FOLDER}/proj-install-server/lib/libproj.a \ From 966d16fe6ab8562ee625cf1e0973984579e79b7e Mon Sep 17 00:00:00 2001 From: xiao-ma-gua <17358869549@163.com> Date: Thu, 22 Oct 2026 21:30:12 +0800 Subject: [PATCH 117/128] =?UTF-8?q?=E9=87=8D=E5=90=AFCI/CD?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- Util/BuildTools/BuildOSM2ODR.sh | 1 + 1 file changed, 1 insertion(+) diff --git a/Util/BuildTools/BuildOSM2ODR.sh b/Util/BuildTools/BuildOSM2ODR.sh index 327d223238..f63fb4a21b 100755 --- a/Util/BuildTools/BuildOSM2ODR.sh +++ b/Util/BuildTools/BuildOSM2ODR.sh @@ -178,3 +178,4 @@ if ${BUILD_OSM2ODR} ; then fi log " OSM2ODR Success!" + From 227be04e4d6d4eb48ea1de9ace41be8ebd62cdae Mon Sep 17 00:00:00 2001 From: xiao-ma-gua <17358869549@163.com> Date: Fri, 23 Oct 2026 02:42:56 +0800 Subject: [PATCH 118/128] =?UTF-8?q?=E8=A7=A3=E5=86=B3=E9=93=BE=E6=8E=A5?= =?UTF-8?q?=E5=99=A8=E6=89=BE=E4=B8=8D=E5=88=B0=20pthread=20=E5=87=BD?= =?UTF-8?q?=E6=95=B0=E7=9A=84=E5=AE=9A=E4=B9=89=E9=97=AE=E9=A2=98?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- Util/BuildTools/Environment.sh | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/Util/BuildTools/Environment.sh b/Util/BuildTools/Environment.sh index 84d612495f..9ae21ab72e 100644 --- a/Util/BuildTools/Environment.sh +++ b/Util/BuildTools/Environment.sh @@ -48,12 +48,12 @@ function _libcxx_paths { function get_libcxx_cflags { _libcxx_paths || return 1 - echo "-stdlib=libc++ -nostdinc++ -isystem $LIBCXX_HEADERS" + echo "-stdlib=libc++ -nostdinc++ -isystem -pthread $LIBCXX_HEADERS" } function get_libcxx_ldflags { _libcxx_paths || return 1 - echo "-stdlib=libc++ -L$LIBCXX_LIBS" + echo "-stdlib=libc++ -pthread -L$LIBCXX_LIBS" } function get_git_repository_version { From b886a929cda52b9c7794913d89aa77f36367b289 Mon Sep 17 00:00:00 2001 From: xiao-ma-gua <17358869549@163.com> Date: Sat, 24 Oct 2026 12:26:18 +0800 Subject: [PATCH 119/128] =?UTF-8?q?=E4=BF=AE=E6=AD=A3=20-pthread=20?= =?UTF-8?q?=E5=8F=82=E6=95=B0=E4=BD=8D=E7=BD=AE=EF=BC=8C=E7=A1=AE=E4=BF=9D?= =?UTF-8?q?=E8=BF=9B=E5=85=A5=E9=93=BE=E6=8E=A5?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- Util/BuildTools/Environment.sh | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/Util/BuildTools/Environment.sh b/Util/BuildTools/Environment.sh index 9ae21ab72e..e8887a7f0a 100644 --- a/Util/BuildTools/Environment.sh +++ b/Util/BuildTools/Environment.sh @@ -48,7 +48,7 @@ function _libcxx_paths { function get_libcxx_cflags { _libcxx_paths || return 1 - echo "-stdlib=libc++ -nostdinc++ -isystem -pthread $LIBCXX_HEADERS" + echo "-stdlib=libc++ -nostdinc++ -pthread -isystem $LIBCXX_HEADERS" } function get_libcxx_ldflags { From 046e4f093ababfb37d1958a11454f9dd72afdb90 Mon Sep 17 00:00:00 2001 From: xiao-ma-gua <17358869549@163.com> Date: Wed, 28 Oct 2026 00:53:49 +0800 Subject: [PATCH 120/128] =?UTF-8?q?=E8=A7=A3=E5=86=B3car=5Fassets.zip?= =?UTF-8?q?=E4=BB=8E=E7=BA=BF=E4=B8=8A=E6=8B=89=E5=8F=96=E9=80=9F=E5=BA=A6?= =?UTF-8?q?=E6=85=A2=E7=9A=84=E9=97=AE=E9=A2=98?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- Util/BuildTools/BuildUE4Plugins.sh | 15 ++++++++++++++- 1 file changed, 14 insertions(+), 1 deletion(-) diff --git a/Util/BuildTools/BuildUE4Plugins.sh b/Util/BuildTools/BuildUE4Plugins.sh index 9abcead28c..e86d5b46e3 100755 --- a/Util/BuildTools/BuildUE4Plugins.sh +++ b/Util/BuildTools/BuildUE4Plugins.sh @@ -146,10 +146,23 @@ if [ "$BUILD_AIR" = "true" ]; then cp -a "$CACHE_DIR/AirSim" "$AIR_BUILD_PATH" else git clone -b "$AIR_BRANCH" "$AIR_REPO" "$AIR_BUILD_PATH" - fi + fi fi cd "$AIR_BUILD_PATH" echo "Current dir: $(pwd)" + + # 资产优先用 dependencies_u(工蜂)里的缓存,避免每次从 GitHub 慢速下载。 + # 预先解压出 SUV/v1.2.0 目录后,AirSim 的 setup.sh 会跳过下载分支。 + CAR_ASSETS_CACHE="$ROOT_PATH/Build/dependencies/Plugins/car_assets.zip" + SUV_TARGET="$AIR_BUILD_PATH/Unreal/Plugins/AirSim/Content/VehicleAdv/SUV/v1.2.0" + if [ ! -d "$SUV_TARGET" ] && [ -f "$CAR_ASSETS_CACHE" ]; then + echo "Using cached car_assets.zip: $CAR_ASSETS_CACHE" + if ! unzip -q "$CAR_ASSETS_CACHE" -d "$AIR_BUILD_PATH/Unreal/Plugins/AirSim/Content/VehicleAdv"; then + echo "Warning: failed to unzip $CAR_ASSETS_CACHE, falling back to download" + rm -rf "$SUV_TARGET" + fi + fi + # Build AirSim # read -p "press enter to continue ..." # 绕过代理,以解决通过代理失败的外部下载(例如 gitlab.com SSL)问题 From 1fc3a516e2bbdfb8c2e7139a4ae4c804fc6ecb14 Mon Sep 17 00:00:00 2001 From: xiao-ma-gua <17358869549@163.com> Date: Wed, 28 Oct 2026 14:36:22 +0800 Subject: [PATCH 121/128] =?UTF-8?q?=E5=81=9A=E5=AE=8Ccheck=E5=86=8D?= =?UTF-8?q?=E4=B8=8A=E4=BC=A0=E5=8F=AF=E6=89=A7=E8=A1=8C=E6=96=87=E4=BB=B6?= =?UTF-8?q?=EF=BC=8C=E9=98=B2=E6=AD=A2=E4=B8=8A=E4=BC=A0=E7=9A=84=E5=8F=AF?= =?UTF-8?q?=E4=BB=A5=E6=89=A7=E8=A1=8C=E6=96=87=E4=BB=B6=E6=89=93=E4=B8=8D?= =?UTF-8?q?=E5=BC=80?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .github/workflows/build_linux.yml | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/.github/workflows/build_linux.yml b/.github/workflows/build_linux.yml index a42e887451..6b1acc8d1c 100644 --- a/.github/workflows/build_linux.yml +++ b/.github/workflows/build_linux.yml @@ -92,11 +92,11 @@ jobs: - name: check run: | source ./setEnv64.sh - make check.upload make check.PythonAPI make check.smoke make check.vr make check.air + make check.upload shell: bash From dc05633e7c4eacf5edddd81242ed5221ab208b2e Mon Sep 17 00:00:00 2001 From: xiao-ma-gua <17358869549@163.com> Date: Thu, 29 Oct 2026 00:36:50 +0800 Subject: [PATCH 122/128] =?UTF-8?q?=E8=BF=98=E5=8E=9F=E4=B9=8B=E5=89=8D?= =?UTF-8?q?=E7=9A=84=E6=96=87=E4=BB=B6=EF=BC=8C=E5=8A=A0=E5=BF=ABxerces-c?= =?UTF-8?q?=E7=9A=84=E4=B8=8B=E8=BD=BD=E9=80=9F=E5=BA=A6?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- Util/BuildTools/BuildOSM2ODR.sh | 9 +---- Util/BuildTools/Environment.sh | 24 ----------- Util/BuildTools/Setup.sh | 70 ++++++++++++++++++++------------- 3 files changed, 44 insertions(+), 59 deletions(-) diff --git a/Util/BuildTools/BuildOSM2ODR.sh b/Util/BuildTools/BuildOSM2ODR.sh index f63fb4a21b..0cd6c8d649 100755 --- a/Util/BuildTools/BuildOSM2ODR.sh +++ b/Util/BuildTools/BuildOSM2ODR.sh @@ -117,10 +117,6 @@ if ${BUILD_OSM2ODR} ; then mkdir -p ${OSM2ODR_SERVER_BUILD_FOLDER} cd ${OSM2ODR_SERVER_BUILD_FOLDER} - if ! LIBCXX_CFLAGS=$(get_libcxx_cflags) || ! LIBCXX_LDFLAGS=$(get_libcxx_ldflags); then - fatal_error "UE4's bundled libc++ was not found (set UE4_ROOT)." - fi - # LLVM_BASENAME=llvm-8.0 # LLVM_INCLUDE="$UE4_ROOT/Engine/Source/ThirdParty/Linux/LibCxx/include/c++/v1" # LLVM_LIBPATH="$UE4_ROOT/Engine/Source/ThirdParty/Linux/LibCxx/lib/Linux/x86_64-unknown-linux-gnu" @@ -138,7 +134,7 @@ if ${BUILD_OSM2ODR} ; then -G "Eclipse CDT4 - Ninja" \ -DCMAKE_C_COMPILER=/usr/bin/clang \ -DCMAKE_CXX_COMPILER=/usr/bin/clang++ \ - -DCMAKE_CXX_FLAGS="-fPIC -std=c++14 ${LIBCXX_CFLAGS} ${LIBCXX_LDFLAGS}" \ + -DCMAKE_CXX_FLAGS="-fPIC -std=c++14 -stdlib=libc++" \ -DCMAKE_INSTALL_PREFIX=${LIBCARLA_INSTALL_SERVER_FOLDER} \ -DPROJ_INCLUDE_DIR=${CARLA_BUILD_FOLDER}/proj-install-server/include \ -DPROJ_LIBRARY=${CARLA_BUILD_FOLDER}/proj-install-server/lib/libproj.a \ @@ -165,7 +161,7 @@ if ${BUILD_OSM2ODR} ; then -G "Eclipse CDT4 - Ninja" \ -DCMAKE_C_COMPILER=/usr/bin/clang \ -DCMAKE_CXX_COMPILER=/usr/bin/clang++ \ - -DCMAKE_CXX_FLAGS="-fPIC -std=c++14 ${LIBCXX_CFLAGS} ${LIBCXX_LDFLAGS}" \ + -DCMAKE_CXX_FLAGS="-fPIC -std=c++14 -stdlib=libc++" \ -DCMAKE_INSTALL_PREFIX=${LIBCARLA_INSTALL_SERVER_FOLDER} \ -DPROJ_INCLUDE_DIR=${CARLA_BUILD_FOLDER}/proj-install-server/include \ -DPROJ_LIBRARY=${CARLA_BUILD_FOLDER}/proj-install-server/lib/libproj.a \ @@ -178,4 +174,3 @@ if ${BUILD_OSM2ODR} ; then fi log " OSM2ODR Success!" - diff --git a/Util/BuildTools/Environment.sh b/Util/BuildTools/Environment.sh index e8887a7f0a..4eda757733 100644 --- a/Util/BuildTools/Environment.sh +++ b/Util/BuildTools/Environment.sh @@ -32,30 +32,6 @@ else fi -# ============================================================================== -# -- libc++ selection ---------------------------------------------------------- -# ============================================================================== -# 统一使用 UE4.26 内置的 libc++ 10,服务端依赖在所有发行版上用同一套标准库 -# 构建,不再依赖系统 libc++ 的版本。 - -function _libcxx_paths { - local ue4="${UE4_ROOT:-$HOME/UnrealEngine_4.26}" - LIBCXX_HEADERS="$ue4/Engine/Source/ThirdParty/Linux/LibCxx/include/c++/v1" - LIBCXX_LIBS="$ue4/Engine/Source/ThirdParty/Linux/LibCxx/lib/Linux/x86_64-unknown-linux-gnu" - - [[ -f "$LIBCXX_HEADERS/__config" && -f "$LIBCXX_LIBS/libc++.a" && -f "$LIBCXX_LIBS/libc++abi.a" ]] -} - -function get_libcxx_cflags { - _libcxx_paths || return 1 - echo "-stdlib=libc++ -nostdinc++ -pthread -isystem $LIBCXX_HEADERS" -} - -function get_libcxx_ldflags { - _libcxx_paths || return 1 - echo "-stdlib=libc++ -pthread -L$LIBCXX_LIBS" -} - function get_git_repository_version { branch=$(git rev-parse --abbrev-ref HEAD) diff --git a/Util/BuildTools/Setup.sh b/Util/BuildTools/Setup.sh index d55a093076..8bf0f461c5 100755 --- a/Util/BuildTools/Setup.sh +++ b/Util/BuildTools/Setup.sh @@ -72,18 +72,16 @@ IFS="," read -r -a PY_VERSION_LIST <<< "${PY_VERSION_LIST}" mkdir -p ${CARLA_BUILD_FOLDER} pushd ${CARLA_BUILD_FOLDER} >/dev/null -if ! LIBCXX_CFLAGS=$(get_libcxx_cflags) || ! LIBCXX_LDFLAGS=$(get_libcxx_ldflags); then - echo "ERROR: the libc++ bundled with UE4 was not found (set UE4_ROOT)." >&2 - exit 1 -fi +#LLVM_INCLUDE="$UE4_ROOT/Engine/Source/ThirdParty/Linux/LibCxx/include/c++/v1" +#LLVM_LIBPATH="$UE4_ROOT/Engine/Source/ThirdParty/Linux/LibCxx/lib/Linux/x86_64-unknown-linux-gnu" #UNREAL_HOSTED_CFLAGS="--sysroot=$UE4_ROOT/Engine/Extras/ThirdPartyNotUE/SDKs/HostLinux/Linux_x64/v17_clang-10.0.1-centos7/x86_64-unknown-linux-gnu/" unset UNREAL_HOSTED_CFLAGS unset LLVM_INCLUDE unset LLVM_LIBPATH -export CXXFLAGS="-std=c++14 $LIBCXX_CFLAGS" -export LDFLAGS="$LIBCXX_LDFLAGS" +export CXXFLAGS="-std=c++14 -stdlib=libc++" +export LDFLAGS="-stdlib=libc++" # ============================================================================== # -- Generate CMake toolchains ------------------------------------------------- @@ -118,9 +116,10 @@ cp ${LIBSTDCPP_TOOLCHAIN_FILE}.gen ${LIBCPP_TOOLCHAIN_FILE}.gen cat >>${LIBCPP_TOOLCHAIN_FILE}.gen </dev/null cmake -G "Ninja" \ - -DCMAKE_CXX_FLAGS="-fPIC -std=c++14 ${LIBCXX_CFLAGS} ${LIBCXX_LDFLAGS} -DBOOST_NO_EXCEPTIONS -DASIO_NO_EXCEPTIONS ${UNREAL_HOSTED_CFLAGS}" \ + -DCMAKE_CXX_FLAGS="-fPIC -std=c++14 -stdlib=libc++ -I${LLVM_INCLUDE} -Wl,-L${LLVM_LIBPATH} -DBOOST_NO_EXCEPTIONS -DASIO_NO_EXCEPTIONS ${UNREAL_HOSTED_CFLAGS}" \ -DCMAKE_INSTALL_PREFIX="../${RPCLIB_BASENAME}-libcxx-install" \ ../${RPCLIB_BASENAME}-source @@ -364,7 +364,7 @@ else pushd ${GTEST_BASENAME}-libcxx-build >/dev/null cmake -G "Ninja" \ - -DCMAKE_CXX_FLAGS="-std=c++14 ${LIBCXX_CFLAGS} ${LIBCXX_LDFLAGS} -DBOOST_NO_EXCEPTIONS -fno-exceptions ${UNREAL_HOSTED_CFLAGS}" \ + -DCMAKE_CXX_FLAGS="-std=c++14 -stdlib=libc++ -I${LLVM_INCLUDE} -Wl,-L${LLVM_LIBPATH} -DBOOST_NO_EXCEPTIONS -fno-exceptions ${UNREAL_HOSTED_CFLAGS}" \ -DCMAKE_INSTALL_PREFIX="../${GTEST_BASENAME}-libcxx-install" \ ../${GTEST_BASENAME}-source @@ -435,7 +435,7 @@ else pushd ${RECAST_BASENAME}-build >/dev/null cmake -G "Ninja" \ - -DCMAKE_CXX_FLAGS="-std=c++14 ${LIBCXX_CFLAGS} -fPIC" \ + -DCMAKE_CXX_FLAGS="-std=c++14 -stdlib=libc++ -fPIC" \ -DCMAKE_INSTALL_PREFIX="../${RECAST_BASENAME}-install" \ -DRECASTNAVIGATION_DEMO=False \ -DRECASTNAVIGATION_TEST=False \ @@ -509,6 +509,7 @@ XERCESC_BASENAME=xerces-c-${XERCESC_VERSION} XERCESC_TEMP_FOLDER=${XERCESC_BASENAME} XERCESC_REPO=https://archive.apache.org/dist/xerces/c/3/sources/xerces-c-${XERCESC_VERSION}.tar.gz +XERCESC_SHA256SUM="fb96fc49b1fb892d1e64e53a6ada8accf6f0e6d30ce0937956ec68d39bd72c7e" XERCESC_SRC_DIR=${XERCESC_BASENAME}-source XERCESC_INSTALL_DIR=${XERCESC_BASENAME}-install @@ -520,12 +521,23 @@ if [[ -d ${XERCESC_INSTALL_DIR} && -d ${XERCESC_INSTALL_SERVER_DIR} ]] ; then log "Xerces-c already installed." else log "Retrieving xerces-c." - start=$(date +%s) - wget ${XERCESC_REPO} - end=$(date +%s) - echo "Elapsed Time downloading from xerces repo: $(($end-$start)) seconds" - # try to use the backup boost we have in Jenkins - if [[ ! -f "${XERCESC_BASENAME}.tar.gz" ]] ; then + + # 优先使用 dependencies_u(工蜂)仓库里的本地包,避免依赖外网下载 + if [ -f "dependencies/src/${XERCESC_BASENAME}.tar.gz" ] ; then + log "Using xerces-c from dependencies_u repository." + cp "dependencies/src/${XERCESC_BASENAME}.tar.gz" . + fi + + # 本地包缺失或校验不通过时,从 Apache 官方源下载 + if [[ ! -f "${XERCESC_BASENAME}.tar.gz" ]] || [[ $(sha256sum "${XERCESC_BASENAME}.tar.gz" | cut -d " " -f 1 ) != "${XERCESC_SHA256SUM}" ]] ; then + start=$(date +%s) + wget ${XERCESC_REPO} + end=$(date +%s) + echo "Elapsed Time downloading from xerces repo: $(($end-$start)) seconds" + fi + + # 仍失败则用 Jenkins 上的备份 + if [[ ! -f "${XERCESC_BASENAME}.tar.gz" ]] || [[ $(sha256sum "${XERCESC_BASENAME}.tar.gz" | cut -d " " -f 1 ) != "${XERCESC_SHA256SUM}" ]] ; then log "Using xerces backup" start=$(date +%s) wget "https://carla-releases.s3.us-east-005.backblazeb2.com/Backup/${XERCESC_BASENAME}.tar.gz" || true @@ -533,6 +545,12 @@ else echo "Elapsed Time downloading from xerces backup: $(($end-$start)) seconds" fi + # 最终校验,不通过直接报错退出,避免后面的 tar 报错 + if [[ $(sha256sum "${XERCESC_BASENAME}.tar.gz" 2>/dev/null | cut -d " " -f 1 ) != "${XERCESC_SHA256SUM}" ]] ; then + log "ERROR: xerces-c package missing or sha256 mismatch (expected ${XERCESC_SHA256SUM})." + exit 1 + fi + log "Extracting xerces-c." start=$(date +%s) @@ -566,9 +584,9 @@ else pushd ${XERCESC_SRC_DIR}/build >/dev/null cmake -G "Ninja" \ - -DCMAKE_CXX_FLAGS="-std=c++14 ${LIBCXX_CFLAGS} -fPIC -w" \ + -DCMAKE_CXX_FLAGS="-std=c++14 -stdlib=libc++ -fPIC -w" \ -DCMAKE_INSTALL_PREFIX="../../${XERCESC_INSTALL_SERVER_DIR}" \ - -DCMAKE_EXE_LINKER_FLAGS="${LIBCXX_LDFLAGS} -lc++ -lc++abi" \ + -DCMAKE_EXE_LINKER_FLAGS="-stdlib=libc++ -lc++ -lc++abi" \ -DCMAKE_BUILD_TYPE=Release \ -DBUILD_SHARED_LIBS=OFF \ -Dtranscoder=gnuiconv \ @@ -663,7 +681,7 @@ if ${USE_CHRONO} ; then mkdir -p ${CHRONO_SRC_DIR}/build pushd ${CHRONO_SRC_DIR}/build >/dev/null cmake -G "Ninja" \ - -DCMAKE_CXX_FLAGS="-fPIC -std=c++14 ${LIBCXX_CFLAGS} ${LIBCXX_LDFLAGS} -Wno-unused-command-line-argument ${UNREAL_HOSTED_CFLAGS}" \ + -DCMAKE_CXX_FLAGS="-fPIC -std=c++14 -stdlib=libc++ -I${LLVM_INCLUDE} -L${LLVM_LIBPATH} -Wno-unused-command-line-argument ${UNREAL_HOSTED_CFLAGS}" \ -DEIGEN3_INCLUDE_DIR="${EIGEN_INCLUDE}" \ -DCMAKE_INSTALL_PREFIX="${CARLA_BUILD_FOLDER}/${CHRONO_INSTALL_DIR}" \ -DCMAKE_BUILD_TYPE=Release \ @@ -781,7 +799,7 @@ else pushd ${PROJ_SRC_DIR}/build >/dev/null cmake -G "Ninja" .. \ - -DCMAKE_CXX_FLAGS="-std=c++14 -fPIC -include cstdint" \ + -DCMAKE_CXX_FLAGS="-std=c++14 -fPIC" \ -DSQLITE3_INCLUDE_DIR=${SQLITE_INCLUDE_DIR} -DSQLITE3_LIBRARY=${SQLITE_LIB} \ -DEXE_SQLITE3=${SQLITE_EXE} \ -DENABLE_TIFF=OFF -DENABLE_CURL=OFF -DBUILD_SHARED_LIBS=OFF -DBUILD_PROJSYNC=OFF \ @@ -799,7 +817,7 @@ else pushd ${PROJ_SRC_DIR}/build >/dev/null cmake -G "Ninja" .. \ - -DCMAKE_CXX_FLAGS="-std=c++14 -fPIC ${LIBCXX_CFLAGS} ${LIBCXX_LDFLAGS} -include cstdint" \ + -DCMAKE_CXX_FLAGS="-std=c++14 -fPIC -stdlib=libc++ -I${LLVM_INCLUDE} -Wl,-L${LLVM_LIBPATH}" \ -DSQLITE3_INCLUDE_DIR=${SQLITE_INCLUDE_DIR} -DSQLITE3_LIBRARY=${SQLITE_LIB} \ -DEXE_SQLITE3=${SQLITE_EXE} \ -DENABLE_TIFF=OFF -DENABLE_CURL=OFF -DBUILD_SHARED_LIBS=OFF -DBUILD_PROJSYNC=OFF \ @@ -838,10 +856,6 @@ PATCHELF_EXE=${PWD}/${PATCHELF_INSTALL_DIR}/bin/patchelf if [[ -d ${PATCHELF_INSTALL_DIR} ]] ; then log "Patchelf already installed." -elif command -v patchelf >/dev/null 2>&1 ; then - log "Using system patchelf: $(command -v patchelf)" - mkdir -p ${PATCHELF_INSTALL_DIR}/bin - ln -sf "$(command -v patchelf)" ${PATCHELF_EXE} else log "Retrieving patchelf" @@ -1039,7 +1053,7 @@ if ${USE_ROS2} ; then cmake -G "Ninja" \ -DCMAKE_INSTALL_PREFIX="${FASTDDS_INSTALL_DIR}" \ -DFORCE_CXX="14" \ - -DCMAKE_CXX_FLAGS="-fPIC -std=c++14 ${LIBCXX_CFLAGS} ${LIBCXX_LDFLAGS} -DBOOST_NO_EXCEPTIONS ${UNREAL_HOSTED_CFLAGS}" \ + -DCMAKE_CXX_FLAGS="-fPIC -std=c++14 -stdlib=libc++ -I${LLVM_INCLUDE} -Wl,-L${LLVM_LIBPATH} -DBOOST_NO_EXCEPTIONS ${UNREAL_HOSTED_CFLAGS}" \ -DBUILD_SHARED_LIBS=OFF \ -DBUILD_TESTING=OFF \ -DCOMPILE_EXAMPLES=OFF \ @@ -1236,4 +1250,4 @@ move_if_changed "${CMAKE_CONFIG_FILE}.gen" "${CMAKE_CONFIG_FILE}" popd >/dev/null -log "Success!" +log "Success!" \ No newline at end of file From 6a410d7841c6bf953179fac3827ec7cae16d9362 Mon Sep 17 00:00:00 2001 From: xiao-ma-gua <17358869549@163.com> Date: Tue, 3 Nov 2026 10:54:24 +0800 Subject: [PATCH 123/128] =?UTF-8?q?=E5=85=88=E4=BB=8E=E5=B7=A5=E8=9C=82?= =?UTF-8?q?=E4=B8=8B=E8=BD=BDosm2odr=E5=8E=8B=E7=BC=A9=E5=8C=85=E5=86=8D?= =?UTF-8?q?=E4=BB=8Egithub?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- Util/BuildTools/BuildOSM2ODR.sh | 40 +++++++++++++++++++++++++++++---- 1 file changed, 36 insertions(+), 4 deletions(-) diff --git a/Util/BuildTools/BuildOSM2ODR.sh b/Util/BuildTools/BuildOSM2ODR.sh index 0cd6c8d649..ea578f8144 100755 --- a/Util/BuildTools/BuildOSM2ODR.sh +++ b/Util/BuildTools/BuildOSM2ODR.sh @@ -82,10 +82,42 @@ if ${BUILD_OSM2ODR} ; then log "Building OSM2ODR." if [ ! -d ${OSM2ODR_SOURCE_FOLDER} ] ; then cd ${CARLA_BUILD_FOLDER} - curl --retry 5 --retry-max-time 120 -L -o OSM2ODR.zip https://github.com/carla-simulator/sumo/archive/${CURRENT_OSM2ODR_COMMIT}.zip - unzip -qq OSM2ODR.zip - rm -f OSM2ODR.zip - mv sumo-${CURRENT_OSM2ODR_COMMIT} ${OSM2ODR_SOURCE_FOLDER} + OSM2ODR_PACKAGE=osm2odr-source.zip + # dependencies_u 里的包(内层目录 osm2odr-source/) + OSM2ODR_SHA256SUM=04f8924bb1c48e8cef0b41b2b9f6d02cc878ec29c49c3a8b70150e0c804b76d5 + # GitHub 归档包(内层目录 sumo-/),源码树相同但压缩字节不同,校验和也不同 + OSM2ODR_GITHUB_SHA256SUM=7347d0158dd6cecf2f3e443b69b1479b396df70b971b3cde5770539f7c9e9f53 + + # 优先使用 dependencies_u(工蜂)仓库里的本地包,避免依赖外网下载 + if [ -f "dependencies/src/${OSM2ODR_PACKAGE}" ] ; then + log "Using osm2odr from dependencies_u repository." + cp "dependencies/src/${OSM2ODR_PACKAGE}" . + fi + + # 本地包缺失或校验不通过时,从 GitHub 下载 + if [ ! -f "${OSM2ODR_PACKAGE}" ] || [[ $(sha256sum "${OSM2ODR_PACKAGE}" | cut -d " " -f 1 ) != "${OSM2ODR_SHA256SUM}" ]] ; then + start=$(date +%s) + curl -fL --connect-timeout 30 --speed-limit 1024 --speed-time 60 \ + -o "${OSM2ODR_PACKAGE}" "https://github.com/carla-simulator/sumo/archive/${CURRENT_OSM2ODR_COMMIT}.zip" || true + end=$(date +%s) + echo "Elapsed Time downloading from GitHub: $(($end-$start)) seconds" + # 下到的是 GitHub 的包,改用它的校验和 + OSM2ODR_SHA256SUM=${OSM2ODR_GITHUB_SHA256SUM} + fi + + # 仍失败则报错 + if [ ! -f "${OSM2ODR_PACKAGE}" ] || [[ $(sha256sum "${OSM2ODR_PACKAGE}" | cut -d " " -f 1 ) != "${OSM2ODR_SHA256SUM}" ]] ; then + fatal_error "Failed to retrieve osm2odr ${CURRENT_OSM2ODR_COMMIT} from dependencies_u and GitHub." + fi + + unzip -qq "${OSM2ODR_PACKAGE}" + rm -f "${OSM2ODR_PACKAGE}" + # dependencies_u 包内是 osm2odr-source/,GitHub 包内是 sumo-/ + if [ -d osm2odr-source ] ; then + mv osm2odr-source ${OSM2ODR_SOURCE_FOLDER} + else + mv sumo-${CURRENT_OSM2ODR_COMMIT} ${OSM2ODR_SOURCE_FOLDER} + fi fi mkdir -p ${OSM2ODR_BUILD_FOLDER} From 3b304aee8d79520a505632516bb8bc46f0357041 Mon Sep 17 00:00:00 2001 From: xiao-ma-gua <17358869549@163.com> Date: Wed, 11 Nov 2026 16:34:39 +0800 Subject: [PATCH 124/128] =?UTF-8?q?=E8=A7=A3=E5=86=B3=E8=B5=84=E4=BA=A7?= =?UTF-8?q?=E7=94=9F=E6=88=90=E6=88=90=E5=8A=9F=E4=BD=86=E8=93=9D=E5=9B=BE?= =?UTF-8?q?=E4=B8=8D=E6=98=BE=E7=A4=BA?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../Source/URLabEditor/Private/MujocoMeshImporter.cpp | 7 +++++++ 1 file changed, 7 insertions(+) diff --git a/Unreal/CarlaUE4/Plugins/UnrealRoboticsLab/Source/URLabEditor/Private/MujocoMeshImporter.cpp b/Unreal/CarlaUE4/Plugins/UnrealRoboticsLab/Source/URLabEditor/Private/MujocoMeshImporter.cpp index cd56e5d92d..cad2b0bfd8 100644 --- a/Unreal/CarlaUE4/Plugins/UnrealRoboticsLab/Source/URLabEditor/Private/MujocoMeshImporter.cpp +++ b/Unreal/CarlaUE4/Plugins/UnrealRoboticsLab/Source/URLabEditor/Private/MujocoMeshImporter.cpp @@ -205,6 +205,13 @@ UStaticMesh* UMujocoGenerationAction::AttemptMeshImport(const FString& SourcePat { if (Obj) ImportedAssets.Add(Obj); } + for (const FString& ObjectPath : ImportTask->ImportedObjectPaths) + { + if (UObject* ImportedObject = LoadObject(nullptr, *ObjectPath)) + { + ImportedAssets.AddUnique(ImportedObject); + } + } // 记录所有导入的资产以进行调试 UE_LOG(LogURLabEditor, Log, TEXT("[ImportSingleMesh] Import returned %d objects:"), ImportedAssets.Num()); From ea69def882362a80c8b2c1b6cc102496e9c9d935 Mon Sep 17 00:00:00 2001 From: xiao-ma-gua <17358869549@163.com> Date: Mon, 16 Nov 2026 22:00:09 +0800 Subject: [PATCH 125/128] =?UTF-8?q?=E9=A6=96=E6=AC=A1=E6=8B=89xml=E6=96=87?= =?UTF-8?q?=E4=BB=B6=E5=88=B0=E5=86=85=E5=AE=B9=E7=BC=96=E8=BE=91=E5=99=A8?= =?UTF-8?q?=E4=B8=AD=EF=BC=8C=E6=89=80=E9=9C=80=E5=8C=85=E8=BF=9B=E8=A1=8C?= =?UTF-8?q?=E5=BC=B9=E7=AA=97=E6=8F=90=E7=A4=BA?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../URLabEditor/Private/MjPythonHelper.cpp | 205 +++++++++--------- 1 file changed, 105 insertions(+), 100 deletions(-) diff --git a/Unreal/CarlaUE4/Plugins/UnrealRoboticsLab/Source/URLabEditor/Private/MjPythonHelper.cpp b/Unreal/CarlaUE4/Plugins/UnrealRoboticsLab/Source/URLabEditor/Private/MjPythonHelper.cpp index 3b11adb09f..fffb5b7209 100644 --- a/Unreal/CarlaUE4/Plugins/UnrealRoboticsLab/Source/URLabEditor/Private/MjPythonHelper.cpp +++ b/Unreal/CarlaUE4/Plugins/UnrealRoboticsLab/Source/URLabEditor/Private/MjPythonHelper.cpp @@ -68,7 +68,7 @@ FString FMjPythonHelper::GetStoredPythonOverride() FString FileContent; if (!FFileHelper::LoadFileToString(FileContent, *IniPath)) return FString(); - // Parse simple INI: look for PythonPath= line + // 解析简单的 INI 文件:查找 PythonPath= 行 TArray Lines; FileContent.ParseIntoArrayLines(Lines); for (const FString& Line : Lines) @@ -101,21 +101,21 @@ void FMjPythonHelper::StorePythonOverride(const FString& PythonPath) FString FMjPythonHelper::ResolvePythonPath() { - // 1. Check user override + // 1. 检查用户覆盖设置 FString Override = GetStoredPythonOverride(); if (!Override.IsEmpty() && FPaths::FileExists(Override)) { return Override; } - // 2. Fall back to UE's bundled Python + // 2. 回退到 UE 内置的 Python FString Bundled = GetUEBundledPythonPath(); if (FPaths::FileExists(Bundled)) { return Bundled; } - // 3. Try system PATH as last resort + // 3. 尝试使用 PATH 系统作为最后手段 return TEXT("python"); } @@ -179,36 +179,37 @@ bool FMjPythonHelper::InstallPythonPackages(const FString& PythonPath, FString& FString FMjPythonHelper::EnsurePythonReady(bool& bOutCancelled) { bOutCancelled = false; + // 这台机器上还没有保存过 Python 解释器路径配置 bool bNeedsFirstTimeSetup = GetStoredPythonOverride().IsEmpty(); FString PythonPath = ResolvePythonPath(); bool bIsUEBundled = (PythonPath == GetUEBundledPythonPath()); - // Validate the resolved Python + // 验证已解析的 Python if (!ValidatePythonBinary(PythonPath)) { - FText Title = FText::FromString(TEXT("Python Interpreter Not Found")); + FText Title = FText::FromString(TEXT("未找到 Python 解释器")); FText Message = FText::FromString(FString::Printf( - TEXT("URLab uses Python to preprocess mesh files during MJCF import.\n\n") - TEXT("This is needed because Unreal Engine does not natively support all mesh formats ") - TEXT("used by MuJoCo (e.g. STL, OBJ with certain features). The preprocessing step ") - TEXT("converts meshes to a compatible format and resolves asset conflicts.\n\n") - TEXT("Could not find a valid Python interpreter at:\n%s\n\n") - TEXT("Click 'Yes' to browse for your Python interpreter, or 'Cancel' to cancel the import."), + TEXT("URLab 在 MJCF 导入过程中使用 Python 对网格文件进行预处理。\n\n") + TEXT("这是因为 Unreal Engine 原生不支持 MuJoCo 使用的所有网格格式") + TEXT("(例如 STL、某些带特殊特性的 OBJ)。预处理步骤会把网格转换为") + TEXT("兼容格式,并解决资产命名冲突。\n\n") + TEXT("在以下路径未找到有效的 Python 解释器:\n%s\n\n") + TEXT("点击“是”浏览选择 Python 解释器,或点击“取消”取消本次导入。"), *PythonPath)); - // EAppReturnType::Type Result = FMessageDialog::Open(EAppMsgType::YesNoCancel, Message, Title); - // if (Result == EAppReturnType::Cancel) - // { - // bOutCancelled = true; - // return FString(); - // } - // if (Result == EAppReturnType::No) - // { - // return FString(); // Skip preprocessing - // } - - // Browse for Python + EAppReturnType::Type Result = FMessageDialog::Open(EAppMsgType::YesNoCancel, Message, &Title); + if (Result == EAppReturnType::Cancel) + { + bOutCancelled = true; + return FString(); + } + if (Result == EAppReturnType::No) + { + return FString(); // 跳过预处理 + } + + // 浏览 Python PythonPath = BrowseForPython(); if (PythonPath.IsEmpty()) { @@ -218,113 +219,116 @@ FString FMjPythonHelper::EnsurePythonReady(bool& bOutCancelled) bIsUEBundled = false; } - // Show package dialog if packages are missing or first-time setup + // 如果缺少软件包或首次设置,显示软件包对话框 if (!CheckPythonPackages(PythonPath) || bNeedsFirstTimeSetup) { FString EnvLabel = bIsUEBundled - ? TEXT("Unreal Engine's bundled Python") - : FString::Printf(TEXT("your selected Python at:\n%s"), *PythonPath); + ? TEXT("Unreal Engine 自带的 Python 环境") + : FString::Printf(TEXT("你选择的 Python:\n%s"), *PythonPath); bool bPackagesPresent = CheckPythonPackages(PythonPath); FText Title = FText::FromString(bPackagesPresent - ? TEXT("Python Setup") - : TEXT("Python Packages Required")); + ? TEXT("Python 环境设置") + : TEXT("需要安装 Python 软件包")); // Button mapping: Yes = proceed, No = browse for different Python, Cancel = abort import FString MessageStr; if (bPackagesPresent) { MessageStr = FString::Printf( - TEXT("URLab uses Python to preprocess mesh files during MJCF import.\n\n") - TEXT("Required packages are already installed in %s.\n\n") - TEXT("Click 'Yes' to use this Python, 'No' to choose a different interpreter, ") - TEXT("or 'Cancel' to cancel the import."), + TEXT("URLab 在 MJCF 导入过程中使用 Python 对网格文件进行预处理。\n\n") + TEXT("所需软件包已在 %s 中安装。\n\n") + TEXT("点击“是”使用此 Python,点击“否”选择其他解释器,") + TEXT("或点击“取消”取消导入。"), *EnvLabel); } else { MessageStr = FString::Printf( - TEXT("URLab needs the 'trimesh', 'numpy', and 'scipy' Python packages to preprocess mesh files.\n\n") - TEXT("Unreal Engine does not natively support all mesh formats used by MuJoCo, ") - TEXT("so these packages are used to convert and prepare meshes for import.\n\n") - TEXT("These will be installed to %s.\n\n") - TEXT("Install now?\n\n") - TEXT("Note: The editor will be unresponsive during installation. ") - TEXT("This may take a minute.\n\n") - TEXT("Alternatively, you can install these manually in your preferred Python environment:\n") + TEXT("URLab 需要 'trimesh'、'numpy'、'scipy' 这些 Python 软件包来预处理网格文件。\n\n") + TEXT("Unreal Engine 原生不支持 MuJoCo 使用的所有网格格式,") + TEXT("因此需要这些软件包来转换并准备要导入的网格。\n\n") + TEXT("这些软件包将安装到:%s\n\n") + TEXT("现在安装吗?\n\n") + TEXT("注意:安装期间编辑器会暂时无响应,") + TEXT("可能需要几分钟左右。\n\n") + TEXT("你也可以在自己的 Python 环境中手动安装:\n") TEXT(" -m pip install trimesh numpy scipy\n") - TEXT("Then set the path in Config/LocalUnrealRoboticsLab.ini in the plugin directory.\n\n") - TEXT("Click 'Yes' to install, 'No' to choose a different interpreter, ") - TEXT("or 'Cancel' to cancel the import."), + TEXT("然后在插件目录的 Config/LocalUnrealRoboticsLab.ini 中设置解释器路径。\n\n") + TEXT("点击“是”开始安装,点击“否”选择其他解释器,") + TEXT("或点击“取消”取消导入。"), *EnvLabel); } - // EAppReturnType::Type Result; // = FMessageDialog::Open(EAppMsgType::YesNoCancel, - // FText::FromString(MessageStr), Title); - - // if (Result == EAppReturnType::Cancel) - // { - // bOutCancelled = true; - // return FString(); - // } - // else if (Result == EAppReturnType::No) - // { - // PythonPath = BrowseForPython(); - // if (PythonPath.IsEmpty()) - // { - // bOutCancelled = true; - // return FString(); - // } -// - // // Re-check packages with new Python - // if (CheckPythonPackages(PythonPath)) - // { - // UE_LOG(LogURLabEditor, Log, TEXT("[Python] Packages already available in selected Python.")); - // StorePythonOverride(PythonPath); - // return PythonPath; - // } -// - // bPackagesPresent = false; -// - // // Ask to install for the new Python - // FText InstallMsg = FText::FromString(FString::Printf( - // TEXT("Install 'trimesh', 'numpy', and 'scipy' to:\n%s?\n\n") - // TEXT("The editor will be unresponsive during installation.\n\n") - // TEXT("Click 'Cancel' to cancel the import."), *PythonPath)); - // // if (FMessageDialog::Open(EAppMsgType::OkCancel, InstallMsg, Title) == EAppReturnType::Cancel) - // // { - // // bOutCancelled = true; - // // return FString(); - // // } - // } - - // Install packages if not already present + EAppReturnType::Type Result = FMessageDialog::Open(EAppMsgType::YesNoCancel, + FText::FromString(MessageStr), &Title); + + if (Result == EAppReturnType::Cancel) + { + bOutCancelled = true; + return FString(); + } + else if (Result == EAppReturnType::No) + { + PythonPath = BrowseForPython(); + if (PythonPath.IsEmpty()) + { + bOutCancelled = true; + return FString(); + } + + // Re-check packages with new Python + if (CheckPythonPackages(PythonPath)) + { + UE_LOG(LogURLabEditor, Log, TEXT("[Python] Packages already available in selected Python.")); + StorePythonOverride(PythonPath); + return PythonPath; + } + + bPackagesPresent = false; + + // Ask to install for the new Python + FText InstallMsg = FText::FromString(FString::Printf( + TEXT("将 'trimesh'、'numpy'、'scipy' 安装到:\n%s?\n\n") + TEXT("安装期间编辑器会暂时无响应。\n\n") + TEXT("点击“取消”将取消本次导入。"), *PythonPath)); + if (FMessageDialog::Open(EAppMsgType::OkCancel, InstallMsg, &Title) == EAppReturnType::Cancel) + { + bOutCancelled = true; + return FString(); + } + } + + // 如果尚未安装,请安装相关软件包 if (!bPackagesPresent) { FString InstallLog; + const FText FailureTitle = FText::FromString(TEXT("软件包安装失败")); if (!InstallPythonPackages(PythonPath, InstallLog)) { - // FMessageDialog::Open(EAppMsgType::Ok, - // FText::FromString(FString::Printf( - // TEXT("Failed to install packages. You can install them manually by running:\n\n") - // TEXT("%s -m pip install trimesh numpy scipy\n\n") - // TEXT("Error log:\n%s"), - // *PythonPath, *InstallLog)), - // FText::FromString(TEXT("Package Install Failed"))); + FMessageDialog::Open(EAppMsgType::Ok, + FText::FromString(FString::Printf( + TEXT("软件包安装失败。你可以手动运行以下命令安装:\n\n") + TEXT("%s -m pip install trimesh numpy scipy\n\n") + TEXT("错误日志:\n%s"), + *PythonPath, *InstallLog)), + &FailureTitle); return FString(); } if (!CheckPythonPackages(PythonPath)) { - // FMessageDialog::Open(EAppMsgType::Ok, - // FText::FromString(TEXT("Packages were installed but still cannot be imported. Check your Python environment.")), - // FText::FromString(TEXT("Package Verification Failed"))); + const FText VerifyTitle = FText::FromString(TEXT("软件包验证失败")); + FMessageDialog::Open(EAppMsgType::Ok, + FText::FromString(TEXT("软件包已安装,但仍然无法导入。请检查你的 Python 环境。")), + &VerifyTitle); return FString(); } } } + // 把当前选定的 Python 解释器绝对路径保存成本机配置,供下次导入直接复用。 StorePythonOverride(PythonPath); UE_LOG(LogURLabEditor, Log, TEXT("[Python] Ready: %s"), *PythonPath); return PythonPath; @@ -338,13 +342,13 @@ FString FMjPythonHelper::BrowseForPython() { DesktopPlatform->OpenFileDialog( FSlateApplication::Get().FindBestParentWindowHandleForDialogs(nullptr), - TEXT("Select Python Interpreter"), + TEXT("选择 Python 解释器"), TEXT(""), TEXT(""), #if PLATFORM_WINDOWS - TEXT("Python Executable (*.exe)|*.exe"), + TEXT("Python 可执行文件 (*.exe)|*.exe"), #else - TEXT("All Files (*)|*"), + TEXT("所有文件 (*)|*"), #endif 0, OutFiles); } @@ -354,9 +358,10 @@ FString FMjPythonHelper::BrowseForPython() FString PythonPath = OutFiles[0]; if (!ValidatePythonBinary(PythonPath)) { - // FMessageDialog::Open(EAppMsgType::Ok, - // FText::FromString(FString::Printf(TEXT("'%s' is not a valid Python interpreter."), *PythonPath)), - // FText::FromString(TEXT("Invalid Python"))); + const FText Title = FText::FromString(TEXT("无效的 Python")); + FMessageDialog::Open(EAppMsgType::Ok, + FText::FromString(FString::Printf(TEXT("“%s”不是有效的 Python 解释器。"), *PythonPath)), + &Title); return FString(); } From e16145be49e66b5790783552e166841a409b4eff Mon Sep 17 00:00:00 2001 From: xiao-ma-gua <17358869549@163.com> Date: Thu, 19 Nov 2026 19:49:50 +0800 Subject: [PATCH 126/128] =?UTF-8?q?=E8=A7=A3=E5=86=B3MS-Human-700.xml=20?= =?UTF-8?q?=E5=AF=BC=E5=85=A5=E5=A4=B1=E8=B4=A5?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../UnrealRoboticsLab/Scripts/clean_meshes.py | 154 +++++++++++------- 1 file changed, 98 insertions(+), 56 deletions(-) diff --git a/Unreal/CarlaUE4/Plugins/UnrealRoboticsLab/Scripts/clean_meshes.py b/Unreal/CarlaUE4/Plugins/UnrealRoboticsLab/Scripts/clean_meshes.py index 989de41c04..86a9064fbf 100644 --- a/Unreal/CarlaUE4/Plugins/UnrealRoboticsLab/Scripts/clean_meshes.py +++ b/Unreal/CarlaUE4/Plugins/UnrealRoboticsLab/Scripts/clean_meshes.py @@ -21,22 +21,23 @@ # CoACD (MIT), and libzmq (MPL 2.0). See ThirdPartyNotices.txt for details. """ -Prepare MuJoCo MJCF meshes for Unreal import. +准备 MuJoCo MJCF 网格以供 Unreal 导入。 -Parses an MJCF XML, converts all referenced meshes (OBJ, STL) to GLB, -resolves naming conflicts (e.g., link1.obj and link1.stl both becoming link1.glb), -and writes an updated XML ready for drag-and-drop import into Unreal. +解析 MJCF XML(展开 文件),将所有引用的网格文件(OBJ、STL)转换为 GLB 格式, +解决命名冲突(例如 link1.obj 和 link1.stl 均被转为 link1.glb),并生成更新后的 XML 文件, +可直接拖拽导入 Unreal. -Installation: +安装: pip install trimesh numpy -Usage: +用法: python clean_meshes_trimesh.py -Example: +示例: python clean_meshes_trimesh.py "path/to/mujoco_menagerie/franka_emika_panda/panda.xml" """ + import trimesh import numpy as np from pathlib import Path @@ -46,14 +47,14 @@ def clean_mesh(mesh): - """Clean up a mesh using trimesh.""" + """使用 trimesh 清理网格。""" print(f" Original: {len(mesh.vertices)} vertices, {len(mesh.faces)} faces") mesh.merge_vertices(merge_tex=False, merge_norm=False) mesh.remove_unreferenced_vertices() mesh.fix_normals() - # Rotate -90 degrees around X for GLTF Y-up -> Unreal Z-up + # 绕 X 轴旋转 -90 度,将 GLTF 的 Y 轴向上坐标系转换为 Unreal 的 Z 轴向上坐标系 rotation_matrix = trimesh.transformations.rotation_matrix(-np.radians(90), [1, 0, 0]) mesh.apply_transform(rotation_matrix) @@ -62,7 +63,7 @@ def clean_mesh(mesh): def convert_mesh(input_path: Path, output_path: Path) -> bool: - """Convert a single mesh file to GLB.""" + """将单个网格文件转换为 GLB。""" print(f"\n Converting: {input_path.name} -> {output_path.name}") try: @@ -74,9 +75,9 @@ def convert_mesh(input_path: Path, output_path: Path) -> bool: cleaned_mesh = clean_mesh(mesh) - # Strip embedded materials/textures to prevent Unreal's Interchange importer - # from creating a Texture2D instead of a StaticMesh. - # Preserve UV coordinates so textures can be applied via material instances. + # 剥离内嵌的材质/纹理,防止 Unreal 的 Interchange 导入器 + # 将其创建为 Texture2D 而不是 StaticMesh。 + # 保留 UV 坐标,以便之后通过材质实例来应用纹理。 if hasattr(cleaned_mesh.visual, 'uv') and cleaned_mesh.visual.uv is not None: uv = cleaned_mesh.visual.uv.copy() cleaned_mesh.visual = trimesh.visual.TextureVisuals(uv=uv) @@ -104,7 +105,7 @@ def convert_mesh(input_path: Path, output_path: Path) -> bool: def process_xml(xml_path: Path): - """Parse MJCF XML, convert meshes, resolve conflicts, write updated XML.""" + """解析MJCF XML,转换网格,解决冲突,生成更新后的XML。""" if not xml_path.exists(): print(f"Error: XML file not found: {xml_path}") @@ -116,24 +117,79 @@ def process_xml(xml_path: Path): print(f"Dir: {xml_dir}") print("=" * 60) - # Parse XML + # ET.parse() 打开文件并解析整个 XML,返回一个 ElementTree 对象。 + # tree.getroot() 取出根元素。对于 MJCF 文件,根就是 标签。 tree = ET.parse(str(xml_path)) root = tree.getroot() - # Find meshdir from compiler + # 从 元素中查找 meshdir 属性 meshdir = "" for compiler in root.iter("compiler"): md = compiler.get("meshdir", "") if md: meshdir = md + # Path 的 / 运算符是路径拼接,不是算术除法。 mesh_base = xml_dir / meshdir if meshdir else xml_dir print(f"Mesh directory: {mesh_base}") - # Collect all mesh elements - mesh_elements = list(root.iter("mesh")) - - # Also convert meshes referenced by + # ------------------------------------------------------------------ + # 收集网格,递归展开 + # 相对的网格路径会相对于声明它们的文件所在目录进行解析(即 MuJoCo 的解析方式)。 + # 具有优先权,且相对于主 XML 目录。 + # ------------------------------------------------------------------ + mesh_elements = [] # (mesh_element, source_path) 列表 + # 递归展开 时的**“已访问文件”集合**,用来防止死循环和重复解析 + seen_includes = {xml_path.resolve()} + + # 递归函数:从给定节点收集网格和 ,并解析包含的文件 + def collect_from_include(node_root: ET.Element, owner_dir: Path): + base_dir = mesh_base if meshdir else owner_dir + + for mesh_el in node_root.iter("mesh"): + file_attr = mesh_el.get("file") + if not file_attr: + continue + + # 确保显式名称属性存在——如果省略,MuJoCo 默认使用文件名前缀。 + # 在更改文件属性之前,我们必须保留它。 + if mesh_el.get("name") is None: + implicit_name = Path(file_attr).stem + mesh_el.set("name", implicit_name) + print(f" Set explicit name='{implicit_name}' on mesh with file='{file_attr}'") + + # 添加网格条目:包括 mesh 元素和源文件路径 + mesh_elements.append((mesh_el, base_dir / file_attr)) + + for include_el in node_root.iter("include"): + file_attr = include_el.get("file") + if not file_attr: + continue + # resolve() 是 pathlib.Path 的方法:把路径规范化成唯一的绝对路径。 + # 它做三件事: + # 1.变成绝对路径(相对路径拼上当前工作目录) + # 2.消掉 . 和 ..(纯字符串/路径层面的规整) + # 3.解析符号链接(symlink 指向哪,就得到真实路径) + include_path = (owner_dir / file_attr).resolve() + if include_path in seen_includes: + continue + seen_includes.add(include_path) + if not include_path.exists(): + print(f" x Included file not found: {include_path}") + continue + print(f" Expanding include: {include_path}") + try: + # 得到include的文件中的根节点 + include_root = ET.parse(str(include_path)).getroot() + except ET.ParseError as e: + print(f" x Failed to parse {include_path}: {e}") + continue + # 再看include文件中的 ,递归展开 + collect_from_include(include_root, include_path.parent) + + collect_from_include(root, xml_dir) + + # 转换由 引用的网格(也包括来自 include 文件的网格)。 for flexcomp in root.iter("flexcomp"): file_attr = flexcomp.get("file") if file_attr: @@ -148,77 +204,63 @@ def process_xml(xml_path: Path): print(f"Found {len(mesh_elements)} mesh assets in XML\n") - # Phase 1: Plan output filenames, detect conflicts - # Map: glb_stem -> list of (mesh_element, source_path) + # 阶段 1:规划输出文件名,检测冲突。 + # 映射:glb_stem -> (mesh_element, source_path) 列表 stem_usage = {} - for mesh_el in mesh_elements: - file_attr = mesh_el.get("file") - if not file_attr: - continue - - source_path = mesh_base / file_attr - glb_stem = source_path.stem # e.g., "link1" from "link1.stl" - - # Ensure explicit name attribute exists — MuJoCo defaults to filename stem - # if omitted. We must preserve it before changing the file attribute. - if mesh_el.get("name") is None: - implicit_name = Path(file_attr).stem - mesh_el.set("name", implicit_name) - print(f" Set explicit name='{implicit_name}' on mesh with file='{file_attr}'") - + for mesh_el, source_path in mesh_elements: + glb_stem = source_path.stem # 例如,从 "/…/assets/geometry/link1.stl" 得到 "link1" if glb_stem not in stem_usage: stem_usage[glb_stem] = [] stem_usage[glb_stem].append((mesh_el, source_path)) - # Phase 2: Assign unique output names and resolve conflicts - # For each mesh: keep the original format (OBJ/STL) in the XML (MuJoCo needs it), - # but rename conflicting files so their stems are unique. Then convert each to GLB - # alongside — Unreal's importer will find the GLB via "higher priority" fallback. - # - # output_plan: list of (mesh_element, source_path, renamed_source_path, output_glb_path, new_file_attr) + # 阶段 2:分配唯一的输出名称并解决冲突 + # 对于每个网格:在 XML 中保留原始格式(OBJ/STL)(MuJoCo 需要它), + # 但重命名冲突的文件,使其文件名主干唯一。然后将每个网格一并转换为 GLB—— + # Unreal 的导入器会通过“更高优先级”的回退机制找到 GLB。 + # output_plan:由 (mesh_el, original_source, actual_source, output_glb) 组成的列表 import shutil output_plan = [] conflicts_found = 0 for glb_stem, entries in stem_usage.items(): if len(entries) == 1: - # No conflict + # 无冲突 mesh_el, source_path = entries[0] output_glb = source_path.with_suffix(".glb") - # file attr stays unchanged in the XML + # XML 中的 file 属性保持不变 output_plan.append((mesh_el, source_path, source_path, output_glb)) else: - # Conflict — multiple source files share the same stem + # 冲突——多个源文件共用同一主干名 conflicts_found += len(entries) - 1 print(f" CONFLICT: {len(entries)} files map to stem '{glb_stem}':") for i, (mesh_el, source_path) in enumerate(entries): mesh_name = mesh_el.get("name", "?") print(f" [{i}] mesh name='{mesh_name}' <- {source_path.name}") - # First one keeps original name, rest get a counter suffix + # 第一个保留原始名称,其余添加计数后缀 for i, (mesh_el, source_path) in enumerate(entries): if i == 0: - # No rename needed + # 无需重命名 output_glb = source_path.with_suffix(".glb") output_plan.append((mesh_el, source_path, source_path, output_glb)) else: - # Rename: link1.stl -> link1_1.stl, link1_1.glb - # Save to meshdir root (not the source subdirectory) so the - # XML file attribute stays a simple filename relative to meshdir. + # 重命名:link1.stl -> link1_1.stl,link1_1.glb + # 保存到 meshdir 根目录(而不是源文件所在子目录), + # 这样 XML 的 file 属性可以保持为相对于 meshdir 的简单文件名。 new_stem = f"{glb_stem}_{i}" ext = source_path.suffix # .stl, .obj, etc. renamed_source = mesh_base / f"{new_stem}{ext}" output_glb = mesh_base / f"{new_stem}.glb" - # Update XML file attribute (relative to meshdir) + # 更新 XML 的 file 属性(相对于 meshdir) new_file_attr = f"{new_stem}{ext}" mesh_el.set("file", new_file_attr) mesh_name = mesh_el.get("name", "?") print(f" -> Renaming '{mesh_name}': {source_path.name} -> {new_stem}{ext}") - # Copy the source file to the new name + # 将源文件复制到新名称 if not source_path.exists(): print(f" x Source file missing: {source_path} — skipping") continue @@ -231,7 +273,7 @@ def process_xml(xml_path: Path): print(f"\n{len(output_plan)} meshes to convert, {conflicts_found} naming conflicts resolved") print("=" * 60) - # Phase 3: Convert meshes to GLB (alongside the originals) + # 阶段 3:将网格转换为 GLB(与原文件放在同一目录) success_count = 0 for mesh_el, original_source, actual_source, output_glb in output_plan: mesh_name = mesh_el.get("name", "?") @@ -240,7 +282,7 @@ def process_xml(xml_path: Path): print(f"\n x Source not found: {actual_source}") continue - # Skip if GLB already exists and is newer than source + # 如果 GLB 已存在且比源文件新,则跳过 if output_glb.exists() and output_glb.stat().st_mtime > actual_source.stat().st_mtime: print(f"\n Skipping '{mesh_name}' (GLB up to date): {output_glb.name}") success_count += 1 @@ -252,7 +294,7 @@ def process_xml(xml_path: Path): else: print(f" x FAILED to convert {actual_source.name}") - # Phase 4: Write updated XML + # 阶段 4:写出更新后的 XML output_xml = xml_path.parent / f"{xml_path.stem}_ue.xml" tree.write(str(output_xml), encoding="unicode", xml_declaration=True) From 504faefa114ac4c2e00d04fbe9197b90363a7129 Mon Sep 17 00:00:00 2001 From: xiao-ma-gua <17358869549@163.com> Date: Sun, 22 Nov 2026 16:46:24 +0800 Subject: [PATCH 127/128] =?UTF-8?q?=E5=8F=96=E6=B6=88=E5=BC=BA=E5=88=B6?= =?UTF-8?q?=E6=8E=A8=E9=80=81=E8=87=B3=E5=B7=A5=E8=9C=82=E4=BB=93=E5=BA=93?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- Util/download_from_git.py | 7 ++----- 1 file changed, 2 insertions(+), 5 deletions(-) diff --git a/Util/download_from_git.py b/Util/download_from_git.py index c6275ff0b2..30b82024c7 100644 --- a/Util/download_from_git.py +++ b/Util/download_from_git.py @@ -551,12 +551,9 @@ def remove_readonly(func, path, _): commit_msg = f"{latest_file_name} - upload chunk {idx}/{total_chunks}" repo.repo.git.commit("-m", commit_msg) - # 3. 执行推送。结合上文可能有 reset_hard,首个包强制覆盖,后续包常规追加 + # 3. 执行推送。 try: - if idx == 1: - repo.repo.git.push(force=True) - else: - repo.repo.git.push() + repo.repo.git.push() print(f"Chunk {idx} successfully pushed.") except Exception as e: print(f"Failed to push chunk {idx}. Error: {e}") From 0f2801652c8ff84918e3c6db9414b80d7f3f91fa Mon Sep 17 00:00:00 2001 From: xiao-ma-gua <17358869549@163.com> Date: Sun, 22 Nov 2026 16:47:10 +0800 Subject: [PATCH 128/128] =?UTF-8?q?=E6=89=8B=E5=8A=A8=E8=A7=A6=E5=8F=91mak?= =?UTF-8?q?e=20check.upload?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .github/workflows/build_linux.yml | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/.github/workflows/build_linux.yml b/.github/workflows/build_linux.yml index 6b1acc8d1c..730df36cf3 100644 --- a/.github/workflows/build_linux.yml +++ b/.github/workflows/build_linux.yml @@ -96,7 +96,8 @@ jobs: make check.smoke make check.vr make check.air - make check.upload + # 只在手动触发时上传 + if [ "${{ github.event_name }}" = "workflow_dispatch" ]; then make check.upload; fi shell: bash