File size: 9,099 Bytes
fb5f375 | 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 | #!/usr/bin/env python3
# SPDX-FileCopyrightText: Copyright (c) 2026 NVIDIA CORPORATION & AFFILIATES. All rights reserved.
# SPDX-License-Identifier: Apache-2.0
"""
Script to update physics material properties (friction, restitution) in USD files.
This script can:
- Update physics materials in individual USD files or entire directories
- Check existing values and only update if different
- Support dry-run mode to preview changes
- Batch process multiple files
Usage:
# Update single file
python update_object_properties.py path/to/object.usd --static 5.0 --dynamic 5.0 --restitution 0.25
# Update directory of files
python update_object_properties.py path/to/objects/ --static 5.0 --dynamic 5.0
# Dry run
python update_object_properties.py path/to/objects/ --static 5.0 --dry-run
"""
import argparse
import sys
from pathlib import Path
from typing import List, Optional, Tuple
try:
from pxr import Usd, UsdGeom, UsdPhysics
except ImportError as exc:
sys.stderr.write("[ERROR] Pixar USD bindings not found. Please install the pxr Python package.\n")
raise
def update_physics_material_in_usd(
usd_path: Path,
static_friction: Optional[float] = None,
dynamic_friction: Optional[float] = None,
restitution: Optional[float] = None,
dry_run: bool = False,
verbose: bool = True
) -> Tuple[bool, str]:
"""
Update physics material properties in a USD file.
This function reads and writes USD files directly without requiring Isaac Sim.
For runtime updates within Isaac Sim, use robolab.core.utils.physics_utils.modify_friction()
Args:
usd_path: Path to the USD file
static_friction: New static friction value (None to skip)
dynamic_friction: New dynamic friction value (None to skip)
restitution: New restitution value (None to skip)
dry_run: If True, only check differences without modifying
verbose: If True, print detailed information
Returns:
Tuple of (changed, message) indicating if changes were made and a status message
"""
if not usd_path.exists():
return False, f"Error: File not found: {usd_path}"
# Open the stage
stage = Usd.Stage.Open(str(usd_path))
if not stage:
return False, f"Error: Could not open USD stage: {usd_path}"
# Get the default prim (root)
default_prim = stage.GetDefaultPrim()
if not default_prim:
return False, f"Error: No default prim found in {usd_path}"
root_prim_path = str(default_prim.GetPath())
# Get current values using our utility function
from robolab.core.utils.usd_utils import get_friction_info
current_values = get_friction_info(default_prim, stage)
if current_values is None:
if verbose:
print(f" No physics material found at {root_prim_path}/physics_material")
return False, "No physics material found"
# Check what needs to be updated
updates_needed = {}
if static_friction is not None and current_values.get('static_friction') != static_friction:
updates_needed['static_friction'] = (current_values.get('static_friction'), static_friction)
if dynamic_friction is not None and current_values.get('dynamic_friction') != dynamic_friction:
updates_needed['dynamic_friction'] = (current_values.get('dynamic_friction'), dynamic_friction)
if restitution is not None and current_values.get('restitution') != restitution:
updates_needed['restitution'] = (current_values.get('restitution'), restitution)
if not updates_needed:
if verbose:
print(f" No changes needed (current values match)")
return False, "No changes needed"
# Print what will be updated
if verbose:
for prop, (old_val, new_val) in updates_needed.items():
print(f" {prop}: {old_val} → {new_val}")
if dry_run:
return True, f"Would update {len(updates_needed)} properties"
# Apply the updates
material_prim_path = current_values['prim_path']
material_prim = stage.GetPrimAtPath(material_prim_path)
material_api = UsdPhysics.MaterialAPI(material_prim)
if 'static_friction' in updates_needed:
material_api.GetStaticFrictionAttr().Set(static_friction)
if 'dynamic_friction' in updates_needed:
material_api.GetDynamicFrictionAttr().Set(dynamic_friction)
if 'restitution' in updates_needed:
material_api.GetRestitutionAttr().Set(restitution)
# Save the stage
stage.Save()
return True, f"Updated {len(updates_needed)} properties"
def find_usd_files(path: Path) -> List[Path]:
"""
Find all USD files in a directory (recursively) or return single file.
Excludes directories starting with '_'.
Args:
path: File or directory path
Returns:
List of USD file paths
"""
if path.is_file():
return [path]
usd_extensions = ["*.usd", "*.usda", "*.usdc", "*.usdz"]
usd_files = []
for extension in usd_extensions:
for file_path in path.glob(f"**/{extension}"):
# Check if any part of the path contains a folder starting with '_'
if not any(part.startswith('_') for part in file_path.relative_to(path).parts[:-1]):
usd_files.append(file_path)
return sorted(usd_files)
def main():
parser = argparse.ArgumentParser(
description="Update physics material properties in USD files",
formatter_class=argparse.RawDescriptionHelpFormatter,
epilog="""
Examples:
# Update single file with all properties
python update_object_properties.py path/to/object.usd --static 5.0 --dynamic 5.0 --restitution 0.25
# Update only friction, leave restitution unchanged
python update_object_properties.py path/to/object.usd --static 8.0 --dynamic 8.0
# Dry run to preview changes
python update_object_properties.py path/to/objects/ --static 5.0 --dry-run
# Update all objects in a directory
python update_object_properties.py path/to/objects/ --static 5.0 --dynamic 5.0 --restitution 0.1
Note:
This script operates on USD files directly. For runtime updates within Isaac Sim,
use: from robolab.core.utils.physics_utils import modify_friction
"""
)
parser.add_argument(
"path",
type=Path,
help="Path to USD file or directory containing USD files"
)
parser.add_argument(
"--static",
"--static-friction",
type=float,
default=5.0,
dest="static_friction",
help="Static friction coefficient"
)
parser.add_argument(
"--dynamic",
"--dynamic-friction",
type=float,
default=5.0,
dest="dynamic_friction",
help="Dynamic friction coefficient"
)
parser.add_argument(
"--restitution",
type=float,
help="Restitution coefficient (bounciness)"
)
parser.add_argument(
"--dry-run",
action="store_true",
help="Preview changes without modifying files"
)
parser.add_argument(
"-q", "--quiet",
action="store_true",
help="Suppress detailed output"
)
args = parser.parse_args()
# Validate inputs
if not args.path.exists():
print(f"Error: Path does not exist: {args.path}")
return 1
if args.static_friction is None and args.dynamic_friction is None and args.restitution is None:
print("Error: At least one property (--static, --dynamic, or --restitution) must be specified")
return 1
# Find USD files
usd_files = find_usd_files(args.path)
if not usd_files:
print(f"No USD files found in {args.path}")
return 1
print(f"Found {len(usd_files)} USD file(s) to process")
if args.dry_run:
print("DRY RUN MODE - No changes will be made\n")
# Process files
updated_count = 0
skipped_count = 0
error_count = 0
for usd_file in usd_files:
if not args.quiet:
print(f"\nProcessing: {usd_file.name}")
changed, message = update_physics_material_in_usd(
usd_file,
static_friction=args.static_friction,
dynamic_friction=args.dynamic_friction,
restitution=args.restitution,
dry_run=args.dry_run,
verbose=not args.quiet
)
if "Error" in message:
error_count += 1
if args.quiet:
print(f"Error: {usd_file.name} - {message}")
elif changed:
updated_count += 1
if args.quiet:
print(f"Updated: {usd_file.name}")
else:
skipped_count += 1
# Summary
print("\n" + "="*60)
print("SUMMARY")
print("="*60)
print(f"Total files: {len(usd_files)}")
if args.dry_run:
print(f"Would update: {updated_count}")
else:
print(f"Updated: {updated_count}")
print(f"Skipped (no changes): {skipped_count}")
print(f"Errors: {error_count}")
return 0 if error_count == 0 else 1
if __name__ == "__main__":
sys.exit(main())
|