Community
Maya Programming
Welcome to Autodesk’s Maya Forums. Share your knowledge, ask questions, and explore popular Maya SDK topics.
cancel
Showing results for 
Show  only  | Search instead for 
Did you mean: 

Scripting random movement (Python)?

1 REPLY 1
Reply
Message 1 of 2
ProfessorBamboo
2492 Views, 1 Reply

Scripting random movement (Python)?

Hi there, I'm learning how to do Maya scripting and as the title suggests, I'm trying to write a Python script for random movement. The goal is to create a script so that an object is at a different position every 10 frames by giving it's x, y, and z a random value between -5 and 5, thus creating "random" movement.

 

I am using a for loop that goes from 1 to 10, then multiplying that that number by 10 to get what frame I have to set the keyframe at. The problem is that setKeyframe requires a time or float value, not an int value. I tried converting the number into a float but it was not accepted and I am not sure how to convert it into a time value.

 

If anyone is can help or offer an alternative solution to create random movement, it would be greatly appreciated.

1 REPLY 1
Message 2 of 2

Hello.

 

You can use currentTime to change the current time and setKeyframe without any parameters to add a keyframe at the current time.

 

By the way :

 

import random

cmds.currentTime(1)

cube = cmds.polyCube()[0] # to get the name of the transform

cmds.setKeyframe()

stepCount = 15
for i in range(stepCount ):
	frame = (i+1) * 10 # one step = 10 frames
	cmds.currentTime(frame)
	cmds.setAttr(cube + ".translateX", random.random() * 20 - 10)
	cmds.setAttr(cube + ".translateY", random.random() * 20 - 10)
	cmds.setAttr(cube + ".translateZ", random.random() * 20 - 10)
	cmds.setKeyframe()

 

 

Can't find what you're looking for? Ask the community or share your knowledge.

Post to forums  

Autodesk Design & Make Report