'Edit a large number of files in Blender using Python
I need to rotate -90 degrees on the x axis mesh in every obj file, then set geometry to origin and export fbx.
from pathlib import Path
import glob
import os
import bpy
import math
#dir = "/Users/admin/Downloads/TRACKS/models/"
#list = os.listdir(dir)
#number_files = len(list)
#print (number_files)
dir = "/Users/admin/Downloads/TRACKS/models/"
os.chdir("/Users/admin/Downloads/TRACKS/models/")
for file in glob.glob("*.obj"):
#for i in range(number_files):
#bpy.ops.object.select_by_type(type='CAMERA')
#bpy.ops.object.delete()
#bpy.ops.object.select_by_type(type='LAMP')
#bpy.ops.object.delete()
objs = [ob for ob in bpy.context.scene.objects if ob.type in ('CAMERA', 'MESH', 'LIGHT')]
bpy.ops.object.delete({"selected_objects": objs})
import_path = dir + file
bpy.ops.import_scene.obj(filepath=import_path, filter_glob="*.obj",
use_edges=True, use_smooth_groups=True, use_split_objects=True, use_split_groups=True,
use_groups_as_vgroups=False, use_image_search=True, split_mode='ON',
global_clamp_size=0, axis_forward='X', axis_up='Y')
file_name = Path(import_path).stem
#objs = [ob for ob in bpy.context.scene.objects if ob.type in ('MESH')]
for o in bpy.context.scene.objects:
# if o.type == 'MESH':
#o.select_set(True)
#bpy.data.objects[o].select_set(True)
objs = [ob for ob in bpy.context.scene.objects if ob.type in ('MESH')]
bpy.context.scene.objects.select_set(objs)
bpy.context.active_object.rotation_euler[0] = math.radians(-90)
string = ("/Users/admin/export/" + file + ".fbx")
bpy.ops.export_scene.fbx(filepath=string)
bpy.ops.object.select_by_type(type="MESH")
bpy.ops.object.delete()
Terminal output:
/Applications/Blender.app/Contents/MacOS/Blender --background --python /Users/admin/blender.py
Blender 3.0.1 (hash dc2d18018171 built 2022-01-26 00:54:26)
Read prefs: /Users/admin/Library/Application Support/Blender/3.0/config/userpref.blend
( 0.0001 sec | 0.0001 sec) Importing OBJ '/Users/admin/Downloads/TRACKS/models/F50-eLabScenery_Chop_L2RA_CT_RoadA_PART0-TRN_CT_PROPS_HIGHLANDS_02_CHOP_F10_R15.obj'...
( 0.0004 sec | 0.0003 sec) Parsing OBJ file...
( 0.0009 sec | 0.0005 sec) Done, loading materials and images...
WARNING, currently unsupported ambient texture, skipped.
WARNING, currently unsupported ambient texture, skipped.
WARNING, currently unsupported ambient texture, skipped.
WARNING, currently unsupported ambient texture, skipped.
( 0.8329 sec | 0.8325 sec) Done, building geometries (verts:66 faces:44 materials: 4 smoothgroups:0) ...
( 0.8341 sec | 0.8337 sec) Done.
( 0.8341 sec | 0.8340 sec) Finished importing: '/Users/admin/Downloads/TRACKS/models/F50-eLabScenery_Chop_L2RA_CT_RoadA_PART0-TRN_CT_PROPS_HIGHLANDS_02_CHOP_F10_R15.obj'
Progress: 100.00%
Traceback (most recent call last):
File "/Users/admin/blender.py", line 53, in <module>
bpy.context.scene.objects.select_set(objs)
AttributeError: 'bpy_prop_collection' object has no attribute 'select_set'
Blender quit
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
| Solution | Source |
|---|
