Undefined global names

MakeHuman python API, python plugins, etc

Moderator: joepal

Undefined global names

Postby Shedeki » Mon Dec 05, 2016 11:23 am

MakeHuman seems to handle scopes in some way I cannot get my head around. Take the following script:
Code: Select all
import random
def foo():
   print(random.uniform(0.0, 1.0))
foo()

Usually, when interpreted by Python, this script prints some number between zero and one. In MakeHuman, it produces the following error:
Crappy script
The script produced an exception: global name 'random' is not defined

Within the definition of foo, random appears to be unknown. How come? A similar problem can be observed when calling one's own functions within functions.

I am running MakeHuman v1.1.0 on Ubuntu 16.04.
Shedeki
 
Posts: 6
Joined: Mon Dec 05, 2016 11:10 am

Re: Undefined global names

Postby joepal » Mon Dec 05, 2016 12:09 pm

Ok, first: Just to be clear, this seems to be a script written in the script editor, not python code in makehuman as such.

The scripts piped via the 7_scripting plugin are somewhat limited in that they are executed via an "exec" (see line 824 in 7_scripting.py). This has certain effects on scope. In particular, you cannot assume that a global variable is immediately available in a local contex without using the "global" keyword. For a longer discussion, see:

http://stackoverflow.com/questions/1250 ... ction-work

Executive summary, your code should probably be written like this instead (not tested) :

Code: Select all
def foo():
    import random
    print(random.uniform(0.0, 1.0))
foo()


Or possibly

Code: Select all
import random
def foo():
    global random
    print(random.uniform(0.0, 1.0))
foo()


However, the scripting plugin is mostly intended for short, simplistic things. If you want more complex program logic, you'd benefit from implementing it as a plugin instead.

As a side-note, MHAPI (see https://github.com/makehumancommunity/c ... /README.md) is intended to replace 7_scripting in the longer run, by making it easier to write plugins. But some things from 7_scripting aren't yet implemented in MHAPI.
Joel Palmius (LinkedIn)
MakeHuman Infrastructure Manager
http://www.palmius.com/joel
joepal
 
Posts: 4465
Joined: Wed Jun 04, 2008 11:20 am

Re: Undefined global names

Postby Shedeki » Mon Dec 05, 2016 4:30 pm

joepal wrote:Ok, first: Just to be clear, this seems to be a script written in the script editor, not python code in makehuman as such.

Correct.
joepal wrote:The scripts piped via the 7_scripting plugin are somewhat limited in that they are executed via an "exec" (see line 824 in 7_scripting.py). This has certain effects on scope. In particular, you cannot assume that a global variable is immediately available in a local contex without using the "global" keyword.

Thank you for the explanation. I was expecting something like that, and knowing for certain I'll be able to structure future code differently from the get go.
joepal wrote:
Code: Select all
def foo():
    import random
    print(random.uniform(0.0, 1.0))
foo()


Yes, that is how I ended up running my scripts.
joepal wrote:However, the scripting plugin is mostly intended for short, simplistic things. If you want more complex program logic, you'd benefit from implementing it as a plugin instead.

As a side-note, MHAPI (see https://github.com/makehumancommunity/c ... /README.md) is intended to replace 7_scripting in the longer run, by making it easier to write plugins. But some things from 7_scripting aren't yet implemented in MHAPI.

Alright -- I'll take your advice and try and look into plugins soon.

I appreciate the quick response.
Shedeki
 
Posts: 6
Joined: Mon Dec 05, 2016 11:10 am


Return to Python scripts

Who is online

Users browsing this forum: No registered users and 1 guest