Skip to content

Instantly share code, notes, and snippets.

@zeffii
Last active December 17, 2015 10:53
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save zeffii/1173f780f8520ced75a8 to your computer and use it in GitHub Desktop.
Save zeffii/1173f780f8520ced75a8 to your computer and use it in GitHub Desktop.
import bpy
from bpy.props import IntProperty
"""
Usage, run the script to add character properies to bpy.types.Object
The UI appears in 3dview Tools, as MISC. You'll get a direct mapping between
character and material index (based on material_slots).
You can set keyframes using the sliders, and you can render / playback the
material updates because of the added frame_change event handler.
"""
def charmat_update(self, context):
# this is used for the visual feedback when adjusting sliders
# and this is also called by the framechange event to update the
# material per character (unless there's a neater way to do this)
b = context.object.textmat_addon
for i, character in enumerate(bpy.data.objects['Text'].data.body_format):
tval = getattr(b, "char_" + str(i))
if tval >= len(context.object.material_slots):
tval = len(context.object.material_slots) - 1
character.material_index = tval
def text_updater(scene):
charmat_update('uchhhh', bpy.context)
class TextualProperties(bpy.types.PropertyGroup):
for i in range(80):
exec("char_{0} = IntProperty(min=0, max=90, update=charmat_update)".format(i))
class CustomPanel(bpy.types.Panel):
"""A Custom Panel in the Viewport Toolbar"""
bl_label = "Generate"
bl_space_type = 'VIEW_3D'
bl_region_type = 'TOOLS'
@classmethod
def poll(self, context):
if context.object and context.object.type == 'FONT':
if context.object.mode == 'OBJECT':
return True
def draw(self, context):
layout = self.layout
col = layout.column()
body = context.object.data.body
props_addon = context.object.textmat_addon
for i in range(len(body)):
row = col.row()
row.label(body[i])
row.prop(props_addon, 'char_' + str(i), text="")
def register():
bpy.utils.register_module(__name__)
bpy.types.Object.textmat_addon = bpy.props.PointerProperty(type=TextualProperties)
handler = bpy.app.handlers.frame_change_post
if not (text_updater in handler):
handler.append(text_updater)
def unregister():
bpy.utils.unregister_module(__name__)
del bpy.types.Object.textmat_addon
handler = bpy.app.handlers.frame_change_post
for func in reversed(handler):
if func.__name__ == 'text_updater':
handler.remove(func)
if __name__ == "__main__":
register()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment