[PYTHON] [Blender] How to dynamically set the selection of EnumProperty

Blender has Property functions to provide a UI so that users can specify values, and among them, there is EnumProperty ** that creates a ** select box. There was no problem with this function and setting the selection statically, but I was a little confused when ** setting the selection dynamically **, so for those who have hit the same problem I would like to introduce it to.

Set selections statically

The sample code for statically setting the selection is shown below for comparison with the dynamic setting. ** A select box is created by specifying the list created in advance in items **.

static_append.py


import bpy
from by.props import *

#Items you want to display in the select box
axis = (
    ("X", "X-axis", ""),      # (identifier,UI display name,Explanatory text)
    ("Y", "Y-axis", ""),
    ("Z", "Z-axis", ""))

transform_axis = EnumProperty(
    name = "Transform Axis",          #name
    description = "Transform Axis",   #Explanatory text
    items = axis)                     #Item list to be displayed in the select box

Dynamically set selections

Next, the sample code that dynamically sets the selection items is shown below. Here, ** item specifies a function to create an item list **.

Items to be set are set in the function that creates the item list, and the created item list is returned as the return value of the function. ** The function specified in items is called every time the variable object_list is referenced **, so the selection items can be set dynamically.

dynamic_append.py


import bpy
from bpy.props import *

#Function to create the item list you want to display in the select box
def get_object_list_callback(scene, context):
    items = []
    #Processing to add items to items...

    return items

object_list = EnumProperty(
    name = "Object List",               #name
    description = "Object List",        #Explanatory text
    items = get_object_list_callback)   #Function to create an item list

Reference information

Recommended Posts

[Blender] How to dynamically set the selection of EnumProperty
[Blender] How to get the selection order of vertices, edges and faces of an object
How to set the output resolution for each keyframe in Blender
[Blender] How to set shape_key with script
How to check the version of Django
How to set the server time to Japanese time
How to set up the development environment of ev3dev [Windows version]
How to calculate the volatility of a brand
How to find the area of the Voronoi diagram
Set the range of active strips to the preview range
How to know the port number of the xinetd service
How to get the number of digits in Python
How to know the current directory in Python in Blender
How to create a submenu with the [Blender] plugin
How to visualize the decision tree model of scikit-learn
[Blender] Summary of how to install / update / uninstall add-ons
Set the specified column of QTableWidget to ReadOnly StyledItemDelegate
[Introduction to Udemy Python3 + Application] 30. How to use the set
[Python] Summary of how to specify the color of the figure
How to hit the document of Magic Function (Line Magic)
How to access the global variable of the imported module
[Selenium] How to specify the relative path of chromedriver?
How to use the generator
How to use the decorator
How to increase the axis
How to start the program
How to increase the processing speed of vertex position acquisition
[Ubuntu] How to delete the entire contents of a directory
How to find the optimal number of clusters in k-means
How to test the attributes added by add_request_method of pyramid
[Blender] Know the selection status of hidden objects in the outliner
[Python] How to set variable names dynamically and speed comparison
How to set the html class attribute in Django's forms.py
How to calculate the amount of calculation learned from ABC134-D
(Note) How to pass the path of your own module
How to run the Export function of GCP Datastore automatically
How to increase the number of machine learning dataset images
How to see the contents of the Jupyter notebook ipynb file
How to find the scaling factor of a biorthogonal wavelet
How to set the extended iso8601 format date to the Dataframe index
How to connect the contents of a list into a string
How to calculate the autocorrelation coefficient
How to get and set the NTP server name by DHCP
How to determine the existence of a selenium element in Python
How to change the log level of Azure SDK for Python
How to use the optparse module
Extract the index of the original set list that corresponds to the list of subsets.
How to implement Java code in the background of RedHat (LinuxONE)
How to know the internal structure of an object in Python
Summary of how to use pandas.DataFrame.loc
How to change the color of just the button pressed in Tkinter
How to get the ID of Type2Tag NXP NTAG213 with nfcpy
[EC2] How to install chrome and the contents of each command
How to check the memory size of a variable in Python
Make the theme of Pythonista 3 like Monokai (how to make your own theme)
[Python] How to get the first and last days of the month
How to read the SNLI dataset
How to get the Python version
How to check the memory size of a dictionary in Python
[TensorFlow 2] How to check the contents of Tensor in graph mode
[Linux] How to disable the automatic update of the /etc/resolv.conf file (AmazonLinux2)