Python: replace '%' with str.format for examples & templates
Use modern/preferred string formatting for user facing scripts.
This commit is contained in:
parent
0bb6317035
commit
98986c6562
@ -21,4 +21,4 @@ if "Cube" in bpy.data.meshes:
|
||||
import os
|
||||
with open(os.path.splitext(bpy.data.filepath)[0] + ".txt", 'w') as fs:
|
||||
for image in bpy.data.images:
|
||||
fs.write("%s %d x %d\n" % (image.filepath, image.size[0], image.size[1]))
|
||||
fs.write("{:s} {:d} x {:d}\n".format(image.filepath, image.size[0], image.size[1]))
|
||||
|
@ -21,8 +21,9 @@ class OBJECT_OT_property_example(bpy.types.Operator):
|
||||
|
||||
def execute(self, context):
|
||||
self.report(
|
||||
{'INFO'}, 'F: %.2f B: %s S: %r' %
|
||||
(self.my_float, self.my_bool, self.my_string)
|
||||
{'INFO'}, "F: {:.2f} B: {:s} S: {!r}".format(
|
||||
self.my_float, self.my_bool, self.my_string,
|
||||
)
|
||||
)
|
||||
print('My float:', self.my_float)
|
||||
print('My bool:', self.my_bool)
|
||||
|
@ -53,9 +53,9 @@ class OBJECT_OT_addon_prefs_example(Operator):
|
||||
preferences = context.preferences
|
||||
addon_prefs = preferences.addons[__name__].preferences
|
||||
|
||||
info = ("Path: %s, Number: %d, Boolean %r" %
|
||||
(addon_prefs.filepath, addon_prefs.number, addon_prefs.boolean))
|
||||
|
||||
info = "Path: {:s}, Number: {:d}, Boolean {!r}".format(
|
||||
addon_prefs.filepath, addon_prefs.number, addon_prefs.boolean,
|
||||
)
|
||||
self.report({'INFO'}, info)
|
||||
print(info)
|
||||
|
||||
|
@ -18,7 +18,7 @@ import bpy
|
||||
|
||||
def dump(obj, text):
|
||||
for attr in dir(obj):
|
||||
print("%r.%s = %s" % (obj, attr, getattr(obj, attr)))
|
||||
print("{!r}.{:s} = {:s}".format(obj, attr, getattr(obj, attr)))
|
||||
|
||||
|
||||
class WM_OT_button_context_test(bpy.types.Operator):
|
||||
|
@ -31,10 +31,10 @@ me = bpy.context.object.data
|
||||
uv_layer = me.uv_layers.active.data
|
||||
|
||||
for poly in me.polygons:
|
||||
print("Polygon index: %d, length: %d" % (poly.index, poly.loop_total))
|
||||
print("Polygon index: {:d}, length: {:d}".format(poly.index, poly.loop_total))
|
||||
|
||||
# range is used here to show how the polygons reference loops,
|
||||
# for convenience 'poly.loop_indices' can be used instead.
|
||||
for loop_index in range(poly.loop_start, poly.loop_start + poly.loop_total):
|
||||
print(" Vertex: %d" % me.loops[loop_index].vertex_index)
|
||||
print(" UV: %r" % uv_layer[loop_index].uv)
|
||||
print(" Vertex: {:d}".format(me.loops[loop_index].vertex_index))
|
||||
print(" UV: {!r}".format(uv_layer[loop_index].uv))
|
||||
|
@ -34,7 +34,7 @@ class SimpleMouseOperator(bpy.types.Operator):
|
||||
def execute(self, context):
|
||||
# rather than printing, use the report function,
|
||||
# this way the message appears in the header,
|
||||
self.report({'INFO'}, "Mouse coords are %d %d" % (self.x, self.y))
|
||||
self.report({'INFO'}, "Mouse coords are {:d} {:d}".format(self.x, self.y))
|
||||
return {'FINISHED'}
|
||||
|
||||
def invoke(self, context, event):
|
||||
|
@ -16,9 +16,8 @@ class DialogOperator(bpy.types.Operator):
|
||||
my_string: bpy.props.StringProperty(name="String Value")
|
||||
|
||||
def execute(self, context):
|
||||
message = (
|
||||
"Popup Values: %f, %d, '%s'" %
|
||||
(self.my_float, self.my_bool, self.my_string)
|
||||
message = "Popup Values: {:f}, {:d}, '{:s}'".format(
|
||||
self.my_float, self.my_bool, self.my_string,
|
||||
)
|
||||
self.report({'INFO'}, message)
|
||||
return {'FINISHED'}
|
||||
|
@ -10,21 +10,21 @@ col.s *= 0.5
|
||||
print("Color R:", col.r)
|
||||
print("Color G:", col[1])
|
||||
print("Color B:", col[-1])
|
||||
print("Color HSV: %.2f, %.2f, %.2f", col[:])
|
||||
print("Color HSV: {:.2f}, {:.2f}, {:.2f}".format(*col))
|
||||
|
||||
|
||||
# components of an existing color can be set
|
||||
col[:] = 0.0, 0.5, 1.0
|
||||
|
||||
# components of an existing color can use slice notation to get a tuple
|
||||
print("Values: %f, %f, %f" % col[:])
|
||||
print("Values: {:f}, {:f}, {:f}".format(*col))
|
||||
|
||||
# colors can be added and subtracted
|
||||
col += mathutils.Color((0.25, 0.0, 0.0))
|
||||
|
||||
# Color can be multiplied, in this example color is scaled to 0-255
|
||||
# can printed as integers
|
||||
print("Color: %d, %d, %d" % (col * 255.0)[:])
|
||||
print("Color: {:d}, {:d}, {:d}".format(*(int(c) for c in (col * 255.0))))
|
||||
|
||||
# This example prints the color as hexadecimal
|
||||
print("Hexadecimal: %.2x%.2x%.2x" % (int(col.r * 255), int(col.g * 255), int(col.b * 255)))
|
||||
print("Hexadecimal: {:02x}{:02x}{:02x}".format(int(col.r * 255), int(col.g * 255), int(col.b * 255)))
|
||||
|
@ -16,7 +16,7 @@ print("Euler Z", eul[-1])
|
||||
eul[:] = 1.0, 2.0, 3.0
|
||||
|
||||
# components of an existing euler can use slice notation to get a tuple
|
||||
print("Values: %f, %f, %f" % eul[:])
|
||||
print("Values: {:f}, {:f}, {:f}".format(*eul))
|
||||
|
||||
# the order can be set at any time too
|
||||
eul.order = 'ZYX'
|
||||
|
@ -18,9 +18,8 @@ quat_out = quat_a @ quat_b
|
||||
# print the quat, euler degrees for mere mortals and (axis, angle)
|
||||
print("Final Rotation:")
|
||||
print(quat_out)
|
||||
print("%.2f, %.2f, %.2f" % tuple(math.degrees(a) for a in quat_out.to_euler()))
|
||||
print("(%.2f, %.2f, %.2f), %.2f" % (quat_out.axis[:] +
|
||||
(math.degrees(quat_out.angle), )))
|
||||
print("{:.2f}, {:.2f}, {:.2f}".format(*(math.degrees(a) for a in quat_out.to_euler())))
|
||||
print("({:.2f}, {:.2f}, {:.2f}), {:.2f}".format(*quat_out.axis, math.degrees(quat_out.angle)))
|
||||
|
||||
# multiple rotations can be interpolated using the exponential map
|
||||
quat_c = mathutils.Quaternion((1.0, 0.0, 0.0), math.radians(15.0))
|
||||
|
@ -108,7 +108,7 @@ class MyCustomShapeWidget(Gizmo):
|
||||
delta /= 10.0
|
||||
value = self.init_value - delta
|
||||
self.target_set_value("offset", value)
|
||||
context.area.header_text_set("My Gizmo: %.4f" % value)
|
||||
context.area.header_text_set("My Gizmo: {:.4f}".format(value))
|
||||
return {'RUNNING_MODAL'}
|
||||
|
||||
|
||||
|
@ -4,7 +4,7 @@ import bpy
|
||||
def write_some_data(context, filepath, use_some_setting):
|
||||
print("running write_some_data...")
|
||||
f = open(filepath, 'w', encoding='utf-8')
|
||||
f.write("Hello World %s" % use_some_setting)
|
||||
f.write("Hello World {:s}".format(use_some_setting))
|
||||
f.close()
|
||||
|
||||
return {'FINISHED'}
|
||||
|
@ -26,7 +26,7 @@ class ViewOperator(bpy.types.Operator):
|
||||
if event.type == 'MOUSEMOVE':
|
||||
self.offset = (self._initial_mouse - Vector((event.mouse_x, event.mouse_y, 0.0))) * 0.02
|
||||
self.execute(context)
|
||||
context.area.header_text_set("Offset %.4f %.4f %.4f" % tuple(self.offset))
|
||||
context.area.header_text_set("Offset {:.4f} {:.4f} {:.4f}".format(*self.offset))
|
||||
|
||||
elif event.type == 'LEFTMOUSE':
|
||||
context.area.header_text_set(None)
|
||||
|
@ -39,7 +39,7 @@ def enum_previews_from_directory_items(self, context):
|
||||
if directory == pcoll.my_previews_dir:
|
||||
return pcoll.my_previews
|
||||
|
||||
print("Scanning directory: %s" % directory)
|
||||
print("Scanning directory:", directory)
|
||||
|
||||
if directory and os.path.exists(directory):
|
||||
# Scan the directory for `*.png` files
|
||||
|
Loading…
Reference in New Issue
Block a user