3

Trying this very basic simple straight forward script, invoked by console command

$ blender --python basic_script.py

#! basic_script.py

add a primitive cylinder

bpy.ops.mesh.primitive_cylinder_add()

toggle into edit

bpy.ops.object.editmode_toggle()

resize, works perfect

bpy.ops.transform.resize(value=(0.01, 0.01, 5))

move, works perfect

bpy.ops.transform.translate(value=(0, 0, 5))

rotate, poll()wrong context, What!??

bpy.ops.transform.rotate(value=1.5708, orient_axis='X', orient_type='GLOBAL')

100 possible answers, none of em worked

hewi
  • 139
  • 3
  • When rotating in the program itself, blender calls bpy.ops.transform.rotate(value=1.5708, orient_axis='X', orient_type='GLOBAL', orient_matrix=((1, 0, 0), (0, 1, 0), (0, 0, 1)), orient_matrix_type='GLOBAL', constraint_axis=(True, False, False), mirror=True, use_proportional_edit=False, proportional_edit_falloff='SMOOTH', proportional_size=1, use_proportional_connected=False, use_proportional_projected=False) - not sure how much of that you truly need - perhaps just the matrix transform. I think if you leave it out, you need to set the rotation in radians which requires you to import math.. – Christopher Bennett Aug 29 '21 at 21:47
  • I tried with full list of attributes, no luck either – hewi Aug 29 '21 at 21:58
  • @ChristopherBennett, it all works from the scripting tab. I think OP needs to specify a context or set one active before the rotation command. – Ron Jensen Aug 29 '21 at 21:58
  • and I cannot figure out how to set the correct context – hewi Aug 29 '21 at 22:02
  • Try looking here - https://blender.stackexchange.com/q/69114/75504 – Christopher Bennett Aug 29 '21 at 22:25
  • it is sooo sad to be stuck for days on something so trivial :( – hewi Aug 29 '21 at 22:53
  • I agree hangups like these suck from time to time, but hang in there - you'll get it eventually, and when you finally do, it will be like an orgasm, after having gone through so much. I wish I could be more helpful, but unfortunately my knowledge of blender python is only slightly above trivial. I will try to hail other users that know much more about the subject. This has also given me reason to get off my butt and learn more bpy, so I'll use this as a basis for a lesson, and I'll get back to you if I find anything. – Christopher Bennett Aug 29 '21 at 23:04
  • IIRC, the bpy.ops are intended to run from the GUI, and bmesh is intended for scripting control – Ron Jensen Aug 29 '21 at 23:29
  • 1
    No idea why there is a poll error. Instead would prompt for matrices based solution as answered by Ron. Somewhat related https://blender.stackexchange.com/questions/214655/rotation-via-python-does-not-the-way-i-would-it-have-expected https://blender.stackexchange.com/a/214497/15543 – batFINGER Aug 30 '21 at 13:08

1 Answers1

5

I converted your script over to use bmesh constructs. Note that matrix multiplication is not commutative, so $A \cdot B$ is not always equal to $B \cdot A$, and Python for some reason decided to use '@' as matrix multiply.

#! basic_script.py
import bpy
import bmesh # https://docs.blender.org/api/current/bmesh.ops.html
import mathutils # https://docs.blender.org/api/current/mathutils.html

lets be sure we're in object mode

bpy.ops.object.mode_set(mode='OBJECT')

add a primitive cylinder

bpy.ops.mesh.primitive_cylinder_add()

our new object should be the active object, so grab a handle to it

obj=bpy.context.active_object me=obj.data

Make a new BMesh

bm = bmesh.new() bm.from_mesh(me) # fill it in from a Mesh

build the transform matrix, order matters

mat = mathutils.Matrix.Diagonal( ( 0.01, 0.01, 5 ) ).to_4x4() mat = mathutils.Matrix.Translation((0, 0, 5)) @ mat mat = mathutils.Matrix.Rotation(1.5708, 4, 'X') @ mat

for v in bm.verts:

v.co = mat @ v.co

Apply the transform matrix

bm.transform(mat)

bm.to_mesh(me) bm.free()

Ron Jensen
  • 3,421
  • 1
  • 9
  • 27
  • There you are.... ;) Thank you for helping this user. I'm over here watching some truly awful tutorials, trying to figure it out. – Christopher Bennett Aug 29 '21 at 23:39
  • 4
    There aren't any good Python programming tutorials for Blender that I've found. The API is too much of a shifting target. – Ron Jensen Aug 29 '21 at 23:41
  • Agreed. Every time I try to learn it, the tutorial I'm watching (or reading) references some method that I don't know. When I go to learn that method, I can't find a tutorial that applies to the current (or same) version of blender, so I just quit. Maybe that should be a goal for future LTS releases - have a standardized API, and a comprehensive changelog for each new LTS release (which then updates the API standard for future releases). – Christopher Bennett Aug 29 '21 at 23:48
  • @RonJensen The reason is PEP465, see: https://wiki.blender.org/wiki/Reference/Release_Notes/2.80/Python_API#Matrix_Multiplication and I think this is a dupe of https://blender.stackexchange.com/questions/6101/poll-failed-context-incorrect-example-bpy-ops-view3d-background-image-add ...? – brockmann Aug 30 '21 at 08:16
  • @ChristopherBennett The issue is the concept of the info area. It's basically just a lists of all operators called by the user within the UI (bpy.ops*), which is pretty much useless in this case because all ops needs to be run in a specific context and most of them within the UI. Copy/paste the lines of the info area is no good and has nothing to do with the API design. The API is pretty much consistent over the last few years and there are just a few minor (specific) API changes from version to version. Also the bmesh module used in this answer was introduced in 2012 or even earlier. – brockmann Aug 30 '21 at 11:36
  • 1
    Can use bm.transform(mat) to transform all vertices. Will be far quicker than looping all vertices. Scale matrix from scale vector S = Matrix.Diagonal(scale).to_4x4() – batFINGER Aug 30 '21 at 12:13
  • 1
    The API is pretty much consistent over the last few years @brockmann but if a silly developer like me has to spend literally days figuring out 'why oh why' the context (which btw has not changed) all of a sudden is incorrect, than sure you must question yourself, mmmmmh, might be we designed something not quite eeeuh user-friendly? 6 hours – hewi Aug 30 '21 at 21:35
  • Again, you can not expect that copy/pasting code from the info area will work out of the box. No matter what it is, all kind of work does take a bit of experience, especially when you are new to something (6 hours is quite ok, I think) @hewi https://docs.blender.org/api/current/info_gotcha.html?highlight=gotchas – brockmann Aug 31 '21 at 05:54
  • @brockmann what is the use of having an info message which you should not use, you might just as well just print: "we have rotated the object", at least like that you know you can't use that info. It is just strange, cause I have done many script templates so for using the info session w/o issues, and now, because of this rotate context poll(), I am stuck, for over a week now (donno where the 6 hours cam from). And that hurts, – hewi Aug 31 '21 at 19:16
  • bpy.ops.object.mode_set(mode='OBJECT') how to manage it in linux console? I get an error every time if he tries to change the value. https://blender.stackexchange.com/questions/272030/runtimeerror-on-bpy-ops-object-mode-set-if-executed-from-terminal-but-works-in-b – Welet Aug 16 '22 at 16:58