I am currently writing a Python script using bpy and running it on Blender 3.1.2. I have imported an object of type CURVE from another .blend file and I need to find its dimensions in the current script.
If I directly run bpy.context.object.dimensions
in the Python console, I get the correct dimensions. I verified this by using the Measure tool and also in the object properties.
However, when I run this exact same line of code from my Python script, the dimensions are incorrect. I have ensured that the correct object is selected, yet the dimensions do not match.
Why could this be happening? Any help is appreciated.
Update (Added code snippet)
def import_collection_from_file(filepath: str, collection_matcher: str):
# Copy collection from file
with bpy.data.libraries.load(filepath) as (data_from, data_to):
data_to.collections = [coll for coll in data_from.collections if collection_matcher in coll]
# Add collection to current scene
for coll in data_to.collections:
bpy.context.scene.collection.children.link(coll)
return data_to.collections
def get_objects_from_collection(collection: bpy.types.Collection, object_matcher: str, direct_children_only: bool = True):
objects = []
children = list(collection.objects) if direct_children_only else list(collection.all_objects)
for object in children:
if object_matcher in object.name:
objects.append(object)
return objects
filepath = '' # path to file to import the collection from
# Import the first collection which contains 'Profile' in its name
collection = import_collection_from_file(filepath, 'Profile')[0]
# Access the first object in the collection which contains 'Profile' in its name
obj = get_objects_from_collection(collection, 'Profile')[0]
# Select the currently added object
bpy.context.view_layer.objects.active = obj
obj.select_set(True)
print('selected objects:', bpy.context.selected_objects[:]) # prints only the object that is currently imported
print('context.object dimensions:', bpy.context.object.dimensions)
profile = bpy.data.objects['Profile']
print('profile.dimensions:', profile.dimensions)
print('selected_objects dimensions:', bpy.context.selected_objects[0].dimensions)