PDA

View Full Version : concatenation in python


stretched
10-13-2010, 07:24 PM
hello everyone,

last month i start to learn python via some books and maya python documantation. all going well atm. except concatenation in python. here is my first script

import maya.cmds as arf
pPx=arf.getAttr("persp.tx")
pPy=arf.getAttr("persp.ty")
pPz=arf.getAttr("persp.tz")
pRx=arf.getAttr("persp.rx")
pRy=arf.getAttr("persp.ry")
pRz=arf.getAttr("persp.rz")

povCamera = arf.camera()
povCameraName = povCamera[0]

arf.setAttr ("camera1.tx", pPx)
arf.setAttr ("camera1.ty", pPy)
arf.setAttr ("camera1.tz", pPz)
arf.setAttr ("camera1.rx", pRx)
arf.setAttr ("camera1.ry", pRy)
arf.setAttr ("camera1.rz", pRz)

arf.lookThru("camera1")



well this script works, it creates a new camera from current perspective camera and look thruu. but the problem is , it works for fist execution , if i go to persp cam and position it and execute the script again it changes my camera1's position and rotation. i know setAttr section of my scripts looks "camera1" rather than povCamera[0] . the problem for me now is not solution for this script. overall concatenation in python is a bit tricky i guess. in any python books we can not find infpormation about python in maya(very normal). also in maya help doc i couldnt find anything usefull. so where should i look.?
thanks for comments.

Kel Solaar
10-13-2010, 07:37 PM
import maya.cmds as cmds

ATTRIBUTES = ("tx", "ty", "tz", "rx", "ry", "rz")

def storeAttributesValues( node, attributes ) :
values = {}
for attribute in attributes :
values[attribute] = cmds.getAttr( node + "." + attribute )
return values

def restoreAttributesValues( node, values ) :
for value in values :
cmds.setAttr( node + "." + value, values[value] )


ATTRIBUTES_VALUES = storeAttributesValues( "pSphere1", ATTRIBUTES )
restoreAttributesValues( "pSphere1" , ATTRIBUTES_VALUES)


Storing the Attributes / Values in a python dictionary with Attribute : Value as Key : Value using the storeAttributesValues definition and then restoring them using the other definition.

It's one way to do it, you could use a tuple also for the values if you have the order of the attributes stored somewhere ( that's the case in my example ).

Cheers,

KS

stretched
10-13-2010, 07:56 PM
import maya.cmds as arf

pPx=arf.getAttr("persp.tx")
pPy=arf.getAttr("persp.ty")
pPz=arf.getAttr("persp.tz")
pRx=arf.getAttr("persp.rx")
pRy=arf.getAttr("persp.ry")
pRz=arf.getAttr("persp.rz")

povCamera = arf.camera()
povCameraName = povCamera[0]

arf.setAttr (povCameraName+".tx", pPx)
arf.setAttr (povCameraName+".ty", pPy)
arf.setAttr (povCameraName+".tz", pPz)
arf.setAttr (povCameraName+".rx", pRx)
arf.setAttr (povCameraName+".ry", pRy)
arf.setAttr (povCameraName+".rz", pRz)

arf.lookThru(povCamera[0])

Kel Solaar
10-13-2010, 08:26 PM
I see u edited your post :) Guess you saw that my example also dealing with what you wanted :)

CGTalk Moderation
10-13-2010, 08:26 PM
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.