0
$\begingroup$

I have created an object programmatically and want to add a modifier to it too, programmatically. Here is my current code

import bpy
# make mesh
vertices = [(0, 0, 0),(0, 1, 0)]
edges = [[0,1]]
faces = []
obj_name="hello"
new_mesh = bpy.data.meshes.new('new_mesh')
new_mesh.from_pydata(vertices, edges, faces)
new_mesh.update()
# make object from mesh
new_object = bpy.data.objects.new(obj_name, new_mesh)
# make collection
# add object to scene collection
new_object.modifier_add(type='SCREW')
new_collection = bpy.data.collections.new('new_collection')
bpy.context.scene.collection.children.link(new_collection)
new_collection.objects.link(new_object)

This returns an error:

AttributeError: 'Object' object has no attribute 'modifier_add'

How would I go about fixing this?

$\endgroup$
2
  • $\begingroup$ The correct function call seems to be bpy.ops.object.modifier_add(type='SCREW'), although you might have to select object and make it the active object first $\endgroup$ – WhatAMesh Feb 14 at 13:48
  • $\begingroup$ I wanted to know if I could do it without that and using the bpy.data object $\endgroup$ – Truly Amazing Videos By Ravi A Feb 16 at 8:39