The proper way is to not use operators:
import bpy
scene = bpy.context.scene
lattice = bpy.data.lattices.new("Lattice")
lattice_ob = bpy.data.objects.new("Lattice", lattice)
lattice_ob.scale = (5, 5, 5)
for ob in scene.objects:
if ob.type == 'MESH':
mod = ob.modifiers.new("Lattice", 'LATTICE')
mod.object = lattice_ob
scene.objects.link(lattice_ob)
scene.update()
The .new()
methods return object references to be used for subsequent method calls and property assignments in contrast to operators, which run isolated and only return a status (e.g. {'FINISHED'}
). Some methods also take objects (the second parameter of objects.new()
), whereas operators don't support objects/pointers, but only references by name (StringProperty).
//update: For Blender 2.8 and above, I believe you would do it like this:
import bpy
collection = bpy.context.collection
lattice = bpy.data.lattices.new("Lattice")
lattice_ob = bpy.data.objects.new("Lattice", lattice)
lattice_ob.scale = (5, 5, 5)
for ob in collection.objects:
if ob.type == 'MESH':
mod = ob.modifiers.new("Lattice", 'LATTICE')
mod.object = lattice_ob
collection.objects.link(lattice_ob)