1
$\begingroup$

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.

enter image description here

enter image description here

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.

enter image description here

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)
$\endgroup$
8
  • 2
    $\begingroup$ Hello, it would be immensely more easier to help you if you shared the part of the script that outputs these values. Thanks :) $\endgroup$
    – Gorgious
    Apr 21, 2022 at 18:43
  • $\begingroup$ +1 to posting the code, Also you should change your units in the viewport to metric instead of inches, and compare those values the python Vector units are in metric, pretty sure and that will simplify sorting it out a bit. $\endgroup$
    – Jakemoyo
    Apr 21, 2022 at 20:36
  • $\begingroup$ @Gorgious, I have added the code snippet for your reference. $\endgroup$ Apr 22, 2022 at 7:33
  • 1
    $\begingroup$ @Jakemoyo Yeah I took the unit conversion into account. The dimensions in the viewport are in inches and are the same as the first screenshot when converted to meters (i.e (1.52, 1.87, 0.0)). The values in the third screenshot (the output of the script) are also in meters but are not equivalent to (60.2, 73.8, 0.0) inches. $\endgroup$ Apr 22, 2022 at 7:37
  • $\begingroup$ your object has any modifiers? $\endgroup$
    – X Y
    Apr 22, 2022 at 9:50

1 Answer 1

2
$\begingroup$

I finally found the solution to the problem. As I am importing the curve from another file, it requires an update for the objects that are modified by the script.

Adding bpy.context.view_layer.update() after importing the object updates the view layer and the dimensions are now correct.

Final Code:

# Functions here

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)

# Update the view layer
bpy.context.view_layer.update()

# .dimensions now gives correct results
$\endgroup$

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .