Skip to content

Instantly share code, notes, and snippets.

@dskjal
Last active June 7, 2021 12:48
Show Gist options
  • Save dskjal/04bf574ce9a9c209fcfa87edaa1c0b04 to your computer and use it in GitHub Desktop.
Save dskjal/04bf574ce9a9c209fcfa87edaa1c0b04 to your computer and use it in GitHub Desktop.
Blender で変数の共有
import bpy
# UI クラスで変数を定義
class UI(bpy.types.Panel):
bl_label = "test"
bl_space_type = "VIEW_3D"
bl_region_type = "TOOLS"
bl_category = "Tools"
#プロパティ
bpy.types.Scene.my_string = bpy.props.StringProperty(name='string')
#ただの変数
bpy.types.Scene.my_list = []
def draw(self,context):
# UI に配置
self.layout.prop(context.scene, "my_string")
self.layout.operator("dskjal.testbutton")
#変数にアクセス
for l in bpy.types.Scene.my_list:
self.layout.label(text=l)
class Button(bpy.types.Operator):
bl_idname = "dskjal.testbutton"
bl_label = "push"
def execute(self, context):
#プロパティは context.作成した場所.変数名 でアクセスする
str = context.scene.my_string
#ただの変数はそのままアクセスする
bpy.types.Scene.my_list.append(str)
return{'FINISHED'}
classes = (
UI,
Button
)
def register():
from bpy.utils import register_class
for cls in classes:
register_class(cls)
def unregister():
from bpy.utils import unregister_class
for cls in reversed(classes):
register_class(cls)
if __name__ == "__main__":
register()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment