Pastebin: Blender StringPropertyに入力した文字列をコマンドとして実行する方法

Format
Python
Post date
2016-02-25 18:20
Publication Period
Unlimited
  1. #
  2. # 質問
  3. # StringPropertyに入力した文字列をコマンドとして実行する方法
  4. #
  5. # [目的]
  6. # Mayaのシェルフのような、簡単にコマンドを登録、実行できるようなアドオンを作りたいと思っています。
  7. # そのためにまず、文字列を入力して、その文字列をコマンドとして実行できるものを作りたいです。
  8. #
  9. # [試したこと]
  10. # StringPropertyを使って文字列を入力できるものは作れたのですが、
  11. # 『StringPropertyに入力した文字列を、コマンドとして実行する』という方法がわかりません。
  12. # どうやればいいのでしょうか?
  13. ################################################################
  14. #UIはプロパティシェルフに『Property panel』がでます
  15. #Simple Object Operator を実行すると入力したコマンドが実行されます。
  16. import bpy
  17. from bpy.props import *
  18. ################################################################
  19. def initSceneProperties(scn):
  20. # StringProperty デフォルトでは円柱追加のコマンドが設定されている
  21. bpy.types.Scene.MyString = StringProperty(
  22. name = "String")
  23. scn['MyString'] = "bpy.ops.mesh.primitive_cylinder_add()"
  24. return
  25. initSceneProperties(bpy.context.scene)
  26. ################################################################
  27. # パネル
  28. class UIPanel(bpy.types.Panel):
  29. bl_label = "Property panel"
  30. bl_space_type = "VIEW_3D"
  31. bl_region_type = "UI"
  32. def draw(self, context):
  33. layout = self.layout
  34. scn = context.scene
  35. # 文字列入力
  36. layout.prop(scn, 'MyString')
  37. layout.operator("object.simple_operator_x")
  38. ################################################################
  39. # コマンド
  40. class SimpleOperator_x(bpy.types.Operator):
  41. """Tooltip"""
  42. bl_idname = "object.simple_operator_x"
  43. bl_label = "Simple Object Operator"
  44. def execute(self, context):
  45. # 成功すれば以下のコマンドが実行され、円柱が追加される
  46. # bpy.ops.mesh.primitive_cylinder_add()
  47. # そのままやってみる
  48. # bpy.types.Scene.MyString
  49. # print してみる
  50. # print(bpy.types.Scene.MyString)
  51. # コンソールに入力してみる(よくわからんエラーが出る
  52. bpy.ops.console.execute(bpy.types.Scene.MyString)
  53. return {'FINISHED'}
  54. ################################################################
  55. # Registration
  56. bpy.utils.register_module(__name__)
Download Printable view

URL of this paste

Embed with JavaScript

Embed with iframe

Raw text