Multiline Python Commands Not Working

Coreform Cubit Version: 2023.11
Platform: macOS 13.6.1


Hi all,

I’ve been running into issues when trying to run Python scripts in Cubit when commands are split over multiple lines. For example, if using some code formatter such as Black, long commands get split over multiple lines to make the code PEP-8 compliant.

Something like the following works in Cubit:

cubit.cmd("select face 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25")

but this doesn’t:

cubit.cmd(
    "select face 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25"
)

Here’s a full minimal working example:

import cubit

cubit.cmd("reset")

cubit.cmd("create surface rectangle width 1 height 1 zplane")
cubit.cmd("surface 1  scheme map")
cubit.cmd("surface 1 size 0.025")
cubit.cmd("mesh surface 1")

# Some arbitrary command that is intentionally very long so that it gets split
# across several lines
cubit.cmd(
    "select face 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25"
)

The above example raises the following error:

%>cubit.cmd(
  File "<string>", line 12
    cubit.cmd(
             ^
SyntaxError: '(' was never closed
%>    "select face 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25"
  File "<string>", line 13
    "select face 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25"
IndentationError: unexpected indent
%>)
  File "<string>", line 14
    )
    ^
SyntaxError: unmatched ')'

Do you have any suggestions for how to get around this, other than disabling the code formatter?

Cheers,
Pat

Hi @pmarty,

that looks like a bug, i will report this to the dev’s.

So how to get around this.

If you put your desired code into a function it should work. So for example your code, just packed into a main().

#!python
import cubit

def main():
 cubit.cmd("reset")

 cubit.cmd("create surface rectangle width 1 height 1 zplane")
 cubit.cmd("surface 1  scheme map")
 cubit.cmd("surface 1 size 0.025")
 cubit.cmd("mesh surface 1")

 # Some arbitrary command that is intentionally very long so that it gets split
 # across several lines
 cubit.cmd(
 	"select face 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25"
 )

if __name__ == "__coreformcubit__" or __name__ == "__main__":
    main()

Hi @Norbert_Hofbauer,

Awesome, that does the trick! Thanks so much for your help :slight_smile:

Have a great day,
Pat