Mon, Dec 23, 10:57 AM CST

Renderosity Forums / Blender



Welcome to the Blender Forum

Forum Moderators: Lobo3433 Forum Coordinators: LuxXeon

Blender F.A.Q (Last Updated: 2024 Dec 23 6:26 am)

Welcome to the Blender Forum!


   Your place to learn about Blender, ask questions,
   exchange ideas, and interact with the other Blender users!


   Gallery | Freestuff | Tutorials

 

Visit the Renderosity MarketPlace. Your source for digital art content!

 





Subject: Python Scriptlink Frame Change Problem


Enivob ( ) posted Wed, 29 November 2006 at 8:08 AM · edited Mon, 23 December 2024 at 6:55 AM

Hi All,

I have been playing around with adding python script to the scriptlink of an object. I have run into a simple problem that I thought I'd ask the community.

In my opiion, the scriptlink framechange event has a serious flaw. Mainly any variables that you create in the script link frame change event fall out of scope and disappear at the end of the event.

If you attach this script to the Frame Changed script link of the default cube and scrub the timeline, you will see that the except case always runs.

[code]

import Blender as B

class myAdder:

 localValue = 0

 def init(self):

  self.localValue = 0

 def add_value(self):

  self.localValue = self.localValue + 1

 def get_value(self):

  return (str(self.localValue))

 

try:

 # All other times we should get here.

 myRecoverClass = B.holdMyClass

 myRecoverClass.add_value()

 print(myRecoverClass.get_value())

except:

 # We should get here only the first time.

 # No class exists.

 myClass = myAdder ()

 myClass.add_value()

 B.holdMyClass = myClass

 print ("exception")

[/code]

It seems that even my Blender variable will fall out of scope in a scriptlink framechange event so I always end up in the except.

What I need is a global place to put my "import Blender as B" line.

Does anyone know how to do this?


nruddock ( ) posted Wed, 29 November 2006 at 1:47 PM

Weird one, but there is a solution.

import __builtin__ as bi<br></br>import Blender as B<br></br>class myAdder:<br></br>    def __init__(self):<br></br>        self.localVal = 0<br></br><br></br>    def addValue(self):<br></br>        self.localVal = self.localVal + 1<br></br><br></br>    def getValue(self):<br></br>        return self.bi.str(self.localVal)<br></br><br></br>try:<br></br>    ma = B.myClass<br></br>    ma.addValue()<br></br>except:<br></br>    ma = myAdder()<br></br>    ma.bi = bi<br></br>    B.myClass = ma<br></br><br></br>print ma.getValue()

If you don't return a string value from myAdder.getValue() everything would work without any messing around. import Blender as B
class myAdder:
    def init(self):
        self.localVal = 0

    def addValue(self):
        self.localVal = self.localVal + 1

    def getValue(self):
        return self.localVal

try:
    ma = B.myClass
    ma.addValue()
except:
    ma = myAdder()
    B.myClass = ma

print ma.getValue()

If your catching an exception, then it's a good idea, while developing to still print the exception out using import traceback
traceback.print_exc()

just in case you run into something weird.


Enivob ( ) posted Thu, 30 November 2006 at 6:37 AM

So what is builtin all about?

I don't really need to return a string, that was just a simplified dummy class.
The main class I am working with will control the position of the object the scriptlink is attached to, much like an enterframe in flash.

Thanks, I'll try it out.


nruddock ( ) posted Thu, 30 November 2006 at 2:59 PM

Quote - So what is builtin all about?

It's a special module that contains all the methods supplied by the Python core.
When a script get executed, the environment that gets passed to it only contains the set of modules/methods etc. that the calling code has included in it.
I don't know yet whether the very minimal environment that the link scripts are running in is intentional or a bug (caused by an oversight, a change in Python internals, or something else).

The standard Python documentation will tell you what other functions will unavailable without importing builtin


Privacy Notice

This site uses cookies to deliver the best experience. Our own cookies make user accounts and other features possible. Third-party cookies are used to display relevant ads and to analyze how Renderosity is used. By using our site, you acknowledge that you have read and understood our Terms of Service, including our Cookie Policy and our Privacy Policy.