Need help with basic one click OBJ exporter to project folder

Become a member of the CGSociety

Connect, Share, and Learn with our Large Growing CG Art Community. It's Free!

THREAD CLOSED
 
Thread Tools Search this Thread Display Modes
Old 04 April 2013   #1
Need help with basic one click OBJ exporter to project folder

Hi Guys-

Today I got sick of manually exporting obj's to a project folder and so I looked around for a script which would do things a bit more automatically. At Creative Crash I found a Dave Girard script which exports .obj's in a sequence... I basically stripped that down to a very simple .obj exporter but I'm unhappy with the way it saves out the .obj with the scene name- I would really prefer to save out with the selected object name, and even (ambitiously) have the script do an incremental save if possible (ie, if filename already exists, then add _v02, _v03, etc).

Since my MEL scripting skills are rubbish, I wondered if anyone would care to have a look? I would be very grateful if someone could alter the script from saving the .obj with the scene filename to the selected object name. I had a go at it and couldn't get it.


Here's what I have so far... as I say, it's a cut down existing script so there may still be a bit of superfluous code in there.





Quote: global proc basicOBJexport()
{

if (!(`pluginInfo -q -l objExport`))
{
loadPlugin objExport;
}

string $mySelection[] = `ls -sl`;
string $filename = `file -q -sn -shn`;
string $bufferName[];
tokenize $filename "." $bufferName;
string $projPath = `workspace -q -rd`;


file -force -options "groups=0;ptgroups=1;materials=0;smoothing=1;normal s=1" -typ "OBJexport" -pr -es ($projPath + "/Models/OBJ/" + $bufferName[0] + ".obj");

}


(You can see that the folder it saves to is in /Models/OBJ. That's the structure we use at work.)


I'd love to get this thing working properly. If successful the next step would be to do something similar as an .fbx exporter script. (We use a combination of .fbx and .obj's). A hotkey or shelf button assigned to fbx and obj export, with files automatically saving into the proper folders would be a real time saver.


Thanks very much for your time,
S Tozer

Last edited by Ekar : 04 April 2013 at 01:01 AM.
 
Old 04 April 2013   #2
Here is a quick python script, hope you don't mind but my MEL is super rusty
You can select multiple objects (groups, geos, ecc) and it will export each one to his file.

import maya.cmds as mc
import os
from maya.OpenMaya import MGlobal

def objToFolder():
    myFolder = 'Models/OBJ'
    objects = mc.ls(sl=True)
    projectFolder = mc.workspace(q=True, rd=True)
    
    if not mc.pluginInfo('objExport', q=True, l=True):
        mc.loadPlugin('objExport')
    
    for obj in objects:
        newFileName = '%s/%s_v001.obj' % (os.path.join(projectFolder, myFolder), obj)
        if os.path.isfile(newFileName):
            myPath, myFile = os.path.split(newFileName)
            myVersion = int(myFile.split('_')[1][1:].split('.')[0])
            while os.path.isfile(newFileName):
                myVersion += 1
                newFileName = '%s/%s_v%03i.obj' % (os.path.join(projectFolder, myFolder), obj, myVersion)
                
        mc.file(newFileName, force=True, options='groups=0;ptgroups=1;materials=0;smoothing  =1;normal s=1', type='OBJexport', pr=True, es=True)
        MGlobal.displayInfo('%s exported to %s' % (obj, newFileName))
            
        
objToFolder()
 
Old 04 April 2013   #3
Hey Nookie-

Wow That's fantastic! Great stuff, and sincere thanks. It works flawlessly, although I embarrassingly forgot to mention last night that the folder where the Models/OBJ is inside of is one folder down/back? from the maya project folder. So, at work we have a main project folder, with a maya folder inside of that, which we set up with the default Maya workspace - but the Models/OBJ folders are in the main project folder, if you understand what I mean.

-Project Folder
..................-Models
........................-OBJ
........................-FBX

..................-Maya (project set to here in Maya)
........................-Default Maya Workspace created in here

Right now, the script is looking for a Models/OBJ folder inside of the maya project folder, but we actually need to move back down the directory hierarchy one step to the main project folder, and then to Models/OBJ. So my question is, how can we do that with the current script? (I believe you use 'os.chdir' but i'm in the dark really).

Anyway, thanks again for your work on this. Really appreciate it.

Cheers


PS- here's my attempt at an fbx version. Of course it doesn't work!

Quote: import maya.cmds as mc
import os
from maya.OpenMaya import MGlobal

def fbxToFolder():
myFolder = 'Models/FBX'
objects = mc.ls(sl=True)
projectFolder = mc.workspace(q=True, rd=True)

if not mc.pluginInfo('fbxmaya', q=True, l=True):
mc.loadPlugin('fbxmaya')

for obj in objects:
newFileName = '%s/%s_v001.fbx' % (os.path.join(projectFolder, myFolder), fbx)
if os.path.isfile(newFileName):
myPath, myFile = os.path.split(newFileName)
myVersion = int(myFile.split('_')[1][1:].split('.')[0])
while os.path.isfile(newFileName):
myVersion += 1
newFileName = '%s/%s_v%03i.obj' % (os.path.join(projectFolder, myFolder), obj, myVersion)


mc.file(newFileName, -force -options "v=0;" -typ "FBX export" -pr -es)
MGlobal.displayInfo('%s exported to %s' % (fbx, newFileName))


fbxToFolder()





S Tozer

Last edited by Ekar : 04 April 2013 at 04:18 PM.
 
Old 04 April 2013   #4
I don't know if there is a simplre built-in command to go one level up, but this works
import maya.cmds as mc
import os
from maya.OpenMaya import MGlobal

def objToFolder():
    myFolder = 'Models/OBJ'
    objects = mc.ls(sl=True)
    projectFolder = mc.workspace(q=True, rd=True)

    myList = projectFolder.split('/')
    projectFolder = '/'.join(myList[:-2]) + '/'
    
    if not mc.pluginInfo('objExport', q=True, l=True):
        mc.loadPlugin('objExport')
    
    for obj in objects:
        newFileName = '%s/%s_v001.obj' % (os.path.join(projectFolder, myFolder), obj)
        if os.path.isfile(newFileName):
            myPath, myFile = os.path.split(newFileName)
            myVersion = int(myFile.split('_')[1][1:].split('.')[0])
            while os.path.isfile(newFileName):
                myVersion += 1
                newFileName = '%s/%s_v%03i.obj' % (os.path.join(projectFolder, myFolder), obj, myVersion)
                
        mc.file(newFileName, force=True, options='groups=0;ptgroups=1;materials=0;smoothing    =1;normal s=1', type='OBJexport', pr=True, es=True)
        MGlobal.displayInfo('%s exported to %s' % (obj, newFileName))
            
        
objToFolder()


please note that there is no check on the output directory, so if it doesn't exist the script wil fail
 
Old 04 April 2013   #5
This is great! Thanks a ton Nookie

Last edited by Ekar : 04 April 2013 at 11:24 PM.
 
Old 04 April 2013   #6
Hey Nookie-

I have the fbx exporter working now. Works fine as far as I can see but maybe you'd like to have a look over the code when you have a second and see if I've done something wrong or missed something. However the fbx saves out fine when I select a group of objects in the outliner- the filename is the group name, which is how I'd want it to be.

Quote: import maya.cmds as mc
import os
from maya.OpenMaya import MGlobal

def fbxToFolder():
myFolder = '_Models/FBX'
objects = mc.ls(sl=True)
projectFolder = mc.workspace(q=True, rd=True)

myList = projectFolder.split('/')
projectFolder = '/'.join(myList[:-2]) + '/'

if not mc.pluginInfo('fbxmaya', q=True, l=True):
mc.loadPlugin('fbxmaya')

for obj in objects:
newFileName = '%s/%s_v001.fbx' % (os.path.join(projectFolder, myFolder), obj)
if os.path.isfile(newFileName):
myPath, myFile = os.path.split(newFileName)
myVersion = int(myFile.split('_')[1][1:].split('.')[0])
while os.path.isfile(newFileName):
myVersion += 1
newFileName = '%s/%s_v%03i.fbx' % (os.path.join(projectFolder, myFolder), obj, myVersion)

mc.file(newFileName, force=True, options='groups=0;ptgroups=1;materials=0;smoothing =1;normal s=1', type='FBX export', pr=True, es=True)
MGlobal.displayInfo('%s exported to %s' % (obj, newFileName))


fbxToFolder()


I can't tell you how much I love these new scripts. Will save tons of time mucking around in folders all day. We export so much stuff that things were getting to be a real pain. Now everything is just a single click, no naming, nothing. Just click! Thanks Nookie-

Cheers,
S Tozer

Last edited by Ekar : 04 April 2013 at 10:15 PM.
 
Old 05 May 2013   #7
Hi Nookie/CG Talkers,

I'm having a go now at a Python script to more quickly import obj's (or fbx's) from specific project folders (same folders as obj/fbx importers in the above scripts). When run, the script would open the import window, navigate to the project's obj (or fbx) folder and select obj (or fbx) as the file type. Then the user would select a file in the normal way.

I can open the maya import window with the command 'Import'. But I am unable to locate 'Import' in either the Maya Mel or Python commands reference to find out more about it. In the code, I'm stuck at being able to tell Import to go to a specific folder, defined by the variable 'objFolder' in this case.

Quote: import maya.cmds as mc
import os
from maya.OpenMaya import MGlobal





def importObj():

objFolder = cmds.internalVar(uwd = True).rpartition("Maya")[0] + "_Models/OBJ/"
cmds.Import(???)


importObj()



Any help/advice?

Last edited by Ekar : 05 May 2013 at 11:31 PM.
 
Old 05 May 2013   #8
you should use 'fileDialog2' to get the file name that you want to import and 'file -i' to import the file.
Something like this:


import maya.cmds as mc
myFile = mc.fileDialog2(fileFilter='*.fbx', dialogStyle=2, fm=1)[0]
mc.file(myFile, i=True)


Check the guide (http://download.autodesk.com/global...ileDialog2.html) for setting up a default folder
 
Old 05 May 2013   #9
Hi Nookie-

Thanks for your reply. I'll check it out
 
Old 05 May 2013   #10
Thread automatically closed

This thread has been automatically closed as it remained inactive for 12 months. If you wish to continue the discussion, please create a new thread in the appropriate forum.
__________________
CGTalk Policy/Legalities
Note that as CGTalk Members, you agree to the terms and conditions of using this website.
 
Thread Closed share thread



Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

vB code is On
Smilies are On
[IMG] code is On
HTML code is Off
CGSociety
Society of Digital Artists
www.cgsociety.org

Powered by vBulletin
Copyright 2000 - 2006,
Jelsoft Enterprises Ltd.
Minimize Ads
Forum Jump
Miscellaneous

All times are GMT. The time now is 02:52 AM.


Powered by vBulletin
Copyright ©2000 - 2017, Jelsoft Enterprises Ltd.