Skip to content

Instantly share code, notes, and snippets.

@mini3d mini3d/attribpanel.py
Last active Dec 17, 2015

Embed
What would you like to do?
Vertex Attribute Panel for Blender 2.66
import bpy
vertexAttributes = [("POSITION", "Position", "" , "MAN_TRANS", 0 ),
("NORMAL", "Normal", "", "MANIPUL", 1),
("TANGENT", "Tangent", "", "OUTLINER_OB_EMPTY", 2),
("BITANGENT", "Bitangent", "", "EMPTY_DATA", 3),
("TEXTURE", "Texture Coordinates", "", "GROUP_UVS", 4),
("GROUPS", "Vertex Groups", "", "GROUP_VERTEX", 5),
("COLOR", "Vertex Color", "", "GROUP_VCOL", 6)]
vertexAttributeDict = {i[0]: [i[1], i[3]] for i in vertexAttributes}
class AddAttributeCollectionOperator(bpy.types.Operator):
bl_idname = "scene.vertex_attribute_collection_add"
bl_label = "Add Vertex Attribute Collection"
def invoke(self, context, event):
index = bpy.context.scene.active_vertex_attribute_collection
collection = bpy.context.scene.vertex_attribute_collection_collection
collection.add()
return{"FINISHED"}
class RemoveAttributeCollectionOperator(bpy.types.Operator):
bl_idname = "scene.vertex_attribute_collection_remove"
bl_label = "Remove Vertex Attribute Collection"
def invoke(self, context, event):
index = bpy.context.scene.active_vertex_attribute_collection
collection = bpy.context.scene.vertex_attribute_collection_collection
collection.remove(index)
if index > 0:
bpy.context.scene.active_vertex_attribute_collection -= 1
return{"FINISHED"}
class MoveAttributeCollectionOperator(bpy.types.Operator):
bl_idname = "scene.vertex_attribute_collection_move"
bl_label = "Move Vertex Attribute"
direction = bpy.props.EnumProperty(
name="direction",
items=(("UP", "Up", ""),
("DOWN", "Down", ""))
)
def invoke(self, context, event):
index = bpy.context.scene.active_vertex_attribute_collection
collection = bpy.context.scene.vertex_attribute_collection_collection
if self.direction == 'DOWN' and index < len(collection) - 1:
collection.move(index, index + 1)
bpy.context.scene.active_vertex_attribute_collection += 1
elif self.direction == 'UP' and index > 0:
collection.move(index, index - 1)
bpy.context.scene.active_vertex_attribute_collection -= 1
return{"FINISHED"}
class AddAttributeOperator(bpy.types.Operator):
bl_idname = "scene.vertex_attribute_add"
bl_label = "Add Vertex Attribute"
def invoke(self, context, event):
index = bpy.context.scene.active_vertex_attribute_collection
collection = bpy.context.scene.vertex_attribute_collection_collection[index]
collection.attributes.add()
return{"FINISHED"}
class RemoveAttributeOperator(bpy.types.Operator):
bl_idname = "scene.vertex_attribute_remove"
bl_label = "Remove Vertex Attribute"
def invoke(self, context, event):
index = bpy.context.scene.active_vertex_attribute_collection
collection = bpy.context.scene.vertex_attribute_collection_collection[index]
collection.attributes.remove(collection.index)
if collection.index > 0:
collection.index -= 1
return{"FINISHED"}
class MoveAttributeOperator(bpy.types.Operator):
bl_idname = "scene.vertex_attribute_move"
bl_label = "Move Vertex Attribute"
direction = bpy.props.EnumProperty(
name="direction",
items=(("UP", "Up", ""),
("DOWN", "Down", ""))
)
def invoke(self, context, event):
index = bpy.context.scene.active_vertex_attribute_collection
collection = bpy.context.scene.vertex_attribute_collection_collection[index]
if self.direction == 'DOWN' and collection.index < len(collection.attributes) - 1:
collection.attributes.move(collection.index, collection.index + 1)
collection.index += 1
elif self.direction == 'UP' and collection.index > 0:
collection.attributes.move(collection.index, collection.index - 1)
collection.index -= 1
return{"FINISHED"}
class VertexAttributeProperty(bpy.types.PropertyGroup):
name = bpy.props.StringProperty(name="Name", default="new_attribute")
type = bpy.props.EnumProperty(
name="Attribute Type",
items=vertexAttributes,
)
class VertexAttributeCollectionProperty(bpy.types.PropertyGroup):
name = bpy.props.StringProperty(name="Name", default="Attribute Collection")
index = bpy.props.IntProperty()
attributes = bpy.props.CollectionProperty(type=VertexAttributeProperty)
class VERTEX_ATTRIBUTE_UL_list(bpy.types.UIList):
def draw_item(self, context, layout, data, item, icon, active_data, active_propname, index):
if self.layout_type in {'DEFAULT', 'COMPACT'}:
layout.label(text=item.name, translate=False, icon_value=icon)
layout.label(text=vertexAttributeDict[item.type][0], translate=False, icon=vertexAttributeDict[item.type][1])
elif self.layout_type in {'GRID'}:
layout.alignment = 'CENTER'
layout.label(text="", icon_value=icon)
class VERTEX_ATTRIBUTE_COLLECTION_UL_list(bpy.types.UIList):
def draw_item(self, context, layout, data, item, icon, active_data, active_propname, index):
if self.layout_type in {'DEFAULT', 'COMPACT'}:
layout.label(text=item.name, translate=False, icon="MESH_DATA")
elif self.layout_type in {'GRID'}:
layout.alignment = 'CENTER'
layout.label(text="", icon_value=icon)
class VertexAttributesGroupPanel(bpy.types.Panel):
bl_label = "Mini3d Vertex Attribute Group"
bl_space_type = "PROPERTIES"
bl_region_type = "WINDOW"
bl_context = "material"
def draw(self, context):
self.layout.template_list('VERTEX_ATTRIBUTE_COLLECTION_UL_list', 'attribute_collection_list', context.scene, "vertex_attribute_collection_collection", context.material, "attribute_group")
class VertexAttributesPanel(bpy.types.Panel):
bl_label = "Mini3d Vertex Attributes"
bl_space_type = "PROPERTIES"
bl_region_type = "WINDOW"
bl_context = "scene"
def draw(self, context):
index = bpy.context.scene.active_vertex_attribute_collection
collection_collection = bpy.context.scene.vertex_attribute_collection_collection
# Currently selected collection
collection = None
if len(collection_collection) > index and index >= 0:
collection = collection_collection[index]
#Currently selected attribute
attribute = None
if collection and len(collection.attributes) > collection.index and collection.index >= 0:
attribute = collection.attributes[collection.index]
# Attribute collection list
mainrow = self.layout.row()
mainrow.label(text="Vertex Attribute Groups:", translate=False)
mainrow = self.layout.row()
col = mainrow.column()
row = col.row()
row.template_list('VERTEX_ATTRIBUTE_COLLECTION_UL_list', 'attribute_collection_list', context.scene, "vertex_attribute_collection_collection", context.scene, "active_vertex_attribute_collection")
if collection:
row = col.row()
row.prop(collection, "name")
# Attribute collection name
col = mainrow.column(align=True)
col.operator("scene.vertex_attribute_collection_add", icon='ZOOMIN', text="")
col.operator("scene.vertex_attribute_collection_remove", icon='ZOOMOUT', text="")
# Attribute collection list buttons
if collection:
col.separator()
col.operator("scene.vertex_attribute_collection_move", icon='TRIA_UP', text="").direction = 'UP'
col.operator("scene.vertex_attribute_collection_move", icon='TRIA_DOWN', text="").direction = 'DOWN'
# Attribute list
if collection:
#separator
mainrow = self.layout.row()
col = mainrow.column()
col.separator()
mainrow = self.layout.row()
mainrow.label(text="Vertex Attributes:", translate=False)
mainrow = self.layout.row()
col = mainrow.column()
row = col.row()
row.template_list('VERTEX_ATTRIBUTE_UL_list', 'attribute_list', collection, "attributes", collection, "index")
# Attribute settings
if attribute:
row = col.row()
row.prop(attribute, "name")
row = col.row()
row.prop(attribute, "type")
# Attribute list buttons
col = mainrow.column(align=True)
col.operator("scene.vertex_attribute_add", icon='ZOOMIN', text="")
col.operator("scene.vertex_attribute_remove", icon='ZOOMOUT', text="")
if attribute:
col.separator()
col.operator("scene.vertex_attribute_move", icon='TRIA_UP', text="").direction = 'UP'
col.operator("scene.vertex_attribute_move", icon='TRIA_DOWN', text="").direction = 'DOWN'
bpy.utils.register_module(__name__)
bpy.types.Scene.vertex_attribute_collection_collection = bpy.props.CollectionProperty(type=VertexAttributeCollectionProperty)
bpy.types.Scene.active_vertex_attribute_collection = bpy.props.IntProperty()
bpy.types.Material.attribute_group = bpy.props.IntProperty()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
You can’t perform that action at this time.