Use self with drivers
Good example to show usage of use self with drivers. This way can put a property on an object (aka self
) and drive other properties of that object with the property.
For example sake I'm using the width 2 as my basis width.
On the to be arrayed object added a custom property "length". Note: The minimum is set to the size of the minimum units length in this case 2 to avoid zero division errors in driver expression. this could be handled for the single unit case
The origin of the object has been moved x-wards to zero such that object scales left to right.
Now it becomes a matter of driving the array count and x scale to fit our length following the rules outlined
The property we created, the array modifier count and scale all belong to the arrayed object so can take advantage of "use self" in drivers. This isn't default, so have to check the use self checkbox in driver editor to use
The variable self
is the driven object. For the case of the array modifier the modifier is the object being driven ie self
is the array modifier object, BUT the ID object it belongs to is self.id_data
. So in the expression this will be the arrayed object. Bit confusing at first. If say we wanted to bring the x offset of the modifier into our calculation it would be self.relative_offset_displace.x
Default array mod setup, with x relative offset 1.
Driving the array count.
How many 2 width base units fit our length. The integer div operator //
gives us this.
self.id_data["length"] // 2
Driving the scale
Knowing the length and count above, how much bigger must a unit be than 2 to fit the length. Notice how for scale use self["length"] // 2
as the scale property belongs to the object..
(self["length"] / (self["length"] // 2)) / 2