2
$\begingroup$

in my blender file, I have created two vehicles objects which named as 'AudiA2' and 'JeepCherokee'

now I have created a simple program to show them on the scene with different positions created from the program.

 def choose_car_position_byname(name, pos):    

    bpy.data.objects[name].hide_render = False;    
    print("location     ", bpy.data.objects[name].location )
    bpy.data.objects[name].location = bpy.data.objects[name].location + mathutils.Vector((pos,1,0))
    print("location     ", bpy.data.objects[name].location ) 

    return bpy.data.objects[name]

 car1 = choose_car_position_byname('AudiA2',1)
 car2 = choose_car_position_byname('JeepCherokee',0)

However from the program executed, I can only see one car on the scene, which is the car2. as shown below

only show one car

Ultimately I found out the second car will block the first car, because if I added the following code, two car will show up

bpy.data.objects["AudiA2"].rotation_euler.z = 270 / 180 * math.pi

as shown in here

two cars shown but overlapped


Now the question is why I can't set the location of the obj. I have tried many ways, but none of them is not working, it is totally puzzling me. My environment is OSX 10.10.1, blender 2.76.

# first approach   
bpy.data.objects["AudiA2"].location.x += 3.0
bpy.data.objects["AudiA2"].location.y += 2.0
bpy.data.objects["AudiA2"].location.z += 0.0


# second approach
vec = mathutils.Vector((3.0, 2.0, 1.0))
inv = bpy.data.objects['AudiA2'].matrix_world.copy()
inv.invert()
vec_rot = vec * inv
bpy.data.objects['AudiA2'].location = bpy.data.objects['AudiA2'].location + vec_rot    

# third approach
bpy.data.objects["AudiA2"].location = (20, 20, 10)

# fourth approach
bpy.data.objects["AudiA2"].location[0] = 3
bpy.data.objects["AudiA2"].location[0] = bpy.data.objects["AudiA2"].location[0] + 3

#fifth approach
bpy.data.objects['AudiA2'].location = bpy.data.objects['AudiA2'].location + mathutils.Vector((3,2,0))

The car objects in the blender

enter image description here


The code and blender files are all uploaded to dropbox. https://www.dropbox.com/s/fxar4owxvkxrxi7/scene2.blend?dl=0 https://www.dropbox.com/s/xvsg879k2bil2pn/batch3.py?dl=0

to work with the code, you have to pip install pypng, which could be used to output the png image.To run the application, the command is similar to this-

./blender_application_path -b blend_path -P python_path

for example

./Applications/Blender/blender.app/Contents/MacOS/blender -b scene2.blend -P batch3.py
$\endgroup$
  • $\begingroup$ what else do you have in the script ? this part seems to be right $\endgroup$ – Chebhou Oct 2 '15 at 21:18
  • $\begingroup$ I have add the whole program in attachements $\endgroup$ – user824624 Oct 2 '15 at 22:05
  • 1
    $\begingroup$ sorry can't find what's wrong here, are you sure it is a render visibility problem not just bad camera settings ( pos, rot .. ) or one car blocking the other one ? how do they appear in the render in the two situations ? $\endgroup$ – Chebhou Oct 2 '15 at 22:35
  • $\begingroup$ Why are you adding .../python2.7/site-packages/ to sys.path? $\endgroup$ – sambler Oct 3 '15 at 8:49
  • $\begingroup$ that is because I am using module pypng which is in my system rather than blender environment $\endgroup$ – user824624 Oct 3 '15 at 8:57
2
$\begingroup$

I ran into a similar problem to this and it had nothing to do with keyframes. Just thought I'd share what I found. It seems that unless at least one of the layers an object is visible in is enabled at the time you change its location via the python API, the position of the object will not be updated when you subsequently enable one of those views. For example, start with the default single Cube visible in layer 1. Switch to viewing just layer 2 and run this script:

bpy.data.objects["Cube"].location.x += 3.0
bpy.data.objects["Cube"].location.y += 2.0
bpy.data.objects["Cube"].location.z += 1.0

When you return to the 3D view and click layer 1, Cube will still be sitting at 0,0,0 even though if you look at its listed location in the transform window it will show 3, 2, 1 like you'd expect.

Now do the same thing starting at the default Cube but run this instead:

layer_save = bpy.context.scene.layers[0]
bpy.context.scene.layers[0] = True
bpy.data.objects["Cube"].location.x += 3.0
bpy.data.objects["Cube"].location.y += 2.0
bpy.data.objects["Cube"].location.z += 1.0
bpy.context.scene.update()
bpy.context.scene.layers[0] = layer_save

What this does is enable layer 0 while the location is being modified and then return its state to how it started. Now when you return to the 3D view and click layer 0, the cube will have actually moved. To be safe, when moving not visible objects like this, it probably makes more sense to save the state of all 20 layers, enable them all, do the move then restore all 20 layers. I don't know how expense that is computationally if you are doing lots of object moving. I only have a couple in my application.

I don't know if this is a Blender bug or not, but the behavior of changing the object's location when its not in a visible layer seems rather counterintuitive to me. I tend to think its not intended behavior since if you repeat the first 3 line script at the top of this post, then return to layer 1 and click Object->Snap->Selection To Cursor, the cube will jump to the offset you specified at 3,2,1 instead of 0,0,0 where the cursor is.

| improve this answer | |
$\endgroup$
4
+50
$\begingroup$

When directly manipulating transform values fails (it shouldn't, but it does sometimes), I often just try to use the transform operators insated, and it often fixes the problem.

 def choose_car_position_byname(name, pos):    
    o = bpy.data.objects[name]
    o.hide_render = False;    
    print("location     ", o.location )

    bpy.ops.object.select_all( action = 'DESELECT' )
    o.select = True
    bpy.context.scene.objects.active = o

    bpy.ops.transform.translate( value = ( pos, 1, 0 ) )

    print("location     ", o.location ) 

    return o

 car1 = choose_car_position_byname('AudiA2',1)
 car2 = choose_car_position_byname('JeepCherokee',0)

By the way, if this still fails, check if you have keyframes on the car objects' locations. If you do, that could definitely explain why they aren't moving (the keyframes will keep them in place).

EDITED: based on blend file, my suspicion has indeed been confirmed that you have keyframes on your car models. You need to delete these keyframes to enable control over the objects' locations via script. Here's how to do this (mouse cursor is a bit off due to weird issues in recording on my laptop's screen, you need to select a car object, then right click on any of the location boxes in the properties panel):

Clear Keyframes

| improve this answer | |
$\endgroup$
  • $\begingroup$ it doens't work. but on the timeline I can see each object are all on the same keyframe $\endgroup$ – user824624 Oct 11 '15 at 19:24
  • $\begingroup$ OK, so that explains it. If you have keyframes on the objects' locations, they control the location value, and setting a value manually will not help. Do you need keyframes on the cars locations? What is your goal with this script / project? $\endgroup$ – TLousky Oct 11 '15 at 19:44
  • $\begingroup$ I had added a picture of the timeline, so you can have a look. The idea of this project is simple, that I can use the program to control several cars appear on the scene with different locations. Now the problem is all the cars are at the same location because I can't manipulate their position. $\endgroup$ – user824624 Oct 11 '15 at 21:39
  • $\begingroup$ Its still unclear from the screenshots whether you have keyframes on your cars. You can make sure by selecting all your cars, then checking whether you have yellow at specific frames lines in your timeline. To clear the animation and remove your keyframes, follow these steps: blender.stackexchange.com/questions/15198/… $\endgroup$ – TLousky Oct 11 '15 at 21:42
  • $\begingroup$ Hi,@TLousky, I have uploaded the code and blender file at the dropbox, shown in my post. Perhaps it is better for you to have a look. $\endgroup$ – user824624 Oct 12 '15 at 11:54

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.