bpy.props.1.py 1.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263
  1. """
  2. Operator Example
  3. ++++++++++++++++
  4. A common use of custom properties is for python based :class:`Operator`
  5. classes. Test this code by running it in the text editor, or by clicking the
  6. button in the 3D Viewport's Tools panel. The latter will show the properties
  7. in the Redo panel and allow you to change them.
  8. """
  9. import bpy
  10. class OBJECT_OT_property_example(bpy.types.Operator):
  11. bl_idname = "object.property_example"
  12. bl_label = "Property Example"
  13. bl_options = {'REGISTER', 'UNDO'}
  14. my_float: bpy.props.FloatProperty(name="Some Floating Point")
  15. my_bool: bpy.props.BoolProperty(name="Toggle Option")
  16. my_string: bpy.props.StringProperty(name="String Value")
  17. def execute(self, context):
  18. self.report(
  19. {'INFO'}, 'F: %.2f B: %s S: %r' %
  20. (self.my_float, self.my_bool, self.my_string)
  21. )
  22. print('My float:', self.my_float)
  23. print('My bool:', self.my_bool)
  24. print('My string:', self.my_string)
  25. return {'FINISHED'}
  26. class OBJECT_PT_property_example(bpy.types.Panel):
  27. bl_idname = "object_PT_property_example"
  28. bl_label = "Property Example"
  29. bl_space_type = 'VIEW_3D'
  30. bl_region_type = 'UI'
  31. bl_category = "Tool"
  32. def draw(self, context):
  33. # You can set the property values that should be used when the user
  34. # presses the button in the UI.
  35. props = self.layout.operator('object.property_example')
  36. props.my_bool = True
  37. props.my_string = "Shouldn't that be 47?"
  38. # You can set properties dynamically:
  39. if context.object:
  40. props.my_float = context.object.location.x
  41. else:
  42. props.my_float = 327
  43. bpy.utils.register_class(OBJECT_OT_property_example)
  44. bpy.utils.register_class(OBJECT_PT_property_example)
  45. # Demo call. Be sure to also test in the 3D Viewport.
  46. bpy.ops.object.property_example(
  47. my_float=47,
  48. my_bool=True,
  49. my_string="Shouldn't that be 327?",
  50. )