Skip to content

Instantly share code, notes, and snippets.

@zeffii
Created December 17, 2015 11:52
Show Gist options
  • Save zeffii/46dba5bd901e97fbea07 to your computer and use it in GitHub Desktop.
Save zeffii/46dba5bd901e97fbea07 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 update_object(obj, context):
b = obj.textmat_addon
for i, character in enumerate(obj.data.body_format):
tval = getattr(b, "char_" + str(i))
if tval >= len(obj.material_slots):
tval = len(obj.material_slots) - 1
character.material_index = tval
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)
obj = context.object
if obj.textmat_addon.use_me:
update_object(obj, context)
def text_updater(scene):
for obj in bpy.data.objects:
if obj.type == 'FONT':
if obj.textmat_addon.use_me:
update_object(obj, bpy.context)
class TextualProperties(bpy.types.PropertyGroup):
use_me = bpy.props.BoolProperty()
for i in range(80):
exec("char_{0} = IntProperty(min=0, max=90, options={{\'ANIMATABLE\'}}, 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
col.prop(props_addon, 'use_me', toggle=True)
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_pre
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_pre
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