Python Tools - Session 1B
3D Mosaic Project — Part 1
January 2020
Session Navigation
Overview
In this session we kick off the 3D Mosaic project — the first hands-on project of the course. You'll learn how variables, FOR loops, and PyMEL nodes work together to load an image into Maya and begin thinking about how to break it into tiles. We also introduce object-oriented "dot notation" and the importance of pseudocode.
Key Concepts
- Variables & snake_case — storing data in named containers using Python's naming convention (
tile_size,image_path) - FOR loops — iterating over lists and
range()/xrange()to repeat operations - PyMEL nodes — object-oriented access to Maya scene elements
- Dot notation — accessing attributes and methods like a directory path:
file_node.fileTextureName.set() - Class syntax preview — thinking in objects (e.g., a
Carhasmake,model,color, and methods likestart()andstop()) - Pseudocode thinking — writing plain-English steps before coding (the PB&J exercise)
- createNode("file") — creating a file texture node in Maya to read image data
Breaking Down the Mosaic Task
Before writing any code, we think through the problem in pseudocode:
- Get the source image into Maya (create a file node, set its path)
- Read the image resolution to know how many tiles we need
- Divide the resolution by our desired tile size
- Loop through X and Y to create and position each tile
Code
This is where we ended the session — loading the image, calculating tile counts, and sketching out the loop structure in pseudocode comments:
import pymel.core as pm
# Pseudocode for my mosaic!
image_path = r"C:\path\to\your\image.jpg"
tile_size = 20
# Get the image inside maya
file_node = pm.createNode("file")
file_node.fileTextureName.set(image_path)
# Get the X/Y resolution
width_count = int(file_node.outSizeX.get() / tile_size)
height_count = int(file_node.outSizeY.get() / tile_size)
print width_count
print height_count
# Calculate number of tiles in X and Y
# For each row in z - build a row
# For each row in x