First time bpy user here.. working on a small automated pipeline. Most of it is already in place, including a database and a PHP framework that provides data for use by the Python code below.
My intent in this particular part is to download JSON lists of projects, asset types, assets, tasks and task statuses, and to load them into dropdown lists inside Blender. Selecting the appropriate values in these dropdowns then saves the file into the correct directory.
The code I'm posting here is a mixture of a variety of answers to questions much like this one. However, none of it seems to work as my dropdown lists simply don't show up. There are no errors, but also no dropdowns. From what I've seen, there seem to be quite a few ways to do this, but I can't figure out what exactly I'm doing wrong or what is the generally accepted way of accomplishing this.
# Imports
import bpy, requests
from bpy.types import Panel, Operator
from bpy.props import EnumProperty
base_url = "http://my.fancy.base.url/"
bl_info = {
"name": "Pipeline",
"description": "Asset management for Blender",
"author": "Sam",
"version": (1, 0),
"blender": (2, 75, 0),
"location": "File Browser",
"category": "System"
}
class FILEBROWSER_PT_Pipeline(Panel):
bl_space_type = 'FILE_BROWSER'
bl_region_type = 'TOOLS'
bl_label = "Asset management"
bl_category = "Pipeline tools"
def draw(self, context):
l = self.layout
# Draw project dropdown
r = l.row(align = True)
r.prop(context.scene, "PL_Project")
# Draw type dropdown
r = l.row(align = True)
r.prop(context.scene, "PL_Type")
# Draw asset dropdown
r = l.row(align = True)
r.prop(context.scene, "PL_Asset")
# Draw task dropdown
r = l.row(align = True)
r.prop(context.scene, "PL_Task")
# Draw status dropdown
r = l.row(align = True)
r.prop(context.scene, "PL_Status")
# Callbacks for dropdown changes
def change_project_list(self, context):
return [(p['p_project'], p['project_reference'], p['project_name']) for p in bpy.data.scenes["Scene"].PL_Project_List]
def change_type_list(self, context):
return [(ty['p_type'], ty['type_reference'], ty['type_name']) for ty in bpy.data.scenes["Scene"].PL_Type_List]
def change_asset_list(self, context):
return [(a['p_asset'], a['asset_reference'], a['asset_name']) for a in bpy.data.scenes["Scene"].PL_Asset_List]
def change_task_list(self, context):
return [(ta['p_task'], ta['task_reference'], ta['task_name']) for ta in bpy.data.scenes["Scene"].PL_Task_List]
def change_status_list(self, context):
return [(s['p_status'], s['status_reference'], s['status_name']) for s in bpy.data.scenes["Scene"].PL_Status_List]
# Registration
def register():
bpy.utils.register_module(__name__)
def unregister():
bpy.utils.unregister_module(__name__)
# Let's go
if __name__ == "__main__":
register()
# Download lists
bpy.data.scenes["Scene"].PL_Project_List = requests.get(base_url + "tool/get-projects").json()
bpy.data.scenes["Scene"].PL_Type_List = requests.get(base_url + "tool/get-types/" + bpy.data.scenes["Scene"].PL_Project).json()
bpy.data.scenes["Scene"].PL_Asset_List = requests.get(base_url + "tool/get-assets/" + bpy.data.scenes["Scene"].PL_Project + "/" + bpy.types.Scene.PL_Type).json()
bpy.data.scenes["Scene"].PL_Task_List = requests.get(base_url + "tool/get-tasks/" + bpy.data.scenes["Scene"].PL_Asset).json()
bpy.data.scenes["Scene"].PL_Status_List = requests.get(base_url + "tool/get-statuses").json()
# Create dropdowns with update callbacks
bpy.types.Scene.PL_Project = EnumProperty(items = change_project_list, name = "Project")
bpy.types.Scene.PL_Type = EnumProperty(items = change_type_list, name = "Type")
bpy.types.Scene.PL_Asset = EnumProperty(items = change_asset_list, name = "Asset")
bpy.types.Scene.PL_Task = EnumProperty(items = change_task_list, name = "Task")
bpy.types.Scene.PL_Status = EnumProperty(items = change_status_list, name = "Status")
register()
would get called before blender's bpy.data is fully ready. you might get a_RestrictData
error in that case. -- but as you are running this from TextEditor and bpy.data is already present, something else is going on. I'd do a print statement to see the value of `.PL_Status_List at various points , it might not be filled with what you think.. $\endgroup$ – zeffii Aug 7 '15 at 6:57