[Bf-python] Find Z location on surface of displaced mesh

Todd McIntosh todd at resonancemedia.ca
Tue Aug 23 21:36:33 CEST 2016


Thanks I’ll check those out, I knew there must be a more elegant way to do it.

Also, I’m not worried about object cleanup in this particular scenario, as it’s only called once per Blender session. Could be cleaner, but I’m not worried about it. 

I’ll try out the ray cast solutions and report back!



Thanks,

Todd McIntosh
Resonance Media + Design

> On Aug 23, 2016, at 1:11 PM, dima glib <dima.glib at gmail.com> wrote:
> 
> Why not just use Object.ray_cast <https://www.blender.org/api/blender_python_api_2_77a_release/bpy.types.Object.html?highlight=ray_cast#bpy.types.Object.ray_cast> or Scene.ray_cast <https://www.blender.org/api/blender_python_api_2_77a_release/bpy.types.Scene.html?highlight=ray_cast#bpy.types.Scene.ray_cast>?
> 
> On Sat, Aug 20, 2016 at 10:20 AM, Bastien Montagne <montagne29 at wanadoo.fr <mailto:montagne29 at wanadoo.fr>> wrote:
> Hi Todd,
> 
> You should rather use <your_mesh_object>.to_mesh() [1], use that temp mesh to get what you want, and the remove() it from data.meshes. Obviously, if you call your func several time at the same frame, cache the temp mesh, no need to re-create it every time.
> Incidentally, you never remove your temp empty object in your code, which is bad, since you keep adding one on every call... ;)
> 
> Cheers,
> Bastien
> 
> [1] https://www.blender.org/api/blender_python_api_2_77_0/bpy.types.Object.html#bpy.types.Object.to_mesh <https://www.blender.org/api/blender_python_api_2_77_0/bpy.types.Object.html#bpy.types.Object.to_mesh>
> Le 20/08/2016 à 08:36, Todd McIntosh a écrit :
>> Hi everyone,
>> 
>> Quick question. I've put together a little function to find the matrix_world Z location of a point on a displaced mesh (plane with ocean mod) given the X and Y coordinates. I'm using an empty with a shrinkwrap modifier to achieve this.
>> 
>> I was wondering if there is a faster or more direct way in python to do this.
>> 
>> Here's what I have so far.
>> 
>> *****************************************
>> 
>> import bpy
>> 
>> def getZLocation(xLoc, yLoc, targetMeshName):
>>     o = bpy.ops.object
>>     d = bpy.data.objects
>>     targetMesh = d[targetMeshName]
>>     o.add(type='EMPTY', location=(xLoc, yLoc, 0))
>>     empOb = bpy.context.active_object
>>     empOb.name = 'empLocate'
>>     o.constraint_add(type='SHRINKWRAP')
>>     empOb.constraints['Shrinkwrap'].shrinkwrap_type='NEAREST_SURFACE'
>>     empOb.constraints['Shrinkwrap'].target = targetMesh
>>     o.select_all(action='DESELECT')
>>     return empOb.matrix_world.to_translation()[2]
>> 
>> print(getZLocation(xLoc=2.2, yLoc=4.1, targetMeshName='Ocean'))
>> 
>> *****************************************
>>  
>> 

-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://lists.blender.org/pipermail/bf-python/attachments/20160823/31ec9192/attachment.html>


More information about the Bf-python mailing list