KK

so I have 2 py files...
1 being the main, and the other, the plugin

I have a list var in the main, that I'd like to append data to from the plugin
how do I do it??

the data to append: (from plugin)
' ('template', '*.tmp') '

the data to append to: (in program)
sup = ' *.old'
im_types = [('Supported', sup),('old','*.old')]

note: google was of no help, and I can't think strait anymore because of it DX<

Edited 6 Years Ago by Tcll: n/a

Stupid demonstration of meaning of mutability of lists.

import plugin
mydata = ['very', 'important', 'thing']
plugin.plug(mydata, 'more to put')
print mydata
"""Output:
['very', 'important', 'thing', 'more to put']
"""

plugin:

def plug(change, something):
    change.append(something)

Edited 6 Years Ago by pyTony: n/a

Stupid demonstration of meaning of mutability of lists.

import plugin
mydata = ['very', 'important', 'thing']
plugin.plug(mydata, 'more to put')
print mydata
"""Output:
['very', 'important', 'thing', 'more to put']
"""

plugin:

def plug(change, something):
    change.append(something)

granted that would work...

thing is...
what about multiple plugins...
(for differant conversion types)

all I'm trying for is something like:

#plugins added here
import\
plugin,
plugin2
#add furthur plugins below

I want to add plugins without having to greatly mod the main prog :/
something like an auto-detect feature for newer plugins...

is that possible??

Edited 6 Years Ago by Tcll: n/a

would something like this work??

import\
plugin as P,
plugin2 as P

list = []
list.append(P.func)

-in plugin-

def func(): return 'string'

-in plugin2-

def func(): return 'string2'


would that return:
list =

sry...
not the best Py programmer :P

Edited 6 Years Ago by Tcll: n/a

you would do

import plugin1
 import plugin2
mylist=[plugin1.func(), plugin2.func()]

hmm...
I don't want to edit the list directly :/
I might as well just add the data to the list myself...

of course... I know how to do that,
but when I come out with a new plugin, other people won't,
and they'll have a hard time working with the data...

the only thing I want to do is:
simply add a plugin to the import section,
have some data in that plugin that will be detected by the list in the main file.
the main file will then update based on what data it recieves...

blender and winamp can do it... (but they auto-detect the plugin entirely)
so why can't I do the same :/

Edited 6 Years Ago by Tcll: n/a

I'm dealing with a bunch of impatient people dude :(
it's beginning to wear on my nerves DX

do you have at least something :/

KK
so here's something I've tried: :/

this is the converter:

from tkFileDialog import askopenfilename as tkFileGet, asksaveasfilename as tkFileSav

#import plugins:
import\
im_dat as I,\
im_mdl0 as I

#export plugins:
import\
ex_dae as E,\
ex_obj as E

sup = I.sup
im_types = [('Supported', sup)]
im_types.append( I.typ )
ex_types = []
ex_types.append( E.typ )
im_file = open( str(tkFileSav(filetypes=im_types)) ,"rb")
ex_file = open( str(tkFileSav(filetypes=ex_types)) ,"w")

and these are the plugins:
importers:

def sup(): return ' *.mdl0'
def typ(): return [('Brawl','*.mdl0')]
def sup(): return ' *.dat'
def typ(): return  [('Melee','*.dat')]

exporters:

def typ(): return  [('Collada','*.dae')]
def typ(): return  [('Wavefront','*.obj')]

I know that doesn't work like that...
but can you give me something that will :/

can I append to the list from the plugin??

KK...
I've gotten part 1 to work...

now I just need a little help with part 2 :/

I generale a list that looks like this:


but how do I get it to look like this:
["A","B","C"]

I still havn't finished looking through the google search library DX
but I hope to have my answer before I finish :/

KK...
I've gotten part 1 to work...

now I just need a little help with part 2 :/

I generale a list that looks like this:


but how do I get it to look like this:
["A","B","C"]

I still havn't finished looking through the google search library DX
but I hope to have my answer before I finish :/

heh...
turns out I didn't need to do that <:D

sry bout waisting a post :/
stupid editing time DX

Edited 6 Years Ago by Tcll: n/a

now,
how would you create a function that indexes all imported modules at once, (without having to re-type them)??

EDIT:
here's my code so far:

"""
Universal Model Converter v2.0a - created by Tcll5850
--Revisions--:
1.# series: brute-force method (scrapped)
2.0a: original
"""
#file types
types = []
im_types = []
ex_types = []

#import plugins:
import im_dat
import im_mdl0

#import extras (will be fixed)
im_dat.inf(types,im_types)
im_mdl0.inf(types,im_types)

#export plugins:
#import ex_hxo
import ex_dae140
import ex_obj
import ex_raw

#import extras (will be fixed)
#ex_hxo.inf(ex_types) #lvl 10
ex_dae140.inf(ex_types) #lvl 8
ex_obj.inf(ex_types) #lvl 3
ex_raw.inf(ex_types) #lvl 1

#file-open/save dialogs
from tkFileDialog import askopenfilename as tkFileGet, asksaveasfilename as tkFileSav
imfl = open( str(tkFileGet(filetypes=[('Supported', " ".join(types))]+im_types)) ,"rb")
exfl = open( str(tkFileSav(filetypes=ex_types)) ,"w")

#raw data types (converted to int/float)
vrts, nors, uvs, tris, mats, grps, bnes = [], [], [], [], [], [], []

yea, it's quite an inconvienience for other people to type out those functions when adding plugins...
but I wasn't told of anything better >:/

but yea...
now I need to dev a code that can tell a specific plugin to return data without having to type in all the plugins :/
just let the prog find the plugin based on the file extension...

really need some help

EDIT2:
oh, the lvl's on the export funcs are basically for how good of a filetype it is
(up to lvl 10)

...hxo format (my format) still in dev :/

Edited 6 Years Ago by Tcll: n/a

well...
looks like we have some lazy people -.-

I found out a better way than what you told me Tony
http://pytute.blogspot.com/2007/04/python-plugin-system.html
now I only need 1 function instead of 2 for each plugin

why couldn't you tell me something like that the 3rd time I asked DX

alright...
sry bout puttin up a hate-post...
but I just got lucky with that :/

when I need some help, I NEED IT

I get P'd when I can't make progress...
sry again <:|

Edited 6 Years Ago by Tcll: n/a

Sorry, but you should not ask specific questions, if you want different answers than you ask for. You should little improve defining the core problem, what you want to do and what are situation which you want to solve and way the solution would make things to work.

I thought to prepare you some dummy pluggins that would handle files by they file type just saying 'Handling .jpg in module jpgmodule' like stuff, but I am very busy with business and supporting my wife as she has three days examination of her studies.

Sorry, but you should not ask specific questions, if you want different answers than you ask for. You should little improve defining the core problem, what you want to do and what are situation which you want to solve and way the solution would make things to work.

I thought to prepare you some dummy pluggins that would handle files by they file type just saying 'Handling .jpg in module jpgmodule' like stuff, but I am very busy with business and supporting my wife as she has three days examination of her studies.

oh, I'm sry :o
I'm only 19 so idk a whole lot about that :/

but yea... KK
I can wait then, and thanx :)

yea...
I tried to explain after the 2nd example you gave...
I suck at communicating

Edited 6 Years Ago by Tcll: n/a

Here my simplified plugin, which assume that plugin directory and the place of files lives under the directory of the script (easy to change to any default place or according to environment variable). Notice extensions of two, three and four letters.

Main file with many prints for seeing the action:

import os
import sys

plugin_dir, process_dir = (os.path.join(os.path.realpath(os.path.curdir), direct)
                           for direct in ( 'plugins', 'test'))

sys.path.append(plugin_dir)
imported_modules = [__import__(fname.split('.')[-2]) for fname in
                    os.listdir(plugin_dir)]
print plugin_dir, process_dir
print 'Modules imported %i' % len(imported_modules)
print imported_modules

# plugins have module variable handler_info,
# which has tuple of supported file extendsion and handler_function
type_info = dict((ext, function)
                 for module in imported_modules
                 for  ext,function in module.handler_info )

print 'Handlers: ', type_info
print 'File types: ', sorted(type_info.keys())

for known in (os.path.join(process_dir, file_name) for  file_name in os.listdir(process_dir)
              if file_name.endswith(tuple(type_info.keys()))):
    thisfile,thisextension = os.path.splitext(known)
    print thisfile, thisextension
    function = type_info[thisextension]
    function(known)

Example jpg plugin:

import os
def handle_jpg(fn):
    print 'Handling jpg in jpg module', __file__
    os.startfile(fn)

handler_info = (('.jpg', handle_jpg),('.jpeg', handle_jpg))

Python file opener plugin.

import os
def handle_py(fn):
    print 'Handling Python file in pymodule', __file__
    os.system('notepad2 %s' % fn)

handler_info = (('.py', handle_py),('.pyw', handle_py))

You must define what kind of interface your plugins will have, input parameter and returned value.

Here we only pass the filename and return nothing instead launching Windows default application for jpeg and jpg, our own program for .py and .pyw, but not other files in directory test.

Edited 6 Years Ago by pyTony: n/a

Here my simplified plugin, which assume that plugin directory and the place of files lives under the directory of the script (easy to change to any default place or according to environment variable)

Main file with many prints for seeing the action:

import os
import sys

plugin_dir, process_dir = (os.path.join(os.path.realpath(os.path.curdir), direct)
                           for direct in ( 'plugins', 'test'))

sys.path.append(plugin_dir)
imported_modules = [__import__(fname.split('.')[-2]) for fname in
                    os.listdir(plugin_dir)]
print plugin_dir, process_dir
print 'Modules imported %i' % len(imported_modules)
print imported_modules

# plugins have module variable handler_info,
# which has tuple of supported file extendsion and handler_function
type_info = dict((ext, function)
                 for module in imported_modules
                 for  ext,function in module.handler_info )

print 'Handlers: ', type_info
print 'File types: ', sorted(type_info.keys())

for known in (os.path.join(process_dir, file_name) for  file_name in os.listdir(process_dir)
              if file_name.endswith(tuple(type_info.keys()))):
    thisfile,thisextension = os.path.splitext(known)
    print thisfile, thisextension
    function = type_info[thisextension]
    function(known)

Example jpg plugin:

import os
def handle_jpg(fn):
    print 'Handling jpg in jpg module', __file__
    os.startfile(fn)

handler_info = (('.jpg', handle_jpg),('.jpeg', handle_jpg))

Python file opener plugin.

import os
def handle_py(fn):
    print 'Handling Python file in pymodule', __file__
    os.system('notepad2 %s' % fn)

handler_info = (('.py', handle_py),('.pyw', handle_py))

You must define what kind of interface your plugins will have, input parameter and returned value.

Here we only pass the filename and return nothing instead launching Windows default application for jpeg and jpg, but not other files in directory test.

am I supposed to get an error like that?? :/

whatever...
it works...

a tricky part though is:
the list generated from your prog exmpl shows all the filetypes included...
I added a bmp plugin and got this:


^I can use that as a 'supported types' selection... thanx :D
but now, what about individual selections :/

my code below gave:
supported *.dat *.mdl0
Melee *.dat
Brawl *.mdl0

but it worked with the limited typing method :/

thanx again :D

Edited 6 Years Ago by Tcll: n/a

Maybe easy way would be to archive the .py as .pyc gets created each time import happens if they do not exist. Of course the Tkinter filedialog can be set to filter by file type .py and not include .pyc for selection purpose. Central Processing Unit may not best place to save the .py's, better to put them to HD (Hard disk not High Definition) ;) Merry Christmas!

Maybe easy way would be to archive the .py as .pyc gets created each time import happens if they do not exist. Of course the Tkinter filedialog can be set to filter by file type .py and not include .pyc for selection purpose. Central Processing Unit may not best place to save the .py's, better to put them to HD (Hard disk not High Definition) ;) Merry Christmas!

._.
you just made me feel like even more of a noob now =3=
IDK how to make it select by just '.py'...

I've tried btw...
but it either kept returning the same result,
or it gave an error...

would you like to take a look at my code??
I gave you credit btw ^_0

and I reduced my versions to more of what they should be...
2.0b sounds a bit high for a first release...

EDIT: :o
oh and btw,
Merry Christmas ;)

Edited 5 Years Ago by Tcll: n/a

ah screw it...
here's my code:

"""
Universal Model Converter v1.0b - created by Tcll5850
--Revisions--:
0.# series: brute-force method (scrapped)
1.0a: original (unreleased)
1.0b: plugin system applied
"""
debug = 1
#-- credit to Tonyjv for the following code --
import os, sys
plugin_dir, process_dir = (os.path.join(os.path.realpath(os.path.curdir), direct) for direct in ( 'plugins', 'test'))
sys.path.append(plugin_dir)
imported_modules = [__import__(fname.split(".")[0]) for fname in os.listdir(plugin_dir)]
#-- --

if debug:
    print plugin_dir
    #print process_dir
    print 'Modules imported %i' % len(imported_modules)
    for i in imported_modules:
        print i


#file types
#support list fmt: "*.type"
#im/ex_types list fmt: ('name','*.type')
support, im_types, ex_types = [], [], []
for module in imported_modules:
    module.inf(support,im_types,ex_types)

if debug:
    print 'supported files:' + str(support)
    print 'import types:' + str(im_types)
    print 'export types:' + str(ex_types)

#file-open/save dialogs
from tkFileDialog import askopenfilename as tkFileGet, asksaveasfilename as tkFileSav
imfl = open( str(tkFileGet(filetypes=[('All Files','*.*'),('Supported', " ".join(support))]+im_types)) ,"rb")
exfl = open( str(tkFileSav(filetypes=ex_types)) ,"wb") #<- will always write in binary


scenes = []

and a few plugins to play around with:

#mdl0 importer v1.0a
def inf(support,im_types,ex_types):
    support.append("*.mdl0")
    im_types.append(('Brawl','*.mdl0'))
    ex_types.append(('Brawl','*.mdl0'))
#obj exporter v1.0a
def inf(support,im_types,ex_types):
    support = ''
    im_types = ''
    ex_types.append(('Wavefront','*.obj'))

hmm... I'm having a slight prob...

when I go to save a file,
I type 'export' and it's supposed to add '.dae' or whatever...
but it don't even though I have the format selected...

any chance of telling me what's up??

Maybe line 13 needs condition if fname.endswith('.py') to take the module only once to list?

Where the extension would come from? Tkinter?

Edited 5 Years Ago by pyTony: n/a

Maybe line 13 needs condition if fname.endswith('.py') to take the module only once to list?

Where the extension would come from? Tkinter?

ah well that's one thing about 50% fixed :D
thanx :)

granted it still creates pyc files, but those don't matter now :D
and yea... it's only version 1 so it's good :)

now to fix this other inconvenience... DX
I don't wanna have to type the file extension every time...

and umm...
well for a future version...
would you know how to drag the import (open) filename to the export (save) filename??
I want to do it using a var...

you don't have to hit up on that right now as it is a future implementation... ;)

Edited 5 Years Ago by Tcll: n/a

I would use normal open(filename,'wb') where filename could be split from last point and extension replaced automatically. Or you could open dialog for choosing destination folder, not filename. You shoud also cope with cancel instead of open selected, so it is anyway better to store the filename before opening the file.

I would use normal open(filename,'wb') where filename could be split from last point and extension replaced automatically. Or you could open dialog for choosing destination folder, not filename. You shoud also cope with cancel instead of open selected, so it is anyway better to store the filename before opening the file.

everything except the open(filename,'wb') part just flew over my head 8P

well, I got the conversion to work on it by changing a few things...

the plugins handle the open case-types now...
the program just gives the path and sends it to the plugins...

how can I get the filename from the import path to show up in the export path??
and how do I make it "noob proof" =P
^(export to the selected extension)

and another thing...
my converter works by importing a model file (whatever extension handled by the plugins),
converting the data to a global type used in a tmp file,
then switches to the export functions where the tmp data is converted to the selected export format.

conversion:
import_format -> tmp_format -> export_format

...now, how do you delete the tmp file after using it??

Edited 5 Years Ago by Tcll: n/a

If you are using tempfile module it should take care of removing temporary files.

hmm...
looks good enough...

but note:
the import function writes the file and closes it
the export function then reads the file and that's when it should be deleted...

can I get an example of how to do that :P

since the Py docs don't give the examples they should -.-

Edited 5 Years Ago by Tcll: n/a

I found something...

import os
os.remove('path/file.extension')

that'll work for what I need

Do you know that is a delete you are exec. on the file.??
;)

i think you know ... dont you ?:)

Edited 5 Years Ago by richieking: n/a

This article has been dead for over six months. Start a new discussion instead.