9
$\begingroup$

I wanna rotate an object with this code below:

import bpy
from math import *
import mathutils

Name = 'Dimi'
me = bpy.ops.mesh.primitive_cylinder_add(radius = 0.5, depth = 1)
scj = bpy.context.scene
me1 = bpy.context.object.data
ob = bpy.data.objects.new(Name, me1)
scj.objects.link(ob)
for ob in scj.objects:
    ob.location.x += 4
    ob.location.y += 0
    ob.location.z += 0

me.transform(ob.matrix)
ob.RotY = 0
ob.RotX = 0

But this function

me.transform(ob.matrix)
ob.RotY = 0
ob.RotX = 0

is from blender 2.49 and can't find how to do this in a 2.74 version.

$\endgroup$
11
$\begingroup$
import bpy
from math import *  # It's considered bad practice to import everything from a library, especially when you're not using any (or just a few) of its function
import mathutils

Name = 'Dimi'

# The primitive_cylinder_add does not return the object, but rather only a status indicating success or failure
exitStatus = bpy.ops.mesh.primitive_cylinder_add(radius = 0.5, depth = 1)
scj = bpy.context.scene

# The added cylinder will be the active object afterits created
ob = bpy.context.object
me = ob.data
ob.name = Name

# These two lines are unnecessary and will generate another copy of your cylinder
#ob = bpy.data.objects.new(Name, me1)
#scj.objects.link(ob)

# If you want to move your object, simply set its location thus:
ob.location = ( 4, 0, 0 )

# And you can rotate the object the same way
ob.rotation_euler = (0,0,0)  # Note that you need to use radians rather than angles here
| improve this answer | |
$\endgroup$
8
$\begingroup$

I think you want something akin to,

import bpy
from math import radians
from mathutils import Vector

context = bpy.context
Name = 'Dimi'
# operators don't return object refs.
bpy.ops.mesh.primitive_cylinder_add(radius = 0.5, depth = 1)

scene = context.scene
ob = scene.objects.active # the newly added cylinder.
ob.name = Name
# move the object
ob.location += Vector((4, 0, 0))
# set the objects rotation
ob.rotation_euler = (0, 0, radians(20))

bpy.ops.mesh.primitive..add returns an object when run in object mode, or adds that primitive to a mesh when run in edit mode. Did you really want to move all objects in the scene?

| improve this answer | |
$\endgroup$
  • $\begingroup$ is there something like radians but for XYZ scaling? or how do I properly change scales of XYZ $\endgroup$ – cxnt Aug 7 '19 at 13:10

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.