Advertisement
pushrbx

Untitled

May 31st, 2014
278
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 14.58 KB | None | 0 0
  1. import bpy
  2. from bpy.props import *
  3. from math import pi
  4.  
  5.  
  6. # Load a font
  7. def load_font(font_path):
  8.     """ Load a new TTF font into Blender, and return the font object """
  9.     # get the original list of fonts (before we add a new one)
  10.     original_fonts = bpy.data.fonts.keys()
  11.    
  12.     # load new font
  13.     bpy.ops.font.open(filepath=font_path)
  14.    
  15.     # get the new list of fonts (after we added a new one)
  16.     for font_name in bpy.data.fonts.keys():
  17.         if font_name not in original_fonts:
  18.             return bpy.data.fonts[font_name]
  19.        
  20.     # no new font was added
  21.     return None
  22. # the stuff
  23. #  
  24. #  name: createDissolveText
  25. #  @param
  26. #  @return
  27. #  
  28. def createDissolveText(title,extrude,bevel_depth,spacemode,textsize,width,font):
  29.     """ Create aned animate the exploding texte """
  30.  
  31.     newText = title
  32.     #create text
  33.     bpy.ops.object.text_add(view_align=False, enter_editmode=False,location=(0, 0, 0), rotation=(0, 0, 0))
  34.     ActiveObjectText = bpy.context.scene.objects.active
  35.  
  36.     newtext = bpy.context.scene.objects.active
  37.     #erasing previous objects
  38.     if bpy.context.scene.objects.active.name != 'Text':
  39.         bpy.ops.object.select_all(action='DESELECT')
  40.         #selecting and erasing Text
  41.         bpy.context.scene.objects.active  = bpy.data.objects['Text']
  42.         bpy.context.scene.objects.active.select = True
  43.         bpy.ops.object.delete(use_global=False)
  44.         bpy.ops.object.select_all(action='DESELECT')
  45.        
  46.         #need to delete other objects
  47.         bpy.ops.object.select_all(action='DESELECT')
  48.         #selecting and erasing Turbulence field
  49.         bpy.context.scene.objects.active  = bpy.data.objects['TurbulenceField']
  50.         bpy.context.scene.objects.active.select = True
  51.         bpy.ops.object.delete(use_global=False)
  52.  
  53.         bpy.ops.object.select_all(action='DESELECT')
  54.         #selecting and erasing Plane
  55.         bpy.context.scene.objects.active  = bpy.data.objects['Plane']
  56.         bpy.context.scene.objects.active.select = True
  57.         bpy.ops.object.delete(use_global=False)
  58.  
  59.         bpy.ops.object.select_all(action='DESELECT')
  60.         #selecting and erasing WindField
  61.         bpy.context.scene.objects.active  = bpy.data.objects['WindField']
  62.         bpy.context.scene.objects.active.select = True
  63.         bpy.ops.object.delete(use_global=False)
  64.  
  65.         #selecting newText
  66.         bpy.context.scene.objects.active  = newtext
  67.         bpy.context.scene.objects.active.select = True
  68.        
  69.     #naming/renaming the text
  70.     bpy.context.scene.objects.active.name = 'Text';
  71.     bpy.context.scene.objects.active = bpy.data.objects['Text']
  72.    
  73.     #placing text in position
  74.     ActiveObjectText.rotation_euler[0]=pi/2 #xaxis
  75.     ActiveObjectText.rotation_euler[1]=0.0  #yaxis
  76.     ActiveObjectText.rotation_euler[2]=0.0  #zaxis
  77.     ActiveObjectText.location[0]=0
  78.     ActiveObjectText.location[1]=0
  79.     ActiveObjectText.location[2]=0
  80.     #changing text
  81.     ActiveObjectText.data.body = title
  82.    
  83.     #text size
  84.     ActiveObjectText.data.size = textsize
  85.     ActiveObjectText.data.space_character = width
  86.     ActiveObjectText.data.font = font
  87.     #centering text
  88.     #ActiveObjectText.data.align='CENTER'
  89.     ActiveObjectText.data.align=spacemode
  90.     #extrude text
  91.     ActiveObjectText.data.extrude=extrude #0.04
  92.  
  93.     #bevel text
  94.     ActiveObjectText.data.bevel_depth = bevel_depth #0.005
  95.     ActiveObjectText.data.bevel_resolution = 5
  96.     #adjust text position
  97.     ActiveObjectText.location.z= -ActiveObjectText.dimensions[1]/3
  98.    
  99.     #convert to mesh to apply effect
  100.     bpy.ops.object.convert(target='MESH', keep_original=False)
  101.    
  102.     #affect dissolve material
  103.     ActiveObjectText.data.materials.append(bpy.data.materials['DissolveMaterial'])
  104.     ActiveObjectText = bpy.context.scene.objects.active
  105.    
  106.     #Don't forget to deselect before select!
  107.     bpy.ops.object.select_all(action='DESELECT')
  108.    
  109.     #selecting Text
  110.     bpy.context.scene.objects.active  = ActiveObjectText
  111.     bpy.context.scene.objects.active.select = True
  112.    
  113.     #add remesh modifier to text
  114.     bpy.ops.object.modifier_add(type='REMESH')
  115.     #modifying parameters
  116.     ActiveObjectText.modifiers['Remesh'].octree_depth = 9 #10 best quality but vertices number too high
  117.     ActiveObjectText.modifiers['Remesh'].scale=0.99
  118.     ActiveObjectText.modifiers['Remesh'].mode='SMOOTH'
  119.     ActiveObjectText.modifiers['Remesh'].remove_disconnected_pieces=False
  120.     #apply this mofifier
  121.     bpy.ops.object.modifier_apply(apply_as='DATA', modifier="Remesh")
  122.    
  123.     #Nb quads for particle system be careful of API version
  124.     if bpy.app.version[1] < 62 :
  125.         NbQuads = len(ActiveObjectText.data.faces)
  126.     if bpy.app.version[1] == 62 :
  127.         if bpy.app.version[2] >= 2 :
  128.             NbQuads = len(ActiveObjectText.data.polygons.values()) #API 2.62.2 and up
  129.         else:
  130.             NbQuads = len(ActiveObjectText.data.faces)
  131.     if bpy.app.version[1] > 62 :
  132.             NbQuads = len(ActiveObjectText.data.polygons.values()) #API 2.63 and up
  133.  
  134.     #Add Particle System
  135.     bpy.ops.object.particle_system_add()
  136.     #Particle parameters
  137.     ActiveObjectText.particle_systems['ParticleSystem'].settings.count = NbQuads
  138.     ActiveObjectText.particle_systems['ParticleSystem'].settings.frame_start = 10
  139.     ActiveObjectText.particle_systems['ParticleSystem'].settings.frame_end = 60
  140.     ActiveObjectText.particle_systems['ParticleSystem'].settings.lifetime = 80
  141.     ActiveObjectText.particle_systems['ParticleSystem'].point_cache.frame_step = 1
  142.     ActiveObjectText.particle_systems['ParticleSystem'].settings.normal_factor = 0.0
  143.     #not useful
  144.     ActiveObjectText.particle_systems['ParticleSystem'].settings.use_dynamic_rotation = True
  145.     ActiveObjectText.particle_systems['ParticleSystem'].settings.render_type='NONE'
  146.     ActiveObjectText.particle_systems['ParticleSystem'].settings.draw_method='DOT'
  147.     ActiveObjectText.particle_systems['ParticleSystem'].settings.effector_weights.gravity = 0
  148.     ActiveObjectText.particle_systems['ParticleSystem'].settings.adaptive_subframes = True
  149.     ActiveObjectText.particle_systems['ParticleSystem'].settings.courant_target = 0.2
  150.  
  151.     bpy.ops.object.select_all(action='DESELECT')
  152.  
  153.     #Adding Wind force field on center and rotate it -90 on Y
  154.     bpy.ops.object.effector_add(type='WIND', view_align=False, enter_editmode=False, location=(0, 0, 0), rotation=(0, -pi/2, 0), layers=(True, False, False, False, False, False, False, False, False, False, False, False, False, False, False, False, False, False, False, False))
  155.     ActiveObjectWindField = bpy.context.scene.objects.active
  156.     ActiveObjectWindField.name = 'WindField'
  157.     #settings
  158.     ActiveObjectWindField.field.strength = 1.0
  159.     ActiveObjectWindField.field.flow = 1.0
  160.     ActiveObjectWindField.field.noise = 0.0
  161.     ActiveObjectWindField.field.seed = 27
  162.     ActiveObjectWindField.field.apply_to_location = True
  163.     ActiveObjectWindField.field.apply_to_rotation = True
  164.     ActiveObjectWindField.field.use_absorption = False
  165.  
  166.     #Adding Turbulence Force Field
  167.     bpy.ops.object.effector_add(type='TURBULENCE', view_align=False, enter_editmode=False, location=(0, 0, 0), rotation=(0, 0, 0), layers=(True, False, False, False, False, False, False, False, False, False, False, False, False, False, False, False, False, False, False, False))
  168.     ActiveObjectTurbulenceField = bpy.context.scene.objects.active
  169.     ActiveObjectTurbulenceField.name = 'TurbulenceField'
  170.     #settings
  171.     ActiveObjectTurbulenceField.field.strength = 15
  172.     ActiveObjectTurbulenceField.field.size = 0.75
  173.     ActiveObjectTurbulenceField.field.flow = 0.5
  174.     ActiveObjectTurbulenceField.field.seed = 23
  175.     ActiveObjectTurbulenceField.field.apply_to_location = True
  176.     ActiveObjectTurbulenceField.field.apply_to_rotation = True
  177.     ActiveObjectTurbulenceField.field.use_absorption = False
  178.  
  179.  
  180.     #Don't forget to deselect before select!
  181.     bpy.ops.object.select_all(action='DESELECT')
  182.  
  183.     #selecting Text
  184.     bpy.context.scene.objects.active  = ActiveObjectText
  185.     bpy.context.scene.objects.active.select = True
  186.  
  187.     #adding wipe texture to text
  188.  
  189.     sTex = bpy.data.textures.new('Wipe', type = 'BLEND')
  190.     sTex.use_color_ramp = True
  191.  
  192.     TexSlot=ActiveObjectText.particle_systems['ParticleSystem'].settings.texture_slots.add()
  193.     TexSlot.texture = sTex
  194.  
  195.     bpy.ops.object.select_all(action='DESELECT')
  196.  
  197.     #Create plane for controlling action of particle system (based on time)
  198.     #if text is created on the fly 'Wipe' texture does not work! don't know really why!
  199.     # so use of an existing plane, and resize it to the text x dimension
  200.     bpy.ops.mesh.primitive_plane_add(view_align=False, enter_editmode=False, location=(0, 0, 0), rotation=(pi/2, 0, 0), layers=(True, False, False, False, False, False, False, False, False, False, False, False, False, False, False, False, False, False, False, False))
  201.     ActiveObjectPlane = bpy.context.scene.objects.active
  202.     ActiveObjectPlane.name = 'Plane'
  203.     #Change dimensions
  204.     ActiveObjectPlane.dimensions = ((ActiveObjectText.dimensions[0]*1.2),(ActiveObjectText.dimensions[1]*1.2),0)
  205.     #hide plane for render
  206.     ActiveObjectPlane.hide_render = True
  207.     #show as wire in 3D
  208.     ActiveObjectPlane.draw_type = 'WIRE'
  209.  
  210.     bpy.ops.object.select_all(action='DESELECT')
  211.  
  212.     #selecting Text
  213.     bpy.context.scene.objects.active  = ActiveObjectText
  214.     bpy.context.scene.objects.active.select = True
  215.  
  216.     TexSlot.texture_coords = 'OBJECT'
  217.     TexSlot.object = ActiveObjectPlane
  218.     TexSlot.use_map_time = True
  219.  
  220.  
  221.     ActiveObjectText.data.update()
  222.  
  223.     bpy.ops.object.modifier_add(type='EXPLODE')
  224.     bpy.ops.mesh.uv_texture_add() #name UVMap by default
  225.     ActiveObjectText.data.materials['DissolveMaterial'].texture_slots['Texture'].texture_coords = 'UV'
  226.     ActiveObjectText.data.materials['DissolveMaterial'].texture_slots['Texture'].uv_layer = 'UVMap'
  227.     ActiveObjectText.data.materials['DissolveMaterial'].texture_slots['Texture'].use_map_alpha = True
  228.     ActiveObjectText.data.materials['DissolveMaterial'].texture_slots['Texture'].alpha_factor = 1.0
  229.  
  230.     ActiveObjectText.modifiers['Explode'].particle_uv = 'UVMap'
  231.     ActiveObjectText.data.update()
  232.  
  233.  
  234.     #Don't forget to deselect before select!
  235.     bpy.ops.object.select_all(action='DESELECT')
  236.  
  237.     #selecting Text
  238.     bpy.context.scene.objects.active  = ActiveObjectText
  239.     bpy.context.scene.objects.active.select = True
  240.     TexSlot.texture_coords = 'OBJECT'
  241.     TexSlot.object = ActiveObjectPlane
  242.  
  243.     TexSlot.use_map_time = False
  244.     TexSlot.use_map_time = True
  245.  
  246.     ActiveObjectText.data.update()
  247.  
  248.  
  249. # Debug Info:
  250. # ./blender -b test.blend -P demo.py
  251. # -b = background mode
  252. # -P = run a Python script within the context of the project file
  253.  
  254. # Init all of the variables needed by this script.  Because Blender executes
  255. # this script, OpenShot will inject a dictionary of the required parameters
  256. # before this script is executed.
  257. params = {     
  258.             'title' : 'Oh Yeah! OpenShot!',
  259.             'extrude' : 0.05,
  260.             'bevel_depth' : 0.01,
  261.             'spacemode' : 'CENTER',
  262.             'text_size' : 1,
  263.             'width' : 1.0,
  264.             'fontname' : 'Bfont',
  265.            
  266.             'color' : [0.8,0.8,0.8],
  267.             'alpha' : 1.0,
  268.            
  269.             'output_path' : '/tmp/',
  270.             'fps' : 24,
  271.             'quality' : 90,
  272.             'file_format' : 'PNG',
  273.             'color_mode' : 'RGBA',
  274.             'horizon_color' : [0, 0, 0],
  275.             'resolution_x' : 1920,
  276.             'resolution_y' : 1080,
  277.             'resolution_percentage' : 100,
  278.             'start_frame' : 20,
  279.             'end_frame' : 25,
  280.             'animation' : True,
  281.         }
  282.  
  283.  
  284. #BEGIN INJECTING PARAMS
  285. params['specular_color'] = [1.0, 1.0, 1.0]
  286. params['fontname'] = '/home/szenya/.fonts/AugieHU.ttf'
  287. params['specular_intensity'] = 0.5
  288. params['start_frame'] = 1
  289. params['spacemode'] = 'CENTER'
  290. params['title'] = '2014 őszén'
  291. params['file_name'] = '2014autumn_1'
  292. params['diffuse_color'] = [0.8045591138945669, 0.37867625092984036, 0.05637409755197975]
  293. params['end_frame'] = 128
  294. params['extrude'] = 0.05
  295. params['bevel_depth'] = 0.01
  296. params['text_size'] = 1.0
  297. params['width'] = 1.0
  298. params['animation_speed'] = '1'
  299. params['resolution_y'] = 720
  300. params['resolution_x'] = 1280
  301. params['file_format'] = 'PNG'
  302. params['resolution_percentage'] = 100
  303. params['color_mode'] = 'RGBA'
  304. params['output_path'] = '/home/szenya/.openshot/blender/17171cca-e8fe-11e3-b76c-28924a1b2703/2014autumn_1'
  305. params['fps'] = 23
  306. params['quality'] = 100
  307. params['animation'] = True
  308. #END INJECTING PARAMS
  309.  
  310.  
  311. # The remainder of this script will modify the current Blender .blend project
  312. # file, and adjust the settings.  The .blend file is specified in the XML file
  313. # that defines this template in OpenShot.
  314. #----------------------------------------------------------------------------
  315.  
  316. # Modify Text / Curve settings
  317. #print (bpy.data.curves.keys())
  318.  
  319.  
  320. #text_object = bpy.data.curves["txtName1"]
  321. #text_object.extrude = params["extrude"]
  322. #text_object.bevel_depth = params["bevel_depth"]
  323. #text_object.body = params["title"]
  324. #text_object.align = params["spacemode"]
  325. #text_object.size = params["text_size"]
  326. #text_object.space_character = params["width"]
  327.  
  328. # Get font object
  329. font = None
  330. if params["fontname"] != "Bfont":
  331.     # Add font so it's available to Blender
  332.     font = load_font(params["fontname"])
  333. else:
  334.     # Get default font
  335.     font = bpy.data.fonts["Bfont"]
  336.  
  337. createDissolveText(params["title"],params["extrude"],params["bevel_depth"],params["spacemode"],params["text_size"],params["width"], font)
  338.  
  339. # Change the material settings (color, alpha, etc...)
  340. material_object = bpy.data.materials["DissolveMaterial"]
  341. material_object.diffuse_color = params["diffuse_color"]
  342. material_object.specular_color = params["specular_color"]
  343. material_object.specular_intensity = params["specular_intensity"]
  344. #don't modify alpha!!!!!
  345. #material_object.alpha = params["alpha"]
  346.  
  347.  
  348.  
  349. # Set the render options.  It is important that these are set
  350. # to the same values as the current OpenShot project.  These
  351. # params are automatically set by OpenShot
  352. bpy.context.scene.render.filepath = params["output_path"]
  353. bpy.context.scene.render.fps = params["fps"]
  354. #bpy.context.scene.render.quality = params["quality"]
  355. try:
  356.     bpy.context.scene.render.file_format = params["file_format"]
  357.     bpy.context.scene.render.color_mode = params["color_mode"]
  358. except:
  359.     bpy.context.scene.render.image_settings.file_format = params["file_format"]
  360.     bpy.context.scene.render.image_settings.color_mode = params["color_mode"]
  361. bpy.data.worlds[0].horizon_color = params["horizon_color"]
  362. bpy.context.scene.render.resolution_x = params["resolution_x"]
  363. bpy.context.scene.render.resolution_y = params["resolution_y"]
  364. bpy.context.scene.render.resolution_percentage = params["resolution_percentage"]
  365. bpy.context.scene.frame_start = params["start_frame"]
  366. bpy.context.scene.frame_end = params["end_frame"]
  367.  
  368. # Animation Speed (use Blender's time remapping to slow or speed up animation)
  369. animation_speed = int(params["animation_speed"])    # time remapping multiplier
  370. new_length = int(params["end_frame"]) * animation_speed # new length (in frames)
  371. bpy.context.scene.frame_end = new_length
  372. bpy.context.scene.render.frame_map_old = 1
  373. bpy.context.scene.render.frame_map_new = animation_speed
  374. if params["start_frame"] == params["end_frame"]:
  375.     bpy.context.scene.frame_start = params["end_frame"]
  376.     bpy.context.scene.frame_end = params["end_frame"]
  377.  
  378. # Render the current animation to the params["output_path"] folder
  379. bpy.ops.render.render(animation=params["animation"])
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement