Python Tools - Session 11A

Scene Data Extraction & CSV Export

March 24, 2020

Session Navigation

Overview

This session analyzes all meshes in a Maya scene and exports their properties to a CSV file. The scripts extract mesh name, triangle count, vertex count, surface area, and calculate polygon density (triangles per unit area). This is useful for performance analysis and optimization in game production.

Key Concepts

  • Trawling scene meshes with listTransforms - using pm.listTransforms(type='mesh') to find all mesh nodes
  • Calculating triangle density - dividing triangle count by surface area for performance metrics
  • Vertex and triangle counting via PyMEL - using numTriangles() and numVertices()
  • CSV file writing with Python's built-in open() - writing formatted data line by line
  • String formatting with %, %i, %f, %s - formatting integers, floats, and strings for output
  • The "with" context manager for file handles - automatically closing files when done

Code

Quick version - extracting mesh data (data_from_meshes.py):

import pymel.core as pm
import os

all_mesh_transforms = pm.listTransforms(type='mesh') 

scene_data = r"your_output_directory\data.csv"
# Create the directory if it doesn't exist
dir_name = os.path.dirname(scene_data)
if not os.path.exists(dir_name):
    os.makedirs(dir_name)

with open(scene_data, 'w') as file_handle:
    file_handle.write('Mesh, Tris, Density\n')

    for mesh_transform in all_mesh_transforms:
        name = mesh_transform.name()
        tri = mesh_transform.numTriangles()
        verts = mesh_transform.numVertices()
        area = mesh_transform.area()
        density = float(tri) / area

        output = "%s, %i, %.2f\n" %(name, tri, density)
        print (output)
        file_handle.write(output)

        """
        %s = string
        %i = integer
        %f = float
        """

Fully commented version (get_scene_data.py):

"""
#############################################################################
filename    get_scene_data.py
author      Matt Osbond 
dP email    m.osbond@digipen.edu
course      CS115
Brief Description:
    Session 11A - Tuesday Mar 24th
    Trawl the scene meshes and get some basic data about them
#############################################################################
"""

import pymel.core as pm
import os

# Get all the mesh transform nodes (remember = Pymel works best on the transforms, not the shapes)
all_mesh_transforms = pm.listTransforms(type='mesh') 

# Define a path for the file to be written to
scene_data = r"your_output_directory\data.csv"

# Create the directory if it doesn't exist
dir_name = os.path.dirname(scene_data)
if not os.path.exists(dir_name):
    os.makedirs(dir_name)

# Open a file handle using the "with" context
with open(scene_data, 'w') as file_handle:

    # Write some column headers to improve readability in Excel
    file_handle.write('Mesh, Tris, Density\n')

    # Loop over all the transforms
    for mesh_transform in all_mesh_transforms:

        # Get the name of the mesh
        name = mesh_transform.name()

        # Get the triangle count of the mesh
        tri = mesh_transform.numTriangles()

        # Get the vertex count of the mesh
        verts = mesh_transform.numVertices()

        # Get the surface area of the mesh
        area = mesh_transform.area()

        # Calculate the triangle density of the mesh
        density = float(tri) / area

        # Format the line to write to the file
        output = "%s, %i, %.2f\n" %(name, tri, density)

        # Print to script editor (this is just for feedback / debugging)
        print (output)

        # Write the current line to file
        file_handle.write(output)

# When the "with" context exits (here) the file handle will be automatically closed
# You can now open the file in Excel to view the data about the scene
# Sort the columns by selecting the column data (excluding the header) ...
# ... then choose "sort" > "largest to smallest" in the top write (choose to expand selection when asked)
← Prev Next ->