Text Explode Addon
par
popularité : 8%

Premier test d’écriture d’Addon pour Blender en python.
Text Explode Addon
Premier test de scripting python pour Blender. J’ai utilisé la page suivante http://blenderclan.tuxfamily.org/html/modules/newbb/viewtopic.php?topic_id=32667&forum=2 en guise de mini formation, ensuite parcours du web et de l’API Blender pour répondre aux choses plus spécifiques :
- http://www.blender.org/documentation/blender_python_api_2_62_release/
- http://blenderartists.org/forum/
- http://blenderclan.tuxfamily.org/html/modules/newbb/index.php
- http://www.blenderaddons.com/home.php
Le but de cette addon est d’automatiser la création d’un texte qui explose. La méthode de création de l’explosion est décrite dans la vidéo http://cgcookie.com/blender/2011/12/02/tip-creating-quick-explosions/
Fichier Blender |
---|
Archive zip Addon |
Code du script Blender
import bpy
#import math
#from mathutils import *; from math import *
from math import pi
ground = '1'
newText = 'Explode'
#create text
bpy.ops.object.text_add(view_align=False, enter_editmode=False, location=(0, 0, 0), rotation=(0, 0, 0))
newtext = bpy.context.scene.objects.active
if bpy.context.scene.objects.active.name != 'Text':
bpy.ops.object.select_all(action='DESELECT')
#selecting Text
bpy.context.scene.objects.active = bpy.data.objects['Text']
bpy.context.scene.objects.active.select = True
bpy.ops.object.delete(use_global=False)
bpy.ops.object.select_all(action='DESELECT')
#selecting Text
bpy.context.scene.objects.active = newtext
bpy.context.scene.objects.active.select = True
#naming the text
bpy.context.scene.objects.active.name = 'Text';
bpy.context.scene.objects.active = bpy.data.objects['Text']
#centering text
bpy.data.objects['Text'].data.align='CENTER'
#extrude text
bpy.data.objects['Text'].data.extrude=0.1
#bevel text
bpy.data.objects['Text'].data.bevel_depth = 0.01
bpy.data.objects['Text'].data.bevel_resolution = 10
#rotating text
#angles are in radians
#bpy.ops.transform.rotate(value=(pi/2,), axis=(1.0, 0.0, 0.0), constraint_axis=(False, False, False), constraint_orientation='GLOBAL', mirror=False, proportional='DISABLED', proportional_edit_falloff='SMOOTH', proportional_size=1, snap=False, snap_target='CLOSEST', snap_point=(0, 0, 0), snap_align=False, snap_normal=(0, 0, 0), release_confirm=False)
#second solution
bpy.data.objects['Text'].rotation_euler[0]=pi/2 #xaxis
bpy.data.objects['Text'].rotation_euler[1]=0.0 #yaxis
bpy.data.objects['Text'].rotation_euler[2]=0.0 #zaxis
#changing text
#bpy.data.curves['Text'].body = 'Hello World' #alternative
bpy.context.scene.objects.active=bpy.data.objects['Text']
bpy.ops.object.editmode_toggle()
bpy.ops.font.delete()
bpy.ops.font.text_insert(text=newText, accent=False)
bpy.ops.object.editmode_toggle()
#convert to mesh to apply effect
bpy.ops.object.convert(target='MESH', keep_original=False)
#solidify
bpy.ops.object.modifier_add(type='SOLIDIFY')
#apply quick explode
bpy.ops.object.quick_explode(style='EXPLODE', amount=100, frame_duration=50, frame_start=1, frame_end=51, velocity=1, fade=True)
#modifying Particle System
#emitfrom
bpy.data.objects['Text'].particle_systems['ParticleSystem'].settings.emit_from = 'VERT' #'VOLUME'#'FACE'#'VERT'
#particle number
bpy.data.objects['Text'].particle_systems['ParticleSystem'].settings.count=200
#particle lifetime
bpy.data.objects['Text'].particle_systems['ParticleSystem'].settings.lifetime=200 # 200 +48 > 150 ;-)
# start/end explosion
bpy.data.objects['Text'].particle_systems['ParticleSystem'].settings.frame_end = 48
bpy.data.objects['Text'].particle_systems['ParticleSystem'].settings.frame_start = 48
#explosion power
bpy.data.objects['Text'].particle_systems['ParticleSystem'].settings.normal_factor=5.5
#integration method
bpy.data.objects['Text'].particle_systems['ParticleSystem'].settings.integrator='RK4'#aa'MIDPOINT' #'RK4'
#size of particles
bpy.data.objects['Text'].particle_systems['ParticleSystem'].settings.particle_size = 0.1
#particles time step
bpy.data.objects['Text'].particle_systems['ParticleSystem'].settings.timestep = 0.02
#mass of particles
bpy.data.objects['Text'].particle_systems['ParticleSystem'].settings.mass = 2.0
bpy.data.objects['Text'].particle_systems['ParticleSystem'].settings.use_multiply_size_mass = True
#affect an existing material
bpy.data.objects['Text'].material_slots[0].material = bpy.data.materials['TextMaterial']
#solidifiy parameter
bpy.data.objects['Text'].modifiers['Solidify'].edge_crease_inner=0.01
bpy.data.objects['Text'].modifiers['Solidify'].thickness = 0.02
#ground management
bpy.data.objects['Text'].particle_systems['ParticleSystem'].point_cache.frame_step = 1
if ground=='1':
bpy.data.objects['Ground'].hide_render = False
bpy.data.objects['Ground'].hide = False
bpy.ops.object.select_all(action='DESELECT')
#selecting Text
bpy.context.scene.objects.active = bpy.data.objects['Ground']
bpy.context.scene.objects.active.select = True
#if bpy.data.objects['Ground'].modifiers.keys()[0] != 'Collision':
if bpy.data.objects['Ground'].modifiers.items() == []:
print("collision = empty")
bpy.ops.object.modifier_add(type="COLLISION")
else:
print("OK")
else:
bpy.ops.object.select_all(action='DESELECT')
#selecting Text
bpy.context.scene.objects.active = bpy.data.objects['Ground']
bpy.context.scene.objects.active.select = True
bpy.ops.object.modifier_remove(modifier="Collision")
bpy.data.objects['Ground'].hide = True
bpy.data.objects['Ground'].hide_render = True
#bpy.ops.ptcache.free_bake_all() # erase baked dynamics
#bpy.ops.ptcache.bake_all() # bake dynamics : take time but needed before rendering animation
Code du Plugin Blender
bl_info = {
"name": "Add Exploding Text",
"author": "Yann Morère",
"version": (0,1,0),
"blender": (2, 6, 1),
"api": 42615,
"location": "View3D > Tool Shelf",
"description": "Add a Text mesh with explode animation.",
"warning": "", # used for warning icon and text in addons panel
"wiki_url": "http://yann.morere.free.fr",
"tracker_url": "http://yann.morere.free.fr",
"category": "Add Mesh"}
#define an interface for my module
import bpy
from bpy.props import *
from math import pi
#
#Define my text explosion
#
def MyTextExplosion(newText):
#newText = Text
if len(newText)==0:
newText="Explode"
#create text
bpy.ops.object.text_add(view_align=False, enter_editmode=False,location=(0, 0, 0), rotation=(0, 0, 0))
ActiveObject = bpy.context.scene.objects.active
#centering text
ActiveObject.data.align='CENTER'
#extrude text
ActiveObject.data.extrude=0.1
#bevel text
ActiveObject.data.bevel_depth = 0.01
ActiveObject.data.bevel_resolution = 10
#rotating text
#angles are in radians
#bpy.ops.transform.rotate(value=(pi/2,), axis=(1.0, 0.0, 0.0), constraint_axis=(False, False, False), constraint_orientation='GLOBAL', mirror=False, proportional='DISABLED', proportional_edit_falloff='SMOOTH', proportional_size=1, snap=False, snap_target='CLOSEST', snap_point=(0, 0, 0), snap_align=False, snap_normal=(0, 0, 0), release_confirm=False)
#second solution
ActiveObject.rotation_euler[0]=pi/2 #xaxis
ActiveObject.rotation_euler[1]=0.0 #yaxis
ActiveObject.rotation_euler[2]=0.0 #zaxis
#changing text
bpy.context.scene.objects.active=ActiveObject
bpy.ops.object.editmode_toggle()
bpy.ops.font.delete()
bpy.ops.font.text_insert(text=newText, accent=False)
bpy.ops.object.editmode_toggle()
#convert to mesh to apply effect
bpy.ops.object.convert(target='MESH', keep_original=False)
#solidify
bpy.ops.object.modifier_add(type='SOLIDIFY')
#apply quick explode
bpy.ops.object.quick_explode(style='EXPLODE', amount=100, frame_duration=50, frame_start=1, frame_end=51, velocity=1, fade=True)
#modifying Particle System
#emitfrom
ActiveObject.particle_systems['ParticleSystem'].settings.emit_from = 'VERT'
#particle number
ActiveObject.particle_systems['ParticleSystem'].settings.count=200
#particle lifetime
ActiveObject.particle_systems['ParticleSystem'].settings.lifetime=200 # 200 +48 > 150 ;-)z
# start/end explosion
ActiveObject.particle_systems['ParticleSystem'].settings.frame_end = 48
ActiveObject.particle_systems['ParticleSystem'].settings.frame_start = 48
#explosion power
ActiveObject.particle_systems['ParticleSystem'].settings.normal_factor=5.5
#integration method
ActiveObject.particle_systems['ParticleSystem'].settings.integrator='RK4'#aa'MIDPOINT'
#size of particles
ActiveObject.particle_systems['ParticleSystem'].settings.particle_size = 0.1
#particles time step
ActiveObject.particle_systems['ParticleSystem'].settings.timestep = 0.02
#mass of particles
ActiveObject.particle_systems['ParticleSystem'].settings.mass = 2.0
ActiveObject.particle_systems['ParticleSystem'].settings.use_multiply_size_mass = True
#affect an existing material
#ActiveObject.material_slots[0].material = bpy.data.materials['TextMaterial']
#solidifiy parameter
ActiveObject.modifiers['Solidify'].edge_crease_inner=0.01
ActiveObject.modifiers['Solidify'].thickness = 0.02
#
#Store properties in the active scene
#
def initSceneProperties(scn):
bpy.types.Scene.MyTextExplodeString = bpy.props.StringProperty(name = "Text", description="My Exploding Text", default="Explode")
#scn['MyTextExplodeString'] = "Explode"
return
initSceneProperties(bpy.context.scene)
#
#Menu in UI region
#
class UIPanel(bpy.types.Panel):
bl_label = 'Add Exploding Text'
bl_space_type = 'VIEW_3D'
bl_region_type = 'TOOLS'
@classmethod
def poll(self,context):
return(bpy.context.mode == 'OBJECT' or bpy.context.mode == 'EDIT_MESH')
def draw(self, context):
scn = context.scene #get the context, and the data
layout = self.layout
# row.label(text="Text to Explode", icon='FONT_DATA')
layout.prop(scn, "MyTextExplodeString")
layout.operator("operator.explodingtext")
#
# Define operator
#
class ExplodeTextButton(bpy.types.Operator):
bl_idname = "operator.explodingtext"
bl_label = "Create Exploding Text"
bl_options = {'REGISTER','UNDO'}
def execute(self, context):
scn = context.scene
MyTextExplosion(bpy.context.scene['MyTextExplodeString'])
return{"FINISHED"}
def register():
bpy.utils.register_class(ExplodeTextButton)
bpy.utils.register_class(UIPanel)
def unregister():
bpy.utils.unregister_class(ExplodeTextButton)
bpy.utils.unregister_class(UIPanel)
if __name__ == '__main__':
register()
Installation et utilisation de l’addon :
le résultat sera le suivant :