Noob Question: Expression for "Position at frame x"

Discussion about scripting with Python
LeJentle46
Posts: 3
Joined: Tue Nov 20, 2012 10:56 am

Noob Question: Expression for "Position at frame x"

Postby LeJentle46 » Wed Jan 09, 2013 2:54 pm

Hi guys,

I would like to attach a null object to an imported animated object with a frame offset of 5 frames.

What would the expression for this be exactly?
What I tried was go into the curves of the null and link the imported objects' x/y/z position and rotation. (which would be the same as parenting it to the object basically..)

So far, so good, but...I'm not sure how to get these values "at frame x". (say 5 frames back)?

Total noob here, can someone help me out? :|

LeJ


User avatar
Otuama
Posts: 1155
Joined: Fri Apr 02, 2010 12:56 am
Location: Swansea
Contact:

Re: Noob Question: Expression for "Position at frame x"

Postby Otuama » Thu Jan 10, 2013 4:14 am

I don't think it'll be as easy as it might seem. I'll see if I can make a script tomorrow.
iCreate Ltd
'Strength does not come from physical capacity. It comes from an indomitable will.' - Gandhi
'What Does Success Look Like?' - iCreate Ltd

User avatar
Otuama
Posts: 1155
Joined: Fri Apr 02, 2010 12:56 am
Location: Swansea
Contact:

Re: Noob Question: Expression for "Position at frame x"

Postby Otuama » Thu Jan 10, 2013 8:06 pm

Right, see how this works.

https://vimeo.com/57161603

I'm not sure if it's what you need because you were talking about rotation too. This is a simple script to offset a nulls X position

This version will only work on Windows but it won't take long to change for a Mac.

Try it on a test scene first to see if it's what you want. For my test scene I used a sphere animated in X and a null.

With the script you can:
1: Set the offset value
2: Enter the name of the tracking object (null)
3: Enter the name of the imported object
4: Display some messages if you want

What the script does is:
1: Makes a folder in your project directory
2: Gets the X position of the animated object
3: Saves the position into a new text file each frame (in the created directory)
4: Depending on the offset value it reads the relevant file
5: Applies the value to the null

To use:
Go to Layout > Simulation Events:

At the top right click SimulationPre > Add Script
Paste this in the box and press OK

Code: Select all

import os

#Get Rf directory
getDir = scene.getRootPath()

#Make a folder in the Rf dir to store text files.  !For Windows!
#If folder doesn't exist, make it
if not (os.path.isdir(getDir + "\\PosExportFiles")):
    os.makedirs(getDir + "\\PosExportFiles")

#Move to the new dir
newDir = (getDir + "\\PosExportFiles")
os.chdir(newDir)

#Make space on the message window
scene.message("\n" * 40)


At the top right click FramesPost> Add Script
Paste this in the box and press OK
You can edit the values at the top of the script

NOTE: Because no values exist before the offset stage, I've told the null to be positioned in the centre of the grid. I didn't know what to do with it before the offset. This can be changed below.

Code: Select all

###################################
#########################################
###################################

#Enter the positon of the null before the offset starts
#0.0 = grid
startPos = 0.0

#For modifying the offset value
offset = 5 #(frames)

#Enter the name of the null object below
trackingObject = "Null01"

#Enter the name of the imported object below
trackedObject = "Sphere01"

#Enter Yes ( "Yes" ) if you want position messages to be shown
#Leave blank if you don't ( "" )
displayMessages = "Yes"

###################################
#########################################
###################################

#Get the current frame
getFrame = scene.getCurrentFrame()

#Get the tracking object
trackingObject = scene.getObject(trackingObject)

#Get the tracked object
trackedObject = scene.getObject(trackedObject)

#Get the X value of the tracked object
getXPos = trackedObject.getParameter("Position").x

#Make a text file for each frame in the export folder
writeFile = open(newDir + "/ObjectPositionFile_%05d.txt"% (getFrame), "w" )

#Write the X value into the frames text file
writeFile.write(str(getXPos))

#Close the file
writeFile.close()

#If before the set, put the null in the start postion
if (getFrame < offset):
   trackingObject.setParameter("Position", Vector.new(startPos,0.0,0.0))

#If not
else:

   #Open the relevant frame's file
   getPosValue = open(newDir + "/ObjectPositionFile_%05d.txt"% (getFrame -offset), "r" )
      
   #Read the value
   readValue = getPosValue.read()

   #Display messages if you want
   if (displayMessages == "Yes"):
      scene.message("Frame #" + str(getFrame) + " actual position: " + str(getXPos))
      scene.message("Frame " + str(getFrame) + " new position: " + str(readValue) + " (file #" + (str(getFrame -offset)) + ")")
      
   #Put the null in the offset position
   trackingObject.setParameter("Position", Vector.new(float(readValue),0.0,0.0))
   


At the top right click SimulationPost> Add Script
Paste this in the box and press OK

Code: Select all

#Get Rf directory
getDir = scene.getRootPath()
os.chdir(getDir)

iCreate Ltd
'Strength does not come from physical capacity. It comes from an indomitable will.' - Gandhi
'What Does Success Look Like?' - iCreate Ltd


Return to “Python-Scripting”

Who is online

Users browsing this forum: No registered users and 1 guest