Alice Community  

Go Back   Alice Community > Alice 2 > How do I...?

Reply
 
Thread Tools Display Modes
Old
arty-fishL
Senior Member
 
arty-fishL's Avatar
 
Status: Offline
Posts: 1,878
Join Date: Mar 2008
Location: In the corner of your eye
Default 05-18-2011, 03:32 AM

Alice runs off of Java, the scripting in Alice is Jython, not Python. To do what you say you would need to directly access the Java components of Alice via Jython scripting. This would require research and knowledge of both the Alice jar and Java 3D.


█░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░█
█░░▓░░░░░░░▓░░░░░░░░░░░▓▓░░▓░░░░░░▓░░░▓░░░░█
█░▓░▓░▓▓▓░▓▓▓░▓░▓░░░░░░▓▒▒░░▒░░▓▓░▓▓▓░▓▒░░░█
█░▓▓▓▒▓▒▒▒░▓▒▒▓▓▓▒▓▓▓░▓▓▓░░▓░░░▓▒▒▓▒▓▒▓▒░░░█
█░▓▒▓▒▓▒░░░▓▓░░▒▓▒░▒▒▒░▓▒▒░▓▓░▓▓▒░▓▒▓▒▓▒░░░█
█░▓▒▓▒░▒░░░░▒▒▓▓▓▒░░░░▓▓▒░░░▒▒░▒▒░░▒░▒▓▓▓░░█
█░░▒░▒░░░░░░░░░▒▒▒░░░░░▒▒░░░░░░░░░░░░░░▒▒▒░█
█░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░█

I have mostly moved on from Alice, but may still respond to messages if important [¬º-°]¬
   
Reply With Quote
Old
chip
Member
 
chip's Avatar
 
Status: Offline
Posts: 24
Join Date: May 2011
Location: New Hampshire
Default 05-19-2011, 09:03 PM

Quote:
Originally Posted by arty-fishL View Post
Alice runs off of Java, the scripting in Alice is Jython, not Python. To do what you say you would need to directly access the Java components of Alice via Jython scripting. This would require research and knowledge of both the Alice jar and Java 3D.
Jython & Python are near-genetic-twins, so scripting in either is very similar. In fact, the Jython site refers to the Python docs quite often for some aspects of its use. Scripts are even saved in .a2c files as .py files (standard Python extension).

Digging into the Java jar and 3D API isn't really needed for what I'm doing, since Alice can do all I need using either standard methods or those plus a little "scripting glue" to do stuff that isn't exposed in the standard methods. I'm not looking to create or edit 3D models within Alice, just move them in a specific manner.

So far things are going well. I've found some aspects of Alice scripting a bit annoying (as have others, from what I can glean off the web), because the actual Alice API is not well-documented. It has some idiosyncrasies that make doing simple things (like reading a List) a little harder than they should be, I think, but as always, there are workarounds, hacks, and kludges, and I'm not too proud to use them all

I made a demo of my work so far if anyone's interested:

MPM_v1.zip

RTFM ALERT!! -- Read the README included in the zip archive, please, since it has specific instructions for how to use the files in the archive. Thanks.

This has been used only in Alice 2.0, so any upward mobility is not assured. As usual, use at your own risk, no warranties, yada, yada, yada.


What the demo does
On opening, the world shows a model of a fence-like object seen from the top. This is what I'd like to use as a collision-detector around the lake in my game world. But because the usual way of importing an object into Alice (at least in my experience) results in all sub-parts having the same center/pivot as the Base object, that won't work for collision, because my routines use object pivots to detect proximity.



So the task was to create a way to use the normal import process, but then also have the sub-objects retain their own geometric centers, or pivots as they are called in Alice. This required a little work in my 3D app (Blender) as well as in Alice. The first step was to make the fence-like "collision hull" in Blender, and then make each face of the "fence" a separate object (just a simple plane). The center of each of these objects was then set to the geometry of the object. If this model is then exported to ASE or another format for conversion to ASE, the result is to have all the object centers moved to the world origin. I think this is a limitation of the ASE format, since I see nothing in the ASE file that could be edited to set an object center.

In Blender, I wrote a small script to write a text file to disk that records the names of the objects and the 3D coordinates for each. The coordinates data should be relative to the world origin for best results, so I made sure to build the model accordingly. The written data then had to be "massaged" in a capable text editor (ConTEXT is good) to make it acceptable to Alice (only certain characters for names, for instance), and also to make each entry in the file a "clean" string (no brackets, parentheses, commas, or whatever) that is space-delimited. The format here is important because Alice has to read this text file to make its own internal List.

After writing this text file, which describes the model as it should appear in Alice, all the objects in the 3D model are then moved to the world origin. In Blender this is a single-command operation, not sure about other 3D apps. The model (all parts and pieces at once) is then exported to ASE format.

Since the resulting ASE file may not conform to Alice's restrictions, some editing may be needed. In my case, a lot of periods (".") that Blender generates when naming objects internally had to be replaced by underscores ("_"). Easy with a good text editor.

Importing the ASE into Alice results in a model where all the sub-parts are sitting at world center. But the coordinates for the pieces have been recorded, so my script takes that data and uses it to re-locate all the pieces to their proper places. The process is pretty simple: Open the text file with all the model name & coordinate data, copy it, then just follow the prompts in Alice after starting the "game". Paste in the data when asked for it, and later enter a model scale and a parts scale. As the demo "game" proceeds, all the individual pieces of the model are placed properly, and they also have their pivots where they are needed for collision detection.

Beyond my use, this script could also be very useful for importing other world assets designed and constructed outside of Alice -- imagine modeling an entire town in Blender or another 3D app, them importing it into Alice as an single model, and using the script to place each item of the town in its proper place... automagically.
   
Reply With Quote
Old
arty-fishL
Senior Member
 
arty-fishL's Avatar
 
Status: Offline
Posts: 1,878
Join Date: Mar 2008
Location: In the corner of your eye
Arrow 05-20-2011, 03:31 AM

Quote:
Originally Posted by chip View Post
Jython & Python are near-genetic-twins, so scripting in either is very similar.
I am fully aware of that. Sorry, I was expressing that its Jython and therefore can run Java commands, which is what I thought you needed.

Quote:
Originally Posted by chip View Post
Digging into the Java jar and 3D API isn't really needed for what I'm doing
Ah, I misunderstood what you wanted to do.

Quote:
Originally Posted by chip View Post
So far things are going well. I've found some aspects of Alice scripting a bit annoying (as have others, from what I can glean off the web), because the actual Alice API is not well-documented. It has some idiosyncrasies that make doing simple things (like reading a List) a little harder than they should be, I think, but as always, there are workarounds, hacks, and kludges, and I'm not too proud to use them all
This is where I would be digging into Java and the Alice jar, and I do. Zone made a really good class for dealing with Alice lists, if you want it I can dig it up for you (it uses some functions from the Alice jar).

Quote:
Originally Posted by chip View Post
I made a demo of my work so far if anyone's interested:

Attachment 4856
I will look at it in a minute. How do you do those BB tags - the ATTACH ones?

Quote:
Originally Posted by chip View Post
What the demo does
On opening, the world show ... eded for collision detection.
I'm not too sure what you are talking about, there was far too much writing, but I will check out the demo.

Quote:
Originally Posted by chip View Post
Beyond my use, this script could also be very useful for importing other world assets designed and constructed outside of Alice -- imagine modeling an entire town in Blender or another 3D app, them importing it into Alice as an single model, and using the script to place each item of the town in its proper place... automagically.
This sounds great


█░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░█
█░░▓░░░░░░░▓░░░░░░░░░░░▓▓░░▓░░░░░░▓░░░▓░░░░█
█░▓░▓░▓▓▓░▓▓▓░▓░▓░░░░░░▓▒▒░░▒░░▓▓░▓▓▓░▓▒░░░█
█░▓▓▓▒▓▒▒▒░▓▒▒▓▓▓▒▓▓▓░▓▓▓░░▓░░░▓▒▒▓▒▓▒▓▒░░░█
█░▓▒▓▒▓▒░░░▓▓░░▒▓▒░▒▒▒░▓▒▒░▓▓░▓▓▒░▓▒▓▒▓▒░░░█
█░▓▒▓▒░▒░░░░▒▒▓▓▓▒░░░░▓▓▒░░░▒▒░▒▒░░▒░▒▓▓▓░░█
█░░▒░▒░░░░░░░░░▒▒▒░░░░░▒▒░░░░░░░░░░░░░░▒▒▒░█
█░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░█

I have mostly moved on from Alice, but may still respond to messages if important [¬º-°]¬
   
Reply With Quote
Old
chip
Member
 
chip's Avatar
 
Status: Offline
Posts: 24
Join Date: May 2011
Location: New Hampshire
Default 05-23-2011, 05:03 PM

Quote:
Originally Posted by arty-fishL View Post
I will look at it in a minute. How do you do those BB tags - the ATTACH ones?
Use the Attachment Manager, which you can open using the paper clip icon in the reply dialog menu.
Quote:
Originally Posted by arty-fishL View Post
... there was far too much writing...
Adverbs and gerunds and nouns, oh my!

I do tend to run off at the mouth, or at the keyboard, depending on how you look at it.

Anyway, just a quick update -- I've been digging into Alice's source to find hooks into much faster routines for doing the stuff I want to do, and now have a version of ModelPartsMover that works, oh, about 1000 x faster (give or take a few hundred). In fact, the parts are moved into place effectively instantaneously. I've also tested the idea of using the model sub-parts as collision sensors and they work fine. The big bottleneck is of course the collision detection, which if done using Alice's normal routines is far too slow to be effective. So I'm looking for the Java classes that can help me speed this up as well. Looks promising so far.
   
Reply With Quote
Old
arty-fishL
Senior Member
 
arty-fishL's Avatar
 
Status: Offline
Posts: 1,878
Join Date: Mar 2008
Location: In the corner of your eye
Lightbulb 05-23-2011, 05:29 PM

Quote:
Originally Posted by chip View Post
Use the Attachment Manager, which you can open using the paper clip icon in the reply dialog menu.
That's what I'm confused about, how did you get tags, doesn't it just attach at the end?

______________________-

If you're looking at the source code; I do not suggest looking at the source code they provide, I suggest using a decompiler (like I do), such as JD-GUI.

There is also a python module called alice, this utilises many built in Alice functions if you want.

I managed to uncover lots of hidden features they were going to include in Alice, but didn't (most of them don't work or aren't unfinished). I made a world with all of these in it and posted it somewhere, I can't find it.
Anyhow, the pictures I have attached show you a function and two events that were to be included, but they scrapped. Unfortunately these don't function properly . Still, you (who has been extremely fast into the complex Alice stuff) might find some use in them.
Attached Images
File Type: png touchingFunction.png (14.2 KB, 16 views)
File Type: png collisionEvents.png (13.3 KB, 16 views)


█░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░█
█░░▓░░░░░░░▓░░░░░░░░░░░▓▓░░▓░░░░░░▓░░░▓░░░░█
█░▓░▓░▓▓▓░▓▓▓░▓░▓░░░░░░▓▒▒░░▒░░▓▓░▓▓▓░▓▒░░░█
█░▓▓▓▒▓▒▒▒░▓▒▒▓▓▓▒▓▓▓░▓▓▓░░▓░░░▓▒▒▓▒▓▒▓▒░░░█
█░▓▒▓▒▓▒░░░▓▓░░▒▓▒░▒▒▒░▓▒▒░▓▓░▓▓▒░▓▒▓▒▓▒░░░█
█░▓▒▓▒░▒░░░░▒▒▓▓▓▒░░░░▓▓▒░░░▒▒░▒▒░░▒░▒▓▓▓░░█
█░░▒░▒░░░░░░░░░▒▒▒░░░░░▒▒░░░░░░░░░░░░░░▒▒▒░█
█░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░█

I have mostly moved on from Alice, but may still respond to messages if important [¬º-°]¬
   
Reply With Quote
Old
chip
Member
 
chip's Avatar
 
Status: Offline
Posts: 24
Join Date: May 2011
Location: New Hampshire
Default 05-23-2011, 05:53 PM

Quote:
Originally Posted by arty-fishL View Post
That's what I'm confused about, how did you get tags, doesn't it just attach at the end?
When you first open it, it lets you attach your files. To place them in the body of you post, click it again and choose from the attachments list it opens up.

I'm not finding the Alice scripting all that complex, just unfamiliar. Since the API is rather opaque, it's much a matter of digging around to find out what's what. Besides investigating the source, I've also looked into the formatting of the .a2w files (the .xml stuff) and learned a lot about how to manipulate the world data outside of the rather clumsy Alice GUI -- it's a very good UI for beginners, not so much for experienced scripters.
   
Reply With Quote
Old
chip
Member
 
chip's Avatar
 
Status: Offline
Posts: 24
Join Date: May 2011
Location: New Hampshire
Default 05-24-2011, 07:53 PM

I have to take back my statement that I'm not finding Alice's source code to be complex -- I hadn't yet tried to dig into event handling. That's really arcane, and it's created a roadblock for me.

Here's the problem: I have a scripted loop that iterates though the 118 sensors I use for collision detection. It's very fast, and is intended to replace the much slower "loop for infinity" approach often done using Alice's tiled functions.

The problem is that my scripted function locks out all other functions while running, including event handling, so my character can't move since the keypress events that cause motion are not responded to.

In the tiled functions the "do together" tile takes care of this, but while a scripted function is running, it doesn't seem to recognize "do together" even if that's where the script call is made.

Has anyone looked into using scripting to set up a concurrently-running thread for such a loop routine. I've tried a number of approaches but they either do not work, or still lock out my character's movement.

Also, what's the best way to implement a class extension in Alice? In UnrealScript I used an IDE that compiled the new packages at run time, but Alice doesn't seem to have that capability. Can it be done with NetBeans? Any tips on this would be most welcome.
   
Reply With Quote
Old
arty-fishL
Senior Member
 
arty-fishL's Avatar
 
Status: Offline
Posts: 1,878
Join Date: Mar 2008
Location: In the corner of your eye
Arrow 05-26-2011, 08:50 AM

Threading ...

without parameters:
Code:
from threading import Thread

class myThread(Thread):
    def run(self):
        print("hi")
        
myThread().start()
with parameters:
Code:
from threading import Thread

class myThread(Thread):
    def __init__(self, var, var2):
        self.var = var
        self.var2 = var2
        Thread.__init__(self)
    def run(self):
        print( self.var, self.var2 )

var, var2 = "hi", "hiya"
myThread( var, var2 ).start()
What do you mean by a class extension ? Are you talking about implementing/extending a class (ie. an abstract class as I done above)?


█░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░█
█░░▓░░░░░░░▓░░░░░░░░░░░▓▓░░▓░░░░░░▓░░░▓░░░░█
█░▓░▓░▓▓▓░▓▓▓░▓░▓░░░░░░▓▒▒░░▒░░▓▓░▓▓▓░▓▒░░░█
█░▓▓▓▒▓▒▒▒░▓▒▒▓▓▓▒▓▓▓░▓▓▓░░▓░░░▓▒▒▓▒▓▒▓▒░░░█
█░▓▒▓▒▓▒░░░▓▓░░▒▓▒░▒▒▒░▓▒▒░▓▓░▓▓▒░▓▒▓▒▓▒░░░█
█░▓▒▓▒░▒░░░░▒▒▓▓▓▒░░░░▓▓▒░░░▒▒░▒▒░░▒░▒▓▓▓░░█
█░░▒░▒░░░░░░░░░▒▒▒░░░░░▒▒░░░░░░░░░░░░░░▒▒▒░█
█░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░█

I have mostly moved on from Alice, but may still respond to messages if important [¬º-°]¬
   
Reply With Quote
Old
chip
Member
 
chip's Avatar
 
Status: Offline
Posts: 24
Join Date: May 2011
Location: New Hampshire
Default 05-26-2011, 03:15 PM

Thanks for code suggestion, arty. I set it up as you show and unfortunately I get the same results as the method I had tried. It seems that an extensive loop in the newly created thread will still cause timing glitches in the operation of the main thread. I'm not that smart about thread operation details, so it could be I'm trying to do something that defeats the purpose of a thread (concurrent operation).

Here's the script:
Code:
import string
import math
from edu.cmu.cs.stage3.alice.core import SpatialRelation
from threading import Thread

## this puts the sensors in place -- they are sub-parts of an imported model
## for testing the pivots are made visible
def moveModelSubParts(ModelData, BaseModelName, ModelScale, PartScale, ShowPivots):
	if ModelData == '':
		print "No Model Data available."
		return()
	try:
		BaseModelObject = getattr(Arboria, BaseModelName)
		PointTargtObj = getattr(Arboria, 'LakeCenter')
	except:
		print ("getattr failed")
		return()
	i = 0
	ModelDataList = string.split(ModelData)
	while i < len(ModelDataList):
		try:
			float(ModelDataList[i])
		except:
			CurrentObject = getattr(BaseModelObject, ModelDataList[i])
			float_1 =  float(ModelDataList[i + 1]) * ModelScale
			float_2 =  float(ModelDataList[i + 2]) * ModelScale
			float_3 =  float(ModelDataList[i + 3]) * ModelScale
			newPosition = Vector3(float_1, float_3, float_2)
			CurrentObject.setPositionRightNow(newPosition, BaseModelObject)
			CurrentObject.visualScale = javax.vecmath.Matrix3d(PartScale,0,0, 0,PartScale,0, 0,0,PartScale)
			CurrentObject.pointAtRightNow(PointTargtObj, javax.vecmath.Vector3d(0,0,0), javax.vecmath.Vector3d(0,1,0), CurrentObject, true)
			if ShowPivots == true:
				CurrentObject.isPivotShowing = (Boolean.TRUE)
			Arboria.shorelines.insertItemValueAtEnd(CurrentObject)
		i += 4


## put collision detection loop in separate thread to prevent hitches in navigation
def doLakeCollision():
    detectThread().start()

## define the threaded collision detection operation
class detectThread(Thread):
    def __init__(self):
        Thread.__init__(self)
    def run(self):
		sensorIndex = 0
		sensorListLength = Arboria.shorelines.size()
		sensorIndexMax = sensorIndex
		try:
			while sensorIndexMax < sensorListLength * 3:
				sensor = Arboria.shorelines.itemValueAtIndex(sensorIndex)
				proximity = sensor.getDistanceBetween( sensor, Arboria.Faella )
				if proximity < 2:
					ProxR = Arboria.Faella.getSpatialRelationDistance( SpatialRelation.RIGHT_OF, sensor, sensor )
					ProxL = Arboria.Faella.getSpatialRelationDistance( SpatialRelation.LEFT_OF, sensor, sensor )
					if (ProxR < 1) or (ProxL < 1):
						if Arboria.Faella.isBehind(sensor) == true:
							ProxBehind = Arboria.Faella.getSpatialRelationDistance( SpatialRelation.BEHIND, sensor, sensor )
							if (ProxBehind < 0.25) and (Arboria.Faella.isWalking == true):
								Arboria.Faella.TurnToLake *= -1
								break
						if Arboria.Faella.isInFrontOf(sensor) == true:
							ProxInFront = Arboria.Faella.getSpatialRelationDistance( SpatialRelation.IN_FRONT_OF, sensor, sensor )
							if (ProxInFront < 1) and (Arboria.Faella.isWalking == true):
								Arboria.Faella.doLakeExit *= -1
								break
				sensorIndex += 1
				if sensorIndex > 117:
					sensorIndex = 0
				sensorIndexMax += 1
		except:
			return()
To get around this I made it so the the thread doesn't loop endlessly -- it's set to go through the set of sensors just 3 times (which it does very fast) every second or so as long as the character is walking and is within a certain distance of the lake. Stopping and starting to walk again restarts the thread. This works better because the loop is short, and 3 times through the sensor list seems to be enough to catch the majority of "collisions," i.e. when the character is close enough to the sensor to trigger the desired response.

Quote:
Originally Posted by arty-fishL View Post
What do you mean by a class extension ? Are you talking about implementing/extending a class (ie. an abstract class as I done above)?
Yes, I mean extending/implementing a class, but not through scripting, but rather in Java itself, compiling the class into bytecode for faster operation and adding the new class to the Alice jar so it can be used like any other class.

UnrealScript uses this approach for its mod scripting -- for UT2003/3004 you basically had to extend a class to do just about anything useful. But they did make that a fairly easy thing to accomplish. I used the WOTgreal IDE to do all my UnrealScript work.

I've tried this in Alice with a test case but have run into a Java version incompatibility roadblock, which I'm currently trying to troubleshoot. Ostensibly I could build my new class to an earlier version spec using NetBeans IDE, but I have to figure out how.

Last edited by chip; 05-26-2011 at 03:17 PM.
   
Reply With Quote
Old
arty-fishL
Senior Member
 
arty-fishL's Avatar
 
Status: Offline
Posts: 1,878
Join Date: Mar 2008
Location: In the corner of your eye
Arrow 05-26-2011, 04:25 PM

Im not sure why there would be any lag if you are using a thread. Never have a while outside a thread, that's all I can think of. You could try inserting pauses with time.sleep in the thread, but I dont think that's what you are looking for.

For the extra Java class can you not just translate it all into Jython. Its not really that big a challenge and there isn't really a noticeable compile difference.


█░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░█
█░░▓░░░░░░░▓░░░░░░░░░░░▓▓░░▓░░░░░░▓░░░▓░░░░█
█░▓░▓░▓▓▓░▓▓▓░▓░▓░░░░░░▓▒▒░░▒░░▓▓░▓▓▓░▓▒░░░█
█░▓▓▓▒▓▒▒▒░▓▒▒▓▓▓▒▓▓▓░▓▓▓░░▓░░░▓▒▒▓▒▓▒▓▒░░░█
█░▓▒▓▒▓▒░░░▓▓░░▒▓▒░▒▒▒░▓▒▒░▓▓░▓▓▒░▓▒▓▒▓▒░░░█
█░▓▒▓▒░▒░░░░▒▒▓▓▓▒░░░░▓▓▒░░░▒▒░▒▒░░▒░▒▓▓▓░░█
█░░▒░▒░░░░░░░░░▒▒▒░░░░░▒▒░░░░░░░░░░░░░░▒▒▒░█
█░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░█

I have mostly moved on from Alice, but may still respond to messages if important [¬º-°]¬
   
Reply With Quote
Reply


Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off

Forum Jump



Copyright ©2024, Carnegie Mellon University
Alice 2.x © 1999-2012, Alice 3.x © 2008-2012, Carnegie Mellon University. All rights reserved.