1
$\begingroup$

I'm trying to build an automatic renderer using Python, loading lots of OBJ files one-by-one and rendering the results.

I can load the files no problem, but because they are different sizes, I need to either: 1. Change the objects to the same size, or 2. Move the camera so the object fits

I can't seem to get either option to work!

Thus far, the best option seems perhaps to use bpy.ops.object.dimensions and then scale, but I can't seem to get the x/y/z dimensions out of object.

$\endgroup$

1 Answer 1

1
$\begingroup$

dimensions is a property of object so you access it with object.dimensions not as a call to an operator. As a list of 3 values you can get the largest one with max(object.dimensions), or you can look at each value with object.dimensions.x

import bpy

obj = bpy.context.active_object

maxDimension = 5.0

scaleFactor = maxDimension / max(obj.dimensions)

obj.scale = (scaleFactor,scaleFactor,scaleFactor)

This assumes you are starting with a scale of 1.0. If not then you may want to use bpy.ops.object.transform_apply(location=False, rotation=False, scale=True) before starting.

$\endgroup$
5
  • $\begingroup$ It doesn't take the camera settings and distance to camera into account. If it's supposed to fit all objects into camera view, it will take quite some more effort. Operators like bpy.ops.view3d.view_all() fit the objects into viewport, but not camera view :( $\endgroup$
    – CodeManX
    Commented May 12, 2014 at 10:03
  • $\begingroup$ Thanks - this is essentially what I've had but when I try to use max I get the error TypeError: 'BPyOpsSubModOp' object is not iterable, and when I try to access the component x I get the error AttributeError: 'BPyOpsSubModOp' object has no attribute 'x'. $\endgroup$ Commented May 12, 2014 at 11:27
  • $\begingroup$ You don't access object data through operators - that is don't use bpy.ops.object in front of dimensions. Once you import and the imported model is selected bpy.context.active_object.dimensions will be the dimensions of the active object. If the object isn't selected it may be easier to access it by name - bpy.data.objects['new import'].dimensions. $\endgroup$
    – sambler
    Commented May 13, 2014 at 2:49
  • $\begingroup$ Thanks! I still really don't understand the difference between ops, context, and data but your suggestion using the named object worked the trick! $\endgroup$ Commented May 13, 2014 at 19:48
  • $\begingroup$ I thought I'd clarify that as a question $\endgroup$
    – sambler
    Commented May 14, 2014 at 2:00

You must log in to answer this question.

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