I'm working on a basic script that gets the maximum x and y values for objects in a scene and generates a base plane based on that, however, I have not been able to get the bounds of the scene with the API
1 Answer
There's probably a better way to do this, but this will work.
Get the evaluated version of all the objects (which takes into account all modifiers), combine them into one object and get the bound box size from that.
import bpy
import bmesh
from mathutils import Matrix
context = bpy.context
bm = bmesh.new()
depsgraph = context.evaluated_depsgraph_get()
for obj in context.scene.objects:
if obj.type == "MESH":
eval_mesh = obj.evaluated_get(depsgraph).data.copy()
bm_tmp = bmesh.new()
bm_tmp.from_mesh(eval_mesh)
tmp_mesh = bpy.data.meshes.new("Tmp Mesh")
bm_tmp.to_mesh(tmp_mesh)
bm_tmp.free()
tmp_obj = bpy.data.objects.new("Tmp Obj", tmp_mesh)
# retain location, rotation and scale of original object
mx = obj.matrix_world
loc, rot, sca = mx.decompose()
scale_mx = Matrix()
for i in range(3):
scale_mx[i][i] = sca[i]
applymx = Matrix.Translation(loc) @ rot.to_matrix().to_4x4() @ scale_mx
tmp_obj.data.transform(applymx)
# add object to bmesh and clean up
bm.from_mesh(tmp_obj.data)
old_mesh = tmp_obj.data
bpy.data.objects.remove(tmp_obj)
bpy.data.meshes.remove(old_mesh)
bpy.data.meshes.remove(eval_mesh)
new_mesh = bpy.data.meshes.new("New Mesh")
bm.to_mesh(new_mesh)
new_object = bpy.data.objects.new("New Obj", new_mesh)
context.view_layer.active_layer_collection.collection.objects.link(new_object)
new_object.display_type = "BOUNDS"