3
$\begingroup$

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

$\endgroup$
9
  • $\begingroup$ 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.. $\endgroup$ Commented Aug 29, 2021 at 21:47
  • $\begingroup$ I tried with full list of attributes, no luck either $\endgroup$
    – hewi
    Commented Aug 29, 2021 at 21:58
  • $\begingroup$ @ChristopherBennett, it all works from the scripting tab. I think OP needs to specify a context or set one active before the rotation command. $\endgroup$
    – Ron Jensen
    Commented Aug 29, 2021 at 21:58
  • $\begingroup$ and I cannot figure out how to set the correct context $\endgroup$
    – hewi
    Commented Aug 29, 2021 at 22:02
  • 1
    $\begingroup$ No idea why there is a poll error. Instead would prompt for matrices based solution as answered by Ron. Somewhat related blender.stackexchange.com/questions/214655/… blender.stackexchange.com/a/214497/15543 $\endgroup$
    – batFINGER
    Commented Aug 30, 2021 at 13:08

1 Answer 1

5
$\begingroup$

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()
$\endgroup$
10
  • $\begingroup$ There you are.... ;) Thank you for helping this user. I'm over here watching some truly awful tutorials, trying to figure it out. $\endgroup$ Commented Aug 29, 2021 at 23:39
  • 4
    $\begingroup$ There aren't any good Python programming tutorials for Blender that I've found. The API is too much of a shifting target. $\endgroup$
    – Ron Jensen
    Commented Aug 29, 2021 at 23:41
  • $\begingroup$ 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). $\endgroup$ Commented Aug 29, 2021 at 23:48
  • 1
    $\begingroup$ 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() $\endgroup$
    – batFINGER
    Commented Aug 30, 2021 at 12:13
  • 1
    $\begingroup$ 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 $\endgroup$
    – hewi
    Commented Aug 30, 2021 at 21:35

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .