I am brand new to both blender and python, so i apologize in advance if i'm missing something simple, but i'm having trouble with my script. The purpose of my script is to hopefully delete the default cube in the scene, scan a folder for an obj file, import that obj and then make a duplicate of the imported obj and apply a decimate modifier to it.
import os
import bpy
decimate = 0.02
resolution = '4k'
bpy.ops.object.select_by_type(type='MESH')
bpy.ops.object.delete(use_global=False)
full_path_to_directory = os.path.join('C:\\', 'Users', 'brand', 'Desktop', 'New folder (2)')
file_list = os.listdir(full_path_to_directory)
obj_list = [item for item in file_list if item[-3:] == 'obj']
for item in obj_list:
full_path_to_file = os.path.join(full_path_to_directory, item)
bpy.ops.import_scene.obj(filepath=full_path_to_file)
bpy.ops.object.duplicate()
bpy.context.active_object
for ob in bpy.context.selected_editable_objects:
ob.active_material_index = 0
for i in range(len(ob.material_slots)):
bpy.ops.object.material_slot_remove({'object': ob})
bpy.ops.object.modifier_add(type='DECIMATE')
bpy.context.object.modifiers["Decimate"].ratio = decimate
bpy.ops.object.modifier_apply(apply_as='DATA', modifier="Decimate")
bpy.context.scene.render.engine = 'CYCLES'
bpy.context.scene.selected_col = True
bpy.context.scene.selected_rough = True
bpy.context.scene.selected_normal = True
bpy.context.scene.texture_res = resolution
bpy.context.scene.restoreOrigUVmap = False
bpy.context.scene.newUVoption = True
When i run the script, i get this error
Traceback (most recent call last):
File "\Text", line 24, in <module>
File "C:\Program Files\Blender Foundation\Blender 2.81\2.81\scripts\modules\bpy\ops.py", line 201, in __call__
ret = op_call(self.idname_py(), None, kw)
RuntimeError: Operator bpy.ops.object.modifier_add.poll() failed, context is incorrect
However, if i delete the section that handles importing the obj from a folder and i simply apply the script to the default cube, everything works correctly and i get a duplicate with a decimate filter applied correctly. I have tried a few different ways of trying to fix this issue but i just cant seem to wrap my head around it, any help would be greatly appreciated. Thanks.