0
$\begingroup$

I'm trying to select objects that meets certain criteria in this case their dimension (not the scale) but I'm unable to figure out the proper way to make it work.

I have a over simplistic script which doesn't work although the context is the right one in the python console.

import bpy

for obj in bpy.context.scene.objects:
    if bpy.context.object.dimensions[2] = 2.0:
        obj.select_set(True)

Here is the final working code following the suggestions:

import bpy
from math import isclose


scene = bpy.context.scene

for obj in scene.objects:
    x = obj.dimensions[0]    
    y = obj.dimensions[1]
    z = obj.dimensions[2]
            
    if obj.type == 'MESH':
        if (isclose(y, 2.1, rel_tol=1e-3)):
            obj.select_set(True)
            bpy.context.view_layer.objects.active = obj
        else: 
            obj.select_set(False)
$\endgroup$
0
0
$\begingroup$

When you're use bpy.context.object you're only accessing the Active object, not the iterated object from your for loop.

Also, your if statement requires a condition (something that returns True/False), not an assigment (=).

Instead you should use:

import bpy

for obj in bpy.context.scene.objects:
    if obj.dimensions[2] == 2.0:
        obj.select_set(True)
$\endgroup$
2
  • $\begingroup$ Secrop, I just wanted you to know that I've read a lot of your stuff. You helped me out a lot starting out. It's good to see you're still around. $\endgroup$ Apr 7 '21 at 18:15
  • $\begingroup$ Thanks for such a compliment @AllenSimpson, I'm glad that I could help. If I had more time I'd be more participative, but better days will come. ;) $\endgroup$
    – Secrop
    Apr 7 '21 at 23:45
2
$\begingroup$

Allow for precision errors.

In addition to @Secrop's answer, it is a good idea to allow for precision errors when testing equality of floats, ie 2.000001 is probably close enough to 2.

A commonly seen test to see if a value is within a tolerance TOL of 2

if abs(value - 2) < TOL

or in the case here, using math.isclose

import bpy
from math import isclose

context = bpy.context

scene = context.scene

for ob in scene.objects:
    ob.select_set(
            isclose(ob.dimensions.z, 2, rel_tol=1e-6)
    )

which is setting the selection of all objects in the scene to the test result.

Using

if some_test_is_true:
    ob.select_set(True)

will make no change to selection if all where selected beforehand.

Can extend a selection using or.

    ob.select_set(
            ob.select_get()
            or
            isclose(ob.dimensions.z, 2, rel_tol=1e-6)
            )

or, as shown in your answer, add other specs with and eg ob.type == 'MESH' and ...

$\endgroup$
1
  • $\begingroup$ That's great tip, thank you. Since I'm importing objects from archicad the decimals can interfere with the proper object selection. $\endgroup$ Apr 7 '21 at 7:22

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.