0
$\begingroup$

With the default cube mesh and Blender 2.77, if I run this code in console:

for f in bpy.data.meshes['Cube'].polygons:
    for v in f.vertices:
        print("%f %f %f" % bpy.data.meshes['Cube'].vertices[v].co[:])
        print("%f %f %f" % f.normal[:])
    print("\n")     

I get the following output (truncated for brevity):

-1.000000 -1.000000 1.000000
-1.000000 0.000000 0.000000
-1.000000 1.000000 1.000000
-1.000000 0.000000 0.000000
-1.000000 1.000000 -1.000000
-1.000000 0.000000 0.000000
-1.000000 -1.000000 -1.000000
-1.000000 0.000000 0.000000


-1.000000 1.000000 1.000000
0.000000 1.000000 -0.000000
...

However, with this:

for f in bpy.data.meshes['Cube'].polygons:
    for v in f.vertices:
        print("%f %f %f %f %f %f\n" % bpy.data.meshes['Cube'].vertices[v].co[:], f.normal[:])
    print("\n")

or even this:

for f in bpy.data.meshes['Cube'].polygons:
    for v in f.vertices:
        print("%f %f %f %f %f %f" % bpy.data.meshes['Cube'].vertices[v].co[0], py.data.meshes['Cube'].vertices[v].co[1], py.data.meshes['Cube'].vertices[v].co[2], f.normal[0], f.normal[1], f.normal[2])
    print("\n")

I get the error:

Traceback (most recent call last):
  File "<blender_console>", line 3, in <module>
TypeError: not enough arguments for format string

Why can I print 3 floats easily but printing 6 floats doesn't work? Is Python's formatted printing different that C's (in this respect)? Or is it something Blender specific? Is there a maximum limit on how many 'arguments' the format string can take?

$\endgroup$
  • $\begingroup$ print( "%f, %f" % (1, 2) ) , more information here stackoverflow.com/questions/15286401/… $\endgroup$ – lemon Sep 4 '16 at 6:30
  • $\begingroup$ @lemon Putting all the floats in a list works for program 3, but it is tedious to type. Is there any way to make it work for program 2 (with two separate lists)? $\endgroup$ – strNOcat Sep 4 '16 at 6:35
  • $\begingroup$ Have a look here stackoverflow.com/questions/455612/… and combine format function as you need $\endgroup$ – lemon Sep 4 '16 at 6:51
  • 1
    $\begingroup$ You can also change the end character of print from its default "\n" (carriage return) with print(str, end=",") for example. Something like cube = bpy.data.meshes.get("Cube") and for v in cube.vertices: would make the code a whole lot more readable... and also I vote to close this q as it is not really about blender python or the console, rather a syntax error. $\endgroup$ – batFINGER Sep 4 '16 at 7:59
  • 2
    $\begingroup$ I'm voting to close this question as off-topic because it is primarily about python. $\endgroup$ – batFINGER Sep 4 '16 at 8:01
1
$\begingroup$

In python the string formatting operation ('%') accepts a single argument and in your first (working) example that's exactly what you've got as it's a single element with 3 components. in your other (not working) examples you're trying to pass multiple values to '%', each containing 3 values. Your solution is to simply combine them into a single tuple by appending them with the '+' operator :

for f in bpy.data.meshes['Cube'].polygons:
    for v in f.vertices:
        print("%f %f %f %f %f %f\n" % (bpy.data.meshes['Cube'].vertices[v].co[:] + f.normal[:]))
    print("\n")
$\endgroup$
  • $\begingroup$ This doesn't work directly (same error), but putting a parenthesis around the entire second argument (list1 + list2) makes it work. I can't edit it because SE wants me to edit more that 6 characters $\endgroup$ – strNOcat Sep 4 '16 at 6:53

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.