プロパティ

公式サイト:

Property Definitions (bpy.props) | Blender Python API [Official]

使い方

定義
削除
引数

定義時に指定する主な引数

  • nameプロパティの名前
  • description説明文を与える
  • defaultデフォルト値を指定する
  • min最小値を指定する
  • max最大値を指定する
  • subtype値の取り扱い方法を指定する
  • updateプロパティ更新時に呼び出されるコールバックを指定する
  • getプロパティから値を取得する時に呼び出されるコールバックを指定する
  • setプロパティに値を設定する時に呼び出されるコールバックを指定する

ドキュメント:

Property Definitions (bpy.props) | Blender Python API [Official]

参考:

スクリプトにおいてクラス間で変数を共有する方法 | dskjal

オペレータプロパティを活用する | はじめての Blender アドオン開発

プロパティを追加して UI で編集する Addon サンプル | トックの CG 部屋

UI パネルに自作のプロパティを追加する | MR が楽しい

Addon のチェックボックスの追加とプロパティへのアクセス | ぬの部屋(仮)

概要

参考:

プロパティとID プロパティおよび両者の違い | wiki.blender.jp

RNA プロパティと ID プロパティとの違い | dskjal

Properties, ID-Properties and Their Differences | wiki.blender.jp

書き方

ドキュメント:

ClassVar – typing | Python 3.x ドキュメント [公式]

Class Property Registration | Blender Developer Wiki [Official]

PEP 526 – Syntax for Variable Annotations | Python.org [Official]

参考:

Can´t see the inputs in dialog box – Blender Stack Exchange

2.93 update throws error with bpy.props – Blender Stack Exchange

パラメーター

共通
  • name
  • description
  • default
オプション
  • override
  • tags
  • subtype
ハンドラ
  • update
  • get
  • set

表示形式 (subtype)

FloatProperty
  • 'PIXEL'ピクセル
  • 'UNSIGNED'符号なし
  • 'PERCENTAGE'パーセント
  • 'FACTOR'係数
  • 'ANGLE'角度
  • 'TIME'時間
  • 'DISTANCE'距離
  • 'DISTANCE_CAMERA'
  • 'POWER'
  • 'TEMPERATURE'温度
  • 'NONE'なし

ドキュメント:

bpy.types.Property.subtype | Blender Python API [Official]

参考:

Display an integer property as a percent? – Blender Artists Community

ブーリアン型 (Bool Property)

ドキュメント:

bpy.props.BoolProperty | Blender Python API [Official]

参考:

Addon のチェックボックスの追加とプロパティへのアクセス | ぬの部屋 (仮)

文字列型 (String Property)

ドキュメント:

bpy.props.StringProperty | Blender Python API [Official]

参考:

How to create text input field with Blender Python API? – Blender Stack Exchange

text input field panel – Blender Stack Exchange

ポインター (Pointer Property)

シーンまたはウィンドウマネージャにポインタプロパティを追加する。

def register():
    bpy.types.Scene.my_pointer         = bpy.props.PointerProperty(type=MyParams)
    bpy.types.WindowManager.my_pointer = bpy.props.PointerProperty(type=MyParams)

def unregister():
    del bpy.types.Scene.my_pointer
    del bpy.types.WindowManager.my_pointerCode language: Python (python)

ドキュメント:

bpy.props.PointerProperty | Blender Python API [Official]

コード:

Examples of bpy.props.PointerProperty | ProgramCreek.com

参考:

Passing PropertyGroup by reference, through a UI panel, to an operator: “read-only” error – Blender Artists Community

Assigning value to PointerProperty? – Blender Artists Community

How do you access the object that a PointerProperty points to? – Blender Stack Exchange

Is it possible to use bpy.props.PointerProperty to store a pointer to an object? – Blender Stack Exchange

How to check if a PointerProperty has a value? – Blender Stack Exchange

How to assign PointerProperty in Operator through UI? – Blender Stack Exchange

Limit “prop_search” to Specific Types of Objects – Blender Stack Exchange

New property type: IDPointerProperty – Right-Click Select

列挙型 (Enum Property)

ドキュメント:

bpy.props.EnumProperty | Blender Python API [Official]

EnumProperty | Blender Python API [Official]

EnumPropertyItem | Blender Python API [Official]

default_flag – bpy.types.EnumProperty | Blender 2.79 Python API [Official]

参考:

Enum Property の選択項目を動的に設定する方法 – Qiita

Enum Property の整数値の取得に失敗するとき | Unity の使い方

How to Get a List with All Possible Values of an Enum Property | Interplanety

Add/remove EnumProperty items? – Blender Artists Community

automatically update EnumProperty from UI Panel – Blender Artists Community

enum property with enum flag – Blender Artists Community

How to go about changing a property to that of another object? – Blender Stack Exchange

How to access number in Enum Property? – Blender Stack Exchange

How do I access the selected number of an Enum Property? – Blender Stack Exchange

How to display EnumProperty control like radio buttons, while keeping the selection exclusive? – Blender Stack Exchange

How do I set the selected value of an Enum Property? – Blender Stack Exchange

Why is my enum property not showing up? – Blender Stack Exchange

プロパティグループ (Property Group)

ドキュメント:

PropertyGroup | Blender Python API [Official]

参考:

UI パネルに自作のプロパティを追加する | MR が楽しい

How to create text input field with Blender Python API? – Blender Stack Exchange

Using new PointerProperty with custom property – Blender Stack Exchange

Store Pointer Property array / list – Blender Stack Exchange

PointerProperty to node of material node tree – Blender Stack Exchange

Can I create a CollectionProperty of brushes? – Blender Stack Exchange

Get previous value of updated property – Blender Stack Exchange

prop_search with a custom PropertyGroup – Blender Stack Exchange

bpy.types.PropertyGroup properties not updating when changed in Redo Panel – Blender Stack Exchange

コレクション (Collection Property)

ドキュメント:

CollectionProperty | Blender Python API [Official]

Collection Example | Blender Python API [Official]

bpy.props.CollectionProperty | Blender Python API [Official]

bpy_prop_collection | Blender Python API [Official]

コード:

bpy_rna.c at v2.93.0 · blender/blender – GitHub

rna_access.c at v2.93.0 · blender/blender – GitHub

参考:

要素ごとに状態を保存するメニュー作る | 忘却まとめ

Role of “name” in Collection Property – Blender Artists Community

Custom CollectionProperty different from builtin collections? – Blender Artists Community

How delete a bpy_prop_collection element – Blender Artists Community

Adding a Property to a CollectionProperty After Creating it – Blender Stack Exchange

How to get the class type of a Blender Collection Property? – Blender Stack Exchange

change item’s index – Blender Stack Exchange

can’t find update callback for CollectionProperty – Blender Stack Exchange

Combining bpy_prop_collection to use in prop_search() – Blender Stack Exchange

How to create dict from Collection Property? – Blender Stack Exchange

Remove from CollectionProperty – Blender Stack Exchange

ベクトル (FloatVectorProperty)

ドキュメント:

bpy.props.FloatVectorProperty | Blender Python API [Official]

参考:

Insert a FloatVectorProperty into a customized interface – Blender Stack Exchange

How to get a vector type from a FloatVectorProperty? – Blender Stack Exchange

更新コールバック (update)

プロパティの値が変更された時に、コールバックを実行する。

import bpy

def update_func(self, context):
    print("my test function", self)

bpy.types.Scene.testprop = bpy.props.FloatProperty(update=update_func)

bpy.context.scene.testprop = 11.0

# >>> my test function <bpy_struct, Scene("Scene")>Code language: Python (python)

(出典Blender Python API

ドキュメント:

Update Example | Blender Python API [Official]

参考:

Updating properties based on object selection state – Blender Artists Community

function update on boolean property – Blender Stack Exchange

How to get an IntProperty to update a function? – Blender Stack Exchange

How to make Blender run check()/redraw when option changed? – Blender Stack Exchange

Temporarily disable add-on UI property update callback – Blender Stack Exchange

poll function for a BoolProperty – Blender Stack Exchange

Get previous value of updated property – Blender Stack Exchange

update function for a custom property? – Blender Artists Community

ゲッター/セッター (getter / setter)

オブジェクトに対して直接に値を設定した場合、コールバックは呼ばれない。

scene['my_prop'] = 123Code language: Python (python)

プロパティとしてアクセスすると、コールバックが呼ばれる。

scene.my_prop = 123Code language: Python (python)

ドキュメント:

Getter / Setter Example | Blender Python API [Official]

参考:

Get previous value of updated property – Blender Stack Exchange

Internal “get / set” function of property? – Blender Stack Exchange

オペレーターに渡す

こちらのページを参照

ドキュメント:

Operator Example – Property Definitions | Blender Python API [Official]

参考:

How to access variable of other user class? – Stack Overflow

カスタムプロパティ

オブジェクトにカスタムプロパティを設定する。

object["prop_name"] = 123.45Code language: Python (python)

カスタムプロパティが未設定の場合に、値を設定する。

if object.get("prop_name") is None:
    object["prop_name"] = 123.45Code language: Python (python)

ドキュメント:

Custom Properties | Blender Manual [Official]

参考:

カスタムプロパティ | S.Fuka

Custom Property | Interplanety

custom pointer property is read only? – Blender Artists Community

How to access the variables of a class – Stack Overflow

How do I read out custom properties? – Stack Overflow

How to check if an object has a custom property? – Blender Stack Exchange

custom pointer property is read only? – Blender Stack Exchange

assign datablock to custom property – Blender Stack Exchange

デフォルト値を設定する

参考:

Property の default 値をユーザー設定にする方法 | 忘却まとめ

How can I change IntProperty default value? – Blender Stack Exchange

How can I change IntProperty default value stored within PropertyGroup? – Blender Stack Exchange

プロパティを読み取り専用にする

ゲッターを指定して、セッターは指定せずにプロパティを定義する。

def get_my_prop(self):
    return self['my_prop']

def register():
    # ...
    bpy.types.Object.my_prop = bpy.proprs.StringProperty(
        get=get_my_prop
    )

def unregister():
    # ...
    del bpy.types.Object.my_prop

if __name__ == '__main__':
    obj = bpy.context.object
    obj['my_prop'] = "Hello!"Code language: Python (python)

参考:

How to block custom properties from being edited by the user? – Blender Stack Exchange

How can I display a FloatVector without it being editable by the user? – Blender Stack Exchange

プロパティを列挙する

参考:

Walking the Property – Blender Artists Community

動的にリストを作成する

参考:

Can bpy.props be used for dynamic lists? – Blender Developer Talk

列挙型プロパティの値一覧を取得する

参考:

How to get a list with all possible values of an EnumProperty | Interplanety

パスを扱う

参考:

How to build an operator that corrects StringProperty(“subtype = ‘DIR_PATH’) from relative path? – Blender Stack Exchange

画像シーケンスを扱う

参考:

Setting custom prop to Sequence – Blender Stack Exchange

Setting custom prop to Sequences – Blender Developer Talk

オブザーバー

参考:

Execute function on object property change – Blender Stack Exchange

辞書型を使いたい

参考:

How can I save a dict in a scene propertyGroup? – Blender Stack Exchange

基底クラス

  • bpy.types.Propertyプロパティの基底クラス

ドキュメント:

Property | Blender Python API [Official]

bpy_struct | Blender Python API [Official]

subscribe_rna

ドキュメント:

bpy.msgbus.subscribe_rna | Blender Python API [Official]

参考:

Update a custom property after leaving edit-mode? – Blender Stack Exchange

読み取り専用エラー

現象:

読み取り専用であるため、プロパティを更新できない旨のエラーが発生する。

AttributeError: bpy_struct: attribute "xxx" from "Hoge" is read-only.

原因:

仕様です。

参考:

Custom pointer property is read only? – Blender Artists Community

How to assign PointerProperty in Operator through UI? – Blender Stack Exchange

Custom pointer property is read only? – Blender Stack Exchange

Tips

RNA プロパティと ID プロパティとの違い | dskjal

チュートリアル

Create Property Group & Enumerator in the Panel (Darkfall)

Property Subtypes and Password Protection (Darkfall)

記事をシェアする:
タグ:

コメントを残す

メールアドレスが公開されることはありません。 が付いている欄は必須項目です

Protected by reCAPTCHA