2
$\begingroup$

I'm writing a script that needs to make geometry using imperial units. The scene is set to imperial however this has no effect on Pythons end of things. I need to know the value that equates to an inch when using traslate functions.

Note this is not a duplicate of another question asking to get imperial lengh from Blender units.

$\endgroup$

4 Answers 4

4
$\begingroup$

If it isn't a duplicate q, the answer can certainly be gleaned from the q marked as dupe Is there a bpy function to convert from blender units to Imperial?

scale /= 0.3048  # BU to feet

which pretty much comes from 1bu = 1 metre

import bpy
import space_view3d_panel_measure as pm
scene = bpy.context.scene
uinfo = pm.getUnitsInfo()    

def bu_to_inches(scene, d):
    scale = scene.unit_settings.scale_length
    # 1bu = 1 / 0.3048 ft
    return 12 * scale * d / 0.3048

def inches_to_bu(scene, d):
    return d / bu_to_inches(scene, 1)

# 1 bu in scene units    
x = pm.convertDistance(1, uinfo)
print(x)
# 1 bu in inches
x = bu_to_inches(scene, 1)
print(x)

# 12.33 bu in scene units
x = pm.convertDistance(12.33, uinfo)
print(x)
# 12.33 bu in inches
x = bu_to_inches(scene, 12.33)
print(x)
# how many inches 12.33 bu is to bu
print(inches_to_bu(scene, x))
# how many bu 1 inch is
print(inches_to_bu(scene, 1))
$\endgroup$
3
  • $\begingroup$ Can someone please explain the factor 12 in the conversion in 'bu_to_inches'? $\endgroup$
    – Jezuz
    Sep 28, 2020 at 9:51
  • 1
    $\begingroup$ The comment mentions a blender unit in relation to feet. There are 12 inches in a foot. $\endgroup$
    – batFINGER
    Sep 28, 2020 at 10:25
  • $\begingroup$ The space_view3d_panel_measure module is not available in new versions of Blender. Anyone know if it was removed, or if there's a replacement? $\endgroup$ Sep 30, 2021 at 21:55
2
$\begingroup$

To directly answer the question...

1ft = 0.3048
1in = 0.0254

As a note to others reading this thread batFINGER's answer includes space_view3d_panel_measure module which is solely for printing out units in an easy human readable format and is not needed for unit conversion. His script also includes 'scale' which is the scale value set in propertys/scene/units/scale. Unless you are using a scale other than 1 (default) it to is unnecessary although it would be good if your script will be used by other people in which case you never know if someone is messing with scale settings or not.

The simplest way of doing unit conversion is...

distance = 2+(11/16)
inch = 0.0254

distance *= inch

Here distance is the value in inches we want converted. In this case we did 2+(11/16) to demonstrate how we would include a fraction. Its the same as saying 2" 11/16" in real life. The simple equation "distance *= inch" will convert distance from inches to Blender Units.

Below we can demonstrate a translation by moving the X axis of a selected object by 2" 11/16"

bpy.ops.transform.translate(value=(distance, 0, 0))

A full script example for this is...

import bpy

inch = 0.0254
distance = 2+(11/16)
distance *= inch

bpy.ops.transform.translate(value=(distance, 0, 0))
$\endgroup$
2
  • $\begingroup$ As a note to those who have down voted this answer, I put this here because the OP explicitly asked for the value that equates to one inch which is 0.0254. That value was not explicitly given by other answer(s). $\endgroup$ Apr 1, 2016 at 7:32
  • $\begingroup$ It would be nice to add the correction needed when the user does tinker with the scaling factor. $\endgroup$
    – kuroi neko
    Jan 9, 2023 at 20:18
0
$\begingroup$

Use:

bpy.ops.script.execute_preset(filepath="C:\\Program Files\\Blender Foundation\\Blender\\2.78\\scripts\\presets\\units_length\\inches.py",
                              menu_idname="SCENE_MT_units_length_presets")

It will change your file's default unit to inches. All subsequent python commands should be given in inches.

$\endgroup$
0
$\begingroup$

Old question but I didn't figure it out from reading the existing answers so I'll leave my own.

I was messing with the area() function and noticed that the returned value wasn't changing when I altered the grid size or the Scene Properties units.

it was returning 1/10.76 the square foot value that I saw in the measurements overlay. And I just couldn't figure out what this magic number meant. It wasn't meters, because 1 foot * 0.304 = 1 meter. Didn't look like there was any relation between these numbers.

But then a coworker helped me realize. The formula to convert square meters to square feet is exactly that multiply it by 10.76. So, the blender units were actually square meters, and I'm bad at math.

$\endgroup$

You must log in to answer this question.

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