Skip to content

Instantly share code, notes, and snippets.

@howiemnet
Created May 11, 2016 18:31
Show Gist options
  • Save howiemnet/8401b1561430226a52eab503fe80e3f5 to your computer and use it in GitHub Desktop.
Save howiemnet/8401b1561430226a52eab503fe80e3f5 to your computer and use it in GitHub Desktop.
#
# POPULATE!
#
import bpy
import math
from bpy_extras import object_utils
from random import randint
from math import radians
def returnObjectByName (passedName= ""):
r = None
obs = bpy.data.objects
for ob in obs:
if ob.name == passedName:
r = ob
return r
startFrame = 381
endFrame = 900
bodyFidgetDistance = 10
bodTargetsRootName = "BodEmpty."
bodModelsRootName = "BodModel"
bodBodyName = "BodBody"
bodHeadName = "BodHead"
bodEyeLName = "BodEyeL"
bodEyeRName = "BodEyeR"
bodHairName = "BodHair"
bodMouthName = "BodMouth"
bodLineEmptyName = "BodLineLookAtEmpty"
bodLineName = "BodLine"
bodStandInName = "BodStandIn"
myCollection = []
myKillCollection = []
print ("-Killing old ones-----------")
bpy.ops.object.select_all(action='DESELECT')
if bpy.context.scene.objects != []:
for obj in bpy.context.scene.objects:
if bodTargetsRootName in obj.name:
print (obj.name)
# one of ours!
#
# FIND THE BODY
for subObj in obj.children:
if bodModelsRootName in subObj.name:
#
# Found body - select it plus kids
theModel = returnObjectByName(subObj.name)
theModel.select = True
bpy.context.scene.objects.active = theModel
bpy.ops.object.select_grouped(extend = True, type='CHILDREN_RECURSIVE')
for newObj in bpy.context.selected_objects:
myKillCollection.append(newObj)
for obj in myKillCollection:
obj.select = True
bpy.ops.object.delete()
skip_objects = 1
skip_count = 0
if bpy.context.scene.objects != []:
for obj in bpy.context.scene.objects:
if bodTargetsRootName in obj.name:
#
# GRAB one only for DEV
#
#if myCollection == []:
myCollection.append(obj)
for obj in myCollection:
print (obj.name)
# one of ours!
#
#is there a height empty?
fallHeight = 0
for subObject in obj.children:
if "BodHeightEmpty" in subObject.name:
heightEmptyPos = ((subObject.location.z )/0.071)-(0.979 / 0.071)
fallHeight = heightEmptyPos
print ("Z is ")
print (fallHeight)
skip_count += 1
if skip_count == skip_objects:
skip_count = 0
# FIND THE BODY
if skip_count == 0:
print (obj["BodyType"])
theModelName = "BodModel" + str(obj["BodyType"])
print (theModelName)
theModel = returnObjectByName(theModelName)
#
# SELECT BODY plus children And DUPE
bpy.ops.object.select_all(action='DESELECT')
theModel.select = True
bpy.context.scene.objects.active = theModel
bpy.ops.object.select_grouped(extend = True, type='CHILDREN_RECURSIVE')
bpy.ops.object.duplicate(linked = True)
#
# find the bits we need, add new unique actions ready for animating
#
for newObj in bpy.context.selected_objects:
# all subobjects need their own animation if only for visibility
if bodModelsRootName in newObj.name:
bodRootObj = newObj
if bodBodyName in newObj.name:
bodBodyObj = newObj
if bodHeadName in newObj.name:
bodHeadObj = newObj
if bodEyeLName in newObj.name:
ea = newObj
if bodEyeRName in newObj.name:
eb = newObj
if bodHairName in newObj.name:
bodHairObj = newObj
if bodMouthName in newObj.name:
bodMouthObj = newObj
if bodLineName in newObj.name:
bodLineObj = newObj
if bodLineEmptyName in newObj.name:
bodLineEmptyObj = newObj
if bodStandInName in newObj.name:
bodStandInObj = newObj
newObj.animation_data_create()
newObj.animation_data.action = bpy.data.actions.new(name="MyAction")
#
# Sort out smile
bodMouthObj.data = bodMouthObj.data.copy()
bodSmileCurve = bodMouthObj.data
bodSmileCurve.animation_data_create()
bodSmileCurve.animation_data.action = bpy.data.actions.new(name="MyAction")
#
#
# Sort out materials
bodBodyObj.material_slots[0].link = "OBJECT"
bodBodyObj.material_slots[1].link = "OBJECT"
bodBodyObj.material_slots[2].link = "OBJECT"
bodBodyObj.material_slots[0].material = bpy.data.materials["Suit" + str(obj["matSuit"])]
bodBodyObj.material_slots[1].material = bpy.data.materials["Shirt" + str(obj["matShirt"])]
bodBodyObj.material_slots[2].material = bpy.data.materials["Tie" + str(obj["matTie"])]
bodHairObj.material_slots[0].link = "OBJECT"
bodHairObj.material_slots[0].material = bpy.data.materials["Hair" + str(obj["matHair"])]
bodHeadObj.material_slots[0].link = "OBJECT"
bodHeadObj.material_slots[0].material = bpy.data.materials["Skin" + str(obj["matSkin"])]
#
# Move model to proper position and parent it to its home empty
bodRootObj.parent = obj
bodRootObj.location[0] = 0
bodRootObj.location[1] = 0
bodRootObj.location[2] = 0
bodRootObj.rotation_euler[0] = 0
bodRootObj.rotation_euler[1] = 0
bodRootObj.rotation_euler[2] = 0
#
# Sort out line
#
#bodLineEmptyObj.parent = obj
#bodLineObj.constraints["LineTracker"].target = bodLineEmptyObj
#
# Set start and end times for animation
#
startFrame = obj["VisAppear"]
endFrame = 1250 #obj["VisDisappear"]
theFrame = startFrame
#
# APPEAR
#
# Line
#
#bodLineObj.animation_data.action.fcurves[1].keyframe_points[0].co[0] = startFrame - 5
#bodLineObj.animation_data.action.fcurves[1].keyframe_points[1].co[0] = startFrame
if fallHeight > 0:
bodBodyObj.scale = [0,0,0]
bodBodyObj.location[2] = 1
bodBodyObj.keyframe_insert(data_path="location", index=-1, frame=theFrame)
bodBodyObj.keyframe_insert(data_path="scale", index=-1, frame=theFrame)
theFrame += 8
bodBodyObj.scale = [1,1,1]
bodBodyObj.location[2] = 0
bodBodyObj.keyframe_insert(data_path="location", index=-1, frame=theFrame)
bodBodyObj.keyframe_insert(data_path="scale", index=-1, frame=theFrame)
#
# The FALL
#
theFrame += 1
bodRootObj.location[2] = fallHeight
bodRootObj.keyframe_insert(data_path="location", index=-1, frame=theFrame)
theFrame += 10 + (fallHeight / 2)
bodRootObj.location[2] = 0
bodRootObj.keyframe_insert(data_path="location", index=-1, frame=theFrame)
#
#
# Fix fall curve - slow start. quick stop
kp = bodRootObj.animation_data.action.fcurves[2].keyframe_points[0]
newHandle = kp.co + 2 * (kp.handle_right - kp.co)
kp.handle_right_type = 'FREE'
kp.handle_right = newHandle
kp = bodRootObj.animation_data.action.fcurves[2].keyframe_points[1]
kp.handle_left_type = 'FREE'
kp.handle_left = kp.co
print(kp.co)
fidget_start = theFrame +1
#bpy.ops.object.paths_calculate()
#
# Looking at target A
#
theFrame += randint(-15,0)
bodHeadObj.constraints["LockedTrackA"].influence = 0
bodHeadObj.constraints["LockedTrackA"].keyframe_insert(data_path="influence", index=-1, frame=theFrame)
bodBodyObj.constraints["LockedTrackA"].influence = 0
bodBodyObj.constraints["LockedTrackA"].keyframe_insert(data_path="influence", index=-1, frame=theFrame)
theFrame += randint(5,8)
bodHeadObj.constraints["LockedTrackA"].influence = 1
bodHeadObj.constraints["LockedTrackA"].keyframe_insert(data_path="influence", index=-1, frame=theFrame)
bodBodyObj.constraints["LockedTrackA"].influence = 1
bodBodyObj.constraints["LockedTrackA"].keyframe_insert(data_path="influence", index=-1, frame=theFrame)
#
# Smile
theFrame += randint(-5,5)
smile_amount = 0.05
bodSmileCurve.bevel_factor_start = 0.5-smile_amount
bodSmileCurve.bevel_factor_end = 0.5+smile_amount
bodSmileCurve.keyframe_insert(data_path="bevel_factor_start", index=-1, frame=theFrame)
bodSmileCurve.keyframe_insert(data_path="bevel_factor_end", index=-1, frame=theFrame)
theFrame += randint(6,8)
smile_amount = randint(10,15) / 100
bodSmileCurve.bevel_factor_start = 0.5-smile_amount
bodSmileCurve.bevel_factor_end = 0.5+smile_amount
bodSmileCurve.keyframe_insert(data_path="bevel_factor_start", index=-1, frame=theFrame)
bodSmileCurve.keyframe_insert(data_path="bevel_factor_end", index=-1, frame=theFrame)
#
# Look at target B
#
theFrame = randint(517,532)
bodHeadObj.constraints["LockedTrackB"].influence = 0
bodHeadObj.constraints["LockedTrackB"].keyframe_insert(data_path="influence", index=-1, frame=theFrame)
bodBodyObj.constraints["LockedTrackB"].influence = 0
bodBodyObj.constraints["LockedTrackB"].keyframe_insert(data_path="influence", index=-1, frame=theFrame)
theFrame += randint(8,12)
bodHeadObj.constraints["LockedTrackB"].influence = 1
bodHeadObj.constraints["LockedTrackB"].keyframe_insert(data_path="influence", index=-1, frame=theFrame)
bodBodyObj.constraints["LockedTrackB"].influence = 1
bodBodyObj.constraints["LockedTrackB"].keyframe_insert(data_path="influence", index=-1, frame=theFrame)
#
# Un-Smile
theFrame += randint(-5,3)
bodSmileCurve.keyframe_insert(data_path="bevel_factor_start", index=-1, frame=theFrame)
bodSmileCurve.keyframe_insert(data_path="bevel_factor_end", index=-1, frame=theFrame)
theFrame += randint(7,14)
smile_amount = 0.05
bodSmileCurve.bevel_factor_start = 0.5-smile_amount
bodSmileCurve.bevel_factor_end = 0.5+smile_amount
bodSmileCurve.keyframe_insert(data_path="bevel_factor_start", index=-1, frame=theFrame)
bodSmileCurve.keyframe_insert(data_path="bevel_factor_end", index=-1, frame=theFrame)
#
#
#
if (randint(0,10) > 4):
#
# UN-Look at target B (ie back at A)
#
theFrame = randint(550,590)
bodHeadObj.constraints["LockedTrackB"].influence = 1
bodHeadObj.constraints["LockedTrackB"].keyframe_insert(data_path="influence", index=-1, frame=theFrame)
bodBodyObj.constraints["LockedTrackB"].influence = 1
bodBodyObj.constraints["LockedTrackB"].keyframe_insert(data_path="influence", index=-1, frame=theFrame)
theFrame += 8 + randint(0,5)
bodHeadObj.constraints["LockedTrackB"].influence = 0
bodHeadObj.constraints["LockedTrackB"].keyframe_insert(data_path="influence", index=-1, frame=theFrame)
bodBodyObj.constraints["LockedTrackB"].influence = 0
bodBodyObj.constraints["LockedTrackB"].keyframe_insert(data_path="influence", index=-1, frame=theFrame)
#
# Look at target B
#
theFrame = randint(760,777)
bodHeadObj.constraints["LockedTrackB"].influence = 0
bodHeadObj.constraints["LockedTrackB"].keyframe_insert(data_path="influence", index=-1, frame=theFrame)
bodBodyObj.constraints["LockedTrackB"].influence = 0
bodBodyObj.constraints["LockedTrackB"].keyframe_insert(data_path="influence", index=-1, frame=theFrame)
theFrame += randint(8,13)
bodHeadObj.constraints["LockedTrackB"].influence = 1
bodHeadObj.constraints["LockedTrackB"].keyframe_insert(data_path="influence", index=-1, frame=theFrame)
bodBodyObj.constraints["LockedTrackB"].influence = 1
bodBodyObj.constraints["LockedTrackB"].keyframe_insert(data_path="influence", index=-1, frame=theFrame)
#
# SMILe on explosion
#
theFrame = randint (790, 794)
bodSmileCurve.keyframe_insert(data_path="bevel_factor_start", index=-1, frame=theFrame)
bodSmileCurve.keyframe_insert(data_path="bevel_factor_end", index=-1, frame=theFrame)
theFrame += randint(3,6)
smile_amount = randint(10,15) / 100
bodSmileCurve.bevel_factor_start = 0.5-smile_amount
bodSmileCurve.bevel_factor_end = 0.5+smile_amount
bodSmileCurve.keyframe_insert(data_path="bevel_factor_start", index=-1, frame=theFrame)
bodSmileCurve.keyframe_insert(data_path="bevel_factor_end", index=-1, frame=theFrame)
#Look at protag again
theFrame = + randint(798,804)
bodHeadObj.constraints["LockedTrackB"].influence = 1
bodHeadObj.constraints["LockedTrackB"].keyframe_insert(data_path="influence", index=-1, frame=theFrame)
bodBodyObj.constraints["LockedTrackB"].influence = 1
bodBodyObj.constraints["LockedTrackB"].keyframe_insert(data_path="influence", index=-1, frame=theFrame)
theFrame += randint(7,13)
bodHeadObj.constraints["LockedTrackB"].influence = 0
bodHeadObj.constraints["LockedTrackB"].keyframe_insert(data_path="influence", index=-1, frame=theFrame)
bodBodyObj.constraints["LockedTrackB"].influence = 0
bodBodyObj.constraints["LockedTrackB"].keyframe_insert(data_path="influence", index=-1, frame=theFrame)
if (randint(1,10)>2):
theFrame = randint(800,820)
bodRootObj.keyframe_insert(data_path="location", frame=theFrame, index=-1)
dist = randint(3,8)/10
theFrame += (randint(15,30)*dist)
bodRootObj.location[1] -= dist
#bodRootObj.location[1] += randint(-bodyFidgetDistance,bodyFidgetDistance)/100
bodRootObj.keyframe_insert(data_path="location", frame=theFrame, index=-1)
if (randint(1,15)>10):
# some bods look down at hand
theFrame = 910
bodHeadObj.constraints["LockedTrackB"].influence = 0
bodHeadObj.constraints["LockedTrackB"].keyframe_insert(data_path="influence", index=-1, frame=theFrame)
bodBodyObj.constraints["LockedTrackB"].influence = 0
bodBodyObj.constraints["LockedTrackB"].keyframe_insert(data_path="influence", index=-1, frame=theFrame)
theFrame += 1
bodHeadObj.constraints["LockedTrackB"].influence = 1
bodHeadObj.constraints["LockedTrackB"].keyframe_insert(data_path="influence", index=-1, frame=theFrame)
bodBodyObj.constraints["LockedTrackB"].influence = 1
bodBodyObj.constraints["LockedTrackB"].keyframe_insert(data_path="influence", index=-1, frame=theFrame)
# if so, they look back
theFrame = randint(987,995)
bodHeadObj.constraints["LockedTrackB"].influence = 1
bodHeadObj.constraints["LockedTrackB"].keyframe_insert(data_path="influence", index=-1, frame=theFrame)
bodBodyObj.constraints["LockedTrackB"].influence = 1
bodBodyObj.constraints["LockedTrackB"].keyframe_insert(data_path="influence", index=-1, frame=theFrame)
theFrame += randint(7,13)
bodHeadObj.constraints["LockedTrackB"].influence = 0
bodHeadObj.constraints["LockedTrackB"].keyframe_insert(data_path="influence", index=-1, frame=theFrame)
bodBodyObj.constraints["LockedTrackB"].influence = 0
bodBodyObj.constraints["LockedTrackB"].keyframe_insert(data_path="influence", index=-1, frame=theFrame)
if (randint(1,10)>3):
theFrame = randint(986,1020)
bodRootObj.keyframe_insert(data_path="location", frame=theFrame, index=-1)
theFrame += randint(18,23)
bodRootObj.location[1] -= randint(10,15)/10
#bodRootObj.location[1] += randint(-bodyFidgetDistance,bodyFidgetDistance)/100
bodRootObj.keyframe_insert(data_path="location", frame=theFrame, index=-1)
#
#
# BLINKS
#
myCurrFrame = startFrame
while myCurrFrame < endFrame:
myCurrFrame += ( 10 + randint(0,50) )
ea.scale[2]=1
eb.scale[2]=1
ea.keyframe_insert(data_path="scale", frame=myCurrFrame, index=2)
eb.keyframe_insert(data_path="scale", frame=myCurrFrame, index=2)
myCurrFrame += 1
ea.scale[2]=0.2
eb.scale[2]=0.2
ea.keyframe_insert(data_path="scale", frame=myCurrFrame, index=2)
eb.keyframe_insert(data_path="scale", frame=myCurrFrame, index=2)
myCurrFrame += randint(2,4)
ea.scale[2]=1
eb.scale[2]=1
ea.keyframe_insert(data_path="scale", frame=myCurrFrame, index=2)
eb.keyframe_insert(data_path="scale", frame=myCurrFrame, index=2)
#
#
# HEAD-TILTS
#
myCurrFrame = startFrame
while myCurrFrame < endFrame:
myCurrFrame += randint(5,20)
bodHeadObj.keyframe_insert(data_path="rotation_euler", frame=myCurrFrame, index=1)
bodHeadObj.rotation_euler[1]=radians(randint(-3,3)/2)
#
#
# MOVE on the spot
#
myCurrFrame = fidget_start
while myCurrFrame < endFrame:
myCurrFrame += randint(10,70)
bodBodyObj.keyframe_insert(data_path="location", frame=myCurrFrame, index=-1)
bodBodyObj.location[0] += randint(-bodyFidgetDistance,bodyFidgetDistance)/100
bodBodyObj.location[1] += randint(-bodyFidgetDistance,bodyFidgetDistance)/100
#
#
# Visibility - on
#
theFrame = obj["VisAppear"]
for tobj in bpy.context.selected_objects:
# tobj.hide = True
tobj.hide_render = True
# tobj.keyframe_insert(data_path="hide", index=-1, frame=theFrame-1)
tobj.keyframe_insert(data_path="hide_render", index=-1, frame=theFrame-1)
for tobj in bpy.context.selected_objects:
# tobj.hide = False
tobj.hide_render = False
# tobj.keyframe_insert(data_path="hide", index=-1, frame=theFrame)
tobj.keyframe_insert(data_path="hide_render", index=-1, frame=theFrame)
#
# Visibility - off
#
#theFrame = obj["VisDisappear"]
#for tobj in bpy.context.selected_objects:
# tobj.hide = True
# tobj.hide_render = False
# tobj.keyframe_insert(data_path="hide", index=-1, frame=theFrame-1)
## tobj.keyframe_insert(data_path="hide_render", index=-1, frame=theFrame-1)
#for tobj in bpy.context.selected_objects:
# tobj.hide = False
# tobj.hide_render = True
# tobj.keyframe_insert(data_path="hide", index=-1, frame=theFrame)
# tobj.keyframe_insert(data_path="hide_render", index=-1, frame=theFrame)
#
#
# Visibility of standin for start:
theFrame = obj["VisAppear"]+9
bodStandInObj.hide_render = False
bodStandInObj.keyframe_insert(data_path="hide_render", index=-1, frame=theFrame)
theFrame = obj["VisAppear"]+10
bodStandInObj.hide_render = True
bodStandInObj.keyframe_insert(data_path="hide_render", index=-1, frame=theFrame)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment