How to get all objects that have a material?

I'm trying to write a script that, given an object's name, returns an array of material names that are assigned to that object, or children of that object.

proc getMatConnections(string $geometry[]){
// Get array of all Shading Groups
string $shadingGroups[] = ls -type shadingEngine;
for ( $shadingGroup in $shadingGroups ) {
for($g in $geometry) {
$shapes = eval("listRelatives -s " + $ng);
if (sets -isMember $shadingGroup $shapes)
...
...

It dies on this if statement if $g is of some type that doesn't get materials, like lattices. I'm not even sure if this is the best way to go about it. Maybe there's a simpler way.
Any help is appreciated.

Start with something like:

listSets -type 1 -o polyShape

This is returning nothing. Any other ideas?

Sorry, but it returns names of all shading groups that "polyShape" is member of or error if "polyShape" does not exist

Yeah, you're right. I wasn't using the object's shape node. Thanks.