View Full Version : Python: Scene Time laps/ Hardware 2.0 Batch render

12 December 2012, 07:57 AM
I thought it would be cool to do a modeling time laps and thought it would be easy. The idea would be to make a camera and animate it floating around the scene watching the modeling.

The camera would be rendering viewport quality images every x seconds and save them to a destination while you model and do whatever in your scene in the perspective camera creating a timelaps of the scene. Im stuck at the render/save from camera stage.

I tried a so manny different commands and could not get what I wanted.
The ones I tried were-
glRender, ogsRender, hwRender and playblasting but that required you to be looking though the camera, as far as I know.
I got the closest with hwRender, which was getting an image to save out to the maya tmp directory with the scene name. So not very far unfortunately :banghead:
I just need help with a way to save out a image from a camera. Any insight?

12 December 2012, 09:22 PM
I think you can get the gl context from M3dView or just use the winapi to get the current wgl context, and then read the gl buffer (look for my previous post on independent opengl plugin), but if you are able to render a file to the temporary directory, why not copy it to where you want it?

12 December 2012, 12:53 AM
I have scratched together a working version of the script here
It works well enough to convince me to do this right.

My first priority is to change the timing funchion. Been looking around and found a couple options, like sockets and possibly scriptjob. not sure yet. I was thinking something that could run outside maya and just tell maya every 10 seconds to run a script.

If anyone has any insight into how to set up a timed function I would love to hear!Thanks

12 December 2012, 12:55 AM
I think you can get the gl context from M3dView or just use the winapi to get the current wgl context, and then read the gl buffer (look for my previous post on independent opengl plugin), but if you are able to render a file to the temporary directory, why not copy it to where you want it?
I couldn't find this, but i am interested

12 December 2012, 07:33 AM
Info about opengl:

If you want to send commands to maya from the outside look into commandPort.

12 December 2012, 05:41 PM

LOL thats awesome. Unfortunately my api knowledge is lacking a bit to really make easy use of those, so I may need to ask you how to peace these together when i get to that stage.

For now Im working on the getting the commands to run in maya. I have the commandPort section working and communicating with the terminal. I guess the best way to handle it from here will be a scriptJob? I havent messed with the commandPort enough to know if i can just send the commands directly though this. Ill have to play with it tonight.

12 December 2012, 06:26 PM
I'm glad that you liked it, people sometimes get offended ;)
If you aren't familiar with the API (OpenMaya module), then it won't be trivial, but since you mentioned gl at the beginning I wrongly assumed C++.

If you got the commandPort working, then I'm not sure why you need the scriptJob. Set a timer on the external (non maya) script and just send commands to maya.

12 December 2012, 08:06 AM
If you don't need a precise time, people use jobScript to set a RecentCommandChanged or SelectionChanged events, and check their timer on these events. For example:

It would save you the hassle of an external script with commandPort.

12 December 2012, 06:57 PM
After messing with the commandPort and Its definitely the way to go (for what i want).
I am sending a command to maya to run my "takePicture" script, but im running into problems with the naming (or passing a variable). I want to be able to pass a variable to the command im sending to maya but it donst want to work form my script. Im sure its something simple but admittedly I haven't done to much of this so im sure its something simple.
right now im using the following in idle

i = "testName"
maya.send(bytes("from takePicture import *", "UTF-8"))# to import the script

maya.send(bytes("reload(takePicture)", "UTF-8"))# reload if running the script again

maya.send(bytes("takePicture(0)".format(i),'UTF-8'))# take picture command

and my looks something like this

def takePicture(name):
print "testing", name

I have a feeling im just handling the import and running the script wrong, so it would be nice if someone could steer me in the right direction.

12 December 2012, 10:07 PM
Are you sending the commands line-by-line or are you executing a full script?
Please provide a full source that can be tested.

Since I found communicating through tcp with the commandPort buggy, I just prefer to execute whole scripts with a small commandPort.exe util. See for example:

post #14.

12 December 2012, 08:07 PM
Im executing a full script. For the set up in maya I have a little ui that makes a file for the outside script to check, so i have the ability to stop the script within maya.

And the outside of maya scirpt im running in idle
Currently just using and if statement, but that will turn into a while once i get this working.

And the script to be exicuted
I have most of it commented out for testing purposes. The goal is to just pass the variable

# timeLaps
def takePicture(fileName):
print "testing", fileName
tmpDirectory = (os.path.expanduser("~/Documents/maya/projects/default/images/tmp/timeLaps.iff"))
filePath = (os.path.expanduser("~/Desktop/"+fileName+".iff"))
hwRender(cam="camera1", cf=1, fnp = 1)
s.copy2(tmpDirectory, filePath)

12 December 2012, 11:02 PM
Let's call the inside script, and the outside script

1. I'm not sure which external external python interpreter you use, but I use python 2.7 and it complained on:

print bytes("from timeLaps import *", "UTF-8")
TypeError: str() takes at most 1 argument (2 given)

so I used instead

print bytes("from timeLaps import *")

2. In when you format the string, you forgot the curly brackets:


3. After each send you should check maya's output:

res = maya.recv(10000)
print "Result:", res

4. You will notice that you need to end your commands with "\n":

maya.send(bytes("from timeLaps import *\n"))

and I think that each line (each send) is executed independently, so you should first format a command string that includes your whole script, then send it. Else I think you are executing line-by-line.

12 December 2012, 01:45 AM
Finally got it!!! Much thanks zoharl!

heres a little test of the script set at 1 fps

Im running it though the terminal but Im going to work on getting to be an .exe or something.
Then I want to figure out how to tell the .exe to run from within maya.

12 December 2012, 02:09 AM
You model quite fast... ;)

I'm not sure why you need .exe for, but if you don't want to be dependent on python you can always write your code in C++.

To execute an external app from maya you can use:

import os

unless you meant something else.

12 December 2012, 04:58 AM
If I can get the terminal to open/run my script from maya then I dont need the .exe.

any chance you know the command to open the terminal? Iv been searching around and cant find any info on os.system

tried "" and also tried with the file path with no luck

And i dont have any C++ experience, so its all python for now.

12 December 2012, 10:00 AM
Forget .cpp, forget .exe, python is more than enough.

Did you try to copy/paste my os.system example to the script editor and run it? The command is quite straight forward, you put in the string the command you want to execute in the cmd. If you need formal doc:

So to run an external script c:\8\ that contains the following code:

print 'hi'

from maya:

import os
os.system("start /b python -E /8/")

or if you want to use maya's python:

import os
os.system("start /b mayapy -E /8/")

CGTalk Moderation
12 December 2012, 10:00 AM
This thread has been automatically closed as it remained inactive for 12 months. If you wish to continue the discussion, please create a new thread in the appropriate forum.