13
$\begingroup$

I need to get the x,y,z values of an object as well as the bounding box through a script. So far I have the area and volume with the following code:

bm = bmesh.new()
bm.from_object(bpy.context.object, bpy.context.scene)
area = str(sum(f.calc_area() for f in bm.faces))
volume = str(bm.calc_volume())

I tried something like this for the bounding box:

bb = str(bm.dimensions())

or

bb = bpy.data.objects["test2-2"].dimensions

but neither have worked. As far as the x,y,z values go I'm stumped. Please let me know if you have any suggestions.

Thanks!

$\endgroup$
  • 2
    $\begingroup$ I think you just need to specify which attribute to get from the dimensions (bpy.data.objects["test2-2"].dimensions.x for example) As for location, use bpy.data.objects["test2-2"].location.z $\endgroup$ – Greg Zaal Apr 10 '14 at 8:09
  • $\begingroup$ wow, that totally makes sense, and works well. Thanks a bunch! $\endgroup$ – Alex Husarenko Apr 10 '14 at 8:53
  • $\begingroup$ Wasn't sure if that's all you wanted to know - added it as an answer. $\endgroup$ – Greg Zaal Apr 10 '14 at 12:58
11
$\begingroup$

Both location and dimensions are vectors, meaning they'll return a vector of three values (x, y, z). If you want to get only one axis, use:

bpy.data.objects['object_name'].location.x
bpy.data.objects['object_name'].location.y
bpy.data.objects['object_name'].location.z

The dimensions of the bounding box of that object:

bpy.data.objects['object_name'].dimensions.x
bpy.data.objects['object_name'].dimensions.y
bpy.data.objects['object_name'].dimensions.z

You can set the values by simply using obj.location.x = 5, or if you would like to set all three axes, you can do obj.location = (1, 2, 3)

| improve this answer | |
$\endgroup$
  • 1
    $\begingroup$ If I try to use dimension.x in a driver variable, it doesn't recognize it as a valid data path. I have to write dimensions[0] to get the x value. $\endgroup$ – Max Kielland Oct 30 '14 at 19:31
17
$\begingroup$

To include parenting and drivers, use this for the actual object location / origin:

ob = bpy.context.object

ob.matrix_world.translation # or .to_translation()


You can get the bounding box corners Object.bound_box in object-space and multiply by Object.matrix_world for world-space:

from mathutils import Vector
bbox_corners = [ob.matrix_world * Vector(corner) for corner in ob.bound_box]
# bbox_corners = [ob.matrix_world @ Vector(corner) for corner in ob.bound_box]  # Use this in V2.80+

Note that the bounding box is not axis aligned.

| improve this answer | |
$\endgroup$
  • $\begingroup$ Starting from 2.8 TypeError: Element-wise multiplication: not supported between 'Matrix' and 'Vector' types: needs to be: bbox_corners = [ob.matrix_world @ Vector(corner) for corner in ob.bound_box] $\endgroup$ – oneiros Nov 5 '19 at 18:14
  • $\begingroup$ @oneiros I also encountered this error did you find any solution for it? $\endgroup$ – AvivSham Jan 28 at 6:40
  • $\begingroup$ @AvivSham it's right there: bbox_corners = [ob.matrix_world @ Vector(corner) for corner in ob.bound_box] (change the asterisk * to an at symbol @) $\endgroup$ – CodeManX Jan 28 at 13:01

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.