Python script link problem

Hi I think I have a little problem with linking of objects.

I am quite new to both Python and Blender and what I have is a log file that has been generated by an other program (CD++, a DEVS toolkit) and want to visualize the results of the simulation by running a Python script.

My objects (passengers and obstacles) get displayed correctly initially, but as they are to move around they mostly disappear. What happens exactly is that the first time a “space” is occupied, a new object is created and linked to the current scene -> works fine. The except bit of script only gets executed if the object does not already exist. Please note that the objects don’t actually move but just appear and disappear and the positions corresponding the cells.


    if logValue == -2: # Occupied cell (passenger)
        try:
            ob = Blender.Object.Get(objectname)
#            scn.objects.link(ob)
            logfile.write("    Object exist or linked
")
        except ValueError:
            human = Blender.Object.Get ('Human')
            human.select(1)
            Blender.Object.Duplicate()
            activeObject = scn.getActiveObject()
            activeObject.name = objectname
            activeObject.LocX = int(xcoord)
            activeObject.LocY = int(ycoord)
            activeObject.LocZ = int(zcoord)
            activeObject.select(0)
            logfile.write("    Object created
")

Then when the passenger is to move (or leave a cell, as the simulation is represented by cells) the passenger gets unlinked with the following chunk of script


    elif logValue == 0: # Empty cell
        try:
            ob = Blender.Object.Get(objectname)
            scn.objects.unlink(ob)
            logfile.write("    Object unlinked
")
        except ValueError:
            logfile.write("    Cell already empty
")
    else:
                logfile.write("    **Log value not recognised as valid: %d**
" %(logValue))

The second time an object is supposed to “appear” at a location that has been used before… Nothing happen. I mean the

ob = Blender.Object.Get(objectname)

executes without error but the object itself does not show in the scene.

Any help would be much appreciated, I have tried everything I can think of and looked on many forums but have not found any relevant info.

Patrick

When you unlink the object, it unlinks it from the scene (unlinking != deleting).

Ideally instead of Object.Get, then you’d want to look in the active scenes objects and find it. If its not there, then you generate one, something like:


import bpy

scn = bpy.data.scenes.active
try:
     ob = [o for o in scn.objects if o.name == objectname][0]
except:
...

Ok… I finally found what the problem is!

I seems not to be in my script itself but mostly in the .log file (that I have been given) coming from the CD++ simulation tool.

What is happening is that the “object” are sometime created more than once in a row in some condition. Therefore my script is trying to get and then link the same object multiple times as well.

I now just need to find a way to skip the link() portion of the script if it has already been linked previously… The manual says that no action should be taken if the object is already linked (see http://www.blender.org/documentation/243PythonDoc/Scene.SceneObjects-class.html) but this does not seem to happen to me. I get a RunTimeError: Object already in scene!..

Any thoughts…
Thanks

My previous post did not seem to make it through…
Thanks forTe, the problem I had with your suggestion is that I get a “module not found: bpy” error. This may be because I am stuck using Python 2.4 and Blender 2.43…

Okay, try:


import Blender
from Blender import Scene

scn = Scene.GetCurrent()
try:
     ob = [o for o in scn.objects if o.name == objectname][0]
except:
....

Thanks for the help forTe,

One thing I only realized this afternoon which was the cause of most of my grief was that the log file I was using has more than one “instantiation” of the same object called before the object is unlinked.

So I had to do


...
 try:
            ob = [o for o in scn.objects if o.name == objectname][0]
            # If object not already linked to current scene
                    if (ob not in scene_obs):
                            scn.objects.link(ob)
                            logfile.write("    Object already exist but has been relinked
")
                    else:
                            logfile.write("    Object already exist and is already linked
")
except:
...

Your post did point me in the right direction and the reply was much appreciated.
Regards