# Rhino 8 Python (.rhinocode) Environment Setup

This document outlines the necessary steps to configure the `.rhinocode` Python environment for Rhino 8. This setup specifically targets the installation of PyTorch and Nvidia Kaolin for GPU-accelerated 3D operations (like voxelization) directly inside Grasshopper.

## Overview
Rhino 8 introduces native Python 3 (CPython) support via the new Script Editor, and it keeps its Python environment hidden in your user folder (`~/.rhinocode/py39-rh8/`). 

Because libraries like PyTorch and Kaolin require strict CUDA graphics card bindings, using Grasshopper's built-in auto-installer (`# r: pytorch`) often fails by downloading the CPU-only version. To ensure full GPU performance, you must manually install these dependencies into Rhino's hidden Python environment via PowerShell.

## Prerequisites
- Rhino 8
- Windows OS with an Nvidia GPU
- Windows PowerShell

## Installation Instructions

Open a fresh **PowerShell** window and run the following commands sequentially. These commands use the explicit path to Rhino's hidden Python executable (`python.exe`).

### 1. Install PyTorch with CUDA 12.4
This installs the core tensor engine with proper hardware acceleration:
```powershell
& "$env:USERPROFILE\.rhinocode\py39-rh8\python.exe" -m pip install torch torchvision torchaudio --index-url https://download.pytorch.org/whl/cu124
```

### 2. Install Nvidia Kaolin
This installs the 3D backend (Kaolin v0.18.0) using the specific binary URL that perfectly matches PyTorch 2.5.1 and CUDA 12.4:
```powershell
& "$env:USERPROFILE\.rhinocode\py39-rh8\python.exe" -m pip install kaolin==0.18.0 --extra-index-url https://nvidia-kaolin.s3.us-east-2.amazonaws.com/torch-2.5.1_cu124.html
```

### 3. Install Numpy
Numpy is used for translating Grasshopper geometry/arrays into PyTorch tensors. It might be included by default, but it's safe to verify:
```powershell
& "$env:USERPROFILE\.rhinocode\py39-rh8\python.exe" -m pip install numpy
```

### 4. Install Mambapy (Required for Inference)
If you are running Mamba-based inference scripts inside Grasshopper, `mambapy` is absolutely essential.
```powershell
& "$env:USERPROFILE\.rhinocode\py39-rh8\python.exe" -m pip install mambapy
```

### 5. Install Additional BIM and Geometry Tools (IfcOpenShell, Trimesh, etc.)
Depending on your workflow, you might also need libraries for processing IFC files and advanced mesh operations. You can easily install `ifcopenshell` along with other useful packages like `trimesh` or `scipy`:
```powershell
& "$env:USERPROFILE\.rhinocode\py39-rh8\python.exe" -m pip install ifcopenshell trimesh scipy
```

## Verifying the Installation

To check if your environment is set up correctly, follow these steps:
1. Open Rhino 8 and drag a new **Python 3 Script Component** onto your Grasshopper canvas.
2. Inside the editor, paste the following boilerplate snippet:

```python
import rhinoscriptsyntax as rs
import torch
import kaolin
import numpy as np

# Verify that the GPU version loaded perfectly inside Grasshopper!
print(f"Grasshopper running Kaolin {kaolin.__version__}")
print(f"CUDA Hardware Acceleration Available: {torch.cuda.is_available()}")

if torch.cuda.is_available():
    print(f"Active GPU: {torch.cuda.get_device_name(0)}")
```

If the console confirms that CUDA is available and prints your active GPU, your `.rhinocode` environment is ready to go!