Previous Thread  Next Thread

chat icon Requesting help for Blender 2.57.1 python console script

World

Posted: Tue Nov 22, 2011 6:44 pm
Joined: 22 Nov 2011
Posts: 5
Hello

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

Code:
//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.4/Tutorials/Render/Import/SolidWorks#Importing_in_Blender
http://www.blender.org/forum/viewtopic.php?t=16090
http://blenderartists.org/forum/archive/index.php/t-123590.html
http://www.macouno.com/2010/03/17/using-python-scripts/
http://vdrift.net/Forum/viewtopic.php?p=8214

So far I have:

Code:

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.
Reply with quote


World

Posted: Tue Nov 22, 2011 9:06 pm
Joined: 22 Nov 2011
Posts: 5
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
Reply with quote


World

Posted: Tue Nov 22, 2011 9:43 pm
Joined: 22 Nov 2011
Posts: 5
So I've been researching even more, and thought I had it this time.

Code:
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:
Python Script fail, look in the console for now ...


Could someone please help fix my code
Reply with quote


World

Posted: Tue Nov 22, 2011 11:30 pm
Joined: 22 Nov 2011
Posts: 5
Well, after much trial and error

Code:

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 Sad
Reply with quote


stiv

Posted: Wed Nov 23, 2011 12:43 am
Joined: 05 Aug 2003
Posts: 3491
Quote:
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
Reply with quote


World

Posted: Wed Nov 23, 2011 6:04 am
Joined: 22 Nov 2011
Posts: 5
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:
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.
Reply with quote


ldo

Posted: Fri Nov 25, 2011 10:05 am
Joined: 07 Nov 2010
Posts: 544
On Windows, Blender runs from a console window. The error messages should appear in that console window.
Reply with quote


brasshat

Posted: Fri Nov 25, 2011 7:56 pm
Joined: 16 Nov 2009
Posts: 63
[quote="World"]
stiv wrote:


Code:
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
Reply with quote


carrollh

Posted: Wed Jun 06, 2012 10:43 pm
Joined: 06 Jun 2012
Posts: 1
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.
Reply with quote


CoDEmanX

Posted: Thu Jun 07, 2012 1:34 am
Joined: 05 Apr 2009
Posts: 693
have a look at
http://www.blender.org/documentation/blender_python_api_2_63_7/bpy.ops.html#overriding-context
_________________
I'm sitting, waiting, wishing, building Blender in superstition...
Reply with quote


 
Jump to:  
Powered by phpBB © 2001, 2005 phpBB Group