0
$\begingroup$

I am a more or less skilled Python user in Maya for quite some time and wanted to look into Python within Blender (Version: 2.79).

This is my code:

def posRotSclObj():
sel = bpy.context.selected_objects
sel2 = sel[0]
sel1 = sel[1]
print(sel1)
print(sel2)

pos1 = sel1.location
rot1 = sel1.rotation_euler
scl1 = sel1.scale
print (scl1)

pos2 = sel2.delta_location

sel2.location = (pos1[0], pos1[1], pos1[2])
sel2.rotation_euler = (rot1[0], rot1[1], rot1[2])
sel2.scale = (scl1[0], scl1[1], scl1[2])

posRotSclObj()

I have 3 problems right now:

  1. When I define my selection by sel = bpy.context.selected_objects then sel[0] is the second object I have selected and sel[1] the first...that's pretty counterintuitive, especially because it was the other way around in Maya. Is this right?

  2. print sel would work in Maya but Blender needs brackets around my variable: print (sel). Why is there difference in syntax?

  3. When I indent beginning from the line after def except for the last where I call my function and paste it into the python console within Blender it gives me indentation errors on every line that has one indent.

$\endgroup$
  • 1
    $\begingroup$ Suggest to start here: docs.blender.org/api/blender_python_api_current/… (pep8 style guide) Notice that you can access the components of e.g. location by attribute Object.location.x, Object.location.y and Object.location.z, which is way more readable. Also paste your code into the text editor and run it from there, that can be cumbersome but there a lot of ways around that and you'll see that Blender's API is well designed (in comparison to Maya's python API). $\endgroup$ – brockmann Sep 4 '18 at 11:55
  • 1
    $\begingroup$ In Blender bpy.context.selected_objects is not ordered by selection, it's just a list of selected objects in no particular order. $\endgroup$ – Jaroslav Jerryno Novotny Sep 4 '18 at 12:36
0
$\begingroup$

As we can see from https://knowledge.autodesk.com/support/maya/learn-explore/caas/CloudHelp/cloudhelp/2018/ENU/Maya-Scripting/files/GUID-C0F27A50-3DD6-454C-A4D1-9E3C44B3C990-htm.html :

Maya installs Python with your Maya installation. Maya uses Python version 2.7.11 on all supported platforms.

Maya uses an old legacy version of Python while Blender uses Python version 3.5.3. There are syntax differences between these versions and brackets is one of them. I am not sure about indentation in 2.7.11, but in the version that comes with Blender it is 4 spaces.

The sequence in which the selected objects are stored is not something that can be intuitive in my opinion and there is no logical reason one way is better than the other, however if you wish to reverse the order you can do so with Python very easily:

sel = bpy.context.selected_objects[::-1]
$\endgroup$

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.