Using Python Scripting to set animation timecode zero in motionbuilder - python

Everyone.
I have some animation file that the start time is not zero. I am trying to create a script that will set the animation timecode to zero. For example, the picture shows the timecode is not from zero. Thank you very much for your help。
enter image description here

I'm a bit late but this is probably still interesting to share!
You can set your current take's timespan easily using an FBTimeSpan() instance like this, by specifying a start and end frame as FBTime() objects:
lStartFrame = 0
lEndFrame = 100
FBSystem().CurrentTake.LocalTimeSpan = FBTimeSpan(FBTime(0, 0, 0, lStartFrame, 0), FBTime(0, 0, 0, lEndFrame, 0))
But I guess that what you are looking for is a way to offset your animation and make it start at 0, isn't it?
Here are two functions from my own library, using the Story mode to offset your current animation at a given frame. The first one works on the current or all characters of your scene (character animation track). The second one on selected conponents (generic animation track). For each one you can pass as argument the frame it should start from and if you want to frame the new timespan at the end (replace your old first/last frames with the new ones).
from pyfbsdk import *
def offset_character_animation_at_frame(frame = 0, all_chars = True, frame_anim=True):
''' Offset current/all(default) characters animation to a given frame, 0 by default '''
# get list of current/all characters
if all_chars:
char_list = FBSystem().Scene.Characters
else:
char_list = [FBApplication().CurrentCharacter]
# get initial timespan
lStartFrame = FBSystem().CurrentTake.LocalTimeSpan.GetStart().GetFrame()
lEndFrame = FBSystem().CurrentTake.LocalTimeSpan.GetStop().GetFrame()
# turn on Story mode
FBStory().Mute = False
# process character list
for char in char_list:
# set timespan
FBSystem().CurrentTake.LocalTimeSpan = FBTimeSpan(FBTime(0, 0, 0, lStartFrame, 0), FBTime(0, 0, 0, lEndFrame, 0))
# set current character
FBApplication().CurrentCharacter = char
# insert character animation track
track = FBStoryTrack(FBStoryTrackType.kFBStoryTrackCharacter, FBStory().RootFolder)
track.Name = '{}_charAnimTrack'.format(FBApplication().CurrentCharacter.Name)
track.Details.append(FBApplication().CurrentCharacter)
# insert take in story mode
take = FBSystem().CurrentTake
inserted_clip = track.CopyTakeIntoTrack(take.LocalTimeSpan, take)
# move inserted clip to given frame
inserted_clip.Start = FBTime(0,0,0,frame)
# frame new timespan
FBSystem().CurrentTake.LocalTimeSpan = FBTimeSpan(FBTime(0, 0, 0, inserted_clip.Start.GetFrame(), 0), FBTime(0, 0, 0, inserted_clip.Stop.GetFrame(), 0))
# defining plot options and plot to current take
PlotOptions = FBPlotOptions()
PlotOptions.ConstantKeyReducerKeepOneKey = True
PlotOptions.PlotAllTakes = False
PlotOptions.PlotOnFrame = True
PlotOptions.PlotPeriod = FBTime( 0, 0, 0, 1 )
PlotOptions.PlotTranslationOnRootOnly = True
PlotOptions.PreciseTimeDiscontinuities = True
PlotOptions.RotationFilterToApply = FBRotationFilter.kFBRotationFilterGimbleKiller
PlotOptions.UseConstantKeyReducer = True
char.PlotAnimation(FBCharacterPlotWhere.kFBCharacterPlotOnSkeleton, PlotOptions)
# empty Story mode
for track in FBStory().RootFolder.Tracks:
for clip in track.Clips:
clip.FBDelete()
track.FBDelete()
# set back original timespan if specified
if not frame_anim:
FBSystem().CurrentTake.LocalTimeSpan = FBTimeSpan(FBTime(0, 0, 0, lStartFrame, 0), FBTime(0, 0, 0, lEndFrame, 0))
# turn off Story mode
FBStory().Mute = True
def offset_generic_animation_at_frame(frame = 0, frame_anim = True):
''' Offset selected components animation to a given frame, 0 by default '''
# get selected components
lModelList = FBModelList()
FBGetSelectedModels(lModelList)
if not lModelList:
raise ValueError("Select at least one component")
# get initial timespan
lStartFrame = FBSystem().CurrentTake.LocalTimeSpan.GetStart().GetFrame()
lEndFrame = FBSystem().CurrentTake.LocalTimeSpan.GetStop().GetFrame()
# turn on Story mode
FBStory().Mute = False
# set timespan
FBSystem().CurrentTake.LocalTimeSpan = FBTimeSpan(FBTime(0, 0, 0, lStartFrame, 0), FBTime(0, 0, 0, lEndFrame, 0))
# insert generic animation track and add selected components to it
track = FBStoryTrack(FBStoryTrackType.kFBStoryTrackAnimation, FBStory().RootFolder)
track.Name = 'genericAnimTrack'
for comp in lModelList:
track.Details.append(comp)
# insert take in story mode
take = FBSystem().CurrentTake
inserted_clip = track.CopyTakeIntoTrack(take.LocalTimeSpan, take)
# move inserted clip to given frame
inserted_clip.Start = FBTime(0,0,0,frame)
# frame new timespan
FBSystem().CurrentTake.LocalTimeSpan = FBTimeSpan(FBTime(0, 0, 0, inserted_clip.Start.GetFrame(), 0), FBTime(0, 0, 0, inserted_clip.Stop.GetFrame(), 0))
# plot selected take
lOptions = FBPlotOptions()
lOptions.ConstantKeyReducerKeepOneKey = False
lOptions.PlotAllTakes = False
lOptions.PlotOnFrame = True
lOptions.PlotPeriod = FBTime( 0, 0, 0, 1 )
lOptions.PlotTranslationOnRootOnly = False
lOptions.PreciseTimeDiscontinuities = True
lOptions.RotationFilterToApply = FBRotationFilter.kFBRotationFilterGimbleKiller
lOptions.UseConstantKeyReducer = False
FBSystem().CurrentTake.PlotTakeOnSelected(lOptions)
# empty Story mode
for track in FBStory().RootFolder.Tracks:
for clip in track.Clips:
clip.FBDelete()
track.FBDelete()
# set back original timespan if specified
if not frame_anim:
FBSystem().CurrentTake.LocalTimeSpan = FBTimeSpan(FBTime(0, 0, 0, lStartFrame, 0), FBTime(0, 0, 0, lEndFrame, 0))
# turn off Story mode
FBStory().Mute = True
# MAIN
offset_generic_animation_at_frame(frame = 0, frame_anim = False)
offset_character_animation_at_frame(frame = 0, all_chars = True, frame_anim=True)

Related

blender 2.92 python overriding

I have seen quite a few questions regarding this issue, and I have not been able to wrap my head around it.
So here is a concrete example.
I create a cube and in edit mode I add loopcuts. No sweat!
I copy the code from the info window and I try it out in a script and I get the ominous RuntimeError: Operator bpy.ops.mesh.loopcut_slide.poll() expected a view3d region & editmesh error message.
I understand now that it is about first giving python the right context by overriding. And alas, I do not know how to do it!
Here is the code:
import bpy
import os
os.system("cls")
# remove the default cube...
objs = bpy.data.objects
for obj in objs:
if obj.name.find("Cube") == 0:
bpy.data.objects.remove(obj, do_unlink=True)
# add a cube!
bpy.ops.mesh.primitive_cube_add(size=2, enter_editmode=True, align='WORLD', location=(0, 0, 0), scale=(1, 1, 1))
# do something to it to be sure that we have it...
bpy.data.objects['Cube'].scale[0] = 10
# THE CODE BELOW GIVES THE RuntimeError: Operator bpy.ops.mesh.loopcut_slide.poll() expected a view3d region & editmesh error message.
# What is the override code I have to use to fix it????????
bpy.ops.mesh.loopcut_slide(MESH_OT_loopcut={"number_cuts":16, "smoothness":0, "falloff":'INVERSE_SQUARE', "object_index":0, "edge_index":4, "mesh_select_mode_init":(True, False, False)}, TRANSFORM_OT_edge_slide={"value":0, "single_side":False, "use_even":False, "flipped":False, "use_clamp":True, "mirror":True, "snap":False, "snap_target":'CLOSEST', "snap_point":(0, 0, 0), "snap_align":False, "snap_normal":(0, 0, 0), "correct_uv":True, "release_confirm":False, "use_accurate":False})
import bpy
import os
os.system("cls")
objs = bpy.data.objects
for obj in objs:
if obj.name.find("Cube") == 0:
bpy.data.objects.remove(obj, do_unlink=True)
# add a cube!
bpy.ops.mesh.primitive_cube_add(size=2, enter_editmode=True, align='WORLD', location=(0, 0, 0), scale=(1, 1, 1))
# do something to it to be sure that we have it...
bpy.data.objects['Cube'].scale[0] = 10
for area in bpy.context.screen.areas:
if area.type == 'VIEW_3D':
for region in area.regions:
if region.type == 'WINDOW':
override = {'area': area, 'region': region, 'edit_object':bpy.context.edit_object}
bpy.ops.mesh.loopcut_slide(override,MESH_OT_loopcut={"number_cuts":16, "smoothness":0, "falloff":'INVERSE_SQUARE', "object_index":0, "edge_index":4, "mesh_select_mode_init":(True, False, False)}, TRANSFORM_OT_edge_slide={"value":0, "single_side":False, "use_even":False, "flipped":False, "use_clamp":True, "mirror":True, "snap":False, "snap_target":'CLOSEST', "snap_point":(0, 0, 0), "snap_align":False, "snap_normal":(0, 0, 0), "correct_uv":True, "release_confirm":False, "use_accurate":False})

Function setting enable to False only works on some buttons?

I am working on setting up a controls dashboard for operators. whenever a heartbeat stops incrementing, after 5 seconds, the LED changes to red. This also enables the acknowledge buttons. When I click them, the LED chages to yellow and I would like to disable the button again. The issue is that it only works for some of the buttons in my code for some reason. any idea why that might be? It only works for the buttons on the left side and not the right side. I am sure that the error is happening at "heartbeats[string][5].setEnabled(False)"
def ack_thread(string):
ack = threading.Thread(target=acknowledge, args=(string,))
ack.start()
def acknowledge(string):
global heartbeats
rowcount = ui.tableWidget.rowCount()
for x in range(0, rowcount, 1):
if ui.tableWidget.item(x,0).text() == string:
print(ui.tableWidget.item(x,0).text())
if ui.tableWidget.item(x,4).text() == 'False':
ui.tableWidget.setItem(x, 4, QtWidgets.QTableWidgetItem("True"))
ui.tableWidget.viewport().update()
heartbeats[string][3].setPixmap(ui.yellowled)
print(heartbeats[string][5])
# heartbeats[string][5].setEnabled(True)
heartbeats[string][5].setEnabled(False)
def gettime():
import datetime
currentDT = datetime.datetime.now()
return currentDT.strftime("%Y-%m-%d %H:%M:%S")
def alarm_handler(source):
rowPosition = ui.tableWidget.rowCount()
ui.tableWidget.insertRow(rowPosition)
ui.tableWidget.setItem(rowPosition, 0, QtWidgets.QTableWidgetItem(source))
ui.tableWidget.setItem(rowPosition, 1, QtWidgets.QTableWidgetItem("text1"))
ui.tableWidget.setItem(rowPosition, 2, QtWidgets.QTableWidgetItem("text2"))
ui.tableWidget.setItem(rowPosition, 3, QtWidgets.QTableWidgetItem(gettime()))
ui.tableWidget.setItem(rowPosition, 4, QtWidgets.QTableWidgetItem("False"))
def heartbeat():
import mhs2
from time import sleep
global heartbeats
mhs2.DB_CONFIG['host'] = '192.168.1.79'
# old value, new value, number of failures, LED object, checkbutton object, Acknowledge object,acknowledged
heartbeats = {
'pd1_hb': [0, 0, 0, ui.label_26, ui.checkBox_7, ui.pushButton_11, True],
'pd9_hb': [0, 0, 0, ui.label_20, ui.checkBox_12, ui.pushButton_13, True],
'pd11_hb': [0, 0, 0, ui.label_21, ui.checkBox_11, ui.pushButton_12, True],
'pd13_hb': [0, 0, 0, ui.label_22, ui.checkBox_13, ui.pushButton_2, True],
'irr1_hb': [0, 0, 0, ui.label_25, ui.checkBox_8, ui.pushButton_5, True],
'irr2_hb': [0, 0, 0, ui.label_24, ui.checkBox_9, ui.pushButton_4, True],
'irr5_hb': [0, 0, 0, ui.label_23, ui.checkBox_10, ui.pushButton_3, True],
'kyledata_hb': [0, 0, 0, ui.label_14, ui.checkBox, ui.pushButton, True],
'standata_hb': [0, 0, 0, ui.label_19, ui.checkBox_3, ui.pushButton_10, True],
'kennydata_hb': [0, 0, 0, ui.label_18, ui.checkBox_4, ui.pushButton_9, True],
'cartmandata_hb': [0, 0, 0, ui.label_17, ui.checkBox_6, ui.pushButton_8, True],
'chefdata_hb': [0, 0, 0, ui.label_16, ui.checkBox_5, ui.pushButton_6, True],
'buttarsdata_hb': [0, 0, 0, ui.label_15, ui.checkBox_2, ui.pushButton_7, True]
}
for item in heartbeats:
heartbeats[item][3].setPixmap(ui.offled)
query = "SELECT * FROM Heartbeat"
enable = mhs2.exec_query(query)
for row in enable:
if row['enable'] == 1:
heartbeats[row['parameter']][4].setChecked(True)
else:
heartbeats[row['parameter']][4].setChecked(False)
while 1:
command = "SELECT * FROM Heartbeat"
hb = mhs2.exec_query(command)
for row in hb:
if heartbeats[row['parameter']][4].isChecked():
heartbeats[row['parameter']][1] = row['value']
if heartbeats[row['parameter']][1] != heartbeats[row['parameter']][0]:
heartbeats[row['parameter']][2] = 0
heartbeats[row['parameter']][3].setPixmap(ui.greenled)
heartbeats[row['parameter']][6] = True
else:
heartbeats[row['parameter']][2] += 1
if heartbeats[row['parameter']][2] > 5 and heartbeats[row['parameter']][6]:
heartbeats[row['parameter']][3].setPixmap(ui.redled)
heartbeats[row['parameter']][5].setEnabled(True)
heartbeats[row['parameter']][6] = False
alarm = threading.Thread(target=alarm_handler,args=(row['parameter'],))
alarm.start()
heartbeats[row['parameter']][0] = heartbeats[row['parameter']][1]
else:
heartbeats[row['parameter']][3].setPixmap(ui.offled)
sleep(1)
if __name__ == "__main__":
import sys
app = QtWidgets.QApplication(sys.argv)
Dialog = QtWidgets.QDialog()
ui = Ui_Dialog()
ui.setupUi(Dialog)
Dialog.show()
hb = threading.Thread(target=heartbeat)
hb.start()
sys.exit(app.exec_())

I want to create a genric format function, what is default value of top,bottom,right,left

def columnandcellformate(sheet_name,bold = 0,font_color = '#000000',bg_color = '#ffffff',align = '' ,bottom = 0 ,top = 3,right = 0,left = 0,font_size = 10 ,starcolumn = 0, endrow = 0 ):
global sheet_format
sheet_format=sheet_name.add_format({
'bottom':bottom,
'top' : top,
'bg_color':bg_color,
'font_color' : font_color,
'align':align,
'font_size':font_size,
'bold': bold,
'font_name':'Batang'
})
What is default value of top,bottom,right,left, My function is making cell top,bottom,right and left blank
I think your default background color may have been causing some issues with the cell borders. I've added a few conditions based on whether you want these called by your function or not. These conditions make use of Format Methods such as format.set_bg_color(), format.set_bottom() (see docs for more information on these). They only provide a background color if you change it from the default.
import xlsxwriter
def columnandcellformate(bold = 0, font_color = '#000000', bg_color = 'none', align = '' , bottom = 999, top = 999, right = 999, left = 999, font_size = 10):
global sheet_format
sheet_format=workbook.add_format({
'font_color' : font_color,
'align': align,
'font_size': font_size,
'bold': bold,
'font_name': 'Batang'
})
if bg_color != 'none':
sheet_format.set_bg_color(bg_color)
if bottom != 999:
sheet_format.set_bottom(bottom)
if top != 999:
sheet_format.set_top(top)
if right != 999:
sheet_format.set_right(right)
if left != 999:
sheet_format.set_left(left)
workbook = xlsxwriter.Workbook('test.xlsx')
ws = workbook.add_worksheet('test_1')
columnandcellformate()
ws.write('B1', 'foo', sheet_format)
columnandcellformate(bold = 1, font_color = '#9C0006', bg_color = '#FFC7CE', align = '', bottom = 2, top = 1, right = 1, left = 1, font_size = 10)
ws.write('B3', 'bar', sheet_format)
workbook.close()
Expected Output:
The default values for format properties are almost all 0/False. See the initialization code for a format object.

Can't attach widget to my grid

I'm trying to create widgets with a loop. This is what I tried :
def set_runways(self, airfield):
i = 0
for rwy in airfield['Runways']:
frame = Gtk.Frame()
frame.set_label('-'.join([rwy['Runway_1'], rwy['Runway_2']]))
frame.set_shadow_type(1)
self.runways_layout.attach(frame, (i / 2), (i % 2), 1, 1)
rwy_layout = Gtk.Grid()
frame.add(rwy_layout)
# Just for testing :
label = Gtk.Label('Hello, World')
rwy_layout.attach(label, 0, 0, 1, 1)
I import my runways_layout in my __init__ with self.runways_layout = builder.get_object('runwaysGrid') which is a Gtk.Grid and I call my function after with self.set_runways(airfield). But even with this, my window doesn't show Hello World, I have a blank window... Why ?
I specify that my rwy isn't empty.
Thanks for your help.
EDIT :
Okay I tried this simple thing :
self.runways_layout = builder.get_object('runwaysGrid')
label = Gtk.Label('Coucou')
self.runways_layout.attach(label, 0, 0, 1, 1)
And it doesn't work too... O_o
Okay I found a solution, I have to do :
self.runways_layout = builder.get_object('runwaysGrid')
label = Gtk.Label('Coucou')
self.runways_layout.attach(label, 0, 0, 1, 1)
label.show() # Add this and it works...
But why ?

"Blinking" animation in Python with libavg

I'm working on a project using libavg and a series of RectNodes. What I'm trying to do is play an animation that makes each node light up white for 2,5 seconds, and then fades back. Every time you click one of the nodes, the same animation should happen for that specific node.
I'm using the AVGApp class, and a list with RectNode id and how many times they are supposed to light up, like (id1, 2)
def playAnim(self, animarr):
for i in range(0, len(animarr)):
i, count = animarr[i]
sid = "r" + str(i)
node = g_player.getElementByID(sid)
while count > 0:
self.blink(node)
count -= 1
return
and my code for blink:
def blink(self, node):
pos = node.pos
size = node.size
covernode = avg.RectNode(pos=pos, size=size, fillopacity=0,
parent = self._parentNode, fillcolor="ffffff",
color="000000", strokewidth=2)
self.animObj = LinearAnim(covernode, 'fillopacity', 1000, 0, 1)
self.animObj.start()
self.animObj = LinearAnim(covernode, 'fillopacity', 1000, 1, 0)
self.animObj.start()
covernode.unlink(True)
return
I'm calling it with:
def _enter(self):
(some other stuff here)
print "Let's get started!"
self.playAnim(self.animArr)
print "Your turn!"
Any help is greatly appreciated, the libavg Reference isn't helping me much.
The problem is that anim.start() is non-blocking.
Instead of only returning after the animation is finished, it will return immediately and the animation will be executed concurrently. This means at the same time your function starts two animations and unlinks the node that should be animated.
So instead you should use the end callbacks that can be given to the animations to trigger one step after another. The the blink function could then look like this:
def blink(self, node):
pos = node.pos
size = node.size
covernode = avg.RectNode(pos=pos, size=size, fillopacity=0,
parent = self._parentNode, fillcolor="ffffff",
color="000000", strokewidth=2)
fadeOutAnim = LinearAnim(covernode, 'fillopacity', 1000, 1, 0, False,
None, covernode.unlink)
fadInAnim= LinearAnim(covernode, 'fillopacity', 1000, 0, 1, False,
None, fadeOutAnim.start)
fadInAnim.start()
If you want the node to stay white for a certain amount of time you have to insert another step using either a WaitAnim or player.setTimeout().
(https://www.libavg.de/reference/current/player.html#libavg.avg.Player)
def blink(self, node):
pos = node.pos
size = node.size
covernode = avg.RectNode(pos=pos, size=size, fillopacity=0,
parent = self._parentNode, fillcolor="ffffff",
color="000000", strokewidth=2)
fadeOutAnim = LinearAnim(covernode, 'fillopacity', 1000, 1, 0, False,
None, covernode.unlink
)
fadInAnimObj = LinearAnim(covernode, 'fillopacity', 1000, 0, 1, False,
None, lambda:self.wait(500, fadeOutAnim.start)
)
fadInAnimObj.start()
def wait(self, time, end_cb):
avg.Player.get().setTimeout(time, end_cb)

Categories