Depsgraph examples: don't assign to names of built-in Python objects

`object` is the superclass of all objects. Old-style code could still be
using `class SomeClass(object)` and assigning something else to `object`
could have unexpected results.

This is now also documented in the
[Python style guide](https://wiki.blender.org/wiki/Style_Guide/Python)
on the wiki.

Differential Revision: https://developer.blender.org/D4922

Reviewed by: sergey
This commit is contained in:
2019-05-23 10:28:24 +02:00
parent 58c4b10a70
commit 8022bd7059
5 changed files with 20 additions and 20 deletions

View File

@@ -17,8 +17,8 @@ class OBJECT_OT_evaluated_example(bpy.types.Operator):
def execute(self, context):
# This is an original object. Its data does not have any modifiers applied.
object = context.object
if object is None or object.type != 'MESH':
obj = context.object
if obj is None or obj.type != 'MESH':
self.report({'INFO'}, "No active mesh object to get info from")
return {'CANCELLED'}
# Evaluated object exists within a specific dependency graph.
@@ -42,7 +42,7 @@ class OBJECT_OT_evaluated_example(bpy.types.Operator):
# but has animation applied.
#
# NOTE: All ID types have `evaluated_get()`, including materials, node trees, worlds.
object_eval = object.evaluated_get(depsgraph)
object_eval = obj.evaluated_get(depsgraph)
mesh_eval = object_eval.data
self.report({'INFO'}, f"Number of evaluated vertices: {len(mesh_eval.vertices)}")
return {'FINISHED'}

View File

@@ -18,16 +18,16 @@ class OBJECT_OT_original_example(bpy.types.Operator):
# to request the original object from the known evaluated one.
#
# NOTE: All ID types have an `original` field.
object = object_eval.original
return object.select_get()
obj = object_eval.original
return obj.select_get()
def execute(self, context):
# NOTE: It seems redundant to iterate over original objects to request evaluated ones
# just to get original back. But we want to keep example as short as possible, but in real
# world there are cases when evaluated object is coming from a more meaningful source.
depsgraph = context.evaluated_depsgraph_get()
for object in context.editable_objects:
object_eval = object.evaluated_get(depsgraph)
for obj in context.editable_objects:
object_eval = obj.evaluated_get(depsgraph)
if self.check_object_selected(object_eval):
self.report({'INFO'}, f"Object is selected: {object_eval.name}")
return {'FINISHED'}

View File

@@ -18,15 +18,15 @@ class OBJECT_OT_object_instances(bpy.types.Operator):
depsgraph = context.evaluated_depsgraph_get()
for object_instance in depsgraph.object_instances:
# This is an object which is being instanced.
object = object_instance.object
obj = object_instance.object
# `is_instance` denotes whether the object is coming from instances (as an opposite of
# being an emitting object. )
if not object_instance.is_instance:
print(f"Object {object.name} at {object_instance.matrix_world}")
print(f"Object {obj.name} at {object_instance.matrix_world}")
else:
# Instanced will additionally have fields like uv, random_id and others which are
# specific for instances. See Python API for DepsgraphObjectInstance for details,
print(f"Instance of {object.name} at {object_instance.matrix_world}")
print(f"Instance of {obj.name} at {object_instance.matrix_world}")
return {'FINISHED'}

View File

@@ -34,22 +34,22 @@ class OBJECT_OT_object_to_mesh(bpy.types.Operator):
def execute(self, context):
# Access input original object.
object = context.object
if object is None:
obj = context.object
if obj is None:
self.report({'INFO'}, "No active mesh object to convert to mesh")
return {'CANCELLED'}
# Avoid annoying None checks later on.
if object.type not in {'MESH', 'CURVE', 'SURFACE', 'FONT', 'META'}:
if obj.type not in {'MESH', 'CURVE', 'SURFACE', 'FONT', 'META'}:
self.report({'INFO'}, "Object can not be converted to mesh")
return {'CANCELLED'}
depsgraph = context.evaluated_depsgraph_get()
# Invoke to_mesh() for original object.
mesh_from_orig = object.to_mesh()
mesh_from_orig = obj.to_mesh()
self.report({'INFO'}, f"{len(mesh_from_orig.vertices)} in new mesh without modifiers.")
# Remove temporary mesh.
object.to_mesh_clear()
obj.to_mesh_clear()
# Invoke to_mesh() for evaluated object.
object_eval = object.evaluated_get(depsgraph)
object_eval = obj.evaluated_get(depsgraph)
mesh_from_eval = object_eval.to_mesh()
self.report({'INFO'}, f"{len(mesh_from_eval.vertices)} in new mesh with modifiers.")
# Remove temporary mesh.

View File

@@ -30,16 +30,16 @@ class OBJECT_OT_mesh_from_object(bpy.types.Operator):
def execute(self, context):
# Access input original object.
object = context.object
if object is None:
obj = context.object
if obj is None:
self.report({'INFO'}, "No active mesh object to convert to mesh")
return {'CANCELLED'}
# Avoid annoying None checks later on.
if object.type not in {'MESH', 'CURVE', 'SURFACE', 'FONT', 'META'}:
if obj.type not in {'MESH', 'CURVE', 'SURFACE', 'FONT', 'META'}:
self.report({'INFO'}, "Object can not be converted to mesh")
return {'CANCELLED'}
depsgraph = context.evaluated_depsgraph_get()
object_eval = object.evaluated_get(depsgraph)
object_eval = obj.evaluated_get(depsgraph)
mesh_from_eval = bpy.data.meshes.new_from_object(object_eval)
self.report({'INFO'}, f"{len(mesh_from_eval.vertices)} in new mesh, and is ready for use!")
return {'FINISHED'}