import bpy
import copy
import math
import bmesh
import sys
from mathutils import Vector
def main():
remove_all_obj()
new_obj_1 = add_cube_1()
new_obj_2 = add_cube_2()
set_origin_cursor(new_obj_1.name, (3,4,5) )
set_dimensions(new_obj_2.name, (3,4,5) )
union_all_objects()
def set_dimensions(obj_name, new_dimensions):
ob = bpy.data.objects[obj_name]
ob.dimensions = new_dimensions
def set_origin_cursor(arg_objectname='Default',
arg_location=(0,0,0) ):
for ob in bpy.context.scene.objects:
ob.select_set(False)
ob = bpy.data.objects[arg_objectname]
ob.select_set(True)
bpy.context.view_layer.objects.active = ob
cursor_org_pos = copy.copy(bpy.context.scene.cursor.location)
bpy.context.scene.cursor.location = arg_location
bpy.ops.object.origin_set(type='ORIGIN_CURSOR')
bpy.context.scene.cursor.location = cursor_org_pos
return ob
def union_all_objects():
for ob in bpy.context.scene.objects:
if ob.type != 'MESH':
ob.select_set(False)
continue
ob.select_set(True)
bpy.context.view_layer.objects.active = ob
bpy.ops.object.join()
def add_cube_1():
bpy.ops.mesh.primitive_cube_add(
size=1.0,
align='WORLD',
location=(1.0, 2.0, 3.0),
rotation=(math.radians(0),
math.radians(0),
math.radians(0)),
scale=(3.6+0.25,
2.475,
2.7+0.25)
)
obj = bpy.context.view_layer.objects.active
return obj
def add_cube_2():
bpy.ops.mesh.primitive_cube_add(
location=( -1, -2, -3 ),
rotation=( math.pi/3, 0, 0 ) )
obj = bpy.context.view_layer.objects.active
obj.name = 'MakeCube'
obj.scale = ( 1, 1, 1 )
obj.select_set(False)
return obj
def remove_all_obj():
for col in bpy.data.collections:
for item in col.objects:
col.objects.unlink(item)
bpy.data.objects.remove(item)
for item in bpy.context.scene.collection.objects:
bpy.context.scene.collection.objects.unlink(item)
bpy.data.objects.remove(item)
for item in bpy.data.meshes:
bpy.data.meshes.remove(item)
for item in bpy.data.materials:
bpy.data.materials.remove(item)
if __name__ == '__main__':
main()