Distance Script + Auto omni light creation


#1

Alright I have two questions.

First: I just need to know how to create a simple distance script operator for a PFlow system such that when Object A and Object B are a certain distance from each other, Object B will fire an event. For a literal example, i’m creating a totally Pflow-driven space battle scene and I want an Object B (black ship) to shoot once it’s close enough to an Object A (white ship)…

Secondly. How would I script a PFlow event that would create an omni-light and automatically keyframe some intensity settings based on a Spawn event. Literal example being: When a ship shoots and it’s missile detonates, I want it to create an Omni light that will light up and decrease in intensity as such with the intensity of the explosion so that I don’t have to manually create and keyframe omni-lights for every PFlow-created explosion…

Hope you guys understood that and can help… Cheers!:smiley:


#2

For the second question, have it spawn one particle, then pass that to an event with a script operator and a delete (all). By running the script, it should create and key the light. I had mine create one particle at frame 10, and so it lit up my teapot from black starting at frame 10, then faded out again. You could extend this script (for example having one light for every particle) by parsing out the particle ID or something, if you so chose. I just have it quite restrictive because it keeps it from creating many duplicate lights.

on ChannelsUsed pCont do
(
	
)

on Init pCont do 
(

)

on Proceed pCont do 
(
if pCont.numparticles() == 1 do (
	try (
		x = execute("$explosionLight")
		select x
		max delete
	)
	catch (
	print "no object found"
	)
	x = omnilight name:"explosionLight" multiplier:0.0
	y = currentTime.frame
	animate on (
	at time (y-1) x.multiplier = 0.0
	at time y x.multiplier = 1.0
	at time (y+10) x.multiplier = 0.0
	print "Light Set"
	)
	)
)

on Release pCont do 
(
 
)

Not sure about the first question you asked though. Someone else will have to answer that.


#3

As for the first question:

 [color=white]on[/color] ChannelsUsed pCont do
(
)
 
on Init pCont do
(
)
 
on Proceed pCont do
(
if (distance $obj1.pos $obj2.pos) < 100.0f then
[indent]for i = 1 to 8 do -- 8 is the spawn rate per frame 
[indent]pCont.AddParticle() 
[/indent][/indent]) 
 
on Release pCont do 
( 
)

This worked for me.
:thumbsup:


#4

First of all, thanks to both of you for your help. However, I have gotten neither to really work so far and I’m having trouble figuring out how to integrate them. Hope i’m not too bold in asking if you guys wouldn’t mind posting some sort of max scene file with the scripts integrated so I know how you got your respective scripts to work? Thanks alot in advance!


#5

The script I gave you was just the basic framework for a particle based event. To actually use it, I have taken it further and built a whole system around it. The script itself was modified slightly too (ie it now does as many lights as I want it too). At the moment, it is generating explosion style lights as particles strike some objects in the scene. Should be easily adaptable for your ship. To tweak the scene, I just change the pflow, delete all the generated lights from the scene (in case the next go round has less explosions), then hit update on the cache. Everything else is done for me. Life is good :wink:

http://ericburnett.homeip.net/explosions.max
http://ericburnett.homeip.net/explosions.mov


#6

Eric, in the help files it is stated that:

<LI>

<LI>You cannot excessively create objects during the evaluation of Particle Flow scripts (for example inside the on Proceed method).

<LI>

You cannot delete objects from inside a Particle Flow script. Note: There are techniques allowing the creation of a limited number of new objects as shown in the Fragmentation script example.

<LI>
And in a thread on the discreet board Oleg and Bobo discussed the creation of objects in Script Operators. Judging from there object creation should only be done in Init Handlers and preferably outside PFlow or in advance. The thread is called “Non-geometry objects as particles in pflow”. I don’t know why but I guess it is because the Script Handlers are called often. What do you think?

Boa


#7

Which may explain why the omni lights, though ~50% successfully created, tend to either light up at the wrong time, or delete themselves, or, again, not create. Hmmm…


#8

I can see why not to excessively create objects, but the script I made will only create a rigidly controlled amount…IE not excessive. And yeah, the proceed method is called very many times, so if it always created objects, the scene would run out of control very quickly. But I do not think there would be a problem doing it this way…every time one is recreated, the old one is deleted. You can NEVER have more objects than particles, and I ended up with way less.

I would be very interested in both where that is said in the manual and a link to that discussion. I am self taught scripting and pflow, so I really have no idea the best way for things to be done. I just go at it until it works :shrug:

Cryptite: Not sure what you mean. They delete themselves every time they are recreated, yes, but I just used a cache operator so that it would only happen as you want it too.

The lighting up at the wrong time would probably be from a previous creation affecting the new run. As I said before, I always delete all the lights before re-caching. And it also might look odd because of the way the scene is set up…some lights you cannot see are causing other objects to light up as well as the ones they are closest to. They also spawn where the particle was two frames ago, so that might throw you off.

About them not creating, there are three possible reasons. 1, they are hitting the objects but not the proxies, because I did not set up the scene very carefully. 2. Two should be created in the same frame. I am not sure if that works, will have to look into it. 3. The collision is not found. Might need to increase the sub-frame sampling.

And if you find that this script is flakey or something, don’t use it. I am still fairly new to this, so I am definitely not the best person to do things. I just try to help people with problems I do not know the answer too, figuring it out and teaching myself in the process.

Anyways, I look forward to hearing from both of you. I just did this for fun, so feel free to drop it and find a better solution. If you do find one though, let me know :wink:


#9

I’m afraid that I don’t have a website to be able to send you any scenes, sorry :(.


#10

Eric, you’ll find the discussion on the discreet webboard (you’ll have to register):

http://support.discreet.com/webboard/wbpx.dll/~3dsmax

Search for a thread called “Non-geometry objects as particles in pflow”.

And for the help files: Look under MAXScript Extensions/MAXScript Extensions for Particle Flow/Limitations on using MAXScript with Particle Flow.

I’m sure no one wants to criticize you. It’s just discussing best/possible ways to achieve the intended result. I’m new to scripting myself and probably wouldn’t have “dared” to script the creation in the Proceed handler. It would be interesting to hear Oleg’s (the creator of PFlow) and Bobo’s opinion to your script.

Boa


#11

I am not sure I understand the maxscript refrence. It just makes a statement with no backup, and I seem to have proved it wrong. Anyways, I will be reading that discussion as soon as I have a password. Thanks!


#12

Wahooney, you can email that scene to me instead… cryptite@hotmail.com is my address.

Flipped, don’t get me wrong, I appreciate the help. The script seems to be working for the most part. As boa said, i’d love to bring amckay or bobo, or oleg in here to see what they think about your script…


#13

Don’t worry (either of you), I’m not offended or anything. It takes a lot to offend me :thumbsup:.

I posted it on the discreet thread, so we will see what they say (I am Eric_Burnett there if you visit that forum too). Both Bobo and Oleg are there, so I should get a good response one way or another. Its always good to hear what they say, even if they are telling you you are wrong ;).

Now, as a mind problem, I have to figure another way to do something like that without creating lights within an on proceed method. They could be created at the start, but you would need to create as many lights as particles…kind of wasteful in my mind. Anyone have any thoughts?


#14

Maybe it is enough to create say 5 lights because not all shots hit a the same time. Creating these in advance, setting their positon and the other properties in a proceed handler plus cycling through them.

Boa


#15

Could you have it perform like “recyclable” lights? Like boa said, have it create 5 lights but instead, take those 5 and have the script move them to where the new particles die and then just redo their intensity, rinse and repeat if you catch what i’m saying. Sounds a bit difficult, if not near impossible…


#16

Should be possible, but that means that there would be an upper limit of lights at any given frame. I think it would just require two global arrays, a 1d one with the objects, and a 2d one with the active and inactive status at any given frame. It would just have to check each light at the given frame and the next 10 or so, and if they were free, it could be used.

I think I could do it, but I will have to sort out global variables first (they don’t seem to work quite right for me), and learn better how the different methods work (on proceed, init, etc). I might try that, depending on the response from the discreet forum.


#17

Before I get started on that, just wanted to give you an update. I can see what you mean about flakey script. I did a test sequence, and it 1) only creates 1 particle per frame max, and 2) had trouble figuring out where to put that one. I have redone the script, so it should work better now. It is http://ericburnett.homeip.net/explosions02.max if you want the scene again.



on Proceed pCont do 
(   
  if sliderTime.frame != 0.0 do (

  for  i=1 to pCont.numParticles() do
  (
    pcont.particleIndex = i
    str = "ExpLight" + (pcont.getParticleID(i) as string)
    try (
      x = execute( ("$" + str) )
      delete x
    )
    catch (
      print "no previous ExpLight found"
    )
    x = omnilight name:str multiplier:0.0 pos: 
      (pCont.getParticlePosition(i)) rgb:
      (color 255 255 255) excludeList: #($Sphere01, $Target_Proxies)
    y = currentTime.frame
    animate on (
      at time (y-1) x.multiplier = 0.0
      at time y x.multiplier = 0.6
      at time (y+10) x.multiplier = 0.0
    )
  
    z = "light created at "
    z += (currentTime.frame as string)
    z += "From particle "
    z += (pCont.getParticleID(i) as string)
    print z
    
    )
  )
)


And thanks for all the feedback guys! Makes everything much easier (and more fun!)


#18

Eric, I couldn’t download your second scene. I’ll try it later again, maybe your server will be up again.

I see that you haven’t got an answer on the discreet board. To get the attention of Oleg or Bobo it could be better to start a new thread.

Boa


#19

Sorry, I am switching servers now, so I accidentally screwed up the port forwarding. Thats the trouble when you run your own stuff…no one else to blame. It should work now.

And I may have to make another thread, but I think I will give it a bit longer. Bobo had said he would return with an example to someone else in the thread, so hopefully he will :shrug:. If he does not, then I will create a new one, yeah.


#20

Hi Eric,
the lights are not positioned properly in your script. Therefore they are all created at 0,0,0. Use “pCont.ParticlePosition” instead of “pCont.getParticlePosition(i)”. Besides that your script functions well.

There is one thing I saw in the listener that I can’t explain: Most lights (not all) are created twice. Do you know why?

Boa