Skip to content

pytha.enumerate_materials

fabian-flassig edited this page Aug 25, 2026 · 2 revisions

Returns an iterator over all materials in the current PYTHA document.

Produces a stateless iterator triple (iterator function, state, initial value) that can be used directly in a for loop. Each iteration yields one material handle. The order in which materials are yielded is not guaranteed.

pytha.enumerate_materials()

No parameters.

Return value:

Three values suitable for use as the control expressions of a generic for loop:

Position Type Description
1 function Iterator function; advances to the next material and returns its handle, or nil when done.
2 integer Total number of materials (loop state).
3 nil Initial control value.

Notes:

  • The iterator follows the same pattern as enumerate_parts: it is safe to consume in a plain for loop.
  • Do not add or delete materials while iterating.

Example:

Print the name of every material in the document:

for mat in pytha.enumerate_materials() do
    print(pytha.get_element_name(mat))
end

Collect all materials whose name starts with "Wood":

local wood = {}
for mat in pytha.enumerate_materials() do
    local name = pytha.get_element_name(mat)
    if name:sub(1, 4) == "Wood" then
        wood[#wood + 1] = mat
    end
end

Read a specific attribute from every material:

for mat in pytha.enumerate_materials() do
    local r = pytha.get_element_attribute(mat, "reflective")
    print(pytha.get_element_name(mat), r)
end

Version Support:

Minimum PYTHA Version: V27

See also:

pytha, element handles, create_material, find_material, get_element_material, enumerate_parts

Clone this wiki locally