Integrating Orientation Adjustments for 3D Models: A Deep Dive

Working with 3D models in MorphoSource presents unique challenges in automation, particularly when handling model orientations. Our solution provides comprehensive control over 3D model views while ensuring reliable automation. Let’s explore how we’ve implemented this functionality.

Core Orientation Control System

The heart of our 3D orientation system lies in the set_orientation function:

def set_orientation(driver, actions, orientation):
    try:
        print(f"\nSetting orientation to: {orientation}")
        
        # Find the orientation controls
        orientation_label = driver.find_element(By.XPATH, 
            "//label[text()='Orientation']")
        parent_div = orientation_label.find_element(By.XPATH, 
            "./ancestor::div[contains(@class, 'grid gap-4')]")
        combobox = parent_div.find_element(By.CSS_SELECTOR, 
            'button[role="combobox"]')

Key Features

  1. Standard Orientations
    orientations = [
     'Default (Y+ Up)',
     'Upside Down (Y- Up)',
     'Forward 90° (Z- Up)',
     'Back 90° (Z+ Up)'
    ]
    
  2. Robust Element Location We use a combination of XPATH and CSS selectors to reliably locate UI elements:
    • Find orientation label
    • Navigate to parent container
    • Locate the combobox control

Advanced Interaction Handling

1. Dropdown Management

We implement multiple strategies for reliable dropdown interaction:

# Primary method
actions.move_to_element(combobox)
actions.click()
actions.perform()

# Fallback methods if needed
if not is_expanded:
    actions.move_to_element(combobox)
    actions.click_and_hold()
    actions.pause(1)
    actions.release()
    actions.perform()

2. Verification and Recovery

We verify successful interactions and implement recovery mechanisms:

is_expanded = driver.execute_script("""
    const btn = document.querySelector('button[role="combobox"]');
    return btn && btn.getAttribute('aria-expanded') === 'true';
""")

if not is_expanded:
    print("Dropdown did not open, trying alternative methods...")
    # Alternative interaction methods...

Screenshot Management

We implement systematic screenshot capture for each orientation:

screenshot_file = f"{file_id}_{orientation.replace(' ', '_')
    .replace('(', '')
    .replace(')', '')
    .replace('+', 'plus')
    .replace('°', '')}.png"

driver.save_screenshot(screenshot_file)

Browser Configuration

Our setup ensures consistent behavior across environments:

def setup_driver():
    chrome_options = Options()
    chrome_options.add_argument('--headless=new')
    chrome_options.add_argument('--no-sandbox')
    chrome_options.add_argument('--disable-dev-shm-usage')
    chrome_options.add_argument('--disable-gpu')
    chrome_options.add_argument('--window-size=1920,1080')

Timing and Synchronization

We implement strategic waits to ensure reliable operation:

# Initial page load
driver.get(url)
time.sleep(10)

# After entering fullscreen
full_screen_btn.click()
time.sleep(9)

# After orientation changes
time.sleep(9)  # Wait for view to update

Challenges and Solutions

1. UI Interaction Reliability

Challenge: Inconsistent UI element responses Solution: Multi-layered interaction strategy:

# Try standard click
actions.click()

# If failed, try alternative methods
actions.click_and_hold()
actions.pause(1)
actions.release()

2. View State Verification

Challenge: Confirming successful orientation changes Solution:

3. Error Recovery

Challenge: Handling failed interactions Solution: Comprehensive error handling:

try:
    # Attempt orientation change
    if set_orientation(driver, actions, orientation):
        # Take screenshot
    else:
        # Log failure
except Exception as e:
    print(f"Error setting orientation: {str(e)}")

Best Practices

  1. Element Location
    • Use robust selectors
    • Implement parent-child navigation
    • Verify element presence
  2. Interaction Pattern
    • Primary interaction method
    • Fallback strategies
    • Verification steps
  3. Resource Management
    • Systematic cleanup
    • Error state capture
    • Progress tracking

Future Improvements

  1. Enhanced Verification
    • Visual state confirmation
    • Orientation accuracy checks
    • Load state detection
  2. Performance Optimization
    • Reduced wait times
    • Smarter retry logic
    • Resource usage optimization
  3. Extended Functionality
    • Additional view angles
    • Custom orientation support
    • Animation capture

Conclusion

Our 3D orientation control system demonstrates how careful attention to UI interaction, state management, and error handling can create a reliable automation solution for complex 3D model manipulation. By implementing robust element location, multiple interaction strategies, and comprehensive error handling, we’ve created a system that can consistently capture 3D models from multiple orientations.

This foundation provides a robust platform for further enhancements while maintaining the reliability needed for production use. As we continue to evolve the system, these core principles will guide our development of even more sophisticated 3D model handling capabilities.


Next Post →