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$
2
$\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$
1
$\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$
  • $\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$ – Jeremy Bayley Apr 1 '16 at 7:32
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$

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.