Posts Tagged ‘inventory’

Creating a Slideshow Viewer, Part 2: Reading the Inventory

Sunday, October 16th, 2011

This is the second installment in my small series about creating a slideshow viewer (based on my new texture vendors). In this one, I’ll be showing you how to add images to the viewer.

In my original texture vendors, the prim which displayed the images was responsible for creating a list of the images available, and then of moving through the images when it received a message from one of the buttons.

In the new version, all of this is handled by the Frame prim, which looks for touch events and determines which button was ‘clicked’, and which then updates the texture on the Display prim. This is made possible by another relatively new script function, llSetLinkTexture(), which allows the texture of a prim to be assigned by another (linked) prim.

There are two elements to handling the images. The simplest is assigning a specified texture to the Display prim. However, in order to do this the Frame needs to have the textures stored in it, and needs to be able to make a list of the available textures.

Let’s start by creating this list. First, edit the Frame prim. As the Frame prim and the Display prim are linked, click the ‘Edit linked’ check box on the build dialog, then click on the Frame. Now switch to the contents tab, and drop your images (textures or snapshots) into it.

Now we need to write a script that will create a list of those images. If you have been following along, you will already have a script in the Frame prim, to handle the buttons. Update this to the following code:

    // A list to hold the image keys
    list images = [];
        // Count the number of textures
        integer count = llGetInventoryNumber(INVENTORY_TEXTURE);
        integer i;
        string name;
        images = [];
        // Look through all the texture
        for (i = 0; i < count; i++)
            name = llGetInventoryName(INVENTORY_TEXTURE, i);
            // Get the key, and add it it to the list
            images += [llGetInventoryKey(name)];
        // If we have at least one image, display the first one.
        if (llGetListLength(images) > 0)
            llSetLinkTexture(LINK_ROOT, llList2Key(images, 0), 0);
    float btn_Cols = 8.0;
    float btn_Rows = 8.0;
    integer btn_Id(vector pos)
        integer button = (btn_Row(pos) * (integer)btn_Cols) + btn_Col(pos);
        return button;
    integer btn_Row(vector pos)
        // Flip the y-axis, so that it runs from top to bottom.
        float y = 1.0 - pos.y;
        integer row = (llFloor((y * 10) * btn_Rows) / 10);
        return row;
    integer btn_Col(vector pos)
        integer col = (llFloor((pos.x * 10) * btn_Cols) / 10);
        return col;

        touch_end(integer count)
            vector mousePos = llDetectedTouchST(0);
            integer id = btn_Id(mousePos);
            llOwnerSay("Button " + (string)id + " pressed");

This finds all the texture items in the prim’s contents, and stores their names in the Image list. Once that is done, it gets the first image from the list, and assigns it on the Display prim.

The llSetLinkTexture() function is used to display the image. The allows us to set the texture of another prim, in this case the display prim, by specifying the prim number, the UUID of the texture (which we read from our list), and the face, which is here assumed to be face 0.

One thing to be careful about here is that it assumes that the Display prim is prim 0, that is, the root prim. If you don’t already know how to do this, the trick is that when you select the prims that you want to link, the root prim should be the last prim that you select before linking. In this case there are only two prims, so select the Frame prim first, then Shift+Click the Display prim to add it to the selection. Finally link the prims (the option to link is on the Build menu in Viewer 2, and in most third-party viewers).

Tutorial 1: Applying Textures

Friday, March 20th, 2009

As textures are Templar Creation’s main product, it seems appropriate to have some tutorials about using them. This is the first in a series which will start with the basics, and eventually progress on to advanced uses of textures.

To begin at the beginning…

I’ll assume that you already know the basics of constructing prims, and at least know how to open the Build dialog and get to the various tabs.

To start this tutorial, open the Build dialog, and rez a simple cube. We’ll use this to go through the different ways of applying textures.

Once you’ve rezzed the cube, close the Build dialog (you’ll find out why in a minute).

Let’s start by actually locating some textures in your inventory. The Inventory is divided into two basic sections – “My Inventory”, and “Library”. The “My Inventory” section holds everything that you have bought or uploaded yourself. The “Library” section holds things which are supplied for free by Linden Lab.

Both sections include “Textures” folders, which are, not surprisingly, the first place to go looking for textures. They aren’t the only places, though. It’s perfectly possible to move textures into other folders. More importantly, and perhaps a little less obviously, snapshots can also be used as textures.

It’s worth noting that when you upload an image from your computer (from the File | Upload Image menu option), it will always go into your Textures folder. Of course, you can move it from there once it has been uploaded.

So, let’s use one of these textures. It’s actually possible to apply a texture without opening the Build dialog. Simply drag the texture from your inventory onto the cube — this will immediately apply the texture on to whichever side of the cube you dropped it.

Obviously, this will only work on prims that belong to you (and for which you have modify permissions).

That’s the quickest and simplest way of applying a texture, and is sometimes quite useful. Usually, though, you will want to do more than just apply a texture to a single side of a prim, so let’s move on to the more normal ways of applying textures.

Right-click the cube, and select “Edit”, to open the Build dialog.

Select the “Textures” tab (it’s the last one).

There are multiple options and controls on this tab, but for the moment we will simply concentrate on the texture image itself. If you’ve followed the previous step for applying a texture, you’ll notice that it has “Multiple” printed over it, to tell you that this prim has more than one texture applied.

Click the image, and a texture selection dialog will appear:

Texture Selection Dialog

Texture Selection Dialog

On the left is a picture of the current texture, along with some buttons. Click the “Blank” button — your cube will turn white. If you click the “Default” button, the cube will be textured with the familiar plywood texture.

There is also a “None” button. I’ve never seen this enabled, and have no idea what it does…

On the right you should see a list of the textures in your inventory. This will go through your entire inventory, finding every copiable texture (the texture has to have copy permissions for you to be able to use it). If you have a lot of textures and/or SL is being laggy, this can take a while to fill.

If you click on one of the listed textures, it will be displayed in the texture image box. If the ‘Apply Immediately’ box is ticked (which it is by default), the texture will also be applied to the prim — this is a useful way of quickly checking what different textures will look like. Click “Select” to make the texture permanent (well, until you change it again!), or “Cancel” to revert to the original texture.

Finally, for yet another way to select a texture, you can click the pipette button that you will see below and to the right of the “Default” and “Blank” buttons. You can then click on any other visible prim, and the texture will be picked up from that prim (provided that is it copiable).

For now, however, click the “Default” button, so that you end up with a plywood cube.

Another way of applying a texture is to drag it from your inventory and drop it on to the texture image in the Build dialog. This tends to be a bit more tedious and awkward than using the Texture Selection dialog.

Back on the Build dialog, to the right of the texture image is a “Color” image. This will be white at the moment. Click on it, and a color selection dialog will appear:

Color Selection Dialog

Color Selection Dialog

If you have ever used paint programs, this will probably seem quite familiar, and I won’t go into details about it.

Select a color.

Your cube will still have the plywood texture, but will now be colored. You can do this with any texture, so it is quite possible to generate different-looking textures from one basic texture.

For example, here is a basic, greyscale “drape” texture, with different colors applied:

Colored Drape Examples

Colored Drape Examples

Now, suppose you want to have different textures on different sides of the cube. Of course, you can use the drag method that we saw above, but this is rather tedious and inflexible. A better way is to use the “Select texture” radio button which you will see on the top half of the Build dialog.

Select Texture Button

Select Texture Button

Click this button, then click on one side of the cube. A symbol will appear on the cube, showing you which part is currently selected:

Selected Face Example

Selected Face Example

If you now click the texture image on the Build dialog and select a new texture, it will only be applied to that side.

You can select multiple sides at once. Hold down the Shift key on your keyboard as you click on the sides you want to select.

In theory, you can also do this with multiple prims, and apply a texture to the sides of several prims at once. In practice, I have found that SL does not handle this very well at all, and it is probably best to stick to one prim at a time.

There is one more way of applying a texture, which is by using a script. This is something you might do if you want the texture to change in reaction to some event, but is well beyond the scope of this tutorial. I shall probably cover this in a later, more advanced tutorial.

That was quite a hefty first tutorial. I’ll be back later with the next one, when I’ll talk about the Repeat, Rotate, and Offset parameters.