Open Source Platform
for interconnected virtual worlds

Content Scripting Python Change Material

From RexWiki

Preface

This page shows how to create a script which can be used for changing materials of 3D models.

To use this script:

  1. Make sure server is down
  2. Add the python class from below to "\rexserver\ScriptEngines\PythonScript\Samples" folder to a file named samplematerialchanger.py
  3. Open "\rexserver\ScriptEngines\PythonScript\Samples\__init__.py" file and add samplematerialchanger.py to the end of the list
  4. Start servers
  5. Create a new material and define mediaurl for it.
  6. Create a new prim and define a 3D model for it. This is the target primitive whose material we are going to change.
  7. Create a new prim and define a 3D model for it. Assign the material with the mediaurl to the 3D model.
  8. To the same prim add to Rex tab-> Data the correct parameters (see below Rex data for prim which changes material when touched). Remember to press the Set button below the Data to set it.
  9. To the same prim define "samplematerialchanger.MatChange" for it's Class Name.
  10. Stop editing the prim.
  11. Touch the prim you created last to change the material of the target prim's 3D model.

Note about the implementation: The reason why 3D models are used in both target prim and changer prim is that the mediaurl works better with them than regular box prims. The reason why the changer prim is assigned the material with the mediaurl is so that the material gets loaded when you enter the world. These tricks can be considered as workarounds to get the mediaurls working properly.


Rex data for prim which changes material when touched

The following text is a sample for Rex tab->Data for the prim which changes the material.

89652789-3fd0-43e6-ac7d-152f9fab7927
0
f0d4907e-0f64-4bcb-9eb3-8a7cd64ec84d

Parameter descriptions line by line are as follows:

  • Line 1: UUID of the target prim
  • Line 2: Material slot
  • Line 3: Material UUID

To get the target prim UUID you can go to View->Object List. Doubleclick your prim on the list or select it from the 3D view and then press the Copy UUID button in the Object List window.


Python script

The following python script should be added to a file named samplematerialchanger.py.

import rxactor
import rxavatar
import sys
import clr

asm = clr.LoadAssemblyByName('OpenSim.Region.ScriptEngine.Common')
Vector3 = asm.OpenSim.Region.ScriptEngine.Common.LSL_Types.Vector3

class MatChange(rxactor.Actor):
    def GetScriptClassName():
        return "samplematerialchanger.MatChange"

    def EventCreated(self):
        super(self.__class__,self).EventCreated()
        print "samplematerialchanger.MatChange"
        self.MyTexturePrim = None
        self.MyTextureSlot = 0
        self.MyTextureUUID = ""
        self.RefreshData()

    def EventTouch(self,vAvatar):
        self.RefreshData()
        if(self.MyTexturePrim != None):
            self.MyTexturePrim.RexSetMaterialUUID(self.MyTextureSlot,self.MyTextureUUID)
        else:
            print "Target prim for material change not found"


    def RefreshData(self):
        myfreedata = self.rexGetPrimFreeData()
        paramlistlines = myfreedata.splitlines()

        templocalid = self.GetPrimLocalIdFromUUID(paramlistlines[0])
        self.MyTexturePrim = self.MyWorld.GetActorByLocalID(str(templocalid))
        self.MyTextureSlot = int(paramlistlines[1])
        self.MyTextureUUID = str(paramlistlines[2])