Random emitters

Discussion about scripting with Python
User avatar
Otuama
Posts: 1155
Joined: Fri Apr 02, 2010 12:56 am
Location: Swansea
Contact:

Random emitters

Postby Otuama » Fri Feb 10, 2012 5:50 pm

Hi all.

I'm trying to adapt a script that splits particles but I've hit an annoying problem and can't figure it out.

The script originally did THIS.

What I've now got it to do is split to extra emitters.

I was telling it the emitter to add to, for example:

blueEmitter.addParticle(particlePos, particleVel)
greenEmitter.removeParticle(getGreenIDs)


But then I decided I wanted the particles to be sent to a random emitter.

I've tried quite a few ways but I keep getting errors and it is getting on my nerves.

Here is a bit of the script but it has everything you'd need to see:

Code: Select all

import random

#Get some emitters
greenEmitter = scene.get_PB_Emitter("Green")
blueEmitter = scene.get_PB_Emitter("Blue")
purpleEmitter = scene.get_PB_Emitter("Purple")
orangeEmitter = scene.get_PB_Emitter("Orange")

#Make a list with emitter names
emitterList = ["greenEmitter", "blueEmitter", "purpleEmitter", orangeEmitter]
randEmitter = random.randint(0,3)

randModulo = random.randint(3,9)

while (greenParticles):

   #If frame is equal to the below value
   if (frameCheck == 6):
      #Get the particles IDs
      getGreenIDs = greenParticles.getId()

      #If a particle ID has a remainder of randModulo
      if ( getGreenIDs %randModulo):
         #Get the particles position
         particlePos = greenParticles.getPosition()
         #And its velocity
         particleVel = greenParticles.getVelocity()
         #Add the particles to the random emitter
         emitterList[randEmitter].addParticle(particlePos, particleVel)
         #And remove it from the Green emitter
         greenEmitter.removeParticle(getGreenIDs)

I get:

#WARNING: Script error: "'str' object has no attribute 'addParticle'" at line number

This works and prints each emitter name randomly

scene.message(emitterList[randEmitter])

I've tried different variations:

I've tried different variations, all of which don't work. For example:

(emitterList[randEmitter] + ".addParticle(particlePos, particleVel)")
str(emitterList[randEmitter]).addParticle(particlePos, particleVel)


I know I'm missing something obvious here but can't figure out what.

-Sean
iCreate Ltd
'Strength does not come from physical capacity. It comes from an indomitable will.' - Gandhi
'What Does Success Look Like?' - iCreate Ltd


User avatar
Otuama
Posts: 1155
Joined: Fri Apr 02, 2010 12:56 am
Location: Swansea
Contact:

Random emitters

Postby Otuama » Fri Feb 10, 2012 6:29 pm

I always seem to figure things out after posting on here.

emitterList[randEmitter] is a string.

so, I thought about trying to strip the strings quote symbol. after realising that's stupid and giving myself a slap it dawned on me.

I needed to convert it.

so, using 'eval' it seems to have fixed it.

eval(emitterList[randEmitter]).addParticle(particlePos, particleVel)

Thanks, me :).
iCreate Ltd
'Strength does not come from physical capacity. It comes from an indomitable will.' - Gandhi
'What Does Success Look Like?' - iCreate Ltd

Ghosteg
Posts: 122
Joined: Sun Feb 07, 2010 4:56 pm
Contact:

Random emitters

Postby Ghosteg » Tue Feb 14, 2012 9:42 pm

just wanted to point out that you made a list of strings and not emitters, but i see you figured it out by yourself :)

User avatar
Otuama
Posts: 1155
Joined: Fri Apr 02, 2010 12:56 am
Location: Swansea
Contact:

Random emitters

Postby Otuama » Wed Feb 15, 2012 12:27 am

I was going through a mad period at the time.

But yeah, could either do a list of strings and convert the lists elements:

Code: Select all

emitterList = ["greenEmitter", "blueEmitter", "purpleEmitter", "orangeEmitter", "redEmitter", yellowEmitter]

convertEmitter = eval(emitterList[randEmitter])

or have them as objects and save myself writing a few " 's & 1 extra word (eval).
iCreate Ltd
'Strength does not come from physical capacity. It comes from an indomitable will.' - Gandhi
'What Does Success Look Like?' - iCreate Ltd


Return to “Python-Scripting”

Who is online

Users browsing this forum: No registered users and 2 guests