PDA

View Full Version : Python troubles



wellsichris
07-02-2013, 12:55 AM
So I have been banging my head against this for a while, with no success. I have done python scripts for other apps, in the past but LW has eluded me,

I have a script that if I past it into the console works.. only on windows, and with a certain folderstructure, but still works great. then when I try to turn it into a plugin that lightwave can use.. I can't get it to work.

the question is then, what am I doing wrong?

here's the script that I can past in the console and it works.

import sys, os, shutil, tempfile
import math
import lwsdk
import pcore

sceneName = pcore.LWSceneInfo().name
latestVersion = 0
versionHolder = 0
contentSceneFolder = (lwsdk.LWDirInfoFunc(LWFTYPE_SCENE))
sceneBase, sep, versionNumber = sceneName.partition("_v")
versionNumber, sep, extension = versionNumber.partition(".")
scenesFolder = str(contentSceneFolder) + "\\" + str(sceneBase) + "\\"

print (scenesFolder)

print (sceneBase)
print (versionNumber)
print (extension)



for root, subFolders, files in os.walk(scenesFolder):
for f in files:
extension = os.path.splitext(f)[1][1:]
if extension == "lws":
if str(sceneBase) in f:
compName, sep, discard = f.partition(("." + str(extension)))
compName, sep, versionNumber = compName.partition("_v")
if int(versionNumber) > latestVersion:
versionHolder = versionNumber
latestVersion = int(versionNumber)

print(latestVersion)
print(versionHolder)


versionNumber = int(versionHolder) + 1

versionNumber = '%03d' % versionNumber

print (versionNumber)



if not os.path.exists(scenesFolder):
os.makedirs(scenesFolder)

contentFolder, sep, garbage = contentSceneFolder.partition("Scenes")


print(contentFolder)

renderFolder = str(contentFolder) + "Renders\\" + str(sceneBase) + "\\" + str(sceneBase) + "_v" + str(versionNumber)
print(renderFolder)

if not os.path.exists(renderFolder):
os.makedirs(renderFolder)

print("SaveSceneAs " + str(scenesFolder) + str(sceneBase) + "_v" + str(versionNumber) + ".lws")

lwsdk.command("SaveSceneAs " + str(scenesFolder) + str(sceneBase) + "_v" + str(versionNumber) + ".lws")
lwsdk.command("SaveRGB 1")
lwsdk.command("SaveRGBPrefix " +str(renderFolder) +"\\" + str(sceneBase) + "_v" + str(versionNumber) + ".")

wellsichris
07-02-2013, 01:05 AM
okay here is the version I thought I had setup properly, I'm not sure how to past these and keep the formating
[CODE]
#! /usr/bin/env python
# -*- Mode: Python -*-
# -*- coding: ascii -*-

"""
This is a LightWave Generic Plugin that versions up the file and renderfolders.
"""
import sys, os, shutil, tempfile
import math
import lwsdk
import pcore

__author__ = "Chris Wells"
__date__ = "7/1/2013"
__copyright__ = "Copyright (C) Chris Wells"
__version__ = "1.0"
__maintainer__ = "Chris Wells"
__email__ = ""
__status__ = "Utility"
__lwver__ = "11"

class version_scene(lwsdk.IGeneric):
def __init__(self, context):
super(version_scene, self).__init__()
# LWGeneric -------------------
sceneName = pcore.LWSceneInfo().name
latestVersion = 0
versionHolder = 0
contentSceneFolder = (lwsdk.LWDirInfoFunc(LWFTYPE_SCENE))
sceneBase, sep, versionNumber = sceneName.partition("_v")
versionNumber, sep, extension = versionNumber.partition(".")
scenesFolder = str(contentSceneFolder) + "\\" + str(sceneBase) + "\\"

for root, subFolders, files in os.walk(scenesFolder):
for f in files:
extension = os.path.splitext(f)[1][1:]
if extension == "lws":
if str(sceneBase) in f:
compName, sep, discard = f.partition(("." + str(extension)))
compName, sep, versionNumber = compName.partition("_v")
if int(versionNumber) > latestVersion:
versionHolder = versionNumber
latestVersion = int(versionNumber)

versionNumber = int(versionHolder) + 1

versionNumber = '%03d' % versionNumber


if not os.path.exists(scenesFolder):
os.makedirs(scenesFolder)

contentFolder, sep, garbage = contentSceneFolder.partition("Scenes")


renderFolder = str(contentFolder) + "Renders\\" + str(sceneBase) + "\\" + str(sceneBase) + "_v" + str(versionNumber)

if not os.path.exists(renderFolder):
os.makedirs(renderFolder)

lwsdk.command("SaveSceneAs " + str(scenesFolder) + str(sceneBase) + "_v" + str(versionNumber) + ".lws")
lwsdk.command("SaveRGB 1")
lwsdk.command("SaveRGBPrefix " +str(renderFolder) +"\\" + str(sceneBase) + "_v" + str(versionNumber) + ".")

return lwsdk.AFUNC_OK

ServerTagInfo = [
( "Version Scene", lwsdk.SRVTAG_USERNAME | lwsdk.LANGID_USENGLISH ),
( "Version Scene", lwsdk.SRVTAG_BUTTONNAME | lwsdk.LANGID_USENGLISH ),
( "Utilities/Python", lwsdk.SRVTAG_MENU | lwsdk.LANGID_USENGLISH )
]

ServerRecord = { lwsdk.GenericFactory("LW_PyVersionScene", version_scene) : ServerTagInfo }

BigHache
07-02-2013, 06:28 AM
Well this was weird because none of your tabs came through. But the first error I encountered when I tried to add this as a plug-in file was 'return outside of function'. So I added:


def process(self, ga):

Right after the LW Generic comment.

That allowed the plug-in to load without error. When I try to run it I get:


NameError: global name 'LWFTYPE_SCENE' is not defined

wellsichris
07-02-2013, 09:25 AM
Sorry about that, here they are in a zip. so formatting will be right. I was hoping someone could just look at what I had done and say, "oh there's your problem" but ya here's the scripts.

so both load as plugins and both have the same error you are getting.

LWFTYPE_SCENE' is not defined

which is the one parts of the script that should absolutely work. it all should work, you can copy the LWVersionUP.py script into the console and run it and it works. or does on my system with my folder structure. but

if you go to console and do

import lwsdk
print(lwsdk.LWDirInfoFunc(LWFTYPE_SCENE))

it returns your scene directory. yet save that as a py file and load it. it sees it's a plugin but when you run it. says

LWFTYPE_SCENE' is not defined

which is why I am stumped. I have no idea why it would work in the console, but not work as a plugin?

thanks for looking into this and for the help!

Chris

wellsichris
07-02-2013, 09:49 AM
okay Oliver Hotz figured it out for me.

LWDirInfoFunc(lwsdk.LWFTYPE_SCENE) vs LWDirInfoFunc(LWFTYPE_SCENE)

in the console you don't need to specify the lwsdk. but outside you do, i thought once it was imported it would be like in the console,

But it works now!! so if you new to this like I am, always put lwsdk. in there

clintonman
07-02-2013, 10:09 AM
I change it to lwsdk.LWFTYPE_SCENE to get past the error.

Oops, a few minutes too late.

BigHache
07-02-2013, 12:47 PM
OK that makes sense. Running in the console is not a 1:1 to running as a plug-in.