Renaming Objects

Hello, I am making a portals like game. I am using a script found on this website. The script works pretty well but for different levels I have the game switch scenes which makes the name of the objects get .001 etc attached to the end. The script does not recognize it like this. So, is there a way to rename the objects using python or something before the scene loads? Because if I change them then the portals in the first level don’t work and so on and so forth .

No, you can’t rename objects.

It seems your script is too much restricted to the names of the objects.
It would be better to make it more generalize by referencing objects that have some properties. This allows a much better flexibility.

Example:

rather than:


obj = GameLogic.getCurrentScene().objects["OBObject"]

use this code:


def findObjectsByProperty(propName, value=None):
  objects = []
  for obj in GameLogic.getCurrentScene().objects:
    if propName in obj:
      if value == None or obj[propName] == value:
        objects.append(obj) 
  return objects
#...
 
objects = findObjectsByProperty("myProp") # finds all objects with property "myProp"
lessObjects = findObjectsByProperty("myProp",True) # finds all objects with property "myProp" containing the value True.

Keep in mind findObjectsByProperty returns a list of objects.
If you want to get just one of the objects do this:


obj = findObjectsByProperty("myProp") [0]

It produces an IndexError if no object is found.

I hope it helps

Hi, thanks for your response, um I kinda understand what you said. But I kinda don’t get where to put it or what to replace in this script


#
# Portals with Blender (2.49a)
# by Warwick Allison (warwickallison on BlenderArtists.org)
#
# Objects entering a portal are transported to a matched portal.
# Objects can be part-way through the transporting process, and
# retain various physical properties throughout the process.
#
# Portals are Z-plane meshes. They can be arbitrarily transformed.
# Connect 2 portals as sensors to this script, or more pairs each
# with 'otherportal' property.
#
# For each portal, there must also be a camera, portalname+"cam", which
# may have the property "videosize" (default 256).
#
# Each portal needs to have a unique image texture. Texture 0 (lowest) will
# be used for the portal (you can put layers on top). For more information,
# see other VideoTexture documentation.
#
# Copyright 2009 Warwick Allison
# You may use this file under the terms of the Creative Commons BY NC SA license.
# See http://creativecommons.org/licenses/by-nc-sa/3.0/ for a copy of the license.
#
# That is, you must give attribution, you may not use commercially, you may modify.

import VideoTexture
from Mathutils import *

cont = GameLogic.getCurrentController()
scene = GameLogic.getCurrentScene()
viewer = scene.active_camera
viewpos = viewer.position
r180 = RotationMatrix(180,3,'y')
unmir = ScaleMatrix(-1,3,Vector([1,0,0]))
for pss in [1,2,3]:
    n = 0
    for s in cont.sensors:
        portal = s.owner
        if 'otherportal' in portal:
            otherportal = scene.objects["OB"+portal['otherportal']]
        else:
            otherportal = cont.sensors[len(cont.sensors)-1-n].owner
        n+=1
        cam = scene.objects[portal.name+"cam"]
        m1=Matrix(*portal.orientation)
        m1.transpose()
        m1.invert()
        m2=Matrix(*otherportal.orientation)
        m2.transpose()
        
        if pss == 1:
            # Update video textures
            offset = Vector(0,0,0)
            if 'offset' in cam:
                offset = cam['offset']
            cam.position = Vector(otherportal.position) + \
                (Vector(viewpos) - Vector(portal.position))*m1*unmir*m2 + offset
            if 'video' in cam:
                cam['video'].refresh(False)
            else:
                matID=0
                video = VideoTexture.Texture(portal, matID)
                video.source = VideoTexture.ImageMirror(scene,cam,otherportal,matID)
                sz = 256
                if 'videosize' in cam:
                    sz = cam['videosize']
                video.source.capsize = [sz,sz]
                video.source.background = [0,0,0,255] # black
                cam['video'] = video
        elif pss == 2:
            # Remove old fake objects
            if 'lasthits' in portal:
                for ob in portal['lasthits']:
                    if ob not in s.hitObjectList:
                        if 'portalcopy' in ob and ob['portalcopy'] != None:
                            ob['portalcopy'].endObject()
                            ob['portalcopy'] = None

        else:
            # Create/move fake objects
            list = []
            for ob in s.hitObjectList:
                if 'fake' not in ob:
                    print ob,"hit",portal
                    if 'portalcopy' in ob and ob['portalcopy'] != None:
                        copy = ob['portalcopy']
                    else:
                        copy = scene.addObject(ob,ob,0)
                        copy['fake'] = 1
                        ob['portalcopy'] = copy
                    opos = (Vector(ob.position) - Vector(portal.position))*m1
                    copy.position = Vector(otherportal.position) + opos*r180*m2
                    copy.localScale = ob.localScale
                    ori = Matrix(*ob.orientation)
                    ori.transpose()
                    ori = ori*m1*r180*m2
                    ori.transpose()
                    copy.orientation = ori
                    if opos[2] < 0: # Portal mesh is Z plane
                        # Teleport
                        ob.position,copy.position = copy.position,ob.position
                        ob.orientation,copy.orientation = copy.orientation,ob.orientation
                        ob.setLinearVelocity(Vector(ob.getLinearVelocity())*m1*r180*m2)
                        ob.setAngularVelocity(Vector(ob.getAngularVelocity())*m1*r180*m2)
                    list += [ob]
            portal['lasthits'] = list
        

You put the function definition (def findOb…) somewhere on the top level of your script. Usually that is after imports before starting with processings.

Anyway, I can’t see a reason why you would try to rename an object. There is no fixed object name in here.
Maybe you should asked the quthor of the script what the restrictions and assumptions are. As far as I can see:

  • A portal object is an owner of the sensor of the controller running this script.
  • It requires whether
    [LIST]
  • a property “otherportal” naming the opposite portal object
  • or a sensor that is owned by the opposite portal.
    [LIST]
  • if the portal owns the first sensor the opposite portal should own the last sensors,
  • if the portal owns the second sensor the opposite owns the second last sensor.
  • etc.

[/LIST]

  • It requires a camera object with the same name as the portal object exeeded by “cam”. E.g. portal = “portal1” camera = “portal1cam”.
    [/LIST]