Python - Importing modules from subdirectory

Discussions concerning programming of SOFTIMAGE©
Judicator
Posts: 39
Joined: 18 Apr 2013, 07:26

Python - Importing modules from subdirectory

Post by Judicator » 26 Apr 2013, 09:49

Hi, guys.

I cannot find information about that anywhere, I hope somebody will be able to help.
So basically I have a file "Template.py" and a folder "Include" at the same place. I need to import modules from "Include" to "Template.py".
Hierarchy looks like this:

Code: Select all

PDB Reader: Template.py
            Include:
                    __init__.py
                    sPDBConverter.py
                    ...
                    Builder.py
            SPDB:
                    ...
My "__init__.py" is empty and I use "from Include.Builder import Builder" in my "Template.py".
It works perfectly in Eclipse, but when I open the "Template.py" in SoftImage it doesn't understand "Include.Builder".
What am I missing? Btw, I'm kinda new at scripting, so big words won't work that well on me :)

User avatar
Hirazi Blue
Administrator
Posts: 5113
Joined: 04 Jun 2009, 10:15

Re: Python - Importing modules from subdirectory

Post by Hirazi Blue » 26 Apr 2013, 10:34

AFAIK, but I am no expert, you have one of two options:
- add the subdirectory to your PYTHONPATH environment variable through your Softimage setenv.bat
- add the directory to your sys.path "on the fly" by adding something like

Code: Select all

import sys
sys.path.append("../include")
to your script before the line(s), where you actually do the importing, then you should be able to directly import your Builder module.

But again, I am no expert :-s
Stay safe, sane & healthy!

Judicator
Posts: 39
Joined: 18 Apr 2013, 07:26

Re: Python - Importing modules from subdirectory

Post by Judicator » 26 Apr 2013, 10:58

This does work, but the problem is you have to write a full path to that folder.
I don't know how to tell that "include" is a sub-folder.
Then, is there a way to find the location of "Template.py"? I could simply add that location + "/Include".
And there's another thing.
I have

Code: Select all

###########################################################
#                                                         #
#                   SOFTIMAGE STUFF                       #
#                                                         #
from win32com.client import constants                     #
root = Application.ActiveSceneRoot                        #
kids = root.Children                                      #
nulls = kids.Filter( "", constants.siNullPrimitiveFamily )#
for n in nulls :                                          #
   Application.LogMessage( n, constants.siComment )       #
#                                                         #
###########################################################
in both "Template.py" and "Builder.py". Both files work great separately, but when I import "Builder", SoftImage tells me "name 'Application' is not defined".
Why could this happen? Do I need to transfer those lines from "Template" to "Builder" somehow?
I'm kinda working on a project and I need to fix this as soon as possible :ymsigh:

User avatar
xsisupport
Posts: 713
Joined: 09 Jun 2009, 09:02
Location: Montreal Canada

Re: Python - Importing modules from subdirectory

Post by xsisupport » 26 Apr 2013, 11:50

Did you look at the "Working with Custom Python Modules in Softimage" topic in the SDK online help?
// Steve Blair
// "You're not a runner, you're just a guy who runs" -- my wife
//
// My Blogs: Arnold | Softimage

Judicator
Posts: 39
Joined: 18 Apr 2013, 07:26

Re: Python - Importing modules from subdirectory

Post by Judicator » 26 Apr 2013, 12:54

Didn't find anything useful there.
But I did find information elsewhere.
So if anyone ever runs into same type of problem, you have to add

Code: Select all

from win32com.client import Dispatch
Application = Dispatch("XSI.Application").Application
to your module. These should be first lines.

And as for finding the location of a file... I think it will be easier just to make an installer, which would create configuration file with all needed information.
But thanks for pointing me into the right direction.

User avatar
xsisupport
Posts: 713
Joined: 09 Jun 2009, 09:02
Location: Montreal Canada

Re: Python - Importing modules from subdirectory

Post by xsisupport » 26 Apr 2013, 12:56

Judicator wrote:Didn't find anything useful there.
But I did find information elsewhere.
So if anyone ever runs into same type of problem, you have to add

Code: Select all

from win32com.client import Dispatch
Application = Dispatch("XSI.Application").Application
to your module. These should be first lines.

And as for finding the location of a file... I think it will be easier just to make an installer, which would create configuration file with all needed information.
But thanks for pointing me into the right direction.
An installer? Why not a plugin?
// Steve Blair
// "You're not a runner, you're just a guy who runs" -- my wife
//
// My Blogs: Arnold | Softimage

EricTRocks
Moderator
Posts: 754
Joined: 25 Nov 2009, 00:41

Re: Python - Importing modules from subdirectory

Post by EricTRocks » 26 Apr 2013, 13:02

Folder structure
myPackage: (folder)
- __init__.py
- template.py
- include: (folder)
-- __init__.py
-- builder.py

once you add the path of the myPackage folder to the sys.path or to your env variable, I believe you should be able to do:

from myPackage import template
from myPackage.include import builder

If you take a look at some of the ways I go about dynamically building a path to the directory to include in some of my plug-ins it's rather simple to do actually.

https://bitbucket.org/EricTRocks/et_too ... ?at=master

you'll want the plug-in with this code to run first so I'd recommend having it named something like "appendPackage.py" or something.

Also as you've discovered when you write Softimage specific code in modules / packages you'll have to dispatch the application and other utils:

from win32com.client.dynamic import Dispatch as d

xsi = d("XSI.Application").Application
XSIMath = d("XSI.Math")
XSIUtils = d("XSI.Utils")
XSIUIToolkit = d("XSI.UIToolkit")
XSIFactory = d("XSI.Factory")
Eric Thivierge
Lead Kraken Developer, Fabric Engine
http://fabric-engine.github.io/Kraken

Judicator
Posts: 39
Joined: 18 Apr 2013, 07:26

Re: Python - Importing modules from subdirectory

Post by Judicator » 26 Apr 2013, 14:18

Thanks, guys.

I'm new to scripting and even newer at scripting in SoftImage.
But since everybody's talking about plugins, I'll just maybe cut to the chase.

Basically I need to make a plugin with lots of options, buttons and stuff. Since I know nothing about them, I'm just using regular Python scripts.
My first task is to read and analyze a file and create a molecular structure from it. It consists of spheres and cylinders, where spheres are atoms and cylinders are their bonds. That I've already done.

But I need to constraint one side of a cylinder to position of one sphere, and other side to the second sphere, so as you move or animate them, cylinders respond accordingly.
Point orientation should also be constrained so cylinders don't deform on sphere's movement.
Since I don't know how to make such constraints in SoftImage, I was thinking about creating some kind of "OnEvent" function, which would just automatically correct cylinder's point position after a sphere was moved.

How do I do that? Well, at least where do I start with plugins? Do you know any step-by-step tutorials? SDK guide is kinda confusing.

luceric
Posts: 1252
Joined: 21 Jun 2009, 22:08

Re: Python - Importing modules from subdirectory

Post by luceric » 26 Apr 2013, 14:26

EricTRocks wrote:Folder structure
myPackage: (folder)
- __init__.py
- template.py
- include: (folder)
-- __init__.py
-- builder.py

once you add the path of the myPackage folder to the sys.path or to your env variable, I believe you should be able to do:

from myPackage import template
from myPackage.include import builder
right.. you should not have to add sub-directories to the PYTHONPATH/sys.path