Skip to content

Instantly share code, notes, and snippets.

@melsov
Created April 10, 2022 19:20
Show Gist options
  • Save melsov/d9d10f8cfb317c06924af955b68d2c83 to your computer and use it in GitHub Desktop.
Save melsov/d9d10f8cfb317c06924af955b68d2c83 to your computer and use it in GitHub Desktop.
bl_info = {
"name": "Melsov EZ Custom Data Editor",
"author": "Melsov",
"version": (1, 0),
"blender": (2, 80, 0),
"location": "View3D > Sidebar > MelCustomDataUtil",
"description": "Convenient buttons for addding custom properties",
"warning": "",
"wiki_url": "",
"category": "3D View"}
# This add-on provides convenient buttons for adding and removing
# custom properties on objects. Facilitates multi-select adding/removing.
# REMINDER: you have to check 'custom properties' when exporting the FBX from blender
# to include custom properties in the exported FBX.
# Or if using the EdyJ Unity to FBX exporter,
# https://github.com/EdyJ/blender-to-unity-fbx-exporter
# modify the code
# in that add-on. Find where it calls the default exporter
# add the parameter: use_custom_props=True
# HERE's the line in blender-to-unity-fbx-exporter.py
# Export FBX file
# params = dict(filepath=filepath, apply_scale_options='FBX_SCALE_UNITS', object_types={'EMPTY', 'MESH', 'ARMATURE'}, use_active_collection=active_collection, use_selection=selected_objects, use_armature_deform_only=deform_bones, add_leaf_bones=leaf_bones, use_custom_props=True)
import bpy
def allSelectedHaveKey(key, context):
for ob in context.selected_objects:
if key not in ob:
return False
return True
# Add key : values as needed
default_data = {
"mel-collider" : 44,
"mel-no-renderer" : 42,
}
class MelDataEditorOperator(bpy.types.Operator):
"""Tooltip"""
bl_idname = "object.simple_operator"
bl_label = "Mel Custom Data Editor"
custom_data : bpy.props.StringProperty()
@classmethod
def poll(cls, context):
return context.active_object is not None
def addCustomData(self, context, keyStr, val = 1):
for ob in context.selected_objects:
self.addTo(ob, keyStr, val)
def removeCustomData(self, context, keyStr):
for ob in context.selected_objects:
del ob[keyStr]
def addTo(self, objet, keyStr, val = 1):
objet[keyStr] = val
def execute(self, context):
if(self.custom_data not in default_data.keys()):
print("invalid custom data key")
return {'FINISHED'}
if allSelectedHaveKey(self.custom_data, context):
self.removeCustomData(context=context, keyStr=self.custom_data)
return {'FINISHED'}
self.addCustomData(context=context, keyStr=self.custom_data, val=default_data[self.custom_data])
return {'FINISHED'}
class ProtoPanel:
bl_category = "Mel"
bl_space_type = "VIEW_3D"
bl_region_type = "UI"
bl_options = {"DEFAULT_CLOSED"}
class EXAMPLE_PT_panel_1(ProtoPanel, bpy.types.Panel):
bl_label = "Mel EZ Custom Prop Editor"
def getSharedKeys(self, context):
result = []
for key in default_data.keys():
if allSelectedHaveKey(key, context):
result.append(key)
return result
def showSharedKeys(self, context):
layout = self.layout
shared = self.getSharedKeys(context)
layout.label(text="Shared keys: ")
for sharedKey in shared:
layout.label(text=sharedKey)
def drawOperatorFor(self, keyStr, context):
layout = self.layout
allHave = allSelectedHaveKey(keyStr, context)
labelText = ''.join(["REMOVE" if allHave else "ADD", " ", keyStr ])
layout.operator('object.simple_operator', text=labelText).custom_data = keyStr
def draw(self, context):
for key in default_data.keys():
self.drawOperatorFor(key, context)
self.showSharedKeys(context)
classes = (EXAMPLE_PT_panel_1, MelDataEditorOperator)
def register():
for cls in classes:
bpy.utils.register_class(cls)
def unregister():
for cls in classes:
bpy.utils.unregister_class(cls)
if __name__ == "__main__":
register()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment