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

Random expression, but also random time?

3 REPLIES 3
Reply
Message 1 of 4
Anonymous
1792 Views, 3 Replies

Random expression, but also random time?

Hi,

 

I'm trying to create a neural network for a brand. I have the objects and materials ready but what I'm trying to achieve is a random pop of glow. 

 

Currently, I have 

NeuralColor01.glowIntensity = rand(0,1)

 

This however is just flickering and doing all kinds of stuff I don't want. I just need a random pop of glow that basically goes from 0 to 100 in about 1 frame, then fades off over the course of about 30 frames, and happens random, and not right back to back... there could be a 60 frame pause, then maybe 1 per second... very medical. 

 

Is this even possible? It's WAY out of my league. I really don't understand how to use expressions like this...

 

THANK YOU!!

Dave

3 REPLIES 3
Message 2 of 4
santd
in reply to: Anonymous

Hello,

 

Thank you for posting to The Area Forums.

 

I'm not sure how to go about setting an expression that does this. It seems like it might be really complicated to setup for an expression. However, I was able to come up with a script that creates keyFrames at random times in the timeline for that parameter:

 

import maya.cmds as mc
import random
attribute = 'glowIntensity'
mc.setKeyframe('lambert1.' + attribute)
for x in range(30):
totalKeys = mc.keyframe('lambert1', attribute = attribute, query = True, keyframeCount = True)
lastKey = (int(totalKeys) - 1)
timeLastKey = mc.keyframe('lambert1',index=(int(lastKey),int(lastKey)), attribute = attribute, query=True)
randNum = random.randint(timeLastKey[0],500)
startAnim = (randNum - 3)
endAnim = (randNum + 20)
mc.setKeyframe('lambert1', time = startAnim, value = 0, at = attribute)
mc.setKeyframe('lambert1', time = randNum, value = 1, at = attribute)
mc.setKeyframe('lambert1', time = endAnim, value = 0, at = attribute)

 

I have attached the file as this doesn't keep the formatting that the script should have. Open the script in notepad or similar program and copy paste to Maya Python tab then run the script. The script will run from were you currently are in the timeline so if you want it to start from frame 1 move to that frame then run the script.

 

As is the script will only go to frame 500 from where you start it and does not overlap the animation. You can edit the line:

 

randNum = rand.randint(timeLastKey[0],500)

 

If you don't want it to overlap the animation then keep "timeLastKey[0]". However, if you don't care if the animation overlaps then set that and "500" to the time range you would like the keys to be randomly placed in. For example the following would set key in a timerange from 10 to 700 without caring if animations overlap.

 

randNum = rand.randint(10,700)

 

If you wish for the animation not to overlap then keep the first value as "timeLastKey[0]" and only edit the last frame you would like it to go to (the second value).

 

I'm sure there probably is a better way of doing this, but this is what I was able to come up with quickly (script is a little messy, but should work).

 

Hope that helps.

 

Cheers,




David Santos

Message 3 of 4
Anonymous
in reply to: santd

ok, I'll see if I can get that to work... since then I did them all manually which took forever, but this would be a cool thing to have.

 

Thanks-

Dave

 

Message 4 of 4
santd
in reply to: Anonymous

Made some edits to the script to make it a litte easier to use:

 

The following script is specific to for the node lambert1 and for the attribute 'glowIntensity'. However, it can be edited for any node and any attribute.

Things to edit in script:

  • Edit the value of the variable "attribute" to change the attribute of the object or node to key.
  • Edit the value of the variable "node" to change the object or node that is being keyed.
  • Edit the value of the variable "cycleAmount" for the amount of times to cycle the script.
  • Edit the value of the variable "space" to edit the amount of allowable space between key frames.

Make note that the script will run from the current time selected or if the specific attribute already has keyframes it will begin from the last key frame in the timeline.

import maya.cmds as mc
import random
attribute = 'glowIntensity'
node = 'lambert1'
space = 20
cycleAmount = 10
mc.setKeyframe(node + '.' + attribute)
for x in range(cycleAmount):
    totalKeys = mc.keyframe(node, attribute = attribute, query = True, keyframeCount = True)
    lastKey = (int(totalKeys) - 1)
    timeLastKey = mc.keyframe(node,index=(int(lastKey),int(lastKey)), attribute = attribute, query=True)
    randNum = random.randint(timeLastKey[0],timeLastKey[0] + space)
    startAnim = (randNum - 3)
    endAnim = (randNum + 20)
    mc.setKeyframe(node, time = startAnim, value = 0, at = attribute)
    mc.setKeyframe(node, time = randNum, value = 1, at = attribute)
    mc.setKeyframe(node, time = endAnim, value = 0, at = attribute)




David Santos

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

Post to forums  

Autodesk Design & Make Report