Requesting help for Blender 2.57.1 python console script

Scripting in Blender with Python, and working on the API

Moderators: jesterKing, stiv

World
Posts: 5
Joined: Tue Nov 22, 2011 7:02 am

Requesting help for Blender 2.57.1 python console script

Postby World » Tue Nov 22, 2011 6:44 pm

Hello

I have just started learning python (I have learned how to open the python console) so that I can do the following:

Code: Select all

//Loop through each .psk file in a folder
     //import current .psk file into scene
     //Get the name of the .psk file for export
     //export as .fbx into another folder and with name of the .psk file
     //clear the scene for the next .psk file


Unfortunately I haven't made much progress towards this, even after going through several web pages such as:

http://wiki.blender.org/index.php/Doc:2 ... in_Blender
http://www.blender.org/forum/viewtopic.php?t=16090
http://blenderartists.org/forum/archive ... 23590.html
http://www.macouno.com/2010/03/17/using-python-scripts/
http://vdrift.net/Forum/viewtopic.php?p=8214

So far I have:

Code: Select all

import bpy.ops

#Import a .psk file
bpy.ops.import_scene.psk(filepath="C:/BlenderImport/Example_01.psk")
#Export as .fbx file
bpy.ops.export_scene.fbx(filepath="C:/BlenderExport/Example_01.fbx")
#Clear existing
bpy.ops.wm.read_homefile()


Any help is appreciated, thank you.

World
Posts: 5
Joined: Tue Nov 22, 2011 7:02 am

Re: Requesting help for Blender 2.57.1 python console script

Postby World » Tue Nov 22, 2011 9:06 pm

Also where does one put the .py file on windows 7 and how can you run it in the python console

Tried typing: blender -P psk2fbx.py

Got:

File "<blender_console>", line 1
blender -P psk2fbx.py
^
SyntaxError: invalid syntax


... or even better yet, is there something like a script area where you can just select to run this script

World
Posts: 5
Joined: Tue Nov 22, 2011 7:02 am

Help with variables for filepath

Postby World » Tue Nov 22, 2011 9:43 pm

So I've been researching even more, and thought I had it this time.

Code: Select all

import bpy
import bpy.ops
import os

def GetAllPSK(dir_name):
    pskList = []
    for file in os.listdir(dir_name):
        dirfile = os.path.join(dir_name, file)
        pskList.append(dirfile)
    return pskList
 
if __name__ == "__main__":
    pskList = GetAllPSK("C:/BlenderImport")
    for p in pskList:
        #Import a .psk file
        bpy.ops.import_scene.psk(filepath=p)
        #Select all
        bpy.ops.object.select_all()
        #Export as .fbx file
        bpy.ops.export_scene.fbx(filepath=p)
        #Clear existing
        bpy.ops.wm.read_homefile()



Sadly, it seems that for filepath I'm getting this error:

Code: Select all

Python Script fail, look in the console for now ...


Could someone please help fix my code

World
Posts: 5
Joined: Tue Nov 22, 2011 7:02 am

Not sure what to do

Postby World » Tue Nov 22, 2011 11:30 pm

Well, after much trial and error

Code: Select all

import bpy
import bpy.ops
import os
import string

def GetAllPSK(dir_name):
    pskList = []
    for file in os.listdir(dir_name):
        dirfile = file.replace('.psk','')
        pskList.append(dirfile)
    return pskList
 
if __name__ == "__main__":
    pskList = GetAllPSK("C:/BlenderImport/")
    for p in pskList:
        #Import a .psk file
        bpy.ops.import_scene.psk(filepath="C:/BlenderImport/" + p + ".psk")
        #Select all
        bpy.ops.object.select_all()
        #Export as .fbx file
        bpy.ops.export_scene.fbx(filepath="C:/BlenderExport/" + p + ".fbx")
        #Delete Mesh
        bpy.ops.object.delete()


Seems I can import the mesh, but it gives an error of check console yet again and my script stops there

Again, I'm open to suggestions :(

stiv
Posts: 3646
Joined: Tue Aug 05, 2003 7:58 am
Location: 45N 86W

Postby stiv » Wed Nov 23, 2011 12:43 am

but it gives an error of check console yet again


As always, knowing the exact error message is useful for debugging problems.

Otherwise, you get advice like:

1) read the error message
2) identify and correct the problem

World
Posts: 5
Joined: Tue Nov 22, 2011 7:02 am

Postby World » Wed Nov 23, 2011 6:04 am

stiv wrote:As always, knowing the exact error message is useful for debugging problems.


The exact error I get in the text editor is:

Code: Select all

Error, python script fail, look in the console for now ...


Do you know how to do enable this "console", when I start blender all I have is blender itself, no log screen or anything to go with it.

ldo
Posts: 544
Joined: Sun Nov 07, 2010 11:30 pm
Location: Hamilton, New Zealand

Postby ldo » Fri Nov 25, 2011 10:05 am

On Windows, Blender runs from a console window. The error messages should appear in that console window.

brasshat
Posts: 147
Joined: Mon Nov 16, 2009 1:06 pm

Postby brasshat » Fri Nov 25, 2011 7:56 pm

World wrote:
stiv wrote:

Code: Select all

Error, python script fail, look in the console for now ...


Do you know how to do enable this "console", when I start blender all I have is blender itself, no log screen or anything to go with it.


On my installation of Blender 2.60a, one activates the Blender console window by selecting from the drop down menu associated with "Help", the item "toggle system console".

ns

carrollh
Posts: 1
Joined: Wed Jun 06, 2012 11:36 pm

Postby carrollh » Wed Jun 06, 2012 11:43 pm

In 2.53 you need to specify one of the following before you enter the filepath:
'INVOKE_DEFAULT', 'INVOKE_REGION_WIN', 'INVOKE_REGION_CHANNELS', 'INVOKE_REGION_PREVIEW', 'INVOKE_AREA', 'INVOKE_SCREEN', 'EXEC_DEFAULT', 'EXEC_REGION_WIN', 'EXEC_REGION_CHANNELS', 'EXEC_REGION_PREVIEW', 'EXEC_AREA', 'EXEC_SCREEN'

So in short, you need to load each of your files using:
bpy.ops.import_scene.psk(<choice from above>, filepath=<path + filename>)

Both of the arguments required is a string. I tested 'EXEC_DEFAULT' as the first and it did what I wanted. I think 2.6 uses python 3, so this probably won't work the same.

CoDEmanX
Posts: 894
Joined: Sun Apr 05, 2009 7:42 pm
Location: Germany

Postby CoDEmanX » Thu Jun 07, 2012 2:34 am

I'm sitting, waiting, wishing, building Blender in superstition...


Return to “Python”

Who is online

Users browsing this forum: Bing [Bot] and 0 guests