0
$\begingroup$

I'm importing a .obj file(s) from another software that results in changes in scale and location. I don't want to have select all objects individually and apply scale and location changes. I've tried the following:

import bpy

bpy.ops.object.select_all(action='DESELECT')
if bpy.context.selected_objects ==[]:
    for obj in bpy.context.scene.objects:
        if obj.type == 'MESH':
            bpy.context.object.location = (-6.0, -3.0, 0.0)
            bpy.context.object.scale = (0.001,0.001,0.001)

It selects one object and that's it. Any help would be appreciated! Thanks!

$\endgroup$
  • $\begingroup$ Are you sure? It should not select any objects. You have bpy.ops.object.select_all(action='DESELECT') which deselects all and then no objects are selected in the code. I am confused how something gets selected. Maybe the active object remains active, but there should be nothing selected. $\endgroup$ – Martin Z Oct 8 '18 at 19:36
  • $\begingroup$ Consider using the scale factor in the importer. There is a distinction between setting and applying a transform. eg above is setting object location and scale to values. For a mesh applying the scale is a process of adjusting vertex (local) coordinates such that the object appears the same at scale (1, 1, 1) ie unit scale full mesh size.. Ditto for location and rotation to (0, 0, 0) to no translation or rotation. There is also the possibility that you want to translate an imported object (eg) 1 x unit from import location, ob.location -= (1, 0, 0) $\endgroup$ – batFINGER Oct 9 '18 at 6:25
1
$\begingroup$

I am not sure why you are worrying about selected objects. You are running code for all scene objects. If you wish to change all scene objects, that are 'MESH' type that will do it:

for obj in bpy.context.scene.objects:
    if obj.type == 'MESH':
        obj.location = (-6.0, -3.0, 0.0)
        obj.scale = (0.001,0.001,0.001)

If you wanted to run the code for every selected object it would be:

for obj in bpy.context.selected_objects:
    if obj.type == 'MESH':
        obj.location = (-6.0, -3.0, 0.0)
        obj.scale = (0.001,0.001,0.001)

The code you have

bpy.ops.object.select_all(action='DESELECT') # deselects all
if bpy.context.selected_objects == []: # checks if nothing is selected
    for obj in bpy.context.scene.objects: 
        if obj.type == 'MESH': # for every object in the scene that is 'MESH' type
            bpy.context.object.location = (-6.0, -3.0, 0.0)
            bpy.context.object.scale = (0.001,0.001,0.001)
            # it changes the location and scale of the same object that 
            # happened to be active before the loop started
$\endgroup$
  • $\begingroup$ This worked great and thanks! I'm new to blender and it was really helpful. $\endgroup$ – S.Now Dec 6 '18 at 16:27
  • $\begingroup$ Glad to hear that. $\endgroup$ – Martin Z Dec 6 '18 at 18:45

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.