ScriptSpot

Forum topic · General Scripting

How to store object keys?!

By JokerMartini · 2010-09-02

Downloads
Description

I've wrote up this basic script that takes a selection of objects and I want it to be able to take each objects individual keys and flip them on the x-axis. I do this by running a function that multiplies the existing keys on the object by -1.

Problems
-It's bugging out when I hit the apply button of the script. It's not applying the function to each key properly when applied.

-I'm not sure if the storing of the objects pre-existing keys is where the problem is.

Help would be appreciated.
Thanks
JokerMartini

Comments (7)

JokerMartini · 2010-09-07

I'm not sure why there is a pick object button?
I'm trying to avoid having a pick button. I just want each object in the select to reference itself for its keys.
That way users to can select an array of objects with different animation on them and flip them.

Anubis · 2010-09-07

Hi Martin, this was in case you copy keys from an object to multiple objects. I just trying to figure out why you use $ sign on multiple objects. Now, reading your last post, I think so you need only the function that I post before --

-- EDIT: simplified function version (strip 2 arguments):
function flipPositionKeys axis =
(
    userSel = getCurrentSelection ()
    for obj in userSel do
    (
        userKeys = obj.position.controller[axis].controller.keys
        if userKeys.count > 0 do
            for k in userKeys do k.value *= -1
    )
)

-- Usage:
flipPositionKeys 1 -- X axis
flipPositionKeys 2 -- Y axis
flipPositionKeys 3 -- Z axis

JokerMartini · 2010-09-03

This does not work with a selection of objects, it only works with one object selected?
I've hit a mind block and can not solve it.


(
ObjKeys = undefined
   
rollout roAdjustAnimation "Animation Abjust"
(
    button btnApply "Apply" width:90 height:90 pos:[5,5]

--New Function
    function flipPositionKeys axisS axisD dir =
    (
        ObjKeys = $.position.controller
        userSel = getCurrentSelection ()
        for i = 1 to userSel.count do
        (
            s = userSel[i]
            if(ObjKeys[axisS].controller.keys.count >= 1) then
            (
                userKeys = ObjKeys[axisS].controller.keys
                for k in userKeys do k.value *= dir 
            )
        )
    )
	  
    on btnApply pressed do
    (
		for Obj in selection do
		(
        flipPositionKeys 1 1 -1
		)
    )

)
createDialog roAdjustAnimation 100 100
)

Anubis · 2010-09-04

This

ObjKeys = $.position.controller

will works only on single selected. Maybe I miss the final goal as well... if the objects already animated, the next function will work on multiple selection. if you need to copy the keys from 1 object to multiple objects, just write separate function.

function flipPositionKeys axisS axisD dir =
(
    userSel = getCurrentSelection ()
    for obj in userSel do
    (
        userKeys = obj.position.controller[axisS].controller.keys
        if userKeys.count > 0 do
            for k in userKeys do k.value *= dir
    )
)

------------- EDIT --------------------
-- Ok, there you go working version...
---------------------------------------

rollout roAdjustAnimation "Animation Abjust"
(
	local srcObj
	
	pickButton pbtSource "Pick Object" width:90 height:40 pos:[6,6]
    button btnApply "Apply" width:90 height:40 pos:[6,46]
 
-- flip pos keys function
	function flipPositionKeys userSel axisS axisD dir =
	(
		--userSel = getCurrentSelection ()
		for obj in userSel do
		(
			userKeys = obj.position.controller[axisS].controller.keys
			if userKeys.count > 0 do
				for k in userKeys do k.value *= dir
		)
	)
-- get pos keys function
	fn getPosKeys obj axis = (obj.position.controller[axis].controller.keys)
		
	on pbtSource picked obj do
		if isValidNode obj do srcObj = obj
	
    on btnApply pressed do
    (
		userSel = getCurrentSelection ()
		if userSel.count > 0 and isValidNode srcObj do
		(
			userKeys = getPosKeys srcObj 1
			-- copy keys...
			for obj in userSel do (
				targetKeys = getPosKeys obj 1
				for key in userKeys do (
					appendKey targetKeys key
				)
			)
			-- flip keys...
			flipPositionKeys userSel 1 1 -1
		)
    )
 
)
createDialog roAdjustAnimation 100 100

Anubis · 2010-09-03

Maybe appendKey() function is buged?... If to say the object has 2 keys #(1f, 30f), the function create 2 new keys at time 1 i.e. #(1f, 1f, 1f, 30f), totaly irregular result.

-- if replace this part of the code:
for k = 1 to userKeys.count do
(
	appendKey s.position.controller[axisD].controller.keys userKeys[k]
	s.position.controller[axisD].controller.keys[k].value *= dir
)

-- with this one:
for k in userKeys do k.value *= dir 

-- it works fine

(ObjKeys = undefined

JokerMartini · 2010-09-02

(
ObjKeys = undefined
   
rollout roAdjustAnimation "Animation Abjust"
(
    button btnApply "Apply" width:90 height:90 pos:[5,5]
       
    function flipPositionKeys axisS axisD dir =
    (
        ObjKeys = $.position.controller
        userSel = getCurrentSelection ()
        for i = 1 to userSel.count do
        (
            s = userSel[i]
            if(ObjKeys[axisS].controller.keys.count >= 1) then
            (
                userKeys = ObjKeys[axisS].controller.keys --holds keys from animated object's X-Position as a variable
                for k = 1 to userKeys.count do
                (
                    deletekeys s.pos.track
                    deletekeys s.rotation.track
                    deletekeys s.scale.track
                    appendKey s.position.controller[axisD].controller.keys userKeys[k]
                    s.position.controller[axisD].controller.keys[k].value *= dir
                )
            )
        )
    )
   
    on btnApply pressed do
    (
        flipPositionKeys 1 1 -1
    )
   
   
)
createDialog roAdjustAnimation 100 100
)

JokerMartini · 2010-09-02

Here is what I have so far. This is more recent than what the attached file is. I'm trying to store the keys in the variable ObjKeys but it is not working when its calling them up later to append the keys.