# How to set the 3D-View clip with an API command?

In a project I noticed a part of my 3D object is clipped. It is easy to correct that in the properties panel (N), View, Clip, End.

I copied the underlying Python command via the scripting Info window:

bpy.context.space_data.clip_end = 50000


but when I run this command via the console it gives me an error telling me that this attribute (clip_end) doesn't exist. In the API documentation, I find this in the section on bpy.types.SpaceView3D, but I can't get it working although my feeling is this should be very easy. So how do I access this property?

The hassle is the context.space_data (generally aka context.area.spaces.active) pertains to the console area, which is in context when you are in the console, rather than a 3d view.

Here is some console code, to find a 3D view in the current (context) screen.

>>> C.space_data.type
'CONSOLE'

>>> for a in C.screen.areas:
...     if a.type == 'VIEW_3D':
...         break
...
>>> a
bpy.data.screens['Scripting']...Area

>>> a.spaces.active.type
'VIEW_3D'

>>> a.spaces.active.clip_end = 500

• Thanks, this makes me understand how to get to that place and edit it!
– Wim
Aug 11, 2016 at 11:22

if you run it within a self written operator, you can access the space_data from the context variable and use the clipping vars as expected. The following operator sets clipping near and clipping far, supports undo and live edition of values with F6:

import bpy

def ClipView(context, _start, _end):

# because you ran it from a 3D View, the path to the clip_start and clip_end
# work as expected
context.space_data.clip_start = _start
context.space_data.clip_end = _end

class ClipViewOp(bpy.types.Operator):
bl_idname = "view3d.clip_view"
bl_label = "ClipViewOp"
bl_description = 'sets clipping values'
bl_options = {'REGISTER', 'UNDO'}

clip_start = bpy.props.FloatProperty(default = 0.5)
clip_end = bpy.props.FloatProperty(default = 50000)

@classmethod
def poll(cls, context):
return context.space_data.type == 'VIEW_3D'

def execute(self, context):
ClipView(context, self.clip_start, self.clip_end)

return {'FINISHED'}

def register():
bpy.utils.register_class(ClipViewOp)

def unregister():
bpy.utils.unregister_class(ClipViewOp)

if __name__ == "__main__":
register()