addScriptLink

Let’s get straight to the problem:

With a python script I’ve created an NMesh called ‘wave’ (so there is a DataBlock called ‘wave’).
Now I want to assign a scriptlink to this mesh, linking it to ‘test.txt’ and with the event FrameChanged. So I have a look at the reference documents and find this: addScriptLink(text,event)
So far, so good.

So I add this line to my script

wave = Blender.Object.addScriptLink(test.txt, "FrameChanged")

With this attribute error as result:
‘module’ object has no attribute ‘addScriptLink’

I’ve tried several variations on the code, but nothing seems to work. Does anybody know what I’m doing wrong?

You can find an example on this page :
http://jmsoler.free.fr/didacticiel/blender/tutor/cpl_meshexplosion_en.htm#scriptlinkexample

Thanks a lot, that was exactly what I was looking for.

Hi

in the example at

http://jmsoler.free.fr/didacticiel/blender/tutor cpl_meshexplosion_en.htm#scriptlinkexample

I can’t see why you need the “if”

 if scriptlink!=None and NAME not in scriptlink: 
   MESH3D.addScriptLink(NAME,EVENT) 
else: 
   MESH3D.addScriptLink(NAME,EVENT) 

macbuse

why don’t you remove the if statement to see the result ?

Hi again

sure I tested it with and without.

If I do alt-P twice I get 2 copies of explode2.py
linked to the object

The “if” is necessary but the “else” is not (I guess).
You have exactly the same code in the 2 branches
of the “if” :-? so there must be some redundancy
right?

Did you make the tests with a new file with no link to no script ?

Hi again

I read a lot of your code and rewrite some of it.
In general if someone (at work) asks me to do something
I start by googling to see if you have something
to build on as I hate starting from scratch.

As for this script - it’s not a big deal but…

I tested it with no scripts linked already

I tested it with a dummy script linked already (Text.py)

I deleted all the scripts and tested again.

I was just trying to understand the (uncommented)
“if” and it seemed strange that you had exactly
the same code in the 2 branches (I checked this
by doing Find/Find Again).

It looked to me as if you were using an “if” to test
to see if there was already a link to the script
and to prevent duplicated execution on “framechange”.

Macbuse

 if NAME not in scriptlink:
   MESH3D.addScriptLink(NAME,EVENT)

Problem : if scriptlink==None returns an error because it’s not a list

if  scriptlink!=None and NAME not in scriptlink:
   MESH3D.addScriptLink(NAME,EVENT)

Problem : if scriptlink==None no script added .

if scriptlink==None or scriptlink!=None and NAME not in scriptlink:
   MESH3D.addScriptLink(NAME,EVENT)

3 tests at least : ‘==’ and ‘or’ and ‘!=’

if  scriptlink!=None and NAME not in scriptlink:
   MESH3D.addScriptLink(NAME,EVENT)
else :
   MESH3D.addScriptLink(NAME,EVENT)

Only one test ‘!=’ (or two if the result is true):

OK

but this still doesn’t address the point of my last mail

You have exactly the same code in the 2 branches
of the “if”

i.e. MESH3D.addScriptLink(NAME,EVENT)

Whatever test you put in the boolean expression
this is the code that gets executed.

I’ve taken the if out and I get exactly the same result
I get a link to 'explode2.py". As I said in the last mail`
I see the point of checking for duplication before
linking but otherwise why bother?

BTW

if scriptlink != None:

and

if scriptlink:

are equivalent in python

macbuse

You are right, it is a security to avoid this kind of situation :
http://cobalt3d.free.fr/didacticiel/blender/tutor/images/python/explode/security_.jpg

… but otherwise why bother"

Are you kidding ?
:slight_smile:

a ce moment la, je vous prie de bien vouloir changer:


if scriptlink!=None and NAME not in scriptlink: 
   MESH3D.addScriptLink(NAME,EVENT) 
else: 
   MESH3D.addScriptLink(NAME,EVENT) 

a l’adresse

http://jmsoler.free.fr/didacticiel/blender/tutor/cpl_meshexplosion_en.htm#scriptlinkexample

en



if scriptlink and NAME  in scriptlink: 
   print "WARNING : <%s> already linked " %NAME 
else: 
   MESH3D.addScriptLink(NAME,EVENT) 

En ce qui concerne l’informatique je deconne pas, c’est pas mon genre

Same result . What do we gain there ?
( Même résultat . Qu’est-ce qu’on y gagne ? )

Ok, i understand the aesthetic side of your argumentation but …
my code carries out the operation only one and single time,
even if it appears twice in the text . If the script is yet linked, it
do just what it has to do : nothing .

The new code would oblige to carry out at least the following
operations:
1/ to seek for the variable NAME again (‘once’ again, we have
just done it in the test above)
2/ to translate it into text whereas it is “already” text ( are you
sure that it is so profitable)
3/ to write in the console, which is very long for an operation for
which nobody has a vital need .

( je comprends le coté esthétique de l’argument mais mon code
ne réalisera l’opération qu’une seule et unique fois,
même si elle apparaît deux fois dans le texte. Si le script est déjà
linké, alors il fera la seule chose qu’il doit faire : rien .

Le nouveau code obligerait à effectuer au moins
les opérations suivantes :
1/ chercher de nouveau NAME (encore une fois, on
vient de le faire dans le test au dessus)
2/ le traduire en texte alors que c’est déjà du texte
(pas très rentable)
3/ ecrire dans la console, ce qui est long pour
une opération dont personne n’a un besoin vital )

as you like.
I’m too busy to keep at you.
Leave it like/at that.

It’s not a question of aesthetics
It’s a question of ethics.

I have 3 rules when i write a script .
First, it must work .
Second, it must be robust and fast .
Third, it must be short and thrifty .

But, seriously, i do not know how it could be ‘ethics’ …