Pastebin: 文字列入力フィールドをプロパティシェルフに追加し、入力された文字列をコマンドとして実行する。 Blender Python スクリプト

Blender Python スクリプト 文字列入力フィールドをプロパティシェルフに追加し、入力された文字列をコマンドとして実行する exec関数を利用して改善。

Format
Python
Post date
2016-02-25 22:59
Publication Period
Unlimited
  1. # 文字列入力フィールドをプロパティシェルフに追加し、入力された文字列をコマンドとして実行する
  2. import bpy
  3. from bpy.props import *
  4. def initSceneProperties(scn):
  5. # StringProperty デフォルトでは円柱追加のコマンドが設定されている
  6. bpy.types.Scene.MyString = StringProperty(
  7. name = "String")
  8. scn['MyString'] = "bpy.ops.mesh.primitive_cylinder_add()"
  9. return
  10. initSceneProperties(bpy.context.scene)
  11. # パネル
  12. class UIPanel(bpy.types.Panel):
  13. bl_label = "Property panel"
  14. bl_space_type = "VIEW_3D"
  15. bl_region_type = "UI"
  16. def draw(self, context):
  17. layout = self.layout
  18. scn = context.scene
  19. # 文字列入力
  20. layout.prop(scn, 'MyString')
  21. layout.operator("object.simple_operator_x")
  22. # コマンド
  23. class SimpleOperator_x(bpy.types.Operator):
  24. """Tooltip"""
  25. bl_idname = "object.simple_operator_x"
  26. bl_label = "Simple Object Operator"
  27. def execute(self, context):
  28. # 成功すれば以下のコマンドが実行され、円柱が追加される
  29. # bpy.ops.mesh.primitive_cylinder_add()
  30. # そのままやってみる
  31. # context.scene.MyString
  32. scn = context.scene
  33. exec(scn.MyString)
  34. # print してみる
  35. # print(bpy.types.Scene.MyString)
  36. # コンソールに入力してみる(よくわからんエラーが出る
  37. # bpy.ops.console.execute(bpy.types.Scene.MyString)
  38. return {'FINISHED'}
  39. # Registration
  40. bpy.utils.register_module(__name__)
Download Printable view

URL of this paste

Embed with JavaScript

Embed with iframe

Raw text