Is there a way to get the data path of object dimensions for using in drivers?
How to get the variable for a specific object y-dimensions for example?
Thank you Chris
Is there a way to get the data path of object dimensions for using in drivers?
How to get the variable for a specific object y-dimensions for example?
Thank you Chris
dimensions
array.The values for the object width are stored in the array as following:
object size of x-axis (width) = dimensions[0]
object size of y-axis (depth) = dimensions[1]
object size of z-axis (height) = dimensions[2]
The complete list of the object data IDs can be found here: https://docs.blender.org/api/current/bpy.types.Object.html
Here's an example how it could look like in the Drivers editor:
You can use the object viewport color to transfer information to the material. Here it's convenient because the dimensions are always positive and the RGB channels can only hold positive values so we can just use the raw value.
You can either add a driver to each of the R, G, B channel of the viewport color property by hand or by using a script.
Use dimensions[0]
for the $X$ dimension, dimensions[1]
for $Y$ and dimensions[2]
for $Z$.
Alternatively (and perhaps more intuitively) you can use dimensions.x
, dimensions.y
, dimensions.z
.
Here is a script to automate it :
import bpy
def main():
ao = bpy.context.active_object
if not ao or ao.type not in ('MESH', 'CURVE', 'SURFACE', 'META', 'FONT', 'VOLUME', 'GPENCIL'):
print("Select a mesh, curve, surface, metaball, text, volume or grease pencil object")
return
for i in range(0, 3):
add_driver_to(ao, "color", "dimensions", "dim", i)
def add_driver_to(obj, prop_to, prop_from, var_name, dim):
"""
Add a driver to obj's prop_to property
Using obj's prop_from property
Both properties must be at least of dimension dim
"""
if not obj:
return
if not hasattr(obj, prop_to):
print(f"Object '{obj.name}' doesn't have a property named '{prop_to}'")
return
driver = obj.driver_add(prop_to, dim).driver
# Make sure we don't add variables indefinitely :
var = driver.variables.get(var_name)
if not var:
var = driver.variables.new()
var.name = "dim"
var.type = 'SINGLE_PROP'
target = var.targets[0]
target.id_type = 'OBJECT'
target.id = obj
target.data_path = f"{prop_from}[{dim}]"
driver.expression = var_name
if __name__ == "__main__":
main()
To visualize what we just did, set the viewport object color to "Object".
Execute the script and scale the object.
To access it in a shader access the "Color" output of the "Object Info" node and separate each channel.
Scale along X for example (Values > 1 are pure white):