diffuse_ramp variable in Material returning NoneType object.

I am trying to make a script to modify the color ramp of every material in the file. According to the API, there is a variable in each Material called diffuse_ramp. However, my code is crashing at this line:

m.diffuse_ramp.interpolation = 'CONSTANT';

…with the error saying that “‘NoneType’ object has no attribute ‘interpolation’”.

The material is not null (the previous line is “m.diffuse_ramp_input = ‘RESULT’;”, and that works fine). And the variable diffuse_ramp definitely exists, otherwise the error would be something like “‘Material’ object has no attribute ‘diffuse_ramp’”.

Is the issue that the Color Ramp hasn’t been created yet? If so, how do I fix that (since diffuse_ramp is read only)?

I figured it out- the variable diffuse_ramp can’t be accessed if use_diffuse_ramp is false. So I needed to set that to true first (instead of at the end, like I was originally doing).