Featured post

new redirect for blender.org bpy docs.

http://www.blender.org/api/blender_python_api_current/ As of 10/11 november 2015 we can now link to the current api docs and not be worr...

May 27, 2012

Relative to Absolute Array Distance

Any time I use blender for more than just a poke around at updates and general bug testing, something inevitable happens. It's what I call "Oh, there should be a script for that"- syndrome. It's not always very convenient to drop what I'm doing and dive into writing a script, but over time a few ideas bubble up and eventually I do relent.

Array, absolute vs relative distance

Typically I will use arrays to quickly knock out repeating geometry, sometimes the relative distance option suffices. In relative mode the bounding box (absolute dimensions in x,y,z) is what determines the distance to array, but what if I want the array distance to be larger than 1 relative unit? well I could use values of more than 1 relative unit, but that isn't really practical for precise geometry

Perhaps this will grow into a larger, more automated script but bpy is missing hooks like 'active modifier' to make it truly tidy (at least, not yet). This snippet gets the absolute value for whatever axis is sought.

>> import bpy

"""
- first switch off any arrays, or set values to count:1
- (doesn't apply to all kinds of arrays, only those that contribute to the dimension that
corresponds with the axis you are arraying into
- apply visual transforms and scale
"""
>> ao = bpy.context.active_object
>> ao.bound_box.data.dimensions
Vector((floatx, floaty, floatz))
That doesn't really take any of the real work away from my side. So instead I end up doing something like the following, but it still forces me to do some pretty mind-numbing steps to get the raw dimensions to array on. On the up-side only a few extra lines of code are needed after this snippet
import bpy

ob = bpy.context.active_object

# get the values of the object without array, disable all modifier states above, and 
# current, not below. Disable = toggle the show_viewport option to off (if it was on)
dims = ob.bound_box.data.dimensions

# Then, re-enable all states.
# pick your array modifier
my_modifier = ob.modifiers[0]
array_data = my_modifier.relative_offset_displace
axis_values = [val*dims[idx] for idx, val in enumerate(array_data)]

# switch off relative_array, set to constant
my_modifier.use_relative_offset = False
my_modifier.use_constant_offset = True

# set constant array values
my_modifier.constant_offset_displace = axis_values
Recommended watching for this week : CS3 lecture 25: Design in Computing

  • refactor (small steps, almost mindless, check in-between)
  • no duplication
  • reusable
  • simple
  • readable
  • loose /low coupling

Next video, if you are into this kind of stuff, like I am : CS3 lecture 28: Design in Computing

  • no magic numbers, declare+assign variable once and use that instead
  • short functions = good, narrows down what the function does - easy to name
  • the overhead of a method call is very very low these days
  • descriptive variable and function names = vital
  • more than one dot (trainwreck) down.the.rabbit.hole - an object should know about as few objects as possible
  • conditional complexity, cyclomatic (few paths through a method)
  • don't have irrelevant assignments, assignments that don't perform work - check c2.com

all videos are great here : cs3 playlist

No comments:

Post a Comment

Please use Blender.StackExchange.com for python scripting questions unrelated to this post.