-
Notifications
You must be signed in to change notification settings - Fork 0
add sample python connector with pythonnet #322
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Draft
abdullah-cognite
wants to merge
5
commits into
main
Choose a base branch
from
example-python-connector-different-strat
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Draft
Changes from all commits
Commits
Show all changes
5 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,15 @@ | ||
| # Local configuration with credentials - do not commit | ||
| config.yml | ||
|
|
||
| # State database | ||
| state.db | ||
|
|
||
| # Python cache | ||
| __pycache__/ | ||
| *.pyc | ||
|
|
||
| bin | ||
| obj | ||
|
|
||
| publish/ | ||
| files/ |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,33 @@ | ||
| using Cognite.Simulator.Utils; | ||
| using Cognite.Simulator.Utils.Automation; | ||
| using CogniteSdk.Alpha; | ||
| using Microsoft.Extensions.DependencyInjection; | ||
| using Sample.PythonConnector.Lib; | ||
|
|
||
| namespace Sample.PythonConnector; | ||
|
|
||
| public static class ConnectorRuntime | ||
| { | ||
| public static void Init() | ||
| { | ||
| DefaultConnectorRuntime<PythonConfig, DefaultModelFilestate, DefaultModelFileStatePoco> | ||
| .ConfigureServices = ConfigureServices; | ||
| DefaultConnectorRuntime<PythonConfig, DefaultModelFilestate, DefaultModelFileStatePoco> | ||
| .ConnectorName = "MuJoCo"; | ||
| DefaultConnectorRuntime<PythonConfig, DefaultModelFilestate, DefaultModelFileStatePoco> | ||
| .SimulatorDefinition = PythonSimulatorDefinitionBridge.Get(); | ||
| } | ||
|
|
||
| static void ConfigureServices(IServiceCollection services) | ||
| { | ||
| services.AddScoped<ISimulatorClient<DefaultModelFilestate, SimulatorRoutineRevision>, | ||
| PythonBridgeClient>(); | ||
| } | ||
|
|
||
| public static async Task RunStandalone() | ||
| { | ||
| Init(); | ||
| await DefaultConnectorRuntime<PythonConfig, DefaultModelFilestate, DefaultModelFileStatePoco> | ||
| .RunStandalone().ConfigureAwait(false); | ||
| } | ||
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,22 @@ | ||
| namespace Sample.PythonConnector; | ||
|
|
||
| public class Program | ||
| { | ||
| public static int Main(string[] args) | ||
| { | ||
| try | ||
| { | ||
| ConnectorRuntime.RunStandalone().Wait(); | ||
| return 0; | ||
| } | ||
| catch (Exception ex) | ||
| { | ||
| Console.Error.WriteLine($"Fatal error: {ex.Message}"); | ||
| if (ex.InnerException != null) | ||
| { | ||
| Console.Error.WriteLine($"Inner exception: {ex.InnerException.Message}"); | ||
| } | ||
| return 1; | ||
| } | ||
| } | ||
| } | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,53 @@ | ||
| # MuJoCo Python Connector | ||
|
|
||
| A sample connector using pythonnet to integrate [MuJoCo](https://mujoco.org/) physics simulator with CDF. Python files are embedded in the executable at build time. | ||
|
|
||
| ## Architecture | ||
|
|
||
| ``` | ||
| ┌─────────────────────────────────────────────────────────────┐ | ||
| │ .NET Connector Runtime │ | ||
| ├─────────────────────────────────────────────────────────────┤ | ||
| │ lib/ (C# Bridge Layer) │ | ||
| ├─────────────────────────────────────────────────────────────┤ | ||
| │ EmbeddedPythonLoader │ Extracts .py files, auto-detect │ | ||
| │ PythonBridgeBase │ GIL handling, large-stack exec │ | ||
| │ PythonBridgeClient │ ISimulatorClient implementation │ | ||
| │ PythonBridgeRoutine │ IRoutineImplementation impl │ | ||
| ├─────────────────────────────────────────────────────────────┤ | ||
| │ python/ (Embedded at build) │ | ||
| ├─────────────────────────────────────────────────────────────┤ | ||
| │ definition.py │ Simulator definition (MuJoCo) │ | ||
| │ client.py │ MuJoCo model loading/validation │ | ||
| │ routine.py │ Simulation execution engine │ | ||
| └─────────────────────────────────────────────────────────────┘ | ||
| ``` | ||
|
|
||
| ## Prerequisites | ||
|
|
||
| - .NET 8.0 SDK | ||
| - Python 3.9+ with: `pip install mujoco find-libpython` | ||
|
|
||
| ## Production Build | ||
|
|
||
| ```bash | ||
| # macOS Apple Silicon | ||
| dotnet publish -c Release -r osx-arm64 --self-contained \ | ||
| -p:PublishSingleFile=true \ | ||
| -p:IncludeNativeLibrariesForSelfExtract=true \ | ||
| -p:DebugType=none -o ./publish | ||
|
|
||
| # Linux x64 | ||
| dotnet publish -c Release -r linux-x64 --self-contained \ | ||
| -p:PublishSingleFile=true \ | ||
| -p:IncludeNativeLibrariesForSelfExtract=true \ | ||
| -p:DebugType=none -o ./publish | ||
|
|
||
| # Windows x64 | ||
| dotnet publish -c Release -r win-x64 --self-contained \ | ||
| -p:PublishSingleFile=true \ | ||
| -p:IncludeNativeLibrariesForSelfExtract=true \ | ||
| -p:DebugType=none -o ./publish | ||
| ``` | ||
|
|
||
| Produces a single ~85MB executable with embedded Python files. |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,26 @@ | ||
| <Project Sdk="Microsoft.NET.Sdk"> | ||
|
|
||
| <PropertyGroup> | ||
| <OutputType>Exe</OutputType> | ||
| <TargetFramework>net8.0</TargetFramework> | ||
| <LangVersion>12.0</LangVersion> | ||
| <ImplicitUsings>enable</ImplicitUsings> | ||
| <Nullable>enable</Nullable> | ||
| </PropertyGroup> | ||
|
|
||
| <ItemGroup> | ||
| <ProjectReference Include="..\Cognite.Simulator.Utils\Cognite.Simulator.Utils.csproj" /> | ||
| </ItemGroup> | ||
|
|
||
| <ItemGroup> | ||
| <PackageReference Include="pythonnet" Version="3.0.4" /> | ||
| </ItemGroup> | ||
|
|
||
| <!-- Embed Python files as resources at build time --> | ||
| <ItemGroup> | ||
| <EmbeddedResource Include="python\*.py"> | ||
| <LogicalName>%(Filename)%(Extension)</LogicalName> | ||
| </EmbeddedResource> | ||
| </ItemGroup> | ||
|
|
||
| </Project> |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,47 @@ | ||
| version: 1 | ||
|
|
||
| logger: | ||
| console: | ||
| level: "debug" | ||
|
|
||
| cognite: | ||
| project: ${COGNITE_PROJECT} | ||
| host: ${COGNITE_HOST} | ||
| idp-authentication: | ||
| tenant: ${COGNITE_TENANT_ID} | ||
| client-id: ${COGNITE_CLIENT_ID} | ||
| secret: ${COGNITE_CLIENT_SECRET} | ||
| # scopes: | ||
| # - https://api.cognitedata.com/.default | ||
| scopes: | ||
| - ${COGNITE_SCOPE} | ||
|
|
||
| connector: | ||
| name-prefix: "mujoco-connector@" | ||
| data-set-id: ${COGNITE_DATA_SET_ID} | ||
|
|
||
| # MuJoCo Connector Configuration | ||
| # ================================= | ||
| # This connector integrates MuJoCo physics simulator with Cognite Data Fusion. | ||
| # Python files (definition.py, client.py, routine.py) are embedded in the executable | ||
| # at build time - no external Python files needed on the target machine. | ||
| # | ||
| # Supported model file types: .xml (MJCF), .mjcf, .urdf | ||
| # | ||
| automation: | ||
| # Path to Python DLL (optional, pythonnet will auto-detect if not specified) | ||
| # On Windows: "python311.dll" or "C:\\Python311\\python311.dll" | ||
| # On Linux: "libpython3.11.so" or "/usr/lib/x86_64-linux-gnu/libpython3.11.so" | ||
| # On macOS: "libpython3.11.dylib" or "/usr/local/opt/[email protected]/Frameworks/Python.framework/Versions/3.11/lib/libpython3.11.dylib" | ||
| # python-dll: "" | ||
|
|
||
| # Python home directory (optional, for setting PYTHONHOME) | ||
| # python-home: "" | ||
|
|
||
| # Timeout for Python operations in milliseconds (5 minutes) | ||
| execution-timeout: 300000 | ||
|
|
||
| # Additional Python paths to add to sys.path (optional) | ||
| # NOTE: MuJoCo must be installed in the Python environment: pip install mujoco | ||
| # python-paths: | ||
| # - "/path/to/additional/modules" |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
The
Mainmethod blocks on an async task using.Wait(), which is a discouraged practice that can lead to deadlocks. Additionally, the top-level exception handler only logs the exception message, omitting the stack trace and other crucial details, which severely hampers debugging of fatal errors.