Python Tools - Session 5A
Advanced Mesh Queries
February 4, 2020
Session Navigation
Overview
This session covers three distinct examples that build on FOR loops, IF statements, and functions. You'll learn to select mesh faces based on their normal direction, randomize vertex positions to create organic shapes, and read web pages using Python's urllib module with performance timing. These examples demonstrate how conditional logic and list operations combine to solve practical problems.
Key Concepts
- Face normals — querying face normal direction with
getNormal()to filter faces by orientation - Conditional selection — using IF statements to build a filtered list of mesh components
- List building — creating empty lists and appending items that match criteria
- Vertex randomization — using
random.uniform()to jitter vertex positions for organic effects - URL reading — using
urllibandtimemodules for web requests and performance measurement
Code — Select Upward-Facing Faces
Select all faces on a sphere whose normals point upward (positive Y).
"""
Select faces on a sphere where the normals point up
"""
import pymel.core as pm
# Create the sphere
my_sphere = pm.polySphere()[0]
# Create an empty list to collect the faces in
face_list = list()
# Iterate over the faces using a FOR loop
for face in my_sphere.faces:
# Get the normal for each face
normal = face.getNormal()
# Check the SECOND element (the Y axis) is above 0
# If Y = 1, it points up
# If Y = -1 it points down
# If Y = 0 it points horizontally
if normal[1] > 0:
# If the face normal points upwards, add it to the list
face_list.append(face)
# Select the whole list of collected faces
pm.select(face_list)
Code — Randomize Vertices
Randomly offset each vertex on a selected mesh, then smooth the result for an organic look.
"""
Randomize verts on an object
"""
import pymel.core as pm
import random
def randomize(object, range):
"""Randomly go through each vertex on the specified object
and move it randomly in between negative range and positive range
"""
# Iterate over each object vertex
for vert in object.verts:
# Get a random number in the range
# e.g. if range = 2, then the number will be between -2 and 2
rand_x = random.uniform(-range, range)
rand_y = random.uniform(-range, range)
rand_z = random.uniform(-range, range)
# Move the vertex in the random direction
# remember to move it RELATIVE to the current position
pm.move(vert, [rand_x, rand_y, rand_z], relative=True)
# Get the first selected object (no error checking here)
selected = pm.ls(selection=True)[0]
# Randomize the verts on the object by calling the function
randomize(selected, 1.2)
# Smooth the object
pm.polySmooth(selected)
Code — Read a URL
Read a web page and report its character count and how long the request took.
"""
Open a URL and read the text source
"""
import time
import urllib
def read_site(url):
"""Takes a URL, and reads it, printing out the character length and how long it took
"""
# Get the time at the beginning of the process
start_time = time.time()
# Read the URL into a string
html = urllib.urlopen(url).read()
# Calculate the millisecond duration, by taking the current time and subtracting the start time
ms = (time.time() - start_time) * 1000.0
# Format the length using a thousand comma separator
html_length = format(len(html), ',d')
# Print the results
print ('Read %s in %.1f ms\nSite contains %s characters' % (url, ms, html_length))
# Run the function on a URL
read_site('https://www.google.com')