Need help understanding - Manual: Controlling the Camera

Hey all,
Quick question on the Tutorial in the Manual.
Why are some of the Includes ‘import X’ and others ‘from X import alias’?
Is it so the functions within the Include can be referenced by the alias?

import direct.directbase.DirectStart
from direct.task import Task
from import Actor
import math


Read this, it explains the answer to your question and a lot more. :slight_smile:

Thanks for the link but I am a moron. This is as clear as mud.

OK, time for a quick geek to english translation attempt.

import X imports the module X, and creates a reference to that module in the current namespace. Or in other words, after you’ve run this statement, you can use to refer to things defined in module X.

import ModuleName allows you to use ModuleName.FunctionName to reference all of the functions in that module.


from X import * imports the module X, and creates references in the current namespace to all public objects defined by that module (that is, everything that doesn’t have a name starting with “_â€

maybe a simple example will work best:

using import modulename:

import math
print math.cos(math.pi/2)
print math.sin(0)

#or using the as statement:
import math as m
print m.cos(m.pi/2)
print m.sin(0)

using from modulename import all:

from math import *
print cos(pi/2)
print sin(0)

using from modulename import objects

from math import cos,pi
print cos(pi/2)
#print sin(0) #won't work...

using import(module): (not recommended unless using dynamic imports (import not known till runtime)

m = __import__('math')
print m.cos(m.pi/2)
print m.sin(0)

Note that in Panda, all the modules except for direct.directbase.DirectStart work like above.

You always use import direc.directbase.DirectStart because it monkeys around with the builtins (putting for example base,render,loader onto the global namespace).

so in the first example, you are importing the panda start stuff.
Then you import the class Task from the file direct/*
Then you import the class Actor from the file direct/*
Then you import the math module like above.

*this is actually in direct/src/filename…but for (almost) all intensive purposes gets changed at runtime to direct/filename

Hope this helps!

OK, I understand now.
Thank you,