How to use the template_icon_view in 2.80

(Peetie) #1

Here I have a simple imaginary addon that is able to append 2 cubes from a blendfile. Instead of having buttons, I want to use a preview of the cubes. Such a preview you see in asset managers. (Tried to analyse their code, but got stuck).

Here is the simple addon:

import bpy

# Here comes bl_info

# Add cube 1 to scene
class ADD_OT_cube1(bpy.types.Operator):
	bl_idname = 'add.cube1'
	bl_description = 'Adds a cube. Blue, small.'
	bl_category = 'SuperCube'
	bl_label = 'Add Blue Cube'

	def execute(self, context):
		path = os.path.dirname(__file__) + "/ojbects/cubes.blend\\Collection\\"
		object_name = "cube_01"
		bpy.ops.wm.append(filename=object_name, directory=path)
		return {"FINISHED"}

# Add cube 2 to scene
class ADD_OT_cube2(bpy.types.Operator):
	bl_idname = 'add.cube2'
	bl_description = 'Adds a cube. Red, Eating a banana.'
	bl_category = 'SuperCube'
	bl_label = 'Add Grazy Cube'

	def execute(self, context):
		path = os.path.dirname(__file__) + "/ojbects/cubes.blend\\Collection\\"
		object_name = "cube_02"
		bpy.ops.wm.append(filename=object_name, directory=path)
		return {"FINISHED"}


# The menu in the N-Panel 
class ADD_MT_menu(bpy.types.Menu):
	bl_label = "Add Cubes"
	bl_idname = "ADD_MT_menu"

	def draw(self, context):
		layout = self.layout
		layout.operator("add.cube1")
		layout.operator("add.cube2")



# Register Classes
classes = (
	ADD_OT_cube1,
	ADD_OT_cube2,
	ADD_MT_menu)

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 classes:
		unregister_class(cls)

if __name__ == "__main__":
	register()

I suppose we can use the template_icon_view as documented on https://docs.blender.org/api/blender2.8/bpy.types.UILayout.html?highlight=template_icon_view#bpy.types.UILayout.template_icon_view but so far I didn’t get a clue.

0 Likes