Getting unexpected indent error when executing python script in script editor.

Getting unexpected indent error when executing python script in script editor.

hitecom8172
Enthusiast Enthusiast
25,276 Views
36 Replies
Message 1 of 37

Getting unexpected indent error when executing python script in script editor.

hitecom8172
Enthusiast
Enthusiast

i am getting an unexpected indent error in the script editor when trying to execute this python script.i didnt write this script just copied it from somewhere else cause i dont know how to write code.i want to create lpe for the light groups automatically.I will attach the script down below.

 

import pymel.core as pmc
import mtoa

interface = mtoa.aovs.AOVInterface()

class Aov(object):
def _init_(self,name,lpe):
self.name = name
self.lpe = lpe
self.lpeGroup = None

def addlightgroup(self,lightgroup):
self.ipegroup = self.lpe.replace('L.', *L.'()'*.format(lightgroup))


aovkeys = [ Aov("diffuse_direct". lpe = "C<RD>L"),
Aov("diffuse_indirect". lpe = "C<RD>[DSVOB].*"),
Aov("specular_direct". lpe = "C<RS[^'coat']>L"),
Aov("specular_indirect". lpe = "C<RS[^'coat']>[DSVOB].*"),
Aov("transmission_direct". lpe = "C<TS>L"),
Aov("transmission_indirect". lpe = "C<TS>[DSVOB].*"),
]

def getlights():
return pmc.ls(type['aiAreaLight', 'aiSkyDomeLight'])

def getlightgroup():
groups = []
for light in getlights():
groups.append(light.aiAov.get())
return groups

for aovKey in aovKeys:
for lightgroup in getlightgroups():
sceneAov = interface.addAOV("{}_{}*.format(lightgroup,aovKey.name))
aiAov = pmc.Pynode(sceneAov.node)
aovkey.addlightgroup(lightgroup)
aiAov.lightPathExpression.set(aovkey.lpeGroup)

 

also im attaching the the screen shot of the script editor.Please help ASAP!!!.lpe script error.jpg

 

0 Likes
Accepted solutions (1)
25,277 Views
36 Replies
Replies (36)
Message 2 of 37

sean.heasley
Alumni
Alumni

Hi @hitecom8172

 

Python uses spacing at the start of the line to determine when code blocks start and end. Errors you can get are:

Unexpected indent. This line of code has more spaces at the start than the one before, but the one before is not the start of a subblock (e.g. if/while/for statement). All lines of code in a block must start with exactly the same string of whitespace. For instance:

>>> def a():
...   print "foo"
...     print "bar"
IndentationError: unexpected indent

This one is especially common when running python interactively: make sure you don't put any extra spaces before your commands. (Very annoying when copy-and-pasting example code!)

>>>   print "hello"
IndentationError: unexpected indent

Unindent does not match any outer indentation level. This line of code has fewer spaces at the start than the one before, but equally it does not match any other block it could be part of. Python cannot decide where it goes. For instance, in the following, is the final print supposed to be part of the if clause, or not?

>>> if user == "Joey":
...     print "Super secret powers enabled!"
...   print "Revealing super secrets"
IndendationError: unindent does not match any outer indentation level

Expected an indented block. This line of code has the same number of spaces at the start as the one before, but the last line was expected to start a block (e.g. if/while/for statement, function definition).

>>> def foo():
... print "Bar"
IndentationError: expected an indented block

If you want a function that doesn't do anything, use the "no-op" command pass:

>>> def foo():
...     pass

Mixing tabs and spaces is allowed (at least on my version of Python), but Python assumes tabs are 8 characters long, which may not match your editor. Just say "no" to tabs. Most editors allow them to be automatically replaced by spaces.

The best way to avoid these issues is to always use a consistent number of spaces when you indent a subblock, and ideally use a good IDE that solves the problem for you. This will also make your code more readable.

 

 

If you have a link for where you got the code from we might be able to copy the format properly and fix this.

 

 

0 Likes
Message 3 of 37

hitecom8172
Enthusiast
Enthusiast

I got this from a youtube video i retyped the code as i did not have access to the source files  I copied it from a screenshot of the video.

 

Link :https://youtu.be/8Q35eXqkjWM

 

If you can reformat it properly and give it to me i would be very grateful. 

0 Likes
Message 4 of 37

Anonymous
Not applicable

The code in the _init_() method is indented two levels deep. It should only be indented one level deep. Compare it to the code in the addlightgroup() method.

It might be that the code in _init_ has been indented with TAB characters while the code in addlightgroup() has been indented with spaces. You shouldn't mix the two forms of indentation in the same script so ensure that all of the code is indented using just tabs or just spaces.

You should also check the empty lines to make sure that they really are empty and don't contain spaces, TABs or other invisible characters.

Message 5 of 37

sean.heasley
Alumni
Alumni

Hi @hitecom8172

 

I don't have time to go through the script manually and resolve it for you however @Anonymous made some good suggestions and I recommend following his advice.

 

 

0 Likes
Message 6 of 37

mspeer
Consultant
Consultant

Hi!

 

If you want someone to check your code then please add it again with the </>  (Insert Code) feature, so formatting will not be lost.

 

Test
	indent
		indent

 

0 Likes
Message 7 of 37

hitecom8172
Enthusiast
Enthusiast
import pymel.core as pmc
import mtoa

interface = mtoa.aovs.AOVInterface()

class Aov(object)
    def _init_(self,name,lpe) :
        self.name = name
        self.lpe = lpe
        self.lpegroup = None

    def addlightgroup(self,lightgroup):
        self.lpegroup = self.lpe.replace('L.',"L.'()'".format(lightgroup))


aovkeys = [ Aov("RGBA", lpe = "C.*"),
]

def getlights():
    return pmc.ls(type['aiArealight', 'aiSkydomelight'])

def getlightgroup():
    groups = []
    for light in getlights():
        groups.append(light.aiAov.get())
    return groups

for aovkey in aovkeys:
    for lightgroup in getlightgroups():
        sceneAov = interface.addAOV("{}_{}".format(lightgroup,aovkey.name))
        aiAov = pmc.Pynode(sceneAov.node)
        aovkey.addlightgroup(lightgroup)
        aiAov.lightPathExpression.set(aovkey.lpegroup)
0 Likes
Message 8 of 37

Anonymous
Not applicable

This line:

class Aov(object)

should have a ':' at the end.

 

This line:

    def _init_(self,name,lpe) :

should not have a space before the ':'.

 

After making those changes I no longer get any syntax or indent errors, but there are errors when executing the code which I don't have time to debug.

0 Likes
Message 9 of 37

hitecom8172
Enthusiast
Enthusiast

oh um i get his error now .TypeError: file <maya console> line 16: object() takes no parameters # .sorry abot the problems i am 13 and i dont know how to write python

0 Likes
Message 10 of 37

hitecom8172
Enthusiast
Enthusiast

um any solutions to this

0 Likes
Message 11 of 37

sean.heasley
Alumni
Alumni

Hi @hitecom8172

 

I believe that error is also linked to mixing tabs and spaces like @Anonymous mentioned above. You would need to go through and pretty much clean up the indents and make sure that they are all done either via spaces or tabs.

 

This forum is primarily dedicated to support issues for problems with Maya or things not working as expected. Not many users have the time to go in depth with this code and potentially re write it.

 

If you really need this script, you could rewrite the code yourself by just copying what you see and just make to either use spaces or tabs to avoid the issue that you're currently seeing.

 

 

0 Likes
Message 12 of 37

Anonymous
Not applicable

Also, the error is likely a generic Python one rather than something specific to Maya, so you could post the code and error message on a Python programming forum and they would likely be able to spot the problem pretty quickly.

Message 13 of 37

sean.heasley
Alumni
Alumni

Hi @hitecom8172

 

@Anonymous has the right idea. If you take your error and do a quick google search with it one of the first links is to stack overflow (which is a really solid site for getting help with programming issues) and it specifically mentions the spaces vs tabs issue. Like @Anonymous said the issue is most likely specific to Python and not Maya itself and I also recommend reaching out to a specific programming forum for more in depth help.

 

 

0 Likes
Message 14 of 37

kevin.picott
Alumni
Alumni

The "ls" command is also being used incorrectly. "type" is an argument so you should have this:

def getlights():
    return pmc.ls(type=['aiArealight', 'aiSkydomelight'])


Kevin "Father of the DG" Picott

Senior Principal Engineer
Message 15 of 37

hitecom8172
Enthusiast
Enthusiast

i checked the code line after line and where you pointed out is the place where i start to get errors how do i solve that?

0 Likes
Message 16 of 37

kevin.picott
Alumni
Alumni

I would recommend you post the errors you are getting. When it comes to debugging code it's next to impossible to do blind (like when I'm debugging my mother's email problems over the phone).



Kevin "Father of the DG" Picott

Senior Principal Engineer
0 Likes
Message 17 of 37

hitecom8172
Enthusiast
Enthusiast
Unexpected indent error right where you mentioned. Every thing before that
works fine.
0 Likes
Message 18 of 37

kevin.picott
Alumni
Alumni

Did you ensure that your script only contains either spaces or tabs but not both? It could visually look the same but Python does not like it when they are mixed.



Kevin "Father of the DG" Picott

Senior Principal Engineer
0 Likes
Message 19 of 37

hitecom8172
Enthusiast
Enthusiast

Yep i have only spaces as i converted everything in sublime. As i said before everything before def getlights is fine. Somewhere in that command is the error. 

0 Likes
Message 20 of 37

kevin.picott
Alumni
Alumni

Can you repost your modified code with the </> formatting? This type of error is always caused by a misplaced character or two so it's impossible to tell from a description what is going wrong.



Kevin "Father of the DG" Picott

Senior Principal Engineer
0 Likes