Modifier from script

Hi everyone.
I’m a noob in python, i just started learing it, and i’m trying a simple script.
I want to add a decimator modifier to a selected mesh

So i wrote in the script windows

import bpy


and running it blender add the modifier to the selected mesh.

The problem starts when i try to set the decimate ratio

Following the manual

I tryed


but it gave

AttributeError: type object 'DecimateModifier' has no attribute 'ratio'

So i tryed


and it gave

TypeError: bpy_struct.__new__() takes exactly 1 argument (0 given)

Another error came out when i tryed


to show the modifier applied in the game engine

AttributeError: 'RNA_Types' object has no attribute 'realtime'

Here the documentation

Can anyone say me what i’m doing wrong?

thanks a lot!! :eyebrowlift:

try it that way might work


the page given is for the bpy.typ but the bpy.ops!


mod =['Cube']'decimate', type='DECIMATE')
mod.ratio = 0.5
mod.realtime = True

Thank you Uncle!!

In blender now it works, but i’m trying to make it works also in blender GE.

The problem is that when i try to run it into the ge the script (i don’t know why) add 2 modifiers, and if i try to remove one of the two then it removes both. And also the applied modifier is not shown, but i need to esc from the ge to update the mesh.

Have you any idea?

i link you the file

@Ricky: the script already was adding the modifier, the problem was that i can’t set the ratio and realtime :slight_smile: