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

bpy.ops.object.modifier_add(type='DECIMATE')

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
http://www.blender.org/documentation/250PythonDoc/bpy.types.DecimateModifier.html#bpy.types.DecimateModifier

I tryed

bpy.types.DecimateModifier.ratio(type='0.5')

but it gave

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

So i tryed

bpy.types.DecimateModifier(ratio='0.5')

and it gave

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

Another error came out when i tryed

bpy.types.realtime('DecimateModifier')

to show the modifier applied in the game engine


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

Here the documentation

http://www.blender.org/documentation/250PythonDoc/bpy.types.Modifier.html#bpy.types.Modifier.realtime

Can anyone say me what i’m doing wrong?

thanks a lot!! :eyebrowlift:

try it that way might work

bpy.ops.object.modifier_add(type=‘MIRROR’)

the page given is for the bpy.typ but the bpy.ops!
http://www.blender.org/documentation/250PythonDoc/bpy.ops.object.html

salutations


mod = bpy.data.objects['Cube'].modifiers.new(name='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 http://www.pasteall.org/blend/3175

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